【pytorch学习】nn.CrossEntropyLoss,label_smooth

文章:
简单的label smooth为什么能够涨点?

label smooth标签平滑的理解


Pytorch 官网:

# from Pytorch官网:https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html

import torch
import torch.nn as nn

# Example of target with class indices
loss = nn.CrossEntropyLoss(label_smoothing=0.1)
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
print(output)
# output.backward()
# Example of target with class probabilities
input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5).softmax(dim=1)
output = loss(input, target)
print(output)
# output.backward()

实现原理:

# from: https://github.com/guochengqian/openpoints/blob/ee100c81b1d9603c0fc76a3ee4e37d10b2af60ba/loss/cross_entropy.py

""" Cross Entropy w/ smoothing or soft targets

Borrowed from Ross Wightman (https://www.github.com/timm)
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
# from .build import LOSS


# @LOSS.register_module()
class LabelSmoothingCrossEntropy(nn.Module):  # Example of target with class probabilities
    """ NLL loss with label smoothing.
    """
    def __init__(self, label_smoothing=0.1):
        super(LabelSmoothingCrossEntropy, self).__init__()
        self.smoothing = label_smoothing
        self.confidence = 1. - self.smoothing

    def forward(self, x: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        logprobs = F.log_softmax(x, dim=1)
        nll_loss = -logprobs.gather(dim=1, index=target.unsqueeze(1))
        nll_loss = nll_loss.squeeze(1)
        smooth_loss = -logprobs.mean(dim=-1)
        loss = self.confidence * nll_loss + self.smoothing * smooth_loss
        return loss.mean()


# @LOSS.register_module()
class SoftTargetCrossEntropy(nn.Module): # Example of target with class indices

    def __init__(self, **kwargs):
        super(SoftTargetCrossEntropy, self).__init__()

    def forward(self, x: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
        loss = torch.sum(-target * F.log_softmax(x, dim=-1), dim=-1)
        return loss.mean()

验证:

loss = nn.CrossEntropyLoss(label_smoothing=0.1)
input = torch.randn(3, 5, requires_grad=True)
target = torch.arange(3)
target
output = loss(input, target)
print(input)
print(target)
print(output)

tensor([[ 0.1871,  0.4518, -0.3632, -0.7217,  0.0318],
        [ 0.9812, -0.5675, -1.3484, -1.0167,  1.2187],
        [-0.4820, -0.2057, -0.0044,  1.6346, -0.3886]], requires_grad=True)
tensor([0, 1, 2])
tensor(2.0178, grad_fn=<AddBackward0>)
-------------------------------------------
loss = LabelSmoothingCrossEntropy()
output = loss(input, target)
print(output)

tensor(2.0178, grad_fn=<MeanBackward0>)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值