评价此页

NLLLoss#

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

负对数似然损失(Negative Log Likelihood Loss)。它在训练具有 C 个类别的分类问题时非常有用。

如果提供,可选参数 weight 应该是一个为每个类别分配权重的一维张量。这在训练集不平衡时特别有用。

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

在神经网络中获得对数概率可以通过在网络的最后一层添加一个 LogSoftmax 层来轻松实现。如果你不想添加额外的层,也可以改用 CrossEntropyLoss

该损失函数期望的 target 应该是一个范围在 [0,C1][0, C-1] 的类别索引,其中 C = 类别数量;如果指定了 ignore_index,此损失也接受该类别索引(此索引不一定必须在类别范围内)。

未约简的(即 reduction 设置为 'none')损失可以描述为

(x,y)=L={l1,,lN},ln=wynxn,yn,wc< = \text{weight}[c] \cdot \mathbb{1}\{c \not= \text{ignore\_index}\},

其中 xx 是输入,yy 是目标,ww 是权重,且 NN 是批大小。如果 reduction 不是 'none'(默认是 'mean'),则:

(x,y)={n=1N1n=1Nwynln,如果 reduction=‘mean’;n=1Nln,如果 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, 可选) – 已弃用(参见 reduction)。默认情况下,损失会在批次中的每个损失元素上取平均值。注意,对于某些损失,每个样本有多个元素。如果 size_average 字段设置为 False,则每个小批量的损失将改为求和。当 reduceFalse 时忽略。默认值:None

  • ignore_index (int, 可选) – 指定一个被忽略且不贡献于输入梯度的目标值。当 size_averageTrue 时,损失将在非忽略的目标上取平均值。

  • reduce (bool, 可选) – 已弃用(参见 reduction)。默认情况下,根据 size_average,损失将在每个小批量的观测值上取平均或求和。当 reduceFalse 时,改为返回每个批次元素的损失并忽略 size_average。默认值:None

  • reduction (str, 可选) – 指定应用于输出的归约(reduction)方式:'none' | 'mean' | 'sum''none':不应用归约;'mean':取输出的加权平均值;'sum':对输出求和。注意:size_averagereduce 正在弃用过程中,在此期间,指定这两个参数中的任何一个都将覆盖 reduction。默认值:'mean'

形状:
  • 输入:(N,C)(N, C)(C)(C),其中 C = 类别数量N = 批大小;或者在 K 维损失的情况下为 (N,C,d1,d2,...,dK)(N, C, d_1, d_2, ..., d_K),满足 K1K \geq 1

  • 目标:(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

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

示例

>>> 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)[源代码]#

执行前向传播。

返回类型:

张量