BackpropagationHard
MSE Loss Gradient
~15 mincode completion
Implement mse_gradient(y_true, y_pred) that returns the gradient vector.
Examples
y_pred above y_true: positive gradient
- Input
- mse_gradient([1, 2, 3], [2, 2, 2])
- Output
- [0.66667, 0, -0.66667]
Perfect predictions: zero gradient
- Input
- mse_gradient([1, 2, 3], [1, 2, 3])
- Output
- [0, 0, 0]
Single prediction
- Input
- mse_gradient([3], [1])
- Output
- [-4]
Hints
Hint 1
Work directly with the arguments y_true, y_pred and return the result rather than printing it.
Hint 2
Watch for this: subtracted y pred from y true wrong sign.
Requirements
y_true: Ground truth values, shape (m,)y_pred: Predicted values, shape (m,)Return Gradient vector of shape (m,): 2 * (y_pred - y_true) / m
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~15 min
••••••••••••••••
8 employers weight this skill
4 frontier labs, 2 big tech firms, 1 autonomy company, 1 enterprise vendor. Top match scores 91.
Python
import numpy as np
def mse_gradient(y_true: np.ndarray, y_pred: np.ndarray) -> np.ndarray:
"""
Compute the gradient of MSE loss w.r.t. predictions.
Args:
y_true: Ground truth values, shape (m,)
y_pred: Predicted values, shape (m,)
Returns:
Gradient vector of shape (m,): 2 * (y_pred - y_true) / m
"""
# YOUR CODE HERE
pass