Search code examples
pythontensorflowkeraskeras-layer

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


Solution

  • 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?