保存并加载 keras.callbacks.History [英] save and load keras.callbacks.History
问题描述
我正在使用 Keras 训练一个深度神经网络,并正在寻找一种方法来保存和稍后加载 keras.callbacks.History
类型的历史对象.设置如下:
I'm training a deep neural net using Keras and looking for a way to save and later load the history object which is of keras.callbacks.History
type. Here's the setup:
history_model_1 = model_1.fit_generator(train_generator,
steps_per_epoch=100,
epochs=20,
validation_data=validation_generator,
validation_steps=50)
history_model_1
是我想在另一个 Python 会话期间保存和加载的变量.
history_model_1
is the variable I want to be saved and loaded during another Python session.
推荐答案
history_model_1
是一个回调对象.它包含各种数据并且不可序列化.
history_model_1
is a callback object. It contains all sorts of data and isn't serializable.
但是,它包含一个字典,其中包含您实际想要保存的所有值(参见您的评论):
However, it contains a dictionnary with all the values that you actually want to save (cf your comment) :
import json
# Get the dictionary containing each metric and the loss for each epoch
history_dict = history_model_1.history
# Save it under the form of a json file
json.dump(history_dict, open(your_history_path, 'w'))
您现在可以像这样访问第 50 个时期的损失值:
You can now access the value of the loss at the 50th epoch like this :
print(history_dict['loss'][49])
重新加载它
history_dict = json.load(open(your_history_path, 'r'))
我希望这会有所帮助.
这篇关于保存并加载 keras.callbacks.History的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!