📘
L2 Regularization Penalty
EasyDeep Learning
Problem Statement
Calculate the L2 regularization term (weight decay) for a neural network.
Background
L2 regularization adds a penalty to the loss function based on the squared magnitude of weights:
L2term=2mλ∑∣∣W∣∣2
Where:
- λ (lambda) is the regularization strength
- m is the number of training examples
- ∣∣W∣∣2 is the sum of squared weights (Frobenius norm squared)
This encourages smaller weights, reducing overfitting.
Your Task
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).
Output Format
Return a float rounded to 4 decimal places.
Example:
Input:
weights_list=[[[3, 4]]], lambda_reg=1.0, m=1
Output:
12.5
Reasoning:
Sum of squares: 3² + 4² = 9 + 16 = 25. Penalty: (1.0 / 2×1) × 25 = 12.5
Constraints:
- 0 < lambda_reg <= 10
- m >= 1
- Each weight matrix is a 2D list
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.