Pytorch 提供的交叉熵相关的函数有:
class torch.nn.CrossEntropyLoss(weight=None, size_average=True, ignore_index=-100, reduce=True)[source]
# 1D
import torch
import torch.nn as nn
loss = nn.CrossEntropyLoss()
# input, NxC=2x3
input = torch.randn(2, 3, requires_grad=True)
# target, N
target = torch.empty(2, dtype=torch.long).random_(3)
output = loss(input, target)
output.backward()
class torch.nn.KLDivLoss(size_average=True, reduce=True
class torch.nn.BCELoss(weight=None, size_average=True, reduce=True)
import torch
import torch.nn as nn
sig = nn.Sigmoid()
loss = nn.BCELoss()
input = torch.randn(3, requires_grad=True)
target = torch.empty(3).random_(2)
output = loss(sig(input), target)
output.backward()
class torch.nn.BCEWithLogitsLoss(weight=None, size_average=True, reduce=True)
class torch.nn.MultiLabelSoftMarginLoss(weight=None, size_average=True, reduce=True)
class torch.nn.MultiLabelMarginLoss(size_average=True, reduce=True)