作者 | 小新

来源 | https://lhyxx.top

编辑 | 极市平台

本文从理论和实践两方面来全面梳理一下常用的损失函数。(避免自己总是一瓶子不满半瓶子晃荡……)。要么理论满分,编码时不会用;要么编码是会调包,但是不明白其中的计算原理。本文来科普一下。

我们将每个损失函数分别从理论和pytorch中的实现两个方面来拆解一下。

另外,解释一下torch.nn.Module 和 torch.nn.functional(俗称F)中损失函数的区别。

Module的损失函数例如CrossEntropyLoss、NLLLoss等是封装之后的损失函数类,是一个类,因此其中的变量可以自动维护。经常是对F中的函数的封装。而F中的损失函数只是单纯的函数。

当然我们也可以自己构造自己的损失函数对象。有时候损失函数并不需要太复杂,没有必要特意封装一个类,直接调用F中的函数也是可以的。使用哪种看具体实现需求而定。

CrossEntropyLoss

交叉熵损失,是分类任务中最常用的一个损失函数。

理论

直接上理论公式:

其中是真实标签,是预测的类分布(通常是使用softmax将模 型输出转换为概率分布), 也就是与中的元素分别表示对应类 别的概率。

举个例子,清晰明了:

# 假设该样本属于第二类

# 因为是分布, 所以属于各个类的和为 1

pytorch函数详解(Pytorch常用损失函数拆解)(1)

pytorch-实现

from torch.nn import CrossEntropyLoss

举例:

实际使用中需要注意几点:

举例,输入的形式大概就像相面这种格式:

pytorch函数详解(Pytorch常用损失函数拆解)(2)

然后就将他们扔到CrossEntropyLoss函数中,就可以得到损失。

loss = CrossEntropyLoss(input, target)

我们看CrossEntropyLoss函数里面的实现,是下面这样子的:

def forward(self, input, target): return F.cross_entropy(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)

是调用的torch.nn.functional(俗称F)中的cross_entropy()函数。

参数

其中参数weight、ignore_index、reduction要在实例化CrossEntropyLoss对象时指定,例如:

loss = torch.nn.CrossEntropyLoss(reduction='none')

我们再看一下F中的cross_entropy的实现

return nll_loss(log_softmax(input, dim=1), target, weight, None, ignore_index, None, reduction)

可以看到就是先调用log_softmax,再调用nll_loss。

log_softmax就是先softmax再取log:

nll_loss 是negative log likelihood loss:

详细介绍见下面torch.nn.NLLLoss,计算公式如下:

例如假设, class,则,classclass

源码中给了个用法例子:

# 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), target) output.backward()

因此,其实CrossEntropyLoss损失,就是softmax log nll_loss的集成。

CrossEntropyLoss(input, target) = nll_loss(log_softmax(input, dim=1), target)

CrossEntropyLoss中的target必须是LongTensor类型。

实验如下:

pred = torch.FloatTensor([[2, 1], [1, 2]]) target = torch.LongTensor([1, 0]) loss_fun = nn.CrossEntropyLoss() loss = loss_fun(pred, target) print(loss) # 输出为tensor(1.3133) loss2 = F.nll_loss(F.log_softmax(pred, dim=1), target) print(loss2) # 输出为tensor(1.3133)

数学形式就是:

torch-nn-BCELoss理论

CrossEntropy损失函数适用于总共有N个类别的分类。当N=2时,即二分类任务,只需要判断是还是否的情况,就可以使用二分类交叉熵损失:BCELoss 二分类交叉熵损失。上公式 (y是真实标签,x是预测值)

其实这个函数就是CrossEntropyLoss的当类别数N=2时候的特例。因为类别数为2,属于第一类的概率为y,那么属于第二类的概率自然就是(1-y)。因此套用与CrossEntropy损失的计算方法,用对应的标签乘以对应的预测值再求和,就得到了最终的损失。

实践

torch.nn.BCELoss(x, y)

x形状(batch,*),y形状与x相同。

x与y中每个元素,表示的是该维度上属于(或不属于)这个类的概率。

另外,pytorch中的BCELoss可以为每个类指定权重。通常,当训练数据中正例和反例的比例差别较大时,可以为其赋予不同的权重,weight的形状应该是一个一维的,元素的个数等于类别数。

实际使用如下例,计算BCELoss(pred, target):

pred = torch.FloatTensor([0.4, 0.1]) # 可以理解为第一个元素分类为是的概率为0.4,第二个元素分类为是的概率为0.1。 target = torch.FloatTensor([0.2, 0.8]) # 实际上第一个元素分类为是的概率为0.2,第二个元素分类为是的概率为0.8。 loss_fun = nn.BCELoss(reduction='mean') # reduction可选 none, sum, mean, batchmean loss = loss_fun(pred, target) print(loss) # tensor(1.2275) a = -(0.2 * np.log(0.4) 0.8 * np.log(0.6) 0.8 * np.log(0.1) 0.2 * np.log(0.9))/2 print(a) # 1.2275294114572126

可以看到,计算BCELoss(pred,target)与上面理论中的公式一样。

内部实现

pytorch 中的torch.nn.BCELoss类,实际上就是调用了F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)

torch.nn.BCEWithLogitsLoss理论

该函数实际上与BCELoss相同,只是BCELoss的输入x,在输入之前需要先手动经过sigmoid激活函数映射到(0, 1)区间,而该函数将sigmoid与BCELoss整合到一起了

也就是先将输入经过sigmoid函数,然后计算BCE损失。

实践

torch.nn.BCEWithLogitsLoss(x, y)

x与y的形状要求与BCELoss相同。

pred = torch.FloatTensor([0.4, 0.1]) target = torch.FloatTensor([0.2, 0.8]) loss_fun = nn.BCEWithLogitsLoss(reduction='mean') # reduction可选 none, sum, mean, batchmean loss = loss_fun(pred, target) print(loss) # tensor(0.7487) # 上面的过程与下面的过程结果相同 loss_fun = nn.BCELoss(reduction='mean') # reduction可选 none, sum, mean, batchmean loss = loss_fun(torch.sigmoid(pred), target) # 先经过sigmoid,然后与target计算BCELoss print(loss) # tensor(0.7487)

可以看出,先对输入pred调用sigmoid,在调用BCELoss,结果就等于直接调用BCEWithLogitsLoss。

torch.nn.L1Loss理论

L1损失很简单,公式如下:

x是预测值,y是真实值。

实践

torch.nn.L1Loss(x, y)

x形状:任意形状

y形状:与输入形状相同

pred = torch.FloatTensor([[3, 1], [1, 0]]) target = torch.FloatTensor([[1, 0], [1, 0]]) loss_fun = nn.L1Loss() loss = loss_fun(pred, target) print(loss) # tensor(0.7500)

其中L1Loss的内部实现为:

def forward(self, input, target): return F.l1_loss(input, target, reduction=self.reduction)

我们可以看到,其实还是对F.l1_loss的封装。

torch.nn.MSELoss理论

L1Loss可以理解为向量的1-范数,MSE均方误差就可以理解为向量的2-范数,或矩阵的F-范数。

x是预测值,y是真实值。

实践

torch.nn.MSELoss(x, y)

x任意形状,y与x形状相同。

pred = torch.FloatTensor([[3, 1], [1, 0]]) target = torch.FloatTensor([[1, 0], [1, 0]]) loss_fun = nn.MSELoss() loss = loss_fun(pred, target) print(loss) # tensor(1.2500)

其中MSELoss内部实现为:

def forward(self, input, target): return F.mse_loss(input, target, reduction=self.reduction)

本质上是对F中mse_loss函数的封装。

torch.nn.NLLLoss理论

NLLLoss(Negative Log Likelihood Loss),其数学表达形式为:

前面讲到CrossEntropyLoss中用的nll_loss,实际上,该损失函数就是对F.nll_loss的封装,功能也和nll_loss相同。

正如前面所说,先把输入x进行softmax,在进行log,再输入该函数中就是CrossEntropyLoss。

实践

torch.nn.NLLLoss(x, y)

x是预测值,形状为(batch,dim)

y是真实值,形状为(batch)

形状要求与CrossEntropyLoss相同。

pred = torch.FloatTensor([[3, 1], [2, 4]]) target = torch.LongTensor([0, 1]) #target必须是Long型 loss_fun = nn.NLLLoss() loss = loss_fun(pred, target) print(loss) # tensor(-3.5000)

其内部实现实际上就是调用了F.nll_loss():

def forward(self, input, target): return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)

torch.nn.KLDivLoss理论

KL散度通常用来衡量两个连续分布之间的距离。两个分布越相似,KL散度越接近0。

KL散度又叫相对熵,具体理论可以参考:https://lhyxx.top/2019/09/15/信息论基础-熵/

注意,这里 x 与 y 都是分布,分布就意味着其中所有元素求和概率为1。

则:

pytorch函数详解(Pytorch常用损失函数拆解)(3)

本例中计算的都是以e为底的。

实践

torch.nn.KLDivLoss(input, target)

试验测试torch.nn.KLDivLoss,计算KL(pred|target):

pred = torch.FloatTensor([0.1, 0.2, 0.7]) target = torch.FloatTensor([0.5, 0.2, 0.3]) loss_fun = nn.KLDivLoss(reduction='sum') # reduction可选 none, sum, mean, batchmean loss = loss_fun(target.log(), pred) print(loss) # tensor(0.4322) #上面的计算过程等价于下面 a = (0.1 * np.log(1/5) 0.2 * np.log(1) 0.7 * np.log(7/3)) print(a) # 0.43216

input应该是log-probabilities,target是probabilities。input和target形状相同。

该函数是对F.kl_div(input, target, reduction=self.reduction)的封装。其原型为: torch.nn.functional.kl_div(input, target, size_average=None, reduce=None, reduction='mean')

注意,使用nn.KLDivLoss计算KL(pred|target)时,需要将pred和target调换位置,而且target需要先取对数:

loss_fun(target.log(), pred)

,