【问题标题】:How to save training history on every epoch in Keras?如何在 Keras 中保存每个 epoch 的训练历史?
【发布时间】:2018-10-12 03:26:36
【问题描述】:
我无法让我的电脑整天运行,为此我需要在每个 epoch 后保存训练历史记录。例如,我在一天内训练了我的模型 100 个 epoch,第二天,我想再训练它 50 个 epoch。我需要为整个 150 个 epoch 生成损失 vs epoch 和accuracy vs epoch 图。我正在使用fit_generator 方法。有什么方法可以在每个 epoch 之后保存训练历史(很可能使用Callback)?我知道如何在培训结束后保存培训历史。我正在使用 TensorFlow 后端。
【问题讨论】:
标签:
python
tensorflow
keras
【解决方案1】:
Keras 有 CSVLogger 回调,它似乎完全符合您的需要;来自documentation:
将纪元结果流式传输到 CSV 文件的回调。
它有一个用于添加到文件的附加参数。同样,来自文档:
追加:布尔值。 True:如果文件存在则追加(对继续培训有用)。 False:覆盖现有文件
from keras.callbacks import CSVLogger
csv_logger = CSVLogger("model_history_log.csv", append=True)
model.fit_generator(...,callbacks=[csv_logger])
【解决方案2】:
要保存模型历史记录,您有两种选择。
- 使用keras ModelCheckPoint回调类
- 创建自定义类
这里是如何创建自定义检查点回调类。
class CustomModelCheckPoint(keras.callbacks.Callback):
def __init__(self,**kargs):
super(CustomModelCheckPoint,self).__init__(**kargs)
self.epoch_accuracy = {} # loss at given epoch
self.epoch_loss = {} # accuracy at given epoch
def on_epoch_begin(self,epoch, logs={}):
# Things done on beginning of epoch.
return
def on_epoch_end(self, epoch, logs={}):
# things done on end of the epoch
self.epoch_accuracy[epoch] = logs.get("acc")
self.epoch_loss[epoch] = logs.get("loss")
self.model.save_weights("name-of-model-%d.h5" %epoch) # save the model
现在使用回调类
checkpoint = CustomModelCheckPoint()
model.fit_generator(...,callbacks=[checkpoint])
现在checkpoint.epoch_accuracy 字典包含给定时期的准确度,checkpoint.epoch_loss 字典包含给定时期的损失
【解决方案3】:
我有类似的要求,我选择了一种幼稚的方法。
1. 运行 50 个 Epoch 的 Python 代码:
我保存了模型的历史和训练了 50 个 epoch 的模型本身。 .history 用于存储训练模型的整个历史记录。
history = model.fit_generator(......) # training the model for 50 epochs
model.save("trainedmodel_50Epoch.h5") # saving the model
with open('trainHistoryOld', 'wb') as handle: # saving the history of the model
dump(history.history, handle)
2.Python 代码用于加载训练好的模型并训练另外 50 个 epoch:
from keras.models import load_model
model = load_model('trainedmodel_50Epoch.h5')# loading model trained for 50 Epochs
hstry = model.fit_generator(......) # training the model for another 50 Epochs
model.save("trainedmodel_50Epoch.h5") # saving the model
with open('trainHistoryOld', 'wb') as handle: # saving the history of the model trained for another 50 Epochs
dump(hstry.history, handle)
from pickle import load
import matplotlib.pyplot as plt
with open('trainHistoryOld', 'rb') as handle: # loading old history
oldhstry = load(handle)
oldhstry['loss'].extend(hstry['loss'])
oldhstry['acc'].extend(hstry['acc'])
oldhstry['val_loss'].extend(hstry['val_loss'])
oldhstry['val_acc'].extend(hstry['val_acc'])
# Plotting the Accuracy vs Epoch Graph
plt.plot(oldhstry['acc'])
plt.plot(oldhstry['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# Plotting the Loss vs Epoch Graphs
plt.plot(oldhstry['loss'])
plt.plot(oldhstry['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
您也可以创建自定义类,如前面提供的答案中所述。