[英]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.