Optimizer Parameter Groups: What Not to Decay

easy
Unit 4 · Optimization & CNNs0/3 to milestone

Standard practice when training transformers (GPT, BERT, ViT) is to apply weight decay to weight matrices but not to biases, normalisation parameters or embeddings (decaying a LayerNorm scale towards 0 just hurts).

Given a dict of named parameters (NumPy arrays), implement:

  • split_param_groups(params, no_decay_keywords=("bias", "norm", "embed")): return (decay, no_decay), two sorted lists of names. A parameter is not decayed if it has fewer than 2 dimensions or its name contains any of the keywords (case-insensitive).
  • count_params(params, names): the total number of elements in those parameters
  • apply_decay(params, decay_names, lr, wd): return a new dict where only the decayed parameters are multiplied by (1 - lr * wd) (decoupled, AdamW-style), leaving the inputs unchanged
#optimization#regularization#training2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Optimizer Parameter Groups: What Not to Decay · TechGradify