Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.

L2 Regularization Gradient

~10 mincode completion

Gradient of L2 Regularization

When using gradient descent, we need the gradient of the regularization term with respect to the weights:

In practice, this gradient is added to the task loss gradient before the parameter update:

This is why L2 regularization is equivalent to weight decay: the update subtracts a small fraction of the current weights at every step.

Your task:

Implement l2_gradient(weights, lambda_reg) that returns .

Example Tests

Standard gradient

Input: {"weights":[1,2,3],"lambda_reg":0.1}

Expected: [0.2,0.4,0.6]

Zero weights: zero gradient

Input: {"weights":[0,0],"lambda_reg":5}

Expected: [0,0]

lambda=0.5: 2*0.5=1 so gradient equals weights

Input: {"weights":[-1,2,-3],"lambda_reg":0.5}

Expected: [-1,2,-3]

Python
import numpy as np

def l2_gradient(weights: np.ndarray, lambda_reg: float) -> np.ndarray:
    """
    Compute the gradient of the L2 penalty w.r.t. weights.

    Args:
        weights:    Weight vector (any shape)
        lambda_reg: Regularization strength (lambda)

    Returns:
        Gradient vector: 2 * lambda_reg * weights
    """
    # YOUR CODE HERE
    pass
Loading docs…