【Pytorch】Label Smoothing

article/2025/8/19 2:42:24

理论介绍可以参考 【Inception-v3】《Rethinking the Inception Architecture for Computer Vision》 中的 4.5 Model Regularization via Label Smoothing

本质就是用右边(意会下就行)的标签替换左边的 one-hot 编码形式,让网络别那么愤青,成了二极管(非黑即白)
在这里插入图片描述

代码实现如下,来自 【Pytorch】label smoothing

class NMTCritierion(nn.Module):"""1. Add label smoothing"""def __init__(self, label_smoothing=0.0):super(NMTCritierion, self).__init__()self.label_smoothing = label_smoothingself.LogSoftmax = nn.LogSoftmax()if label_smoothing > 0:self.criterion = nn.KLDivLoss(size_average=False)else:self.criterion = nn.NLLLoss(size_average=False, ignore_index=100000)self.confidence = 1.0 - label_smoothingdef _smooth_label(self, num_tokens):# When label smoothing is turned on,# KL-divergence between q_{smoothed ground truth prob.}(w)# and p_{prob. computed by model}(w) is minimized.# If label smoothing value is set to zero, the loss# is equivalent to NLLLoss or CrossEntropyLoss.# All non-true labels are uniformly set to low-confidence.one_hot = torch.randn(1, num_tokens)one_hot.fill_(self.label_smoothing / (num_tokens - 1))return one_hotdef _bottle(self, v):return v.view(-1, v.size(2))def forward(self, dec_outs, labels):scores = self.LogSoftmax(dec_outs)num_tokens = scores.size(-1)# conduct label_smoothing modulegtruth = labels.view(-1)if self.confidence < 1:tdata = gtruth.detach()one_hot = self._smooth_label(num_tokens)  # Do label smoothing, shape is [M]if labels.is_cuda:one_hot = one_hot.cuda()tmp_ = one_hot.repeat(gtruth.size(0), 1)  # [N, M]tmp_.scatter_(1, tdata.unsqueeze(1), self.confidence)  # after tdata.unsqueeze(1) , tdata shape is [N,1]gtruth = tmp_.detach()loss = self.criterion(scores, gtruth)return loss

使用方式如下:

loss_func = NMTCritierion(0.1) # label smoothing
loss = loss_func(out,label)

其中 out 是网络的输出,label 是输入对应的标签


http://chatgpt.dhexx.cn/article/qfxMOmsx.shtml

相关文章

关于label smoothing(标签平滑)

目的&#xff1a; label smoothing常用于分类任务&#xff0c;防止模型在训练中过拟合&#xff0c;提高模型的泛化能力。 意义&#xff1a; 对于分类问题&#xff0c;我们通常使用one-hot编码&#xff0c;“非黑即白”&#xff0c;标签向量的目标类别概率为1&#xff0c;非目标…

【AI面试】hard label与soft label,Label Smoothing Loss 和 Smooth L1 Loss

往期文章: AI/CV面试,直达目录汇总【AI面试】NMS 与 Soft NMS 的辨析【AI面试】L1 loss、L2 loss和Smooth L1 Loss,L1正则化和L2正则化在一次询问chatGPT时候,在他的回答中,由smooth L1联想提到了Label Smoothing Loss 。我把问题贴到下面,和chatGPT的回答,供你参考。不…

标签平滑(label smoothing)

目录 1.标签平滑主要解决什么问题&#xff1f; 2.标签平滑是怎么操作的&#xff1f; 3.标签平滑公式 4.代码实现 标签平滑&#xff08;label smoothing&#xff09;出自GoogleNet v3 关于one-hot编码的详细知识请见&#xff1a;One-hot编码 1.标签平滑主要解决什么问题&…

指数平滑法 Exponential Smoothing

指数平滑法 Exponential Smoothing 指数平滑法&#xff0c;用于中短期经济发展趋势预测。 1 时间序列分析基础知识 1.1 时间序列分析前提假设 时间序列分析一般假设我们获得的数据在时域上具有一定的相互依赖关系&#xff0c;例如股票价格在t时刻很高&#xff0c;那么在t1时…

label smoothing(标签平滑)

label smoothing是一种在分类问题中&#xff0c;防止过拟合的方法。 label smoothing&#xff08;标签平滑&#xff09; 交叉熵损失函数在多分类任务中存在的问题label smoothing&#xff08;标签平滑&#xff09;参考资料 交叉熵损失函数在多分类任务中存在的问题 多分类任务…

When Does Label Smoothing Help?

原文链接&#xff1a;When Does Label Smoothing Help? Hinton老师的这篇paper&#xff0c;解释了标签平滑策略在什么情况下是有效的&#xff1f; 摘要 通过从hard targets加权平均得到的soft targets&#xff0c;可以显著提升多分类神经网络的泛化性和训练速度。这种标签平…

【NLP基础理论】02 N-grams语言模型和Smoothing

注&#xff1a; Unimelb Comp90042 NLP笔记 相关tutorial代码链接 N-grams Language Model &#xff08;N-grams语言模型&#xff09; 目录 N-grams Language Model &#xff08;N-grams语言模型&#xff09;1.1 Deriving n-gram language models&#xff08;推导&#xff0…

Good-Turning Smoothing介绍及推理

在介绍Good-Turning Smoothing之前&#xff0c;我们可以先看一个有趣的例子&#xff1a; 假设你在钓鱼&#xff0c;已经抓到了18只鱼&#xff1a; 10条鲤鱼&#xff0c;3条黑鱼&#xff0c;2条刀鱼&#xff0c;1条鲨鱼&#xff0c;1条草鱼&#xff0c;1条鳗鱼… Q1&#xff1a;…

关于label smoothing的理解

背景介绍 提到label smoothing&#xff08;标签平滑&#xff09;&#xff0c;首先介绍一下什么是hard label和soft label. 简单来说&#xff0c;hard label就是非1即0&#xff0c;不存在既可能是A也可能是B的情况&#xff0c;soft label则不同&#xff0c;它并不要求所有的“精…

Label-Smoothing

论文&#xff1a;Rethinking the Inception Architecture for Computer Vision 个人理解&#xff1a; 就是让softmax不那么相信某一类的数据&#xff0c;增强泛化性。主要操作就是&#xff0c;在制作标签的时候&#xff0c;属于那一类就让网络90%相信他&#xff0c;其他…

MATLAB Smoothing Spline 拟合

参考 The Elements of Statistical Learning (chapter 5.4) MATLAB - Smoothing Splines MATLAB - fit 1. 基础 Smoothing Spline 可以用于离散数据的函数拟合。考虑下面的问题&#xff1a;在所有存在二阶连续导数的函数中寻找拟合函数 f ( x ) f(x) f(x)&#xff0c;可以使…

Label Smoothing分析

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶” 重磅干货&#xff0c;第一时间送达 作者丨王峰知乎 来源丨https://zhuanlan.zhihu.com/p/302843504 编辑丨极市平台 转自 | 极市平台 导读 Label Smoothing在图像识别中能稳定涨点&#xff0c;但在人脸的…

label smoothing

label smoothing 背景:当我们将交叉熵损失函数用于分类任务的时候,我们期望真实的标签为1,而其他虚假的标签为0。换句话来说,我们认为原始数据的标注是准确无误的。但是&#xff0c;真实情况并不是这样&#xff0c;在某些领域&#xff0c;或者使用一些数据增强的方法时,都会存…

标签平滑(Label Smoothing)详解

一、什么是label smoothing&#xff1f; 标签平滑&#xff08;Label smoothing&#xff09;&#xff0c;像L1、L2和dropout一样&#xff0c;是机器学习领域的一种正则化方法&#xff0c;通常用于分类问题&#xff0c;目的是防止模型在训练时过于自信地预测标签&#xff0c;改善…

标签平滑Label Smoothing

Lable Smoothing 是分类问题中错误标注的一种解决方法。 对于分类问题&#xff0c;特别是多分类问题&#xff0c;常常把向量转换成one-hot-vector&#xff08;独热向量&#xff09; one-hot带来的问题&#xff1a;&#xff08;对于独热的简单解释&#xff1a;https://blog.csd…

模型优化之Label Smoothing

1. 引言 Label Smoothing 又被称之为标签平滑&#xff0c;常常被用在分类网络中来作为防止过拟合的一种手段&#xff0c;整体方案简单易用&#xff0c;在小数据集上可以取得非常好的效果。 Label Smoothing 做为一种简单的训练trick&#xff0c;可以通过很少的代价&#xff08…

Smoothing

文章目录 返回主目录Add-one SmoothingAdd-K SmoothingInterpolationGood-Turning Smoothing 返回主目录 这是一个系列的文章&#xff0c;点击返回综合目录页 Add-one Smoothing P A d d − 1 ( W i ∣ W i − 1 ) C ( W i − 1 , W i ) 1 C ( W i ) V P_{Add-1}(W_i|W_{i-…

分类任务中常用的Label smoothing

目录 1.Label smoothing的原理 2.pytorh中如何使用Label smoothing 3.适用场景 1.Label smoothing的原理 交叉熵损失&#xff08;softmax cross Entropy&#xff09;中&#xff0c;常用公式&#xff1a; yi: 表示样本i的label,正类为1&#xff0c;负类为0&#xff1b; pi:…

平滑(smoothing)

1 问题的提出 由于在现实生活中&#xff0c;我们的观察尺度有限&#xff0c;我们的样本&#xff08;输入&#xff09;很可能没有办法包含所有可能的情况&#xff0c;那么我们怎么去处理先前看不见的事件呢&#xff1f; 举个例子&#xff0c;莎士比亚使用了30000个双连词(bigra…

【简单理解】自然语言处理-平滑方法(Smoothing)

【简单理解】自然语言处理-平滑方法(Smoothing) 简单介绍平滑策略 平滑策略的引入&#xff0c;主要使为了解决语言模型计算过程中出现的零概率问题。零概率问题又会对语言模型中N-gram模型的Perplexity评估带来困难。 零概率问题&#xff0c;就是在计算实例的概率时&#xf…