保存并加载keras.callbacks.History [英] save and load keras.callbacks.History

查看:408
本文介绍了保存并加载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屋!

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