Stretch: L2 Regularization (Ridge)
Intermediate classical-mllinear-regressionregularizationstretch
Implement:
def ridge_grad(
input: np.ndarray,
weight: np.ndarray,
bias: np.ndarray | None,
target: np.ndarray,
lam: float,
) -> tuple[np.ndarray, np.ndarray | None]:
"""
Compute the gradient of the MSE loss with L2 regularization.
"""
Your function should:
- Compute the base MSE gradient using
03-mse-gradient's mse_gradient.
- Extend just the weight gradient with the additional penalty term derived in Theory, one that grows with both the weight's own magnitude and
lam.
- Leave the bias gradient untouched, bias is never regularized, including the
bias is None case.