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()