We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Loss Functions step 1 of 8
Implement Mean Squared Error
Mean squared error (MSE) measures how far numerical predictions are from their targets. Subtract each prediction from its target, square the error, and average those squared errors.
For targets [1, 2, 3] and predictions [1, 4, 2], the errors are
[0, -2, 1]. Their squares are [0, 4, 1], so the MSE is 5 / 3,
approximately 1.666667.
Squaring prevents positive and negative errors from cancelling. It also makes large errors contribute more: an error of 4 contributes 16, while an error of 2 contributes 4. An exact match has zero loss.
Your task
Implement mse_loss(y_true, y_pred) for two nonempty 1D floating-point
tensors of the same shape. Return a scalar tensor containing the mean
of all squared differences. Do not return the sum or take a square root.
Use basic tensor operations rather than a built-in MSE loss function. Inputs have matching shapes, so you do not need to implement broadcasting or input validation.
The plot shows one prediction against a target of zero. Its height is the squared error for that prediction; this exercise averages such errors.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Predict a numerical value
Regression predicts a number, such as a price or a temperature. A linear model combines the input features using weights and a bias:
$$\hat y=Xw+b.$$
Mean squared error (MSE) averages the squared differences between predictions
and targets. For N examples with one target each,
$$L=\frac{1}{N}\sum_i(\hat y_i-y_i)^2.$$
Squaring makes larger errors count more. An error of 4 contributes 16 to the
sum, while an error of 2 contributes 4.
The gradient with respect to each prediction is 2 * (prediction - target) / N.
For several outputs per example, check whether the loss averages over examples
only or over all output elements. The denominator, and therefore the gradient,
will differ.
More features can represent curves
Polynomial regression uses features such as [1, x, x²]. The predicted curve
can be nonlinear in x while remaining linear in its learned coefficients.
Large powers can create very different feature scales, making the fitting
problem more sensitive to numerical error and learning-rate choices.
Regularization discourages large weights
Adding the penalty lambda * sum(w²) adds 2 * lambda * w to the weight
gradient. Some definitions include a factor of one half, so follow the stated
loss rather than assuming a convention.
This penalty is used in ridge regression. It can make fitting more stable when features are strongly correlated, but feature scaling still matters: rescaling a feature changes the coefficient needed to represent the same prediction.
Why squared error favors the mean
For targets [1, 3], predicting 2 for both gives squared errors [1, 1] and
MSE 1. Predicting 1 for both gives errors [0, 4] and MSE 2.
The mean minimizes squared error for a constant prediction. More generally,
when the relevant expectations exist, the best prediction under expected squared
loss is the target's conditional mean given the input.
Large outliers can strongly affect this mean and the fitted model. Evaluate on held-out data to check whether a low training loss carries over to new examples.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.