Search code examples
keraskeras-layer

Keras: layer trainable set false not working


I have a pretrained keras based Bidirectional LSTM based model. I want to set trainable false for the first few layers. But it is not working.

 classifier.layers[1].trainable=False

I also tried creating a dummy copy of the model using config with trainable key changed to false. But even the weights of that dummy model change after training.


Solution

  • Since something is strange, you can try to deeply find all the layers involved and set trainable=False in all of them:

    classifier.layers[i].forward_layer.trainable = False
    classifier.layers[i].backward_layer.trainable = False 
    classifier.layers[i].trainable = False
    

    And use classifier.compile after that.

    In case your model involves submodels, you might need recurrency:

    classifier.layers[i].layers[j].trainable = False