2017-06-25 283 views
5

我有一個Jupyter筆記本電腦中運行下面的代碼:Keras + TensorFlow實時培訓圖表

# Visualize training history 
from keras.models import Sequential 
from keras.layers import Dense 
import matplotlib.pyplot as plt 
import numpy 
# fix random seed for reproducibility 
seed = 7 
numpy.random.seed(seed) 
# load pima indians dataset 
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",") 
# split into input (X) and output (Y) variables 
X = dataset[:,0:8] 
Y = dataset[:,8] 
# create model 
model = Sequential() 
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu')) 
model.add(Dense(8, kernel_initializer='uniform', activation='relu')) 
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid')) 
# Compile model 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
# Fit the model 
history = model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, verbose=0) 
# list all data in history 
print(history.history.keys()) 
# summarize history for accuracy 
plt.plot(history.history['acc']) 
plt.plot(history.history['val_acc']) 
plt.title('model accuracy') 
plt.ylabel('accuracy') 
plt.xlabel('epoch') 
plt.legend(['train', 'test'], loc='upper left') 
plt.show() 
# summarize history for loss 
plt.plot(history.history['loss']) 
plt.plot(history.history['val_loss']) 
plt.title('model loss') 
plt.ylabel('loss') 
plt.xlabel('epoch') 
plt.legend(['train', 'test'], loc='upper left') 
plt.show() 

代碼收集時期的歷史,則顯示進度的歷史。


問:我怎樣才能使圖表的變化而培訓,讓我可以看到實時的變化?

回答

4

Keras附帶callback for TensorBoard

您可以輕鬆地將此行爲添加到您的模型中,然後在日誌記錄數據的頂部運行tensorboard。

callbacks = [TensorBoard(log_dir='./logs')] 
result = model.fit(X, Y, ..., callbacks=callbacks) 

,然後在你的shell:

tensorboard --logdir=/logs 

如果你需要在你的筆記本上,你也可以寫自己的回調以獲取指標,而訓練:

class LogCallback(Callback): 

    def on_epoch_end(self, epoch, logs=None): 
     print(logs["train_accuracy"]) 

這在當前時代結束時獲得訓練準確性並打印出來。 There's some good documentation around it on the official keras site.