简体   繁体   English

Keras节省了单个图层的权重,而不是模型

[英]Keras saving weights of individual layers instead of a model

I'm currently trying to create multiple models that will reuse certain layers, including their weights. 我目前正在尝试创建多个模型,这些模型将重用某些图层,包括其权重。 I've achieved this by creating a list table that initializes these layers, then call them when creating each individual models. 我通过创建一个列表表来初始化这些层,然后在创建每个单独的模型时调用它们来实现这一点。

column = []
column.append(Conv2D(self.out_filters, (3, 3), padding='same', kernel_initializer='he_normal', activation='relu'))
column.append(Conv2D(self.out_filters, (5, 5), padding='same', kernel_initializer='he_normal', activation='relu'))

then when creating models 然后在创建模型时

layer = column[0](input)

Now my question is, how do I save the weights of all the layers in the list? 现在的问题是,如何保存列表中所有图层的权重? As far as I know, keras' save function only saves entire models that have been properly built. 据我所知,keras的保存功能仅保存已正确构建的整个模型。

Edit: Just to clarify, I want to save the "column" list, and not the final models. 编辑:只是为了澄清,我想保存“列”列表,而不是最终模型。 I am randomly generating model structures while using the layers stored inside "column". 我在使用存储在“列”中的图层时随机生成模型结构。 So 2 models may have different architectures, but they have weights shared (training on one model will also affect the weights of the other model). 因此,两个模型可能具有不同的体系结构,但是它们具有权重共享(对一个模型的训练也会影响另一个模型的权重)。

like so. 像这样

model.save_weights('my_model_weights.h5')

model.get_weights() can also be used to get the weights of the model, then manually save them to use later model.get_weights()也可用于获取模型的权重,然后手动保存它们以供以后使用

model.get_weights()

see Link . 参见链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM