評價此頁

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)[原始碼]#

執行前向傳播。

返回型別

張量