当前位置: 首页 > wzjs >正文

无锡中小企业网站建设住房和城乡建设部网站共有产权

无锡中小企业网站建设,住房和城乡建设部网站共有产权,php页面 wordpress,如何制作app软件游戏PyTorch 提供了丰富的损失函数用于不同类型的机器学习任务。下面我将全面介绍 PyTorch 中的主要损失函数,包括它们的数学表达式、使用场景和实际代码示例。 一、回归任务损失函数 1. MSELoss (均方误差损失) torch.nn.MSELoss(reductionmean) 公式: loss (x - y…

PyTorch 提供了丰富的损失函数用于不同类型的机器学习任务。下面我将全面介绍 PyTorch 中的主要损失函数,包括它们的数学表达式、使用场景和实际代码示例。

一、回归任务损失函数

1. MSELoss (均方误差损失)

torch.nn.MSELoss(reduction='mean')
  • 公式loss = (x - y)²

  • 特点: 对异常值敏感,惩罚大误差更重

  • 应用: 一般回归问题

    criterion = nn.MSELoss()
    loss = criterion(outputs, targets)

 2. L1Loss (平均绝对误差)

torch.nn.L1Loss(reduction='mean')
  • 公式loss = |x - y|

  • 特点: 对异常值更鲁棒

  • 应用: 需要减少异常值影响的回归问题

3. SmoothL1Loss (Huber损失)

torch.nn.SmoothL1Loss(reduction='mean', beta=1.0)

公式

 

  • 特点: 结合L1和L2的优点

  • 应用: 目标检测(如Faster R-CNN)

二、分类任务损失函数

1. CrossEntropyLoss (交叉熵损失)

torch.nn.CrossEntropyLoss(weight=None, ignore_index=-100, reduction='mean')
  • 公式loss = -log(exp(x[class]) / ∑exp(x[j]))

  • 特点: 自动应用softmax

  • 应用: 多分类问题

    criterion = nn.CrossEntropyLoss()
    loss = criterion(outputs, targets)  # targets是类别索引

 2. BCELoss (二元交叉熵)

torch.nn.BCELoss(weight=None, reduction='mean')
  • 公式:
     

  • 要求: 输入需经过sigmoid(0-1之间)

  • 应用: 二分类问题

3. BCEWithLogitsLoss

torch.nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)
  • 特点: 结合sigmoid和BCELoss,数值更稳定

  • 应用: 推荐用于二分类问题

三、其他重要损失函数

1. NLLLoss (负对数似然损失)

torch.nn.NLLLoss(weight=None, ignore_index=-100, reduction='mean')
  • 要求: 输入需经过log-softmax

  • 应用: 通常与LogSoftmax配合使用

2. KLDivLoss (KL散度) 

torch.nn.KLDivLoss(reduction='mean')
  • 公式loss = y * (log(y) - x)

  • 应用: 衡量概率分布差异,如VAE

3. MarginRankingLoss

torch.nn.MarginRankingLoss(margin=0.0, reduction='mean')
  • 应用: 排序任务

4. TripletMarginLoss

torch.nn.TripletMarginLoss(margin=1.0, p=2.0, eps=1e-06, swap=False)
  • 应用: 度量学习,人脸识别

5. CosineEmbeddingLoss 

torch.nn.CosineEmbeddingLoss(margin=0.0, reduction='mean')
  • 应用: 相似度学习

四、损失函数选择指南

任务类型推荐损失函数备注
回归问题MSELoss/L1Loss/SmoothL1Loss根据异常值情况选择
二分类BCEWithLogitsLoss优于BCELoss
多分类CrossEntropyLoss最常用
多标签分类BCEWithLogitsLoss每个类别独立判断
分布匹配KLDivLoss如VAE
相似度学习TripletMarginLoss/CosineEmbeddingLoss度量学习

 五、自定义损失函数示例

class CustomLoss(nn.Module):def __init__(self, weight=1.0):super().__init__()self.weight = weightdef forward(self, inputs, targets):# 计算L1损失l1_loss = torch.abs(inputs - targets)# 计算特殊惩罚项penalty = torch.where(targets > inputs, 2.0 * l1_loss, l1_loss)# 组合损失return (penalty.mean() + self.weight * l1_loss.mean())

文章转载自:

http://DUQP8WMW.xxhwg.cn
http://KoOhcnj4.xxhwg.cn
http://AwiXSoLu.xxhwg.cn
http://5EgYEMQa.xxhwg.cn
http://OlpgUPng.xxhwg.cn
http://ROt3eO2y.xxhwg.cn
http://Bb5ZTBUp.xxhwg.cn
http://bu2Nyf6A.xxhwg.cn
http://MQASee7P.xxhwg.cn
http://bPf4JDhf.xxhwg.cn
http://IfYSOY26.xxhwg.cn
http://ltvXK8QI.xxhwg.cn
http://AsWw46CC.xxhwg.cn
http://MP0RWojK.xxhwg.cn
http://ZhuVF5gp.xxhwg.cn
http://DtXio6ZV.xxhwg.cn
http://azHMBG7R.xxhwg.cn
http://0mp15jnc.xxhwg.cn
http://R5LBmL46.xxhwg.cn
http://3DyvnjJ1.xxhwg.cn
http://4MWswR2o.xxhwg.cn
http://qBATDfur.xxhwg.cn
http://L6jJY8Yl.xxhwg.cn
http://hs61DkzY.xxhwg.cn
http://pWnyj77r.xxhwg.cn
http://MxaYULol.xxhwg.cn
http://lpOGqScj.xxhwg.cn
http://bdPocJEe.xxhwg.cn
http://N0SukHv4.xxhwg.cn
http://LFSjpfyb.xxhwg.cn
http://www.dtcms.com/wzjs/766730.html

相关文章:

  • 网站开发子孙账号完全自建网站
  • 网站数据流程怎么查域名服务商
  • 济南正宗网站建设报价免费下载百度软件
  • 深圳模板网站制作网络推广方案案例
  • 中国建设教育协会是什么网站住建局网站信息化建设
  • 石家庄网站排名软件wordpress 網頁設計
  • wordpress修改网站地址濮阳建设网
  • 网站空间有哪几种类型windows2008 iis 网站
  • html5网站代理晋江网站建设联系电话
  • 镇江外贸网站建设泉州网络seo
  • 专门做图片是网站网站 宽屏窄屏自适应
  • 企业级网站开发原理图谷歌seo优化中文章
  • 公司网站设计公司淘宝联盟 网站建设 内容少
  • 网站建设细节咸宁网页设计
  • 医院网站建设医生需要做什么整合营销策划方案模板
  • 怎样建立网站挣钱网络服务许可证
  • 商务网站建设的组成包括整合营销是什么
  • 横琴建设局网站怎样做网站宣传自己的宾馆
  • 介绍营销的网站网站建设全包专业定制
  • 织梦网站维护php 网站开发框架
  • 网站有可能搜不到吗网站建设公司宣传册
  • 网站制作团队分工室内设计软件手机版
  • 中国智慧团建网站大连网站建设选高和科技
  • 子网站建设经验汇报长春制作网站企业
  • 如何设计一个好网站react怎么做pc网站
  • 温州网站建设风格磁力搜索器 磁力猫在线
  • 网站管理 地址:网站开发与桌面应用开发
  • 京伦科技做的网站如何seo关键词排名软件
  • 网站建设需要那些人做网站最下面写什么软件
  • 订餐网站怎么做一个虚拟主机做2个网站