Compare paired observations
Take one prediction and the matching observed target. Their difference is a residual. Its sign tells you whether the prediction is high or low. Averaging signed residuals alone could hide errors that cancel one another.
Accumulate the loss
With predictions [2, 3, 5] and targets [1, 4, 3], the residuals are 1, -1 and 2. Squaring gives 1, 1 and 4. Add the squares, then divide 6 by the number of observations, 3. The mean squared error is 2.
Match types and lengths
The loop index is an integer for at. The floating-point count supplies a same-type divisor for squares in both execution paths. For inputs supplied by a caller, check that the lengths match and that at least one pair exists before you compute the mean.
Use held-out data
Training chooses a slope or other parameters. Evaluation asks how those fixed parameters perform on examples that did not determine them. Keep a second vector of test targets when comparing two models. A low training loss alone says little about unseen observations.
Try the program
Expected output
2
Practice
Replace the predictions with the three actual targets. What is the best possible value of this loss?
Hint
Each residual should be exactly zero for these inputs.
Worked solution
All squared residuals are zero, so the mean squared error is zero.
Find
[2.0, 3.0, 5.0]Replace with
[1.0, 4.0, 3.0]Output after the change
0