pytorch 标签平滑代码

import torch
import torch.nn as nn
import torch.nn.functional as F

class CELossWithLabelSmoothing(nn.Module):
    ''' Cross Entropy Loss with Label Smoothing '''

    def __init__(self, label_smoothing=0.1):
        super().__init__()
        self.label_smoothing = label_smoothing

    def forward(self, pred, target):
        num_classes = pred.size(1)
        print(target)
        # 使用标签平滑
        one_hot = torch.full((pred.size(0), num_classes), self.label_smoothing / (num_classes - 1))
        one_hot.scatter_(1, target.unsqueeze(-1), 1 - self.label_smoothing)
        print(one_hot)
        # 计算交叉熵损失
        loss = -torch.sum(one_hot * torch.log_softmax(pred, dim=1), dim=1)
        return loss.mean()

创建一个示例模型输出和真实标签

pred = torch.tensor([[1.0, 2.0, 0.5], [0.5, 1.5, 2.0], [2.0, 1.0, 0.5]])
target = torch.tensor([0, 1, 2])

使用自定义损失函数

criterion = CELossWithLabelSmoothing(label_smoothing=0.1)
loss = criterion(pred, target)

打印损失

print("损失:", loss.item())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值