Keras + TensorFlow 实时训练图 [英] Keras + TensorFlow Realtime training chart

查看:48
本文介绍了Keras + TensorFlow 实时训练图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jupyter 笔记本中运行以下代码:

# 可视化训练历史从 keras.models 导入顺序从 keras.layers 导入密集导入 matplotlib.pyplot 作为 plt导入 numpy# 修复随机种子以提高可重复性种子 = 7numpy.random.seed(种子)# 加载皮马印第安人数据集数据集 = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")# 拆分为输入(X)和输出(Y)变量X = 数据集[:,0:8]Y = 数据集[:,8]# 创建模型模型 = 顺序()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'))# 编译模型model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])# 拟合模型history = model.fit(X,Y,validation_split=0.33,epochs=150,batch_size=10,verbose=0)# 列出历史记录中的所有数据打印(history.history.keys())# 总结历史的准确性plt.plot(history.history['acc'])plt.plot(history.history['val_acc'])plt.title('模型精度')plt.ylabel('准确度')plt.xlabel('时代')plt.legend(['train', 'test'], loc='左上')plt.show()# 总结历史损失plt.plot(history.history['损失'])plt.plot(history.history['val_loss'])plt.title('模型损失')plt.ylabel('损失')plt.xlabel('时代')plt.legend(['train', 'test'], loc='左上')plt.show()

代码收集纪元历史,然后显示进度历史.

<小时>

问:如何在训练时更改图表以便实时查看变化?

解决方案

I have the following code running inside a Jupyter notebook:

# 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()

The code collects epochs history, then displays the progress history.


Q: How can I make the chart change while training so I can see the changes in real time?

解决方案

There is livelossplot Python package for live training loss plots in Jupyter Notebook for Keras (disclaimer: I am the author).

from livelossplot import PlotLossesKeras

model.fit(X_train, Y_train,
          epochs=10,
          validation_data=(X_test, Y_test),
          callbacks=[PlotLossesKeras()],
          verbose=0)

To see how does it work, look at its source, especially this file: https://github.com/stared/livelossplot/blob/master/livelossplot/outputs/matplotlib_plot.py (from IPython.display import clear_output and clear_output(wait=True)).

A fair disclaimer: it does interfere with Keras output.

这篇关于Keras + TensorFlow 实时训练图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆