N-Grams

N-Gram是一种简单有效的语言模型,用于计算给定历史词语序列下下一个词的概率。本文介绍了N-Gram的基本概念,如bigram和trigram,以及如何解决概率为0的问题。通过最大似然估计方法估计概率,并讨论了实际操作中的问题,如处理短历史和使用对数概率。此外,还提到了评估语言模型的困惑度指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给一系列的词语计算概率的模型叫做语言模型(Language Models),其中,n-gram是最简单的一种。一个n-gram就是一个长度为N的词语组成的序列:

  • N=2,则是2-gram(bigram)
  • N=3,则是3-gram(trigram)

一个简单的例子

有一个任务,要计算 P ( w ∣ h ) P(w\vert h) P(wh),即给定历史 h h h计算 w w w的概率。假设 h = 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 h=its\ water\ is\ so\ transparent\ that h=its water is so transparent 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\ \vert\ its\ water\ is\ so\ transparent\ that) P(the  its water is so transparent that)

一个可行的方式是:在一个很大的语料库中,统计 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 its\ water\ is\ so\ transparent\ that its water is so transparent that出现的次数,然后统计 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 its\ water\ is\ so\ transparent\ that\ the its water is so transparent 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 ) = 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\ \vert\ 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)

这种方式在很多情况下可行。但是某些情况下仍然会有以下问题:

  • 有些词语在预料中出现次数为0。

相似的问题还出现在:如果我们想知道整个序列的联合概率,例如 P ( i t s   w a t e r   i s   s o   t r a n s p a r e n t ) P(its\ water\ is\ so\ transparent) P(its water is so transparent),那我们就可以将问题转化为:“在所有5个词语的序列中,its water is so transparent出现了几次?”

为了解决这个问题,我们需要更好地方式来估计 w w w基于 h h h的概率,或者整个序列的概率。

我们把一个长度为N的序列表示为 w 1 , w 2 , … , w n w_1,w_2,\dots,w_n w1,w2,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值