【语言模型】

本文介绍了语言模型的基本概念,探讨了如何用n元语法估计联合概率,以及其在预训练模型、文本生成和序列判断等场景的应用。通过马尔科夫假设简化长序列计算,并详细阐述了一元语法和二元语法的计算方式。

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

  • 给定文本序列 x 1 , . . . , x T x_1,...,x_T x1,...,xT,语言模型的目标是估计联合概率 p ( x 1 , . . . , x T ) p(x_1,...,x_T) p(x1,...,xT)
  • 它的应用包括
    • 做预训练模型(BERT, GPT-3)
    • 生成文本,给定前几个词,不断使用 x t ∼ p ( x t ∣ x 1 , . . . , x t − 1 ) x_t\sim p(x_t|x_1,...,x_{t-1}) xtp(xtx1,...,xt1)来生成后续文本
    • 判断多个序列中哪个更常见(语音识别发音相似的词句、打字时补全提示以及纠错)

使用计数来建模

  • 假设序列长度为2,我们预测 p ( x , x ′ ) = p ( x ) p ( x ′ ∣ x ) = n ( x ) n n ( x , x ′ ) n ( x ) p(x,x^{'})=p(x)p(x^{'}|x)=\frac{n(x)}{n}\frac{n(x,x^{'})}{n(x)} p(x,x)=p(x)p(xx)=nn(x)n(x)n(x,x)
    • 其中 n n n代表总词数, n ( x ) n(x) n(x)表示词 x x x出现的次数, n ( x , x ′ ) n(x,x^{'}) n(x,x)为单词 x x x x ′ x^{'} x连续出现的概率
  • 很容易拓展到长为3的情况
    p ( x , x ′ , x ′ ′ ) = p ( x ) p ( x ′ ∣ x ) p ( x ′ ′ ∣ x , x ′ ) = n ( x ) n n ( x , x ′ ) n ( x ) n ( x , x ′ , x ′ ′ ) n ( x , x ′ ) p(x,x^{'},x^{''})=p(x)p(x^{'}|x)p(x^{''}|x,x^{'})=\frac{n(x)}{n}\frac{n(x,x^{'})}{n(x)}\frac{n(x,x^{'},x^{''})}{n(x,x^{'})} p(x,x,x)=p(x)p(xx)p(xx,x)=nn(x)n(x)n(x,x)n(x,x)n(x,x,x)

N元语法

  • 当序列很长时,又会出现难以计算的情况,因此可以使用马尔科夫假设缓解这个问题
  • 这里我们假设序列长度为4
    • 一元语法(假设所有词的出现是相互独立的): p ( x 1 , x 2 , x 3 , x 4 ) = p ( x 1 ) p ( x 2 ) p ( x 3 ) p ( x 4 ) = n ( x 1 ) n n ( x 2 ) n n ( x 3 ) n n ( x 4 ) n p(x_1,x_2,x_3,x_4)=p(x_1)p(x_2)p(x_3)p(x_4)=\frac{n(x_1)}{n}\frac{n(x_2)}{n}\frac{n(x_3)}{n}\frac{n(x_4)}{n} p(x1,x2,x3,x4)=p(x1)p(x2)p(x3)p(x4)=nn(x1)nn(x2)nn(x3)nn(x4)
    • 二元语法(词的出现跟前一个词有关): p ( x 1 , x 2 , x 3 , x 4 ) = p ( x 1 ) p ( x 2 ∣ x 1 ) p ( x 3 ∣ x 2 ) p ( x 4 ∣ x 3 ) = n ( x 1 ) n n ( x 1 , x 2 ) n n ( x 2 , x 3 ) n n ( x 3 , x 4 ) n p(x_1,x_2,x_3,x_4)=p(x_1)p(x_2|x_1)p(x_3|x_2)p(x_4|x_3)=\frac{n(x_1)}{n}\frac{n(x_1,x_2)}{n}\frac{n(x_2,x_3)}{n}\frac{n(x_3,x_4)}{n} p(x1,x2,x3,x4)=p(x1)p(x2x1)p(x3x2)p(x4x3)=nn(x1)nn(x1,x2)nn(x2,x3)nn(x3,x4)
    • 因此就可以扩展到 N元语法

总结

  • 语言模型估计文本序列的联合概率
  • 使用统计方法时常采用n元语法
import random
import torch
from d2l import torch as d2l

tokens = d2l.tokenize(d2l.read_time_machine())
corpus = [token for line in tokens for token in line]
vocab = d2l.Vocab(corpus)
vocab.token_freqs[:10]

[('the', 2261),
 ('i', 1267),
 ('and', 1245),
 ('of', 1155),
 ('a', 816),
 ('to', 695),
 ('was', 552),
 ('in', 541),
 ('that', 443),
 ('my', 440)]
# 统计单个单词出现的频率
freqs = [freq for token, freq in vocab.token_freqs]
d2l.plot(freqs, xlabel='token: x', ylabel='frequency: n(x)',
         xscale='log', yscale='log')

在这里插入图片描述

bigram_tokens = [pair for pair in zip(corpus[:-1], corpus[1:])]
bigram_vocab = d2l.Vocab(bigram_tokens)
bigram_vocab.token_freqs[:10]
[(('of', 'the'), 309),
 (('in', 'the'), 169),
 (('i', 'had'), 130),
 (('i', 'was'), 112),
 (('and', 'the'), 109),
 (('the', 'time'), 102),
 (('it', 'was'), 99),
 (('to', 'the'), 85),
 (('as', 'i'), 78),
 (('of', 'a'), 73)]
# 这里我们并没有去除停用词,可以看到三元词开始如果去除停用词高频词都是有具体意义的
trigram_tokens = [triple for triple in zip(
    corpus[:-2], corpus[1:-1], corpus[2:])]
trigram_vocab = d2l.Vocab(trigram_tokens)
trigram_vocab.token_freqs[:10]
[(('the', 'time', 'traveller'), 59),
 (('the', 'time', 'machine'), 30),
 (('the', 'medical', 'man'), 24),
 (('it', 'seemed', 'to'), 16),
 (('it', 'was', 'a'), 15),
 (('here', 'and', 'there'), 15),
 (('seemed', 'to', 'me'), 14),
 (('i', 'did', 'not'), 14),
 (('i', 'saw', 'the'), 13),
 (('i', 'began', 'to'), 13)]
bigram_freqs = [freq for token, freq in bigram_vocab.token_freqs]
trigram_freqs = [freq for token, freq in trigram_vocab.token_freqs]
d2l.plot([freqs, bigram_freqs, trigram_freqs], xlabel='token: x',
         ylabel='frequency: n(x)', xscale='log', yscale='log',
         legend=['unigram', 'bigram', 'trigram'])

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟炼丹师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值