PIXELBANKv8.2.1
Menu

L2 Regularization Penalty

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=λ2mW2L2_{term} = \frac{\lambda}{2m} \sum ||W||^2

Where:

  • λ\lambda (lambda) is the regularization strength
  • mm is the number of training examples
  • W2||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×12 \times 1) × 25 = 12.5

Constraints:

  • 0 < lambda_reg <= 10
  • m >= 1
  • Each weight matrix is a 2D list
Editor

Test Results

0/0
Run code to see test results.