Search code examples
pythondeep-learningkeraskeras-layer

Keras layers unfreezing causes training process to start from scratch


I trained a model with several layers than for each layer in model.layers set

layer.trainable = False

I added several layers to this model, called

model.compile(...)

And trained this new model for several epochs with part of the layers frozen.

Later I decided to unfreeze layers and ran

for layer in model.layers:
   layer.trainable = True
model.compile(...)

When I start learning the model with unfrozen layers I get loss function value very high even though I just wanted to continue training from previously learned weights. I also checked that after model.compile(...) model still predicts well (not resetting previously learned weights) but as soon as learning process starts everything gets 'erased' and I start as from scratch.

Could someone clarify, whether this behavior is ok? How to recompile the model and not start from scratch?

P.S. I also asked manually saving weights and assigning them back to a newly compiled model using layer.get_weights() and layer.set_weights()

I used the same compile parameters (similar optimizer and similar loss)


Solution

  • You might need to lower your learning rate while starting fine-tuning the trained layers. For example, a learning rate of 0.01 might work for your new dense layers (top) with all others layers set to untrainable. But when setting all layers to be trainable, you might need to reduce the learning rate to say 0.001 There is no need to manually copy or set weights.