评价此页

NLLLoss#

class torch.nn.NLLLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')[source]#

负对数似然损失。它适用于训练具有 C 个类别的分类问题。

如果提供了可选参数 weight,它应该是一个一维张量,为每个类别分配权重。当您拥有不平衡的训练数据集时,这一点尤其有用。

通过前向调用输入的 input 预期包含每个类别的对数概率。 input 必须是大小为 (minibatch,C)(minibatch, C)(minibatch,C,d1,d2,...,dK)(minibatch, C, d_1, d_2, ..., d_K) 张量,其中 K1K \geq 1 用于 K 维情况。后者对于更高维度的输入很有用,例如计算 2D 图像每个像素的 NLL 损失。

通过在网络的最后一层添加 LogSoftmax 层,可以轻松获得神经网络中的对数概率。如果您更愿意不添加额外的层,则可以使用 CrossEntropyLoss

此损失期望的 target 应该是类索引,其范围在 [0,C1][0, C-1] 内,其中 C = 类别数量;如果指定了 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,则损失将改为对每个小批量进行求和。在 reduceFalse 时忽略。默认值: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 = 类别数量N = 批量大小,或 (N,C,d1,d2,...,dK)(N, C, d_1, d_2, ..., d_K),其中 K1K \geq 1 用于 K 维损失。

  • 目标: (N)(N)()(),其中每个值都是 0targets[i]C10 \leq \text{targets}[i] \leq C-1,或 (N,d1,d2,...,dK)(N, d_1, d_2, ..., d_K),其中 K1K \geq 1 在 K 维损失情况下。

  • 输出:如果 reduction'none',形状为 (N)(N)(N,d1,d2,...,dK)(N, d_1, d_2, ..., d_K),其中 K1K \geq 1 在 K 维损失情况下。否则,为标量。

示例

>>> 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)[source]#

执行前向传播。

返回类型

张量