Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is freezing/unfreezing a layer in neural networks?

I have been playing around with neural networks for quite a while now, and recently came across the terms "freezing" & "unfreezing" the layers before training a neural network while reading about transfer learning & am struggling with understanding their usage.

  • When is one supposed to use freezing/unfreezing?
  • Which layers are to freezed/unfreezed? For instance, when I import a pre-trained model & train it on my data, is my entire neural-net except the output layer freezed?
  • How do I determine if I need to unfreeze?
  • If so how do I determine which layers to unfreeze & train to improve model performance?
like image 352
Nizam Avatar asked Jun 06 '20 08:06

Nizam


People also ask

What does freezing a layer mean?

What does Freezing a Layer mean? Freezing a layer prevents its weights from being modified. This technique is often used in transfer learning, where the base model(trained on some other dataset)is frozen.

What does freezing a model mean?

Freezing the model means producing a singular file containing information about the graph and checkpoint variables, but saving these hyperparameters as constants within the graph structure.

Do we freeze layers in fine-tuning?

As explained here, the initial layers learn very general features and as we go higher up the network, the layers tend to learn patterns more specific to the task it is being trained on. Thus, for fine-tuning, we want to keep the initial layers intact ( or freeze them ) and retrain the later layers for our task.

How many layers does it take to unfreeze transfer?

I usually freeze the feature extractor and unfreeze the classifier or last two/three layers. It depends on your dataset, if you have enough data and computation power you can unfreeze more layers and retrain the model.


2 Answers

I would just add to the other answer that this is most commonly used with CNNs and the amount of layers that you want to freeze (not train) is "given" by the amount of similarity between the task that you are solving and the original one (the one that the original network is solving).

If the tasks are very similar, let's say that you are using CNN pretrained on imagenet and you just want to add some other "general" objects that the network should recognize then you might get away with training just the dense top of the network.

The more dissimilar the tasks are, the more layers of the original network you will need to unfreeze during the training.

like image 163
Matus Dubrava Avatar answered Oct 10 '22 15:10

Matus Dubrava


By freezing it means that the layer will not be trained. So, its weights will not be changed.

Why do we need to freeze such layers?

Sometimes we want to have deep enough NN, but we don't have enough time to train it. That's why use pretrained models that already have usefull weights. The good practice is to freeze layers from top to bottom. For examle, you can freeze 10 first layers or etc.


For instance, when I import a pre-trained model & train it on my data, is my entire neural-net except the output layer freezed?
- Yes, that's may be a case. But you can also don't freeze a few layers above the last one.

How do I freeze and unfreeze layers?
- In keras if you want to freeze layers use: layer.trainable = False
And to unfreeze: layer.trainable = True

If so how do I determine which layers to unfreeze & train to improve model performance?
- As I said, the good practice is from top to bottom. You should tune the number of frozen layers by yourself. But take into account that the more unfrozen layers you have, the slower is training.

like image 20
Yoskutik Avatar answered Oct 10 '22 15:10

Yoskutik