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
None
{'decay_param_count': 2, 'no_decay_param_count': 4, 'decay_param_names': ['0.weight', '2.weight'], 'total_params': 6}- The function
selective_decay_test()creates aSequentialmodel with twoLinearlayers and oneBatchNorm1dlayer, resulting in 6 parameters: 2 weights and 2 biases from theLinearlayers, and 2 parameters from theBatchNorm1dlayer. - The parameters are then separated into two groups:
decay(weight parameters ofLinearlayers) andno_decay(bias parameters andBatchNormparameters), yielding 2 parameters in thedecaygroup and 4 parameters in theno_decaygroup. - The names of the parameters with weight decay are
0.weightand2.weight, corresponding to the weights of the twoLinearlayers. - 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