简体   繁体   English

如何从已保存的模型加载权重

[英]How to load weights from a saved model

I have a trained keras model saved with model.save(). 我有一个保存在model.save()中的训练好的keras模型。 When I load it and print a summary it appears as below. 当我加载它并打印摘要时,它显示如下。

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 2)                 0         
_________________________________________________________________
model_1 (Model)              (None, 8)                 83208     
=================================================================
Total params: 83,208
Trainable params: 83,208
Non-trainable params: 0
_________________________________________________________________ 

Now I want to load the weights of the model_1 (a 6 layer network) in above network to a model with the same architecture. 现在,我想将上述网络中的model_1(6层网络)的权重加载到具有相同架构的模型中。 When I try to load from model.load_weights() I get an error saying that I cannot load from a 2 layer network to a 6 layer network. 当我尝试从model.load_weights()加载时,出现一条错误消息,提示我无法从2层网络加载到6层网络。 This is due to that model_1 is just a layer in the above model. 这是因为model_1只是上述模型中的一层。 How do I separately access and load weights from this layer? 如何从该层分别访问和加载权重?

You can use model.layers to access the various layers of a model and thus model.layers[1] to access model_1 . 您可以使用model.layers访问模型的各个层,从而可以使用model.layers model.layers[1]访问model_1 Then you can load the weights via model.layers[1].load_weights(...) . 然后,您可以通过model.layers[1].load_weights(...)加载权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用从 save_weights 保存的张量流模型加载和预测? - How to load and predict with a tensorflow model saved from save_weights? 如何从 saved_model.pb 文件加载 model 架构和 model 权重? - How to load model architecture and model weights from saved_model.pb file? 如何使用保存在JSON文件中的权重重建keras模型? - How to rebuild keras model from weights that are saved in a JSON file? 如何加载训练模型的权重,这些权重是用 tf.keras.models.save_model 保存的? - How to load a trained model's weights, which were saved with tf.keras.models.save_model? 如何从 Tensorflow 检查点将权重加载到 Keras 模型 - How to load_weights to a Keras model from a Tensorflow checkpoint 保存预训练 model 层权重,但无法通过 H5PY 加载权重 - Saved pre-train model layer weights but cannot load the weights through H5PY 来自检查点的负载权重在 keras model 中不起作用 - Load weights from checkpoint not working in keras model 从以前保存的hdf5文件加载模型权重的一部分 - Load parts of models weights from previously saved hdf5 file 如何从 Colaboratory 中保存的检查点加载 TensorFlow Keras model? - How to load TensorFlow Keras model from a saved checkpoint in Colaboratory? 如何从 Python 加载保存为 RDS 文件的模型并进行预测? - How to load a model saved as RDS file from Python and make predictions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM