Neural Mastery
← Practice
Mean Squared Error Loss Gradienteasy

Mean Squared Error Loss Gradient

easy Neural Networks by Hand 15 min

NumPy allowed · plain Python earns 10 bonus points

Gradient descent step 1/4

Loss moves downhill

The update rule changes the parameters on each step.
Open lesson →
01parameters
02prediction
03loss
04gradient
θ = 4.000loss = 16.000∂L/∂θ = 8.000

θ ← θ − 0.14 · ∂L/∂θ

Compute the analytical gradient of Mean Squared Error loss w.r.t network predictions to initiate backpropagation.

Task

Implement mse_loss_backward(y_pred, y_true): with MSE = (1 / (N * D)) * sum((y_pred - y_true)^2), compute the derivative with respect to each prediction y_pred[i][j], which is (2 / (N * D)) * (y_pred[i][j] - y_true[i][j]). Here N is the number of rows and D is the number of columns. Round each gradient element to 4 decimal places.

Function Signature

mse_loss_backward(y_pred: list[list[float]], y_true: list[list[float]]) -> list[list[float]]

Examples

Example 1: Single sample 1D output
Input: y_pred = [[3, 5]] y_true = [[2, 1]]
Output: [[1, 4]]
Example 2: Batch of 2 samples
Input: y_pred = [[1], [4]] y_true = [[1], [2]]
Output: [[0], [2]]
Example 3: Exact match zero gradient
Input: y_pred = [[2.5, -1]] y_true = [[2.5, -1]]
Output: [[0, 0]]

Constraints

  • y_pred and y_true have matching shape (N, D)
  • 1 <= N, D <= 100
Python3Saved ✓
def mse_loss_backward(y_pred: list[list[float]], y_true: list[list[float]]) -> list[list[float]]:
"""Compute gradient of mean squared error with respect to predictions y_pred.

Args:
y_pred: Model predictions of shape (N, D).
y_true: Ground truth targets of shape (N, D).

Returns:
Gradient matrix dL/dy_pred of shape (N, D) rounded to 4 decimals.
"""
pass

Case 1: Single sample 1D output
Input: y_pred = [[3, 5]] y_true = [[2, 1]]
Expected: [[1, 4]]
Case 2: Batch of 2 samples
Input: y_pred = [[1], [4]] y_true = [[1], [2]]
Expected: [[0], [2]]
Case 3: Exact match zero gradient
Input: y_pred = [[2.5, -1]] y_true = [[2.5, -1]]
Expected: [[0, 0]]