Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a layer from one CNN model to other. (layer_from_config not working in ver 2)

In Keras 2, layer_from_config was removed from keras.utils.layer_utils. Does anyone know any replacement to that.

Detailed Description: I have a trained CNN model. I need to copy a layer from that model to another. Earlier I used to do layer_from_config and set_weights functions. But they are removed in Keras 2.0. Needed help to do this functionality.

Thanks

like image 752
Harsha Pokkalla Avatar asked Dec 04 '25 15:12

Harsha Pokkalla


1 Answers

The function is now a class function of the class Layers (which seems to make more sense). Same for set_weights. The way to use it (the doc is up to date) :

layer = Dense(32)
config = layer.get_config()
reconstructed_layer = Dense.from_config(config)

So you need to know the class name of the layer you want to rebuild. Or you can build a dictionnary like below, which contains the class name (so that you can store the config somewhere to rebuild the layer in an empty code) :

from keras import layers

config = layer.get_config()
layer = layers.deserialize({'class_name':      layer.__class__.__name__,
                        'config': config})

Does it help?

like image 122
Nassim Ben Avatar answered Dec 07 '25 05:12

Nassim Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!