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

article/2025/8/19 2:41:06

注:

Unimelb Comp90042 NLP笔记

相关tutorial代码链接

N-grams Language Model (N-grams语言模型)

目录

  • N-grams Language Model (N-grams语言模型)
  • 1.1 Deriving n-gram language models(推导)
  • 1.2 Smoothing(平滑)
    • 1.2.1 Laplacian (Add-one)
    • 1.2.2 Add-k smoothing
    • 1.2.3 Absolute Discounting
    • 1.2.4 Backoff (Katz Backoff)
    • 1.2.5 Kneser-Ney Smoothing
    • 1.2.6 Interpolation(插值)
    • 1.2.7 Interpolation Kneser-Ney
    • 1.3 应用

语句生成demo网站,大家可以去玩一玩。

N-gram一种基于统计语言模型的算法。它的基本思想是将文本里面的内容按照字节进行大小为N的滑动窗口操作,形成了长度是N的字节片段序列。

普遍应用:搜索框下方提示、机器翻译、生成总结、对话系统…现代的nlp系统的脊柱就是预训练语言模型。

如何理解N-gram:1

目前有一句话“its water is so transparent that”,然后我们想知道这个that后面第一个词是the的概率是什么该怎么办?首先我们将这件事用公式表达:
P ( t h e ∣ i t s w a t e r i s s o t r a n s p a r e n t t h a t ) P(the\ |\ its\ water\ is\ so\ transparent\ that) P(the  its water is so transparent that)
为了计算这个概率,最简单的办法就是在一个巨大的语料库中去数数,然后得到:
P ( t h e ∣ i t s w a t e r i s s o t r a n s p a r e n t t h a t ) = C ( i t s w a t e r i s s o t r a n s p a r e n t t h a t t h e ) C ( i t s w a t e r i s s o t r a n s p a r e n t t h a t ) P(the\ |\ its\ water\ is\ so\ transparent\ that)=\\ \frac{C(its\ water\ is\ so\ transparent\ that\ the)}{C(its\ water\ is\ so\ transparent\ that)} P(the  its water is so transparent that)=C(its water is so transparent that)C(its water is so transparent that the)
但就算找了很多,也达不到很好的估计,因为语言总是千变万化,有可能我们在网上看到的是一句"Walden Pond’s water is so transparent that the",那和我们想要计数的句子就不一样了。
除此之外,当我们想知道一串任意单词的概率一起出现的概率是什么,比如 P ( i t s w a t e r i s s o t r a n s p a r e n t t h a t ) P(its\ water\ is\ so\ transparent\ that) P(its water is so transparent that),最简单的方法就是这句话出现的数量比上所有任意单词中挑选五个的数量,但根本不能实现。那我们得用更聪明的方法来计算概率,接下来往下看!

1.1 Deriving n-gram language models(推导)

联合概率转变条件概率
目的是得到一串任意单词的概率: P ( w 1 , w 2 , … , w m ) P(w_1,w_2,\dots,w_m) P(w1,w2,,wm)
用chain rule将联合(joint)概率变成条件(conditional)概率
P ( w 1 , w 2 , … , w m ) = P ( w 1 ) P ( w 2 ∣ w 1 ) P ( w 3 ∣ w 1 , w 2 ) … P ( w m ∣ w 1 , … , w m − 1 ) P(w_1,w_2,\dots,w_m) = \\P(w_1)P(w_2|w_1)P(w_3|w_1,w_2)\dots P(w_m|w_1,\dots,w_{m-1}) P(w1,w2,,wm)=P(w1)P(w2w1)P(w3w1,w2)P(wmw1,,wm1)
但这似乎没有改变计算难度,所以就要用到马可夫假设。

马可夫假设(The Markov Assumption)
TODO:markov证明
Markov可以假设 P ( w i ∣ w 1 , … , w i − 1 ) ≈ P ( w i ∣ w i − n + 1 , … , w i − 1 ) P(w_i|w_1,\dots,w_{i-1}) \approx P(w_i|w_{i-n+1},\dots,w_{i-1}) P(wiw1,,wi1)P(wiwin+1,,wi1).
也就是原本要考虑前面单词1、单词2…单词i-1出现的时候,单词i才出现的概率是什么,现在通过假设,我们只要在意单词i-n+1到单词i-1这部分单词出现,然后单词i出现的概率。
举个例子:
P ( t h e ∣ i t s w a t e r i s s o t r a n s p a r e n t t h a t ) ≈ P ( t h e ∣ t r a n s p a r e n t t h a t ) P(the\ |\ its\ water\ is\ so\ transparent\ that) \approx \\ P(the\ |\ transparent\ that) P(the  its water is so transparent that)P(the  transparent that)
我们现在只关心当前面是 t r a n s p a r e n t t h a t transparent\ that transparent that 的时候,后面出现 t h e the the的概率有多少,然后用这个概率来估计。

这里我们引出N-gram的本意,就是用前面N-1个词就可以来估计前面一大段词语的概率。

  1. n=1,称为unigram model,只关注这个词出现的概率,那一串词语同时出现的概率就是这些词单独出现概率进行相乘(他们各自都独立互不影响):
    P ( w 1 , w 2 , … , w m ) = ∏ i = 1 m P ( w i ) P(w_1,w_2,\dots,w_m) = \prod_{i=1}^m P(w_i) P(w1,w2,,wm)=i=1mP(wi)
  2. n=2,称为bigram model,看要预测的词前面的单词是什么。就相当于遍历这些词看每一个词在前面是某个词的情况下出现的概率是多少,并相乘。:
    P ( w 1 , w 2 , … , w m ) = ∏ i = 1 m P ( w i ∣ w i − 1 ) P(w_1,w_2,\dots,w_m) = \prod_{i=1}^m P(w_i|w_{i-1}) P(w1,w2,,wm)=i=1mP(wiwi1)
    比如 the dog barks,我们想知道 P ( b a r k s ∣ t h e l i t t l e d o g ) P(barks\ |\ the\ little\ dog) P(barks  the little dog)的概率,那在bigram下我们可以只看 P ( b a r k s ∣ d o g ) P(barks\ |\ dog) P(barks  dog)的概率。
  3. n=3, 称为trigram model:
    P ( w 1 , w 2 , … , w m ) = ∏ i = 1 m P ( w i ∣ w i − 2 w i − 1 ) P(w_1,w_2,\dots,w_m) = \prod_{i=1}^m P(w_i|w_{i-2}w_{i-1}) P(w1,w2,,wm)=i=1mP(wiwi2wi1)
    P ( b a r k s ∣ l i t t l e d o g ) P(barks\ |\ little\ dog) P(barks  little dog)的概率预估。

那我们现在该怎么计算这些变简单的概率?

最大似然估计(Maximum Likelihood Estimation)
通过在语料库中数数,然后将它们归一化(通常会对概率结果进行log,但此处没有)。

比如在bigram model里,我们通过数 w n − 1 w n w_{n-1}w_n wn1wn一起出现的次数,再比上 w n − 1 w w_{n-1}w wn1w出现的次数。这里 w w w是指任意一个单词但前面一定是 w n − 1 w_{n-1} wn1
P ( w n ∣ w n − 1 ) = C ( w n − 1 w n ) ∑ w C ( w n − 1 w ) P(w_n|w_{n-1}) = \frac{C(w_{n-1}w_n)}{\sum_wC(w_{n-1}w)} P(wnwn1)=wC(wn1w)C(wn1wn)
这个公式我们也可以进行简化,不同 w n − 1 w w_{n-1}w wn1w出现的次数就等于 w n − 1 w_{n-1} wn1出现的次数。
P ( w n ∣ w n − 1 ) = C ( w n − 1 w n ) C ( w n − 1 ) P(w_n|w_{n-1}) = \frac{C(w_{n-1}w_n)}{C(w_{n-1})} P(wnwn1)=C(wn1)C(wn1wn)
总结

  1. unigram,
    P ( w i ) = C ( w i ) M , M 是 所 有 出 现 过 单 词 的 数 量 P(w_i) = \frac{C(w_i)}{M} ,\quad M是所有出现过单词的数量 P(wi)=MC(wi),M
  2. bigram,
    P ( w i ∣ w i − 1 ) = C ( w i − 1 w i ) C ( w i − 1 ) , C ( d o g b a r k s ) C ( d o g ) P(w_i|w_{i-1}) = \frac{C(w_{i-1}w_{i})}{C(w_{i-1})} ,\quad \frac{C(dog\ barks)}{C(dog)} P(wiwi1)=C(wi1)C(wi1wi),C(dog)C(dog barks)
  3. n-gram,
    P ( w i ∣ w i − n + 1 … w i − 1 ) = C ( w i − n + 1 … w i − 1 w i ) C ( w i − n + 1 … w i − 1 ) P(w_i|w_{i-n+1}\dots w_{i-1}) = \frac{C(w_{i-n+1}\dots w_{i-1}w_{i})}{C(w_{i-n+1}\dots w_{i-1})} P(wiwin+1wi1)=C(win+1wi1)C(win+1wi1wi)

Trigram计算例子
补充:在每一句需要处理的话前后需要加上两个特殊标签
<s> = 语句开始; </s> = 语句结束,tag可以当做一个单词来做处理。<s>的数量根据n决定,n > 1时,数量为n-1。
句子1 “yes no no no no yes”
句子2 “no no no yes yes yes no”
问:在trigram model下,"yes no no yes"会出现的概率是多少?
答:给句子加上tag
句子1:<s> <s> yes no no no no yes </s>
句子2: <s> <s> no no no yes yes yes no </s>
遍历每一个词和该词前面两个词,注意语句结束tag也需要计算概率,因为需要确定最后结束语句的单词是什么。
P ( y e s n o n o y e s ) = P ( y e s ∣ < s > < s > ) × P ( n o ∣ < s > y e s ) × P ( n o ∣ y e s n o ) × P ( y e s ∣ n o n o ) × P ( < / s > ∣ n o y e s ) P(yes\ no\ no\ yes) = P(yes|<s><s>) \times \\P(no|<s>yes) \times \\P(no|\ yes\ no) \times \\P(yes|\ no\ no) \times \\P(</s>|\ no\ yes) P(yes no no yes)=P(yes<s><s>)×P(no<s>yes)×P(no yes no)×P(yes no no)×P(</s> no yes)

P(x)Px
P(yes | <s> <s>)1/2<s> <s> yes
<s> <s> no
P(no | <s> yes)1/1<s> yes no
P(no | yes no)1/2yes no no
yes no </s>
P(yes | no no)2/5no no no
no no no
no no yes
no no no
no no yes
P( </s> | no yes)1/2no yes </s>
no yes yes
P ( y e s n o n o y e s ) = 0.05 P(yes\ no\ no\ yes) = 0.05 P(yes no no yes)=0.05

存在的问题

  1. 如果一句话中有很多单词,那就面临着越来越多概率的相乘,结果会越来越小,导致underflow。(如果每次相乘都要避免underflow,那会增加很大算量,所以可以用log probability来帮忙。使得 p 1 × p 2 × p 3 = e x p ( l o g p 1 + l o g p 2 + l o g p 3 ) p_1 \times p_2 \times p_3 = exp(log\ p_1 + log\ p_2 + log\ p_3) p1×p2×p3=exp(log p1+log p2+log p3),让每次概率用log之和来计算,并在最后通过e的指数来得到最终概率。)
  2. 一句话中前后相互影响的单词间隔可能很长,则需要的n会很大。
    比如: The lecture/s that took place last week was/were on preprocessing. 前面的单复数会影响后面be动词的形态。
  3. 当出现没有见过的单词组合(但见过单词)怎么办?那这样概率就会为0,所以需要smooth。 **注:**如果出现没见的单词,则需要另一种处理方法(增加的token)

:一般一个模型的好坏可以通过困惑度(perplexity) 来表示,句子概率越大,模型越好,困惑度越小。(参考)

1.2 Smoothing(平滑)

Basic idea:给每个没见过的事件都来点概率。但同时要保证整体概率还是1。
不同的smoothing方法2

  • Laplacian (add-one) smoothing (拉普拉斯平滑)
  • Add-k smoothing
  • Absolute discounting
  • Kneser-Ney

1.2.1 Laplacian (Add-one)

Simple idea: 假装每一个n-gram都多了一个,那这样分母就会多|V|个(当前词表的数量,不重复)。
P ( w i ∣ w i − n + 1 … w i − 1 ) = C ( w i − n + 1 … w i − 1 w i ) + 1 C ( w i − n + 1 … w i − 1 ) + ∣ V ∣ P(w_i|w_{i-n+1}\dots w_{i-1}) = \frac{C(w_{i-n+1}\dots w_{i-1}w_{i})+1}{C(w_{i-n+1}\dots w_{i-1})+|V|} P(wiwin+1wi1)=C(win+1wi1)+VC(win+1wi1wi)+1


句子<s> the rat ate the cheese </s> P ( a t e ∣ r a t ) P(ate|rat) P(aterat) P ( a t e ∣ c h e e s e ) P(ate|cheese) P(atecheese)在add-one smoothing下的bigram probability是多少?
答:
P = C ( r a t a t e ) + 1 C ( r a t ) + ∣ V ∣ = 2 6 P = \frac{C(rat\ ate)+1}{C(rat)+|V|} = \frac{2}{6} P=C(rat)+VC(rat ate)+1=62,
V = { t h e , r a t , a t e , c h e e s e , < / s > } V=\{the,rat,ate,cheese,</s>\} V={the,rat,ate,cheese,</s>},这里不用加上<s>是因为不需要去预测它出现的概率;
P = C ( c h e e s e a t e ) + 1 C ( c h e e s e ) + ∣ V ∣ = 1 6 P = \frac{C(cheese\ ate)+1}{C(cheese)+|V|} = \frac{1}{6} P=C(cheese)+VC(cheese ate)+1=61

注:M可以理解为 M = ∑ i = 1 V c i M=\sum_{i=1}^Vc_i M=i=1Vci, c_i是token i的数量。V可以理解为有多少不同的单词出现。

1.2.2 Add-k smoothing

通常加1已经很多了,所以我们会在1前面加一个因子 k k k(hyper parameter)。Add-k smoothing, aka Lidstone Smoothing 或者 Add- α \alpha α Smoothing。
P a d d k ( w i ∣ w i − n + 1 … w i − 1 ) = C ( w i − n + 1 … w i − 1 w i ) + k C ( w i − n + 1 … w i − 1 ) + k ∣ V ∣ P_{addk}(w_i|w_{i-n+1}\dots w_{i-1}) = \frac{C(w_{i-n+1}\dots w_{i-1}w_{i})+k}{C(w_{i-n+1}\dots w_{i-1})+k|V|} Paddk(wiwin+1wi1)=C(win+1wi1)+kVC(win+1wi1wi)+k
问题就在于如何选取 k k k


以bigram model为例子,假设我们当前要预测alleged ____ 后面出现某个词的概率。这里给了已有的情况:alleged impropriety总共出现了8次,alleged offense出现了5次…(见下表)
假设要预测 P ( o f f e n s e ∣ a l l e g e d ) = C ( a l l e g e d o f f e n s e ) C ( a l l e g e d ) P(offense|alleged) = \frac{C(alleged\ \ offense)}{C(alleged)} P(offensealleged)=C(alleged)C(alleged  offense),经过平滑处理之后可以算的结果为 8 + 0.1 20 + 7 ∗ 0.1 = 0.391 \frac{8+0.1}{20+7*0.1} = 0.391 20+70.18+0.1=0.391
根据add-k算出smoothed probability之后再去乘上总数量,就有有效计数(effective counts)。
在这里插入图片描述
在这里,M是20,V是7。

1.2.3 Absolute Discounting

通过从已经观察到的n-gram上借一个固定的概率质量(probability mass),然后将它们重新分配到没见到的单词上面。下图第一组出现的"effective counts"和"smoothed probability"是add- k k k的,用于对比展示。最后两列是根据absolute discounting得到的结果。
与add- k k k不同的是,absolute discounting先算出effective count,再得到smoothed probability。
下图中有5个单词是出现过的,所以我们设 d = 0.1 d=0.1 d=0.1,即从每一个出现过的单词数量中扣除0.1,然后扣除总和再去平方给没出现过的单词(这里是两个,所以是0.1 * 5 / 2 = 0.25)
在这里插入图片描述

1.2.4 Backoff (Katz Backoff)

与absolute discounting类似,但backoff是将概率质量根据 低阶模型(a lower order model)将值分给所有没见过的单词。

  • 如果我们想知道某个trigram的概率 P ( w n ∣ w n − 1 w n − 2 ) P(w_n|w_{n-1}w_{n-2}) P(wnwn1wn2),但是这个概率当前为0。
  • 那我们可以通过bigram的概率 P ( w n ∣ w n − 1 ) P(w_n|w_{n-1}) P(wnwn1)来估计。
  • 如果bigram的也是0,那就看unigram的概率 P ( w n ) P(w_n) P(wn)
  • 我们只会在高n没有概率的时候采用backoff的方法到一个低n的概率(a lower order model)
  • 如果tirgram原本就存在,那我们就用absolute discounting给它进行平滑。

以bigram model为例:
P k a t z ( w i ∣ w i − 1 ) = { C ( w i − 1 , w i ) − D C ( w i − 1 ) , i f C ( w i − 1 w i ) > 0 , e l s e α ( w i − 1 ) P ( w i ) ∑ w j : C ( w i − 1 , w j ) = 0 P ( w j ) , o t h e r w i s e P_{katz}(w_i|w_{i-1})= \left\{ \begin{array}{lr} \frac{C(w_{i-1},w_i)-D}{C(w_i-1)}, & if\ C(w_{i-1}w_i) \gt 0,else\\ \alpha (w_{i-1})\frac{P(w_i)}{\sum_{w_j:C(w_{i-1},w_j)=0}P(w_j)}, & otherwise\\ \end{array} \right. Pkatz(wiwi1)=C(wi1)C(wi1,wi)D,α(wi1)wj:C(wi1,wj)=0P(wj)P(wi),if C(wi1wi)>0,elseotherwise
其中, α ( w i − 1 ) \alpha (w_{i-1}) α(wi1)表示从 w i − 1 w_{i-1} wi1中扣除的概率质量; P ( w i ) P(w_i) P(wi)表示 w i w_i wi的unigram概率; ∑ w j : C ( w i − 1 , w j ) = 0 P ( w j ) \sum_{w_j:C(w_{i-1},w_j)=0}P(w_j) wj:C(wi1,wj)=0P(wj)表示所有没有和 w i − 1 w_{i-1} wi1共现的单词的unigram概率之和(以前面表格为例, w j w_j wj就是infirmitycephalopods)。

  • 如果 C ( w i − 1 , w i ) > 0 C(w_{i-1},w_i)\gt0 C(wi1,wi)>0,backoff就等于先前的absolute discounting
  • 当为想要的概率不存在,那么 α ( w i − 1 ) \alpha (w_{i-1}) α(wi1)就为分配到 w i − 1 w_{i-1} wi1的概率质量(以前面表格为例就是 5 * 0.1 = 0.5)
  • 以前面表格为例,假设infirmitycephalopods出现的更频繁(因为bigram没有出现过,但是unigram的时候会出现),就给infirmity更高的权重(公式来看的话,因为分母相同分子大所以概率更大)。

Backoff会有的问题:
可能由于语料库数据分布不均匀,导致结果不合理。
举个例子,现有个句子:
I cannot see without my reading ____
假设我们现在用bigram model,然后来看看reading后面跟glasses和Francisco的概率如何?
但不巧,现有语料库中从来没有出现过reading glassesreading Francisco这两种组合,那我们使用Backoff Smoothing来尝试解决。由于bigram下两者Count都是0,所以就去看他们的unigram model的概率。假设语料库中,Francisco出现的次数比glasses多很多,那根据公式就得出,Francisco出现在reading后面的概率更大。
但实际上 reading glasses在语境中才更有意义。

1.2.5 Kneser-Ney Smoothing

为了解决backoff的缺陷,有了这个平滑方法。
我们将概率质量的分配基于低阶模型的多功能性上(versatility)。一个词的多功能性,就是说这个词会和多少个不同的词一起出现。
以上文中glassesFrancisco为例,我们经常能看到men’s glasses, buy glasses, etc,但是Francisco一般只会和San Francisco一起出现。那glasses的多功能性就比Francisco高。
这种多功能性也叫做连续概率(continuation probability)。

以bigram model为例:
P K N ( w i ∣ w i − 1 ) = { C ( w i − 1 , w i ) − D C ( w i − 1 ) , i f C ( w i − 1 w i ) > 0 , e l s e β ( w i − 1 ) P c o n t ( w i ) , o t h e r w i s e P_{KN}(w_i|w_{i-1}) = \left\{ \begin{array}{lr} \frac{C(w_{i-1},w_i)-D}{C(w_i-1)}, & if\ C(w_{i-1}w_i) \gt 0,else\\ \beta (w_{i-1})P_{cont}(w_i), & otherwise\\ \end{array} \right. PKN(wiwi1)={C(wi1)C(wi1,wi)D,β(wi1)Pcont(wi),if C(wi1wi)>0,elseotherwise
其中,
P c o n t ( w i ) = ∣ { w i − 1 : C ( w i − 1 , w i ) > 0 } ∑ w i ∣ { w i − 1 : C ( w i − 1 , w i ) > 0 } ∣ P_{cont}(w_i) = \frac{|\{w_{i-1}:C(w_{i-1},w_i)>0\}}{\sum_{w_i}|\{w_{i-1}:C(w_{i-1},w_i) >0\}|} Pcont(wi)=wi{wi1:C(wi1,wi)>0}{wi1:C(wi1,wi)>0}
P c o n t P_{cont} Pcont中,分子部分计算的是一共有多少个 唯一 的单词 w i − 1 w_{i-1} wi1 和当前单词 w i w_i wi 共同出现过(co-occur);而分母部分就是将所有可能的单词 w i w_i wi对应的不同共现上下文单词数量(即分子部分)进行一个累加。可以想做分子是分母的去重。
β ( w i − 1 ) \beta(w_{i-1}) β(wi1)和前文的 α \alpha α是一个意思。

1.2.6 Interpolation(插值)

相比之前的回退算法,我们可以将不同阶的n-gram做一个结合。
以一个trigram model为例子:
P I N ( w i ∣ w i − 1 , w i − 2 ) = λ 3 P 3 ( w i ∣ w i − 2 , w i − 1 ) + λ 2 P 2 ( w i ∣ w i − 1 ) + λ 1 P 1 ( w i ) λ 1 + λ 2 + λ 3 = 1 \begin{aligned} P_{IN}(w_i|w_{i-1},w_{i-2}) &= \lambda _3P_3(w_i|w_{i-2},w_{i-1}) \\&+ \lambda _2P_2(w_i|w_{i-1})\\ &+ \lambda _1P_1(w_i)\\ \lambda_1 + \lambda_2 + \lambda_3 = 1 \end{aligned} PIN(wiwi1,wi2)λ1+λ2+λ3=1=λ3P3(wiwi2,wi1)+λ2P2(wiwi1)+λ1P1(wi)
λ \lambda λ不需要人工设置,而是通过模型对于一些留存数据学习到的,这样可以学习到一个最好的平衡。
当所有的概率都0的时候,我们需要 zero gram概率,也就是OOV概率,即一个单词他没有出现的概率是什么,一般最简单的做法就是统计当前词典中所有词出现的次数,这个数分之一就是OOV的概率。

1.2.7 Interpolation Kneser-Ney

目前所有已经介绍过的平滑中最有效的方法。
以bigram model为例子:
P I K N ( w i ∣ w i − 1 ) = C ( w i − 1 , w i ) − D C ( w i − 1 ) + β ( w i − 1 ) P c o n t ( w i ) P_{IKN}(w_i|w_{i-1}) = \frac{C(w_{i-1},w_i)-D}{C(w_{i-1})}+\beta(w_{i-1})P_{cont}(w_i) PIKN(wiwi1)=C(wi1)C(wi1,wi)D+β(wi1)Pcont(wi)
但这里 β ( w i − 1 ) \beta(w_{i-1}) β(wi1)是一个归一化常数,以确保两项之和小于等于1。

1.3 应用

可参考链接
一般用于判断句子是否合理,通过看 N 个词出现的频次如何。现实中的应用就是在搜索栏打字的时候,下方会进行联想,也就是根据当前你所输入的内容弹出下一个概率最大的词
在这里插入图片描述


  1. N-gram standford pdf:https://web.stanford.edu/~jurafsky/slp3/3.pdf ↩︎

  2. n-gram平滑的方法:https://blog.csdn.net/fuermolei/article/details/81353746 ↩︎


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

相关文章

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…

二十二、动网格Smoothing Spring方法及实例

1 概念介绍 最近一直忙着其他的事情&#xff0c;好久都没有更新&#xff0c;让各位家人们久等了。 今天我们接着聊一聊动网格问题&#xff0c;我们在文章二十一中介绍了动网格的Layering方法&#xff0c;但是Layering方法的局限性很高&#xff0c;只能对四边形网格或六面体网格…

numpy安装

1.以管理员的形式打开cmd 2.安装numpy插件 pip install numpy 3.安装成功后&#xff0c;输入pip list 命令查看是否安装成功 pip list 3.创建 #使用array创建一维数组 list01 [1,2,3,4] np01 np.array(list01) print(np01) print(type(np01)) #使用array创建二维数组 …

Matplotlib安装

安装matplotlib 使用python中pip命令安装 使用python中pip命令安装 第一步&#xff1a;使用winR输入cmd命令打开命令窗口&#xff0c;找到python安装目录下的Scripts目录&#xff0c;使用python -m pip install matplotlib命令 如果出现以下错误或警告&#xff0c;则需按照指示…

node安装

1、进入node官网&#xff0c;下载长期支持版&#xff0c;安装 2、输入node -v&#xff0c;能查询到版本号即为安装成功 3、为方便管理&#xff0c;修改默认的缓存和模块存放路径&#xff08;C:\Users\Administrator\AppData\Roaming\npm&#xff09;&#xff0c;在nodede 安装文…

如何安装仿宋GB2312字体

步骤&#xff1a; 一、下载仿宋_GB2312.zip压缩包并解压缩&#xff1b; 百度网盘&#xff1a;https://pan.baidu.com/s/1L7yYKhoDmUw1RpNXVS5KDQ 提取码&#xff1a;eclz 阿里云盘&#xff1a;https://www.aliyundrive.com/s/ih3kDDidcGa 二、双击打开字体文件&#xff0c;…

安装Matplotlib

文章目录 一、pip安装二、conda安装1、anaconda navigator安装2、condan命令安装 一、pip安装 运行如下的命令即可 pip install matplotlib二、conda安装 Anaconda包括Conda、Python以及一大堆安装好的工具包&#xff0c;比如&#xff1a;numpy、pandas等。conda是一个开源的…

cnpm安装

1.将node安装到D:\Program Files\nodejs目录。 2.设置npm全局安装路径与缓存路径 npm config set prefix "D:\Program Files\nodejs\node_global" npm config set cache "D:\Program Files\nodejs\node_cache" 3.安装cnpm npm install -g cnpm --regis…