CLASS torch.nn.
MSELoss
(size_average=None, reduce=None, reduction='mean')
Creates a criterion that measures the mean squared error (squared L2 norm) between each element in the input x and target yy.
创建一个准则测量输入x和目标y每个元素间的平方二范数。
x和y可以是任意形状的张量,每个张量总的元素个数为n。
Note: size_average
and reduce
are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction
. Default: 'mean'
size_average和reduce正在被弃用,因此我们只看reduction参数。
reduction='none'
The unreduced (i.e. with reduction
set to 'none'
) loss can be described as:
返回每个元素的平方二范数,shape与输入相同。
reduction='sum'和reduction='mean'
对所有对应元素的平方二范数求和或求平均。
a = torch.tensor([[[1., 2.]], [[1, 2]]]) # [2, 1, 2] b = torch.tensor([[[2., 4.]], [[2, 4.]]]) # [2, 1, 2] C = torch.nn.MSELoss(reduction='none') loss = C(a, b) print(a) print(b) print(loss) >>> tensor([[[1., 2.]], [[1., 2.]]]) tensor([[[2., 4.]], [[2., 4.]]]) tensor([[[1., 4.]], [[1., 4.]]]) # 分别计算对应元素的平方二范数
C = torch.nn.MSELoss(reduction='sum') loss = C(a, b)
print(loss)
>>> tensor(10.) # 所有元素平方二范数之和
C = torch.nn.MSELoss(reduction='mean') loss = C(a, b)
print(loss)
>>> tensor(2.5000) # 所有元素平方二范数的均值