https://blog.csdn.net/weixin_40476348/article/details/94562240
class torch.nn.NLLLoss(weight=None, size_average=None, ignore_index=-100,
reduce=None, reduction='mean')
計算公式:loss(input, class) = -input[class] 公式理解:input = [-0.1187, 0.2110, 0.7463],target = [1],那么 loss = -0.2110
個人理解:感覺像是把 target 轉換成 one-hot 編碼,然后與 input 點乘得到的結果
代碼理解:
import torch
import torch.nn as nn
import torch.nn.functional as F
torch.manual_seed(2019)
output = torch.randn(1, 3) # 網絡輸出
target = torch.ones(1, dtype=torch.long).random_(3) # 真實標簽
print(output)
print(target)
# 直接調用
loss = F.nll_loss(output, target)
print(loss)
# 實例化類
criterion = nn.NLLLoss()
loss = criterion(output, target)
print(loss)
"""
tensor([[-0.1187, 0.2110, 0.7463]])
tensor([1])
tensor(-0.2110)
tensor(-0.2110)
"""
如果 input 維度為 M x N,那么 loss 默認取 M 個 loss 的平均值,reduction='none' 表示顯示全部 loss
import torch
import torch.nn as nn
import torch.nn.functional as F
torch.manual_seed(2019)
output = torch.randn(2, 3) # 網路輸出
target = torch.ones(2, dtype=torch.long).random_(3) # 真實標簽
print(output)
print(target)
# 直接調用
loss = F.nll_loss(output, target)
print(loss)
# 實例化類
criterion = nn.NLLLoss(reduction='none')
loss = criterion(output, target)
print(loss)
"""
tensor([[-0.1187, 0.2110, 0.7463],
[-0.6136, -0.1186, 1.5565]])
tensor([2, 0])
tensor(-0.0664)
tensor([-0.7463, 0.6136])
"""