损失函数总结(六):KLDivLoss、BCEWithLogitsLoss

损失函数总结(六):KLDivLoss、BCEWithLogitsLoss

  • [1 引言](#1 引言)
  • [2 损失函数](#2 损失函数)
    • [2.1 KLDivLoss](#2.1 KLDivLoss)
    • [2.2 BCEWithLogitsLoss](#2.2 BCEWithLogitsLoss)
  • [3 总结](#3 总结)

1 引言

在前面的文章中已经介绍了介绍了一系列损失函数 (L1LossMSELossBCELossCrossEntropyLossNLLLossCTCLossPoissonNLLLossGaussianNLLLoss)。在这篇文章中,会接着上文提到的众多损失函数继续进行介绍,给大家带来更多不常见的损失函数的介绍。这里放一张损失函数的机理图:

2 损失函数

2.1 KLDivLoss

Kullback-Leibler散度(KL Divergence),通常称为KLDivLoss,是机器学习深度学习中的一种损失函数,特别用于概率模型的上下文中,比如变分自动编码器(VAE)。它用来度量两个概率分布之间的差异。KLDivLoss的数学表达式如下:
K L D i v L o s s ( p ∥ q ) = E p ( x ) log ⁡ p ( x ) q ( x ) = ∑ i = 1 N p ( x i ) ⋅ ( log ⁡ p ( x i ) − log ⁡ q ( x i ) ) KLDivLoss(p∥q)=E_{p(x)} \log \frac{p(x)}{q(x)} = \sum_{i=1}^{N}p(x_i )⋅(\log p(x_i)−\log q(x_i)) KLDivLoss(p∥q)=Ep(x)logq(x)p(x)=i=1∑Np(xi)⋅(logp(xi)−logq(xi))

其中:

  • p p p 表示模型预测值。
  • q q q 表示模型实际值。
  • N N N 表示样本量。

代码实现(Pytorch):

python 复制代码
import torch.nn.functional as F
kl_loss = nn.KLDivLoss(reduction="batchmean")
# input should be a distribution in the log space
input = F.log_softmax(torch.randn(3, 5, requires_grad=True), dim=1)
# Sample a batch of distributions. Usually this would come from the dataset
target = F.softmax(torch.rand(3, 5), dim=1)
output = kl_loss(input, target)

kl_loss = nn.KLDivLoss(reduction="batchmean", log_target=True)
log_target = F.log_softmax(torch.rand(3, 5), dim=1)
output = kl_loss(input, log_target)

KLDivLoss在知识蒸馏任务中有广泛应用,值得大家关注和了解。。。。

2.2 BCEWithLogitsLoss

BCEWithLogitsLoss 是二进制交叉熵损失函数(Binary Cross-Entropy Loss)的一种变体,通常用于深度学习中的二分类问题。这个损失函数常用于神经网络训练,特别是对二元分类任务(两个类别的分类)进行模型训练。BCEWithLogitsLoss的数学表达式如下:
L ( y , y ′ ) = − 1 n ∑ i = 1 n [ y i log ⁡ ( σ ( y i ′ ) ) + ( 1 − y i ) log ⁡ ( 1 − σ ( y i ′ ) ) ] L(y, y') = -\frac{1}{n} \sum_{i=1}^{n} [y_i \log(\sigma(y_i')) + (1 - y_i) \log(1 - \sigma(y_i'))] L(y,y′)=−n1i=1∑n[yilog(σ(yi′))+(1−yi)log(1−σ(yi′))]

其中:

  • L ( y , y ′ ) L(y, y') L(y,y′) 是整个数据集上的损失
  • n n n 是样本数量。
  • y i y_i yi 是第 i i i 个样本的实际标签,通常是0或1(表示两个类别中的一个)。
  • y i ′ y_i' yi′ 是第 i i i 个样本的模型预测的概率,通常在0和1之间
  • σ ( ) \sigma() σ() 是sigmoid激活函数。

代码实现(Pytorch):

python 复制代码
loss = nn.BCEWithLogitsLoss()
input = torch.randn(3, requires_grad=True)
target = torch.empty(3).random_(2)
output = loss(input, target)
output.backward()

整体来说和BCELoss没有什么区别。BCELoss+Sigmoid=BCEWithLogitsLoss

3 总结

到此,使用 损失函数总结(六) 已经介绍完毕了!!! 如果有什么疑问欢迎在评论区提出,对于共性问题可能会后续添加到文章介绍中。如果存在没有提及的损失函数也可以在评论区提出,后续会对其进行添加!!!!

如果觉得这篇文章对你有用,记得点赞、收藏并分享给你的小伙伴们哦😄。

相关推荐
Listennnn11 分钟前
基于 Flickr30k-Entities 数据集 的 Phrase Localization
人工智能
伊克罗德信息科技17 分钟前
基于RPA技术的ECRobot企业智能体解决方案,打通企业自动化业务流程的最后一公里
大数据·人工智能
水银嘻嘻19 分钟前
03 APP 自动化-定位元素工具&元素定位
python·appium·自动化
初恋叫萱萱31 分钟前
边缘计算场景下的大模型落地:基于 Cherry Studio 的 DeepSeek-R1-0528 本地部署
人工智能·边缘计算
蹦蹦跳跳真可爱58935 分钟前
Python----目标检测(《用于精确目标检测和语义分割的丰富特征层次结构》和R-CNN)
人工智能·python·深度学习·神经网络·目标检测·cnn
抽风的雨6101 小时前
【python深度学习】Day 42 Grad-CAM与Hook函数
开发语言·python·深度学习
Steve lu1 小时前
回归任务损失函数对比曲线
人工智能·pytorch·深度学习·神经网络·算法·回归·原力计划
UQI-LIUWJ1 小时前
论文笔记:Towards Explainable Traffic Flow Prediction with Large Language Models
论文阅读·人工智能·语言模型
Mikhail_G1 小时前
Python应用for循环临时变量作用域
大数据·运维·开发语言·python·数据分析
兔兔西2 小时前
【AI学习】检索增强生成(Retrieval Augmented Generation,RAG)
人工智能