torch.nn中有哪些损失函数?

在PyTorch库的torch.nn模块中,提供了许多预定义的损失函数,用于不同的机器学习任务。以下是一些常见的损失函数:

MSELoss(均方误差损失): 常用于回归问题,计算预测值与真实值之间的平均平方误差。

import torch.nn as nn  
  
criterion = nn.MSELoss()  
output = ... # your network's output, shape should be the same as target  
loss = criterion(output, target)

CrossEntropyLoss(交叉熵损失): 常用于分类问题,计算预测概率[logits]与真实标签之间的交叉熵。

import torch.nn as nn  
  
criterion = nn.CrossEntropyLoss()  
output = ... # your network's output, shape should be (batch_size, num_classes)  
target = ... # target labels, shape should be (batch_size)  
loss = criterion(output, target)

在这里插入图片描述

BCELoss(二分类交叉熵损失): 用于二分类问题,计算预测概率与真实标签之间的二进制交叉熵。

import torch.nn as nn  
  
criterion = nn.BCELoss()  
output = ... # your network's output, shape should be (batch_size, 1)  
target = ... # target labels, shape should be (batch_size)  
loss = criterion(output, target)

NLLLoss(负对数似然损失): 常用于多分类问题,计算预测概率与真实标签之间的负对数似然。

import torch.nn as nn  
  
criterion = nn.NLLLoss()  
output = ... # your network's output, shape should be (batch_size, num_classes)  
target = ... # target labels, shape should be (batch_size)  
loss = criterion(output, target)

HingeEmbeddingLoss(铰链嵌入损失): 用于铰链损失问题,计算预测值与真实值之间的铰链嵌入损失。

import torch.nn as nn  
criterion = nn.HingeEmbeddingLoss()  
output = ... # your network's output, shape should be the same as target  
target = ... # target labels, shape should be the same as output  
loss = criterion(output, target)

MultiMarginLoss(多边损失): 常用于多分类问题,计算预测标签与真实标签之间的多边损失。

import torch.nn as nn  
  
criterion = nn.MultiMarginLoss()  
output = ... # your network's output, shape should be (batch_size, num_classes)  
target = ... # target labels, shape should be (batch_size)  
loss = criterion(output, target)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万物琴弦光锥之外

给个0.1,恭喜老板发财

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值