我正在训练 Keras 模型,使用 model.save() 保存它们,然后加载它们并恢复训练。
我想在每次训练后绘制整个训练历史,但 model.fit_generator() 只返回最后一次训练的历史。
我可以保存初始 session 的历史记录并自己更新它,但我想知道 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()
