Python 在 keras 中保存和加载权重
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47266383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save and load weights in keras
提问by Ryan
Im trying to save and load weights from the model i have trained.
我试图从我训练过的模型中保存和加载权重。
the code im using to save the model is.
我用来保存模型的代码是。
TensorBoard(log_dir='/output')
model.fit_generator(image_a_b_gen(batch_size), steps_per_epoch=1, epochs=1)
model.save_weights('model.hdf5')
model.save_weights('myModel.h5')
Let me know if this an incorrect way to do it,or if there is a better way to do it.
让我知道这是否是一种不正确的方法,或者是否有更好的方法。
but when i try to load them,using this,
但是当我尝试加载它们时,使用这个,
from keras.models import load_model
model = load_model('myModel.h5')
but i get this error:
但我收到此错误:
ValueError Traceback (most recent call
last)
<ipython-input-7-27d58dc8bb48> in <module>()
1 from keras.models import load_model
----> 2 model = load_model('myModel.h5')
/home/decentmakeover2/anaconda3/lib/python3.5/site-
packages/keras/models.py in load_model(filepath, custom_objects, compile)
235 model_config = f.attrs.get('model_config')
236 if model_config is None:
--> 237 raise ValueError('No model found in config file.')
238 model_config = json.loads(model_config.decode('utf-8'))
239 model = model_from_config(model_config,
custom_objects=custom_objects)
ValueError: No model found in config file.
Any suggestions on what i may be doing wrong? Thank you in advance.
关于我可能做错了什么的任何建议?先感谢您。
回答by blackHoleDetector
Here is a YouTube video that explains exactly what you're wanting to do: Save and load a Keras model
这是一个 YouTube 视频,它准确地解释了您想要做什么:保存并加载 Keras 模型
There are three different saving methods that Keras makes available. These are described in the video link above (with examples), as well as below.
Keras 提供了三种不同的保存方法。这些在上面的视频链接(带有示例)和下面都有描述。
First, the reason you're receiving the error is because you're calling load_model
incorrectly.
首先,您收到错误的原因是您调用load_model
错误。
To save and load the weights of the model, you would first use
要保存和加载模型的权重,您首先要使用
model.save_weights('my_model_weights.h5')
to save the weights, as you've displayed. To load the weights, you would first need to build your model, and then call load_weights
on the model, as in
保存权重,如您所显示的。要加载权重,您首先需要构建模型,然后调用load_weights
模型,如
model.load_weights('my_model_weights.h5')
Another saving technique is model.save(filepath)
. This save
function saves:
另一种保存技术是model.save(filepath)
. 此save
功能可节省:
- The architecture of the model, allowing to re-create the model.
- The weights of the model.
- The training configuration (loss, optimizer).
- The state of the optimizer, allowing to resume training exactly where you left off.
- 模型的架构,允许重新创建模型。
- 模型的权重。
- 训练配置(损失、优化器)。
- 优化器的状态,允许从您停止的地方恢复训练。
To load this saved model, you would use the following:
要加载这个保存的模型,您将使用以下内容:
from keras.models import load_model
new_model = load_model(filepath)'
Lastly, model.to_json()
, saves only the architecture of the model. To load the architecture, you would use
最后,model.to_json()
仅保存模型的架构。要加载架构,您将使用
from keras.models import model_from_json
model = model_from_json(json_string)
回答by Daniel M?ller
For loading weights, you need to have a model first. It must be:
要加载weights,您首先需要有一个模型。肯定是:
existingModel.save_weights('weightsfile.h5')
existingModel.load_weights('weightsfile.h5')
If you want to save and load the entire model (this includes the model's configuration, it's weights and the optimizer states for further training):
如果要保存和加载整个模型(这包括模型的配置、权重和优化器状态以供进一步训练):
model.save_model('filename')
model = load_model('filename')
回答by Sam Vanhoutte
Since this question is quite old, but still comes up in google searches, I thought it would be good to point out the newer (and recommended) way to save Keras models. Instead of saving them using the older h5 format like has been shown before, it is now advised to use the SavedModel format, which is actually a dictionary that contains both the model configuration and the weights.
由于这个问题已经很老了,但仍然会出现在谷歌搜索中,我认为指出保存 Keras 模型的更新(和推荐)方法会很好。现在建议使用 SavedModel 格式,而不是像之前显示的那样使用旧的 h5 格式保存它们,它实际上是一个包含模型配置和权重的字典。
More information can be found here: https://www.tensorflow.org/guide/keras/save_and_serialize
更多信息可以在这里找到:https: //www.tensorflow.org/guide/keras/save_and_serialize
The snippets to save & load can be found below:
可以在下面找到要保存和加载的片段:
model.fit(test_input, test_target)
# Calling save('my_model') creates a SavedModel folder 'my_model'.
model.save('my_model')
# It can be used to reconstruct the model identically.
reconstructed_model = keras.models.load_model('my_model')
A sample output of this :
此示例输出: