PIXELBANKv9.1.0
Menu

Weight Decay for Specific Layers

Problem Statement

Apply weight decay only to weight parameters, not biases or normalization layers.

Background

Weight decay (L2 regularization) should typically only be applied to weight matrices, not bias terms or BatchNorm parameters. This is a common best practice.

Your Task

The starter code creates a model with Linear and BatchNorm layers. Separate the parameters into two groups: Linear weight matrices (which should get weight decay of 0.01) and everything else (no decay). Create an AdamW optimizer with these two parameter groups.

Output Format

Returns a dictionary with "decay_param_count", "no_decay_param_count", "decay_param_names", and "total_params".

Example:

Input:
None
Output:
{'decay_param_count': 2, 'no_decay_param_count': 4, 'decay_param_names': ['0.weight', '2.weight'], 'total_params': 6}
Reasoning:
  • The function selective_decay_test() creates a Sequential model with two Linear layers and one BatchNorm1d layer, resulting in 6 parameters: 2 weights and 2 biases from the Linear layers, and 2 parameters from the BatchNorm1d layer.
  • The parameters are then separated into two groups: decay (weight parameters of Linear layers) and no_decay (bias parameters and BatchNorm parameters), yielding 2 parameters in the decay group and 4 parameters in the no_decay group.
  • The names of the parameters with weight decay are 0.weight and 2.weight, corresponding to the weights of the two Linear layers.
  • The function returns a dictionary with the counts and names of parameters with and without weight decay, as well as the total parameter count, resulting in the output {'decay_param_count': 2, 'no_decay_param_count': 4, 'decay_param_names': ['0.weight', '2.weight'], 'total_params': 6}.

Constraints:

  • Weight decay only on Linear weight matrices
  • No decay on biases and BatchNorm params
  • Use AdamW optimizer
🔒

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.
Weight Decay for Specific Layers - Medium | PixelBank