评价此页

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 1K 维情况。后者对于更高维度的输入很有用,例如计算 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 的 Tensor。否则,它被视为全为 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),其中 K ≥ 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),其中 K ≥ 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()