There are mainly three ways to implement transfer learning:
- Use a pre-trained model with pre-realized weights and biases; that is, completely freeze the pre-trained model of your network and train on a new dataset.
- Partially freeze a few layers of the pre-trained model of our network and train it on a new dataset.
- Retain only the architecture of the pre-trained model and train your complete network for new weights and biases.
The following code snippet demonstrates how to partially freeze the pre-trained part of the network. Before we unfreeze selected layers of the pre-trained network, we must define the holistic model and freeze the pre-trained part:
unfreeze_weights(pre_trained_base, from = "block5_conv1", to = "block5_conv3")
The from and to arguments of the unfreeze_weights() function let us define the layers between which we want to unfreeze the weights. Please note that both the from and to layers are inclusive.
We should use a very low learning rate while we are tuning layers of a pre-trained model on a new dataset. A low learning rate is advised because, on the layers that we are fine-tuning, we should restrict the magnitude of the modifications we make to the representations.