oneflow.nn.MSELoss¶
-
class
oneflow.nn.
MSELoss
(reduction: str = 'mean')¶ Creates a criterion that measures the mean squared error (squared L2 norm) between each element in the input \(x\) and target \(y\).
The unreduced (i.e. with
reduction
set to'none'
) loss can be described as:\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = \left( x_n - y_n \right)^2,\]where \(N\) is the batch size. If
reduction
is not'none'
(default'mean'
), then:\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}\end{split}\]\(x\) and \(y\) are tensors of arbitrary shapes with a total of \(n\) elements each.
The mean operation still operates over all the elements, and divides by \(n\).
The division by \(n\) can be avoided if one sets
reduction = 'sum'
.The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.nn.MSELoss.html.
- Parameters
reduction (string, optional) – Specifies the reduction to apply to the output:
'none'
|'mean'
|'sum'
.'none'
: no reduction will be applied,'mean'
: the sum of the output will be divided by the number of elements in the output,'sum'
: the output will be summed. Default:'mean'
- Shape:
Input: \((N, *)\) where \(*\) means, any number of additional dimensions
Target: \((N, *)\), same shape as the input
For example:
>>> import oneflow as flow >>> import numpy as np >>> input = flow.tensor( ... [[-0.02557137, 0.03101675, 1.37493674], ... [0.25599439, -1.08372561, -0.21006816]], dtype=flow.float32) >>> target = flow.tensor( ... [[-1.53105064, -0.68137555, 0.5931354], ... [-0.49158347, 0.93673637, 0.1324141]], dtype=flow.float32) >>> m = flow.nn.MSELoss(reduction="none") >>> out = m(input, target) >>> out tensor([[2.2665, 0.5075, 0.6112], [0.5589, 4.0823, 0.1173]], dtype=oneflow.float32) >>> m = flow.nn.MSELoss(reduction="mean") >>> out = m(input, target) >>> out tensor(1.3573, dtype=oneflow.float32) >>> m = flow.nn.MSELoss(reduction="sum") >>> out = m(input, target) >>> out tensor(8.1436, dtype=oneflow.float32)