简体   繁体   中英

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.

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()

see Link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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