Back to Practice
#0080

L2 Regularized Loss

MediumML18 min20 XP

Problem

Given base_loss, weights, and lambda_value, return base_loss + lambda_value * sum(weight squared). Do not penalize bias separately; weights contains only penalized weights.

Why This Matters

Regularization discourages overly large weights and can improve generalization when models overfit.

Function Signature

def l2_loss(base_loss, weights, lambda_value):

Examples

Example 1
Inputbase_loss=2, weights=[3,4], lambda=0.1
Output4.5

Penalty is 9 + 16 = 25, so add 2.5.

Constraints

  • weights can be empty.
  • lambda_value can be zero.
  • Return a number.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Two weights
Input2, [3,4], 0.1
Expected4.5

2 + 0.1 * 25.

Hidden Test Categories
zero lambdaempty weightsnegative weights