PIXELBANKv9.1.0
Menu

Selective Parameter Freezing

Problem Statement

Freeze specific layers of a model while keeping others trainable.

Background

In transfer learning, you often freeze pretrained layers and only train the top layers. You can freeze parameters by setting requires_grad = False.

Your Task

The starter code creates a 2-layer model. Freeze layer1's parameters so they don't update during training, then count the total, trainable, and frozen parameters in the model.

Output Format

Returns a dictionary with "total_params", "trainable_params", "frozen_params", and "layer1_frozen".

Example:

Input:
None
Output:
{'total_params': 58, 'trainable_params': 18, 'frozen_params': 40, 'layer1_frozen': True}
Reasoning:
  • The model is created with nn.Sequential containing two linear layers: nn.Linear(4, 8) (layer1) and nn.Linear(8, 2) (layer2).
  • Layer1 has 4â‹…8+8=404 \cdot 8 + 8 = 40 parameters (weights and bias), and layer2 has 8â‹…2+2=188 \cdot 2 + 2 = 18 parameters, making a total of 40+18=5840 + 18 = 58 parameters.
  • By setting requires_grad=False on layer1's parameters, all 40 parameters in layer1 are frozen, leaving 18 parameters in layer2 trainable.
  • The function then returns a dictionary with the total parameter count (58), trainable parameter count (18), frozen parameter count (40), and a boolean indicating that all layer1 parameters are frozen (True).

Constraints:

  • Use nn.Sequential with named layers
  • Freeze by setting requires_grad = False
  • Count parameters using .numel()
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.
Selective Parameter Freezing - Medium | PixelBank