easy primitives

Implement L2 Regularization

Implement L2 regularization (weight decay) penalty.

The L2 regularization term is:

$$\text{L2} = \frac{\lambda}{2} \sum_{i} w_i^2$$

where $\lambda$ is the regularization strength and $w$ are the model weights.

Input:

  • weights: 1D tensor of model weights
  • lambda_: regularization strength (float)

Output: A scalar representing the L2 penalty

Hints

regularization optimization basics
Detecting runtime...