简单总结torch.nn封装的18种损失函数。

本节简单总结torch.nn封装的18种损失函数。【文中思维导图采用MindMaster软件,Latex公式采用在线编码器
注意:目前仅详细介绍CrossEntropyLoss、BCELoss、L1Loss、MSELoss、SmoothL1Loss,后续随着代码需要,再逐步更新。

目录

  • 1.nn.CrossEntropyLoss()交叉熵损失函数
  • 2.nn.BCELoss()二分类交叉熵损失函数
  • 3.nn.BCEWithLogitsLoss()结合Sigmoid的二分类交叉熵损失函数
  • 4.nn.L1Loss
  • 5.nn.MSELoss
  • 6.nn.SmoothL1Loss

Python 损失率图像 最新损失函数_Pytorch学习

1.nn.CrossEntropyLoss()交叉熵损失函数

这里大家可以进入“一文搞懂交叉熵在机器学习中的使用,透彻理解交叉熵背后的直觉”,这个博主讲的非常好!

loss_f = nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction='mean')

inputs = torch.tensor([[1, 2], [1, 3], [1, 3]], dtype=torch.float)
target = torch.tensor([0, 1, 1], dtype=torch.long)

loss = loss_f(inputs, target)

# 参数:
#      weight:设置各类别的loss的权重,(防止类别数目不平衡)e.g. weights = torch.tensor([1, 2], dtype=torch.float)  两个类别
#      ignore_index: 忽略某个类别
#      reduction:计算模式  1.'None':逐元素计算,返回张量;2.'sum':所有元素求和,返回标量;3.'mean':加权平均,返回标量

  nn.CrossEntropyLoss是nn.LogSoftmax与nn.NLLLoss的结合,公式为:

\[loss\left ( x,class \right )= -\log{\left ( \frac{\exp x\left [ class \right ] }{\sum_{j} \exp x\left [ j \right ] } \right ) }=weight\left [ class \right ] \left ( -x\left [ class \right ] +\log{\sum_{j} \exp x\left [ j \right ]} \right ) \]

上述公式的由来:交叉熵 = 信息熵 + 相对熵

  • 信息熵:描述整个概率分布上事件的不确定性
  • Python 损失率图像 最新损失函数_损失函数_02相对熵(KL散度):描述两个分布之间的距离
  • Python 损失率图像 最新损失函数_Python 损失率图像_03交叉熵

Python 损失率图像 最新损失函数_损失函数_04其中 \(P\) 为真实数据分布,\(H(P)\) 在优化时为常数,故而 Python 损失率图像 最新损失函数_Pytorch学习_05。实际代码中, \(P\) 即为标签,\(Q\) 为数据经过网络得到的分布,即取\(softmax\)。

2.nn.BCELoss()二分类交叉熵损失函数

注意:输入值必须在[0,1]之间,表示一个分布。

loss_f = nn.BCELoss(weight=None,reduction='mean')

inputs = torch.tensor([[1, 2], [2, 2], [3, 4], [4, 5]], dtype=torch.float)
target = torch.tensor([[1, 0], [1, 0], [0, 1], [0, 1]], dtype=torch.float)

loss = loss_f(inputs, target)

3.nn.BCEWithLogitsLoss()结合Sigmoid的二分类交叉熵损失函数

loss_f = nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)
# 参数:
#      pos_weight:正样本(标签为1)的权值

Python 损失率图像 最新损失函数_二分类_06

4.nn.L1Loss

  计算inputs与label之间差值的绝对值

Python 损失率图像 最新损失函数_损失函数_075.nn.MSELoss

  计算inputs与label之间的平方差

Python 损失率图像 最新损失函数_Pytorch学习_086.nn.SmoothL1Loss

  平滑的L1Loss,由图2红色线与蓝色线对比可以看出。通过下面的公式我们也可以知道,SmoothL1损失结合了L1和MSE两者的优点。

Python 损失率图像 最新损失函数_损失函数_09 吾志所向,一往无前;愈挫愈勇,再接再厉。