Loading...
Calculate the L2 regularization term (weight decay) for a neural network.
L2 regularization adds a penalty to the loss function based on the squared magnitude of weights:
L2term=2mλ∑∣∣W∣∣2
Where:
This encourages smaller weights, reducing overfitting.
Write a function l2_penalty(weights_list, lambda_reg, m) that computes the L2 regularization term.
weights_list is a list of weight matrices (each as a list of lists).
Return a float rounded to 4 decimal places.
weights_list=[[[3, 4]]], lambda_reg=1.0, m=1
12.5
Sum of squares: 3² + 4² = 9 + 16 = 25. Penalty: (1.0 / 2×1) × 25 = 12.5