📘
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^ and actual targets y, given by the formula n1∑i=1n(y^i−yi)2. To minimize this loss, we need to calculate its gradient with respect to each prediction y^i.
Here are the steps to calculate the gradient:
- Define the MSE loss function.
- Apply the chain rule and the sum rule of calculus to differentiate the MSE loss with respect to each y^i.
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
Python 3.13.1
Test Results
0/0Run code to see test results.