MSE Loss Gradient

Hard
~15 min
code completion

Gradient of MSE Loss

To train a model with gradient descent, we need to compute how the loss changes with respect to the predictions — the gradient .

For MSE loss :

This gradient vector is then backpropagated through the network to update the weights.

Note: prediction minus true (not true minus prediction). Getting this sign wrong flips gradient descent into gradient ascent!

Your task:

Implement mse_gradient(y_true, y_pred) that returns the gradient vector.

Example Tests

y_pred above y_true: positive gradient

Input: {"y_pred":[2,2,2],"y_true":[1,2,3]}

Expected: [0.66667,0,-0.66667]

Perfect predictions: zero gradient

Input: {"y_pred":[1,2,3],"y_true":[1,2,3]}

Expected: [0,0,0]

Single prediction

Input: {"y_pred":[1],"y_true":[3]}

Expected: [-4]

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.