PIXELBANKv8.2.1
Menu

Mean Squared Error Gradient

Implement a function to compute the gradient of the Mean Squared Error (MSE) loss with respect to predictions, a crucial step in training models using optimization techniques. This task involves understanding the mathematical foundation of MSE and its derivative.

The Mean Squared Error is a measure of the average squared difference between predictions y^\hat{y} and actual targets yy, given by the formula 1ni=1n(y^iyi)2\frac{1}{n}\sum_{i=1}^{n}(\hat{y}_i - y_i)^2. To minimize this loss, we need to calculate its gradient with respect to each prediction y^i\hat{y}_i.

Here are the steps to calculate the gradient:

  1. Define the MSE loss function.
  2. Apply the chain rule and the sum rule of calculus to differentiate the MSE loss with respect to each y^i\hat{y}_i.
MSE=1ni=1n(y^iyi)2\text{MSE} = \frac{1}{n}\sum_{i=1}^{n}(\hat{y}_i - y_i)^2

This technique is widely used in machine learning for training regression models.

Example:

Input:
mse_gradient([2.0, 4.0], [1.0, 3.0])
Output:
[1.0, 1.0]
Reasoning:

gradient_i = 2/n × (pred_i - target_i) = 2/2 × 1 = 1 for each

Constraints:

  • Both predictions and targets have n elements where 1 ≤ n ≤ 1000
  • Return the gradient vector rounded to 4 decimal places
Editor

Test Results

0/0
Run code to see test results.