在北京作某個項目的時候,客戶要求可以對數據進行訓練、預測,同時能導出模型,還有在頁面上顯示訓練的進度。前面的幾個要求都不難實現,但在頁面上顯示訓練進度當時筆者並無實現。
本文將會分享如何在Keras中將模型訓練的過程實時可視化。
幸運的是,已經有人幫咱們作好了這件事,這個項目名叫hualos,Github的訪問網址爲:https://github.com/fchollet/h..., 做者爲François Chollet和Eder Santana,前面的做者就是Keras的創造者,同時也是書籍《Deep Learning with Python》的做者。
大神的工做大大地方便了咱們的使用。調用該項目僅須要三行代碼,示例以下:python
from keras import callbacks remote = callbacks.RemoteMonitor(root='http://localhost:9000') model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(X_test, Y_test), callbacks=[remote])
該項目使用Python2寫的,用到的第三方模塊爲Flask, gevent,其中Flask爲網頁端框架,gevent用於併發。用到的JavaScript的第三方模塊爲D3.js和C3.js。該項目使用起來很是方便,只須要切換至hualos項目所在文件夾,而後python api.py
便可。
下面將介紹其使用方法,咱們的項目結構以下:
其中hualos能夠從Github上直接clone下來,筆者對代碼和HTML網頁稍做了修改,便於本身使用。model_train.py爲Keras模型訓練腳本,iris.csv爲著名的鳶尾花數據集。
model_train.py中利用Keras搭建了簡單的DNN模型對鳶尾花數據集進行訓練及預測,該模型的介紹已經在文章Keras入門(一)搭建深度神經網絡(DNN)解決多分類問題中給出,其完整代碼以下:git
# 導入模塊 import numpy as np import keras as K import tensorflow as tf import pandas as pd from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelBinarizer from keras import callbacks # 讀取CSV數據集,並拆分爲訓練集和測試集 # 該函數的傳入參數爲CSV_FILE_PATH: csv文件路徑 def load_data(CSV_FILE_PATH): IRIS = pd.read_csv(CSV_FILE_PATH) target_var = 'class' # 目標變量 # 數據集的特徵 features = list(IRIS.columns) features.remove(target_var) # 目標變量的類別 Class = IRIS[target_var].unique() # 目標變量的類別字典 Class_dict = dict(zip(Class, range(len(Class)))) # 增長一列target, 將目標變量進行編碼 IRIS['target'] = IRIS[target_var].apply(lambda x: Class_dict[x]) # 對目標變量進行0-1編碼(One-hot Encoding) lb = LabelBinarizer() lb.fit(list(Class_dict.values())) transformed_labels = lb.transform(IRIS['target']) y_bin_labels = [] # 對多分類進行0-1編碼的變量 for i in range(transformed_labels.shape[1]): y_bin_labels.append('y' + str(i)) IRIS['y' + str(i)] = transformed_labels[:, i] # 將數據集分爲訓練集和測試集 train_x, test_x, train_y, test_y = train_test_split(IRIS[features], IRIS[y_bin_labels], \ train_size=0.7, test_size=0.3, random_state=0) return train_x, test_x, train_y, test_y, Class_dict if __name__ == '__main__': # 0. 開始 print("\nIris dataset using Keras") np.random.seed(4) tf.set_random_seed(13) # 1. 讀取CSV數據集 print("Loading Iris data into memory") CSV_FILE_PATH = 'iris.csv' train_x, test_x, train_y, test_y, Class_dict = load_data(CSV_FILE_PATH) # 2. 定義模型 init = K.initializers.glorot_uniform(seed=1) simple_adam = K.optimizers.Adam() model = K.models.Sequential() model.add(K.layers.Dense(units=5, input_dim=4, kernel_initializer=init, activation='relu')) model.add(K.layers.Dense(units=6, kernel_initializer=init, activation='relu')) model.add(K.layers.Dense(units=3, kernel_initializer=init, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=simple_adam, metrics=['accuracy']) # 3. 訓練模型 b_size = 1 max_epochs = 100 print("Starting training ") remote = callbacks.RemoteMonitor(root='http://localhost:9000') h = model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=b_size, epochs=max_epochs, shuffle=True, verbose=1, callbacks=[remote]) print("Training finished \n") # 4. 評估模型 eval = model.evaluate(test_x, test_y, verbose=0) print("Evaluation on test data: loss = %0.6f accuracy = %0.2f%% \n" \ % (eval[0], eval[1] * 100) ) # 5. 使用模型進行預測 np.set_printoptions(precision=4) unknown = np.array([[6.1, 3.1, 5.1, 1.1]], dtype=np.float32) predicted = model.predict(unknown) print("Using model to predict species for features: ") print(unknown) print("\nPredicted softmax vector is: ") print(predicted) species_dict = {v:k for k,v in Class_dict.items()} print("\nPredicted species is: ") print(species_dict[np.argmax(predicted)])
咱們切換至hualos文件夾,運行python api.py
,而後再用Python3運行model_train.py文件,在瀏覽器中輸入網址:http://localhost:9000,便可看到在網頁中顯示的模型訓練的實施可視化的結果,圖像以下:
由於這裏沒法給出視頻,須要觀看視頻的讀者能夠移步網址:https://mp.weixin.qq.com/s?__biz=MzU2NTYyMDk5MQ==&mid=2247484522&idx=1&sn=dab46a55945baf2411e30bd109cee76f&chksm=fcb9bdfacbce34ec02f3e958988e9b400676d29f88c1efad5ce01fb1f4ce2f5f96ccf0e4af66&token=1377830530&lang=zh_CN#rd 。github
本項目的Github地址爲:https://github.com/percent4/keras_train_visualization 。
本期分享到此結束,感謝你們閱讀~api