torch.nn.functional.nll_loss#
- torch.nn.functional.nll_loss(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')[source]#
计算负对数似然损失。
有关详情,请参阅
NLLLoss
。- 参数
input (Tensor) – ,其中 C = 类别数,或者在 2D 损失的情况下为 ,或者在 K 维损失的情况下为 ,其中 。
target (Tensor) – ,其中每个值都满足 ,或者在 K 维损失的情况下为 ,其中 。
weight (Tensor, optional) – 为每个类手动指定的重缩放权重。如果提供,则必须是大小为 C 的 Tensor。
size_average (bool, optional) – 已弃用(请参阅
reduction
)。ignore_index (int, optional) – 指定一个目标值,该值将被忽略且不计入输入梯度。当
size_average
为True
时,损失将根据非忽略的目标进行平均。默认值:-100。reduce (bool, optional) – 已弃用 (参见
reduction
)。reduction (str, optional) – 指定应用于输出的规约:
'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()