Search code examples
python-3.xmachine-learningkerasneural-networkkeras-layer

How can I load a trained autoencoder (Keras), take the encoder part, and freeze it?


I trained an autoencoder, and would now like to use the encoder part (including weights) for another purpose. Can I load the full model, get the encoder part, and then set the 'trainable' parameter in these layers? Or do I need to recompile the adapted encoder architecture (i.e. all layers set to untrainable), and only then load the weights?


Solution

  • You can iterate through the model list of layers ( model.layers ) and modify the layers of encoder so that trainable flag is False.

    for layer in model.layers:
        layer.trainable = False