Keras-管理历史



我正在训练Keras模型,用model.save()保存它们,然后加载它们并恢复训练。

我想在每次训练后绘制整个训练历史,但model.fit_generator()只返回最后一次训练的历史。

我可以保存初始训练的历史记录并自己更新,但我想知道Keras是否有管理训练历史的标准方法。

history1 = model.fit_generator(my_gen)
plot_history(history1)
model.save('my_model.h5')
# Some days afterwards...
model = load_model('my_model.h5')
history2 = model.fit_generator(my_gen)
# here I would like to reconstruct the full_training history
# including the info from history1 and history2
full_history = ???

使用numpy连接您感兴趣的特定历史密钥。

例如,假设这是你的两次训练:

history1 = model.fit_generator(my_gen)
history2 = model.fit_generator(my_gen)

您可以查看字典键,每次运行时都会将其标记为相同的:

print(history1.history.keys())

这将打印输出,如:

dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])

然后可以使用numpy连接。例如:

import numpy as np
combo_accuracy=np.concatenate((history1.history['accuracy'],history2.history['accuracy']),axis=0)
combo_val_accuracy=np.concatenate((history1.history['val_accuracy'],history2.history['val_accuracy']),axis=0)

然后,您可以使用matplotlib:绘制连接的历史数组

import matplotlib.pyplot as plt
plt.plot(combo_acc, 'orange', label='Training accuracy')
plt.plot(combo_val_acc, 'blue', label='Validation accuracy')
plt.legend()

让我们说这行

print(history.history.keys())

产生以下输出:

['acc', 'loss', 'val_acc', 'val_loss']

基于加载的模型应该与保存的模型具有相同性能的假设,您可以尝试连接历史。例如,在加载模型的加载精度历史上连接新的精度历史。

它应该从绘图空间中加载模型结束的同一点开始(也许你必须为绘图添加先前训练的模型的(+(个历元,这样新的精度值就不会从历元0开始,而是从加载模型的最后一个历元开始(。

我希望你能理解我的想法,这将对你有所帮助:(

事实证明,在Keras中还没有标准的方法来实现这一点。

请参阅本期内容。

最新更新