Freeze Backbone, Train Head
Problem Statement
Implement the standard transfer learning pattern: freeze the backbone and only optimize the head.
Background
In transfer learning, you freeze pretrained layers (backbone) and only train new layers (head). Frozen parameters don't receive gradient updates.
Your Task
The starter code creates a backbone and head. Freeze the backbone parameters so they won't be updated, and create an optimizer that only trains the head.
The training loop and verification are pre-filled.
Output Format
Returns a dictionary with "backbone_changed" (should be False), "head_changed" (should be True), "trainable_params", and "total_params".
Example:
None
{'backbone_changed': False, 'head_changed': True, 'trainable_params': 10, 'total_params': 78}- The function
freeze_backbone_test()starts by seeding withtorch.manual_seed(42)to ensure reproducibility, then creates a backbone with two linear layers and ReLU activations, and a head with one linear layer. - The backbone parameters are frozen, meaning their values will not be updated during training, while the head parameters are left trainable.
- The function then runs 3 training steps with input
[[1.0, 2.0, 3.0, 4.0]]and target[[1.0, 0.0]], which updates the head parameters but leaves the backbone parameters unchanged. - After training, the function checks for changes in the backbone and head parameters, counts the number of trainable and total parameters, and returns a dictionary with the results, including
trainable_paramsbeing the number of parameters in the head (10 in this case, since a linear layer with input size 4 and output size 2 has 2â‹…(4+1)=10 parameters) andtotal_paramsbeing the total number of parameters in the model (78 in this case).
Constraints:
- Freeze backbone with requires_grad=False
- Only pass head params to optimizer
- Verify backbone unchanged after training
Background Knowledge
The problem involves transfer learning, a technique in machine learning where a model trained on one task is re-purposed or fine-tuned for another related task. In this case, we're dealing with a backbone (a pre-trained model) and a head (a new, smaller model added on top of the backbone). The goal is to freeze the backbone, meaning its parameters are not updated during training, and only train the head. This approach is useful when we want to leverage the features learned by the backbone and adapt them to a new task.
In PyTorch, modules (like Linear and ReLU) have parameters (learnable weights and biases) that are updated during training. We can control which parameters are updated by creating an optimizer that only operates on a specific subset of parameters. In this problem, we need to create an optimizer that only updates the head's parameters, leaving the backbone's parameters unchanged. This requires understanding how to work with PyTorch's modules, parameters, and optimizers.
The problem also involves understanding how to freeze parameters in PyTorch. When a parameter is frozen, its value is not updated during training, even if it's included in the optimizer. This is typically done by setting the requires_grad attribute of the parameter to False. However, simply freezing the parameters is not enough; we also need to ensure that the optimizer is only operating on the head's parameters.
Algorithm/Approach
The general approach to solving this problem involves the following steps:
- Create the backbone and head models
- Freeze the backbone's parameters
- Create an optimizer that only operates on the head's parameters
- Train the model for a few steps to verify that the backbone's parameters are not updated
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.