评价此页

MSELoss#

class torch.nn.MSELoss(size_average=None, reduce=None, reduction='mean')[源代码]#

创建一个标准,用于衡量输入 xx 和目标 yy 之间每个元素的平均平方误差(平方 L2 范数)。

未约简的(即 reduction 设置为 'none')损失可以描述为

(x,y)=L={l1,,lN},ln=(xnyn)2,\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = \left( x_n - y_n \right)^2,

其中 NN 是批次大小。如果 reduction 不是 'none' (默认为 'mean'),则:

(x,y)={mean(L),if reduction=‘mean’;sum(L),if reduction=‘sum’.\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}

xx and yy are tensors of arbitrary shapes with a total of NN elements each.

The mean operation still operates over all the elements, and divides by NN.

可以通过将 reduction = 'sum' 来避免除以 NN

参数
  • size_average (bool, optional) – 已弃用 (参见 reduction)。默认情况下,损失值在批次中的每个损失元素上取平均值。请注意,对于某些损失,每个样本有多个元素。如果字段 size_average 设置为 False,则损失值在每个小批次中而是求和。当 reduceFalse 时忽略。默认值:True

  • reduce (bool, optional) – 已弃用 (参见 reduction)。默认情况下,损失值在每个小批次中根据 size_average 对观测值进行平均或求和。当 reduceFalse 时,返回每个批次元素的损失值,并忽略 size_average。默认值:True

  • reduction (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_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'

形状
  • 输入: ()(*),其中 * 表示任意数量的维度。

  • 目标:()(*),与输入形状相同。

示例

>>> loss = nn.MSELoss()
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5)
>>> output = loss(input, target)
>>> output.backward()
forward(input, target)[源代码]#

执行前向传播。

返回类型

张量