MSELoss#
- class torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')[源代码]#
创建一个标准,用于衡量输入 和目标 之间每个元素的平均平方误差(平方 L2 范数)。
未约简的(即
reduction设置为'none')损失可以描述为其中 是批次大小。如果
reduction不是'none'(默认为'mean'),则:and are tensors of arbitrary shapes with a total of elements each.
The mean operation still operates over all the elements, and divides by .
可以通过将
reduction = 'sum'来避免除以 。- 参数
size_average (bool, optional) – 已弃用 (参见
reduction)。默认情况下,损失值在批次中的每个损失元素上取平均值。请注意,对于某些损失,每个样本有多个元素。如果字段size_average设置为False,则损失值在每个小批次中而是求和。当reduce为False时忽略。默认值:Truereduce (bool, optional) – 已弃用 (参见
reduction)。默认情况下,损失值在每个小批次中根据size_average对观测值进行平均或求和。当reduce为False时,返回每个批次元素的损失值,并忽略size_average。默认值:Truereduction (str, 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. Note:size_averageandreduceare in the process of being deprecated, and in the meantime, specifying either of those two args will overridereduction. Default:'mean'
- 形状
输入: ,其中 表示任意数量的维度。
目标:,与输入形状相同。
示例
>>> loss = nn.MSELoss() >>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randn(3, 5) >>> output = loss(input, target) >>> output.backward()