評價此頁

torch.nn.functional.nll_loss#

torch.nn.functional.nll_loss(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')[原始碼]#

計算負對數似然損失。

更多細節請參見 NLLLoss

引數
  • input (Tensor) – (N,C)(N, C),其中 C = 類別數,或者在 2D Loss 的情況下為 (N,C,H,W)(N, C, H, W),或者在 K 維損失的情況下為 (N,C,d1,d2,...,dK)(N, C, d_1, d_2, ..., d_K),其中 K1K \geq 1input 應該為對數機率。

  • target (Tensor) – (N)(N),其中每個值滿足 0targets[i]C10 \leq \text{targets}[i] \leq C-1,或者在 K 維損失的情況下為 (N,d1,d2,...,dK)(N, d_1, d_2, ..., d_K),其中 K1K \geq 1

  • weight (Tensor, 可選) – 為每個類手動分配的重縮放權重。如果提供,則必須是大小為 C 的 Tensor。

  • size_average (bool, optional) – 已棄用(請參閱 reduction)。

  • ignore_index (int, 可選) – 指定一個被忽略且不計入輸入梯度的目標值。當 size_averageTrue 時,損失將根據非忽略目標進行平均。預設為 -100。

  • reduce (bool, optional) – 已棄用(請參閱 reduction)。

  • reduction (str, 可選) – 指定應用於輸出的歸約方式:'none' | 'mean' | 'sum''none':不進行歸約;'mean':輸出的總和將除以輸出中的元素數量;'sum':將對輸出進行求和。注意:size_averagereduce 正在被棄用,在此期間,指定這兩個引數中的任何一個都將覆蓋 reduction。預設為 'mean'

返回型別

張量

示例

>>> # input is of size N x C = 3 x 5
>>> input = torch.randn(3, 5, requires_grad=True)
>>> # each element in target has to have 0 <= value < C
>>> target = torch.tensor([1, 0, 4])
>>> output = F.nll_loss(F.log_softmax(input, dim=1), target)
>>> output.backward()