【NLP】3 Language Model

3 Language Modelling

3.1 Direct Application of LM: Noisy Channel Model

3.2 How to estimate the probabilities

3.2.1 General idea: Probability Factorization

3.2.2 Try an easy one first: The Unigram Model

3.2.3 General idea: N-gram Language Model

3.3 Evaluation of Language Model

3.4 Smoothing

3.4.1 Add-one Smoothing (Laplace Smoothing)

3.4.2 Add-K Smoothing (Laplace Smoothing)

3.4.3 Interpolation


3 Language Modelling

Probability of a sentence := how likely is it to occur in natural language.

Language Model: used for compute the probability of a given sentence w = w_1,w_2,…,w_n.

With the language model, we could know:


3.1 Direct Application of LM: Noisy Channel Model

Noisy Channel Model:是一个从原始序列经过加工(encoding)到输出序列的过程。我们最终希望的是通过输出序列能够反推原始序列,也就是想求得 P(Y|X) 。进一步,可通过贝叶斯公式:

 其中 P(Y) 和 P(X|Y) 是比较容易求得的。

举个例子,我们在说话(speech)的过程中,Y 就是我们脑中想要表达的意思,经过大脑的加工以及从嘴说出等参在一系列的加工过程后 P(X|Y),得到一段语音序列(X)。那么 Speech recognition 要做的,就是训练出 将 X 转化为 Y 的概率分布 P(Y|X),最终可通过语言(X)得到文本含义(Y)。

在 Noisy Channel Model 中,P(Y) 就是一个语言模型。它表示的是 Y 这句话是不是人话。 


3.2 How to estimate the probabilities

For a sentence w, we could try to directly estimate probability of each sentence: 

 Nevertheless, for nearly all w, C(w) = 0. 这就会导致 Sparse Data Problem。

Sparse Data Problem:not enough observations to estimate probabilities well.

因此,我们需要想办法去近似这个概率。

3.2.1 General idea: Probability Factorization

使用条件概率的链式法则展开来计算该句子出现的概率。

 例子:He has changed China.

P(he, has, changed, china) = P(china|he, has, changed) * P(changed|he, has) * P(has|he) * P(he)

但是,对于比较长的序列,许多被分解的条件概率(某些词的组合)仍然还是过于稀疏

因此,进一步,Use Markov Assumption: Only a finite history matters.

3.2.2 Try an easy one first: The Unigram Model

The simplest model of sentence probabilities.

Generative Processeschoose each word in sentence independently.

即认为每个出现在句子中的词,都是独立的,互不干扰的。 

也就是按照之前的例子,P(he, has, changed, china) = P(china) *P(he) * P(has) * P(changed)

但这假设很离谱呀,因为按照这样计算,就会得到:P(he, has, changed, china) = P(he, changed, has, china)。但它能有效地解决数据稀疏的问题了,因为不再去考虑条件概率了。

Yet (again) … unigram (or so-called Bag-of-word) models are surprisingly useful for some applications, including lexical semantics, information retrieval, text classification …

怎么更进一步呢?放宽假设!

3.2.3 General idea: N-gram Language Model

Markov Assumption : Only N previous words matters.

我们并不假设每个词都是独立的,而是假设每个词仅与它前面的一个词是相关的,而与其他词都是独立的,这种思想也被称为 Bigram Model

对于:<s> <s> he has changed china

P(he, has, changed, china) = P(china|changed) * P(changed|has) * P(has|he) * P(he|<s>)

同理,也有 Trigram Model

P(he, has, changed, china) = P(china|changed, has) * P(changed|he, has) * P(has|he, <s>) * P(he|<s>, <s>)


3.3 Evaluation of Language Model

在理想情况下:

  1. 假设有两个语言模型 A,B;

  2. 选定一个特定的任务,比如拼写纠错;

  3. 把两个模型 A,B 都应用在此任务中;

  4. 最后比较准确率,从而判断 A,B 的表现。

但这样比较麻烦,需要找一个下流任务和制定相应的数据。那么进一步,有人提出了 perplexity 的概念去评估语言模型。

Cross Entropy measures how well model predicts the data.

For data w1, …, wn with large n, well approximated by:

Witch can also be represented as perplexity

即是计算句子中的 每个 token 生成的概率的几何平均值。若句子的 perplexity 值越小,则句子的出现概率越大。

那么如何进行评估呢?

我们可以准备一个测试数据集,在测试数据集中的每句话都是合法的,一个性能好的语言模型应该在测试集上的 perplexity 都是很低的。


3.4 Smoothing

为什么需要使用 Smoothing?

对于一些训练预料不够大的情况,很容易导致一些合法的句子却算得概率为0。我们希望使用某种方式给这种没有出现的句子分配某种合理的近似值。

3.4.1 Add-one Smoothing (Laplace Smoothing)

分子+1,分母+V(词表大小)

但因为 V 通常很大,会导致Add-one Smoothing 容易出现平滑过度的情况。因此,我们使用超参数 k 调整一下。

3.4.2 Add-K Smoothing (Laplace Smoothing)

 那么 k 如何选择呢?

我们将数据集划分为两半,一部分为验证数据集,一部分为验证数据集。通过某个 k 的值,在训练语料上训练模型,然后在验证数据集上算一下 perplexity,使用得到最低 perplexity 的 k 值。

3.4.3 Interpolation

Idea:在计算Trigram概率时同时考虑Unigram, Bigram, Trigram出现的频次。

相当于就是使用低阶 N-Gram 在兜底,不至于为0。 


如有错误,欢迎指正!

おすすめ

転載: blog.csdn.net/weixin_41960890/article/details/120391714
おすすめ