Recommended Free Tools
torch.nn.MSELoss squares the difference between corresponding input and target elements. Its default, reduction="mean", averages those squared differences across every element in both tensors—not just across the batch. Choose "sum" to add them or "none" to keep the elementwise losses. For the intended comparisons, make input and target the same shape; a broadcastable mismatch can produce a result that is mathematically valid but compares the wrong values.
What does PyTorch MSELoss calculate?
For each corresponding element, mean squared error is:
As an Amazon Associate I earn from qualifying purchases.
(input - target) ** 2
The PyTorch MSELoss API accepts tensors of any number of dimensions and documents the input and target as having the same shape. It then applies the selected reduction to the elementwise squared differences.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsHow do the reduction options change the result?
Let N be the total number of elements in the tensors. The reduction controls what happens to the elementwise squared errors:
#1 Best Overall
| reduction | Result | Output shape |
|---|---|---|
"none" |
Leaves every squared difference intact. | Same shape as input and target. |
"sum" |
Adds all squared differences. | A scalar. |
"mean" (default) |
Adds all squared differences and divides by N. |
A scalar. |
For an input shaped [batch, channels, height, width], the default mean includes every batch, channel, height, and width element. It is not automatically a mean of per-sample means. If each sample should have equal weight regardless of its number of elements, calculate that aggregation explicitly rather than assuming the default reduction does it.
Reduction affects the scale and aggregation of the loss, so it can also affect the scale of gradients during training. The API lists size_average and reduce as deprecated legacy arguments; when specified, they override reduction. New code should use reduction.
Rank #2
Should you use nn.MSELoss or F.mse_loss?
Both provide the usual "none", "sum", and "mean" reductions. Use the module form when you want a reusable loss object, or the functional form for a direct call:
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchimport torch.nn as nn
import torch.nn.functional as F
criterion = nn.MSELoss(reduction="mean")
loss_from_module = criterion(prediction, target)
loss_from_function = F.mse_loss(prediction, target, reduction="mean")
The functional API documentation also lists an optional weight argument. Check the documentation for your installed PyTorch version before relying on that option, since the cited functional page is the current main documentation and can differ from a released version.
Rank #3
Why should input and target shapes match?
Matching shapes makes the elementwise pairing explicit: each prediction is compared with its intended target. PyTorch’s MSELoss API documents same-shaped input and target. When shapes differ, a compatible pair may trigger a warning and broadcast, while an incompatible pair may fail.
Broadcasting aligns dimensions from the trailing end. Dimensions must match, one must be size 1, or one tensor must have no dimension at that position. A size-one dimension can expand to match the other tensor. These rules are described in PyTorch’s broadcasting semantics.
Rank #4
A subtle [B, 1] versus [B] mismatch
Suppose predictions have shape [B, 1] and targets have shape [B]. From the right, PyTorch aligns the prediction’s size-1 dimension with the target’s size-B dimension. The tensors can therefore broadcast to [B, B]. Instead of comparing each prediction with its matching target, the resulting loss can compare each prediction with every target.
Free tools Windows power users keep installed
One-click scans. No signup required.
If each row is meant to pair with one target, make the target shape [B, 1] before computing the loss. Do not infer that equal element counts make different shapes interchangeable: shape alignment determines which values are compared.
How to check and correct shapes
- Inspect both shapes immediately before calculating the loss, for example with
print(prediction.shape, target.shape). - Confirm that each dimension represents the intended batch, channel, or feature pairing—not merely that the tensors contain the same number of elements.
- If dimensions should match but are arranged differently, use an intentional operation such as
reshapeorunsqueezeto create the target shape that reflects the intended pairing. - Check the resulting shapes again. If a broadcast is deliberate, inspect the expanded dimensions and verify that they represent the comparisons you want.
- Pass the aligned tensors to
nn.MSELossorF.mse_loss.
PyTorch’s broadcasting documentation notes that older pointwise behavior that flattened some equal-element-count inputs was deprecated. Do not rely on flattening or accidental compatibility to define your loss; express the intended dimensions explicitly.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




