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) – ,其中 C = 類別數,或者在 2D Loss 的情況下為 ,或者在 K 維損失的情況下為 ,其中 。 input 應該為對數機率。
target (Tensor) – ,其中每個值滿足 ,或者在 K 維損失的情況下為 ,其中 。
weight (Tensor, 可選) – 為每個類手動分配的重縮放權重。如果提供,則必須是大小為 C 的 Tensor。
size_average (bool, optional) – 已棄用(請參閱
reduction)。ignore_index (int, 可選) – 指定一個被忽略且不計入輸入梯度的目標值。當
size_average為True時,損失將根據非忽略目標進行平均。預設為 -100。reduce (bool, optional) – 已棄用(請參閱
reduction)。reduction (str, 可選) – 指定應用於輸出的歸約方式:
'none'|'mean'|'sum'。'none':不進行歸約;'mean':輸出的總和將除以輸出中的元素數量;'sum':將對輸出進行求和。注意:size_average和reduce正在被棄用,在此期間,指定這兩個引數中的任何一個都將覆蓋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()