評價此頁

NLLLoss#

class torch.nn.modules.loss.NLLLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')[原始碼]#

負對數似然損失。它適用於訓練一個有 C 個類別的分類問題。

如果提供了可選引數 weight,它應該是一個一維張量,為每個類別分配權重。當你有一個不平衡的訓練集時,這尤其有用。

透過前向呼叫輸入的 input 期望包含每個類別的對數機率。 input 必須是一個大小為 (minibatch,C)(minibatch, C)(minibatch,C,d1,d2,...,dK)(minibatch, C, d_1, d_2, ..., d_K) with K1K \geq 1 for the K-dimensional case. The latter is useful for higher dimension inputs, such as computing NLL loss per-pixel for 2D images.

在神經網路中,透過在網路的最後一層新增 LogSoftmax 層可以輕鬆獲得對數機率。如果你不想新增額外的層,也可以使用 CrossEntropyLoss

此損失期望的 target 應該是類索引,範圍在 [0,C1][0, C-1] 內,其中 C = number of classes。如果指定了 ignore_index,此損失也將接受該類索引(該索引不一定在類別範圍內)。

未約簡的(即 reduction 設定為 'none')損失可以描述為

(x,y)=L={l1,,lN},ln=wynxn,yn,wc=weight[c]1{cignore_index},\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \\ l_n = - w_{y_n} x_{n,y_n}, \\ w_{c} = \text{weight}[c] \cdot \mathbb{1}\{c \not= \text{ignore\_index}\},

其中 xx 是輸入,yy 是目標,ww 是權重,NN 是批次大小。如果 reduction 不是 'none' (預設為 'mean'),則:

(x,y)={n=1N1n=1Nwynln,if reduction=‘mean’;n=1Nln,if reduction=‘sum’.\ell(x, y) = \begin{cases} \sum_{n=1}^N \frac{1}{\sum_{n=1}^N w_{y_n}} l_n, & \text{if reduction} = \text{`mean';}\\ \sum_{n=1}^N l_n, & \text{if reduction} = \text{`sum'.} \end{cases}
引數
  • weight (Tensor, optional) – 手動為每個類別指定的重縮放權重。如果指定,它必須是一個大小為 C 的張量。否則,假定其所有元素為 1。

  • size_average (bool, optional) – 已棄用 (請參閱 reduction)。預設情況下,損失在批次中的每個損失元素上進行平均。請注意,對於某些損失,每個樣本有多個元素。如果 size_average 欄位設定為 False,則損失在每個小批次中進行累加。當 reduce 設定為 False 時忽略。預設為 None

  • ignore_index (int, optional) – 指定一個被忽略的目標值,它不會對輸入梯度做出貢獻。當 size_averageTrue 時,損失在非忽略的目標上進行平均。

  • reduce (bool, optional) – 已棄用 (請參閱 reduction)。預設情況下,損失根據 size_average 在每個小批次中進行平均或累加。當 reduceFalse 時,返回每個批次元素的損失,並忽略 size_average。預設為 None

  • reduction (str, optional) – 指定應用於輸出的縮減方式:'none' | 'mean' | 'sum''none':不應用縮減,'mean':取輸出的加權平均值,'sum':對輸出進行累加。注意:size_averagereduce 正在被棄用,在此期間,指定其中任何一個引數將覆蓋 reduction。預設為 'mean'

形狀:
  • 輸入:(N,C)(N, C)(C)(C),其中 C = number of classesN = batch size,或者 (N,C,d1,d2,...,dK)(N, C, d_1, d_2, ..., d_K) with K1K \geq 1 for the K-dimensional loss.

  • Target: (N)(N) or ()(), where each value is 0targets[i]C10 \leq \text{targets}[i] \leq C-1, or (N,d1,d2,...,dK)(N, d_1, d_2, ..., d_K) with K1K \geq 1 in the case of K-dimensional loss.

  • 輸出:如果 reduction'none',形狀為 (N)(N)(N,d1,d2,...,dK)(N, d_1, d_2, ..., d_K) with K1K \geq 1 in the case of K-dimensional loss. Otherwise, scalar.

示例

>>> log_softmax = nn.LogSoftmax(dim=1)
>>> loss_fn = nn.NLLLoss()
>>> # input to NLLLoss is of size N x C = 3 x 5
>>> input = torch.randn(3, 5, requires_grad=True)
>>> # each element in target must have 0 <= value < C
>>> target = torch.tensor([1, 0, 4])
>>> loss = loss_fn(log_softmax(input), target)
>>> loss.backward()
>>>
>>>
>>> # 2D loss example (used, for example, with image inputs)
>>> N, C = 5, 4
>>> loss_fn = nn.NLLLoss()
>>> data = torch.randn(N, 16, 10, 10)
>>> conv = nn.Conv2d(16, C, (3, 3))
>>> log_softmax = nn.LogSoftmax(dim=1)
>>> # output of conv forward is of shape [N, C, 8, 8]
>>> output = log_softmax(conv(data))
>>> # each element in target must have 0 <= value < C
>>> target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, C)
>>> # input to NLLLoss is of size N x C x height (8) x width (8)
>>> loss = loss_fn(output, target)
>>> loss.backward()
forward(input, target)[原始碼]#

執行前向傳播。

返回型別

張量