Language Models
目的:Predict the probability of a sentence or sequence of words in a document. 预测某个单词在某个句子中或者某个句子在某个文档中出现的概率
应用场景:
定义
定义: something that specifies the following two quantities, for all words in the vocabulary
1. Direct MLE估计
假设单词a和单词b
P(a|b)= Count(ab)/Count(b) eg:P(jams | traffic)= traffic jams出现次数/ traffric出现次数
扩展到N元: Pr(dogs | I love cats and) = Count(I love cats and dogs) / Count(I love cats and)
所以某句话出现的概率可以表示为:
P(the weather is snowy outside) = P(the) * P(weather | the) * P(is | the weather) * P(snowy | the weather is) * P(outside | the weather is snowy)
2. Markov Assumption马尔可夫假设
马尔科夫假设:一个单词出现的概率依赖于前面单词出现的概率
3. N-gram Sequence Model
Unigram:不考虑前后单词,只考虑自己出现的概率。效果:Not very good at modeling a sequence,因为荒谬的短语或句子可能会很容易出现。
Bi-grams:Next word is dependent on the previous word alone
以此类推
4. Smoothing平滑
如果一个单词aaaaaaaa从来没在语料库里出现过:eg. Pr(frog said that toad likes frog aaaaaaaa),那么这句话的概率就是0,这显然是不对的。
解决方法:Smoothing:assign a low (non-zero) probability to words or n-grams not observed in the text collection (training data)防止零概率的语法影响计算,为零概率的词分配非零的较低概率
1) Laplace平滑 =>所有单词概率下降,0概率上升
------------>
eg.
t表示term, N为整篇文章的单词数,V为|V|,但是+1平滑往往效果不理想,虽然很常用,原因参照:
于是有了Add-k方法
--------------->
eg.
2) Interpolated平滑,插值法
3) Backoff回退法
eg. 如果 P(you | I love)== 0
则 P(you | I love)= P(you | love), 如果 P(you | love)也==0,
则 P(you | I love) = P(you)
4)Good-Turning 打折法
例如: Witten-Bell 打折法, Kneser-ney 平滑法
5)Reference
这篇文章讲得很详细,比教科书还细
5. Evaluation评估
都说trigram model要比bigram model要好,可是怎么界定好?
1)Extrinsic外在评估:measure performance on downstream application
用应用去测评性能,比如用这两个模型分别跑某个train dataset,看准确率
2)Intrinsic内在评估:design a measure inherent to the current task
一个指标是entropy,越小表示这个东西越确定。
对于两个东西(预测和groud truth)而言, 用cross-entropy
cross-entropy >= entropy,因为 模型预测的熵>=真实情况的熵
一个很好的例子帮助理解:这句话用bigram model预测出来的熵是6,比unigram预测出来的11要小,说明确定性更大,更接近真实ground truth
一个较好的指标是 perplexity 困惑度
困惑度越低,信息量越大,越确定。
6. Document Language Model
P代表每个term在这个doc中的频率(概率)
Dirichlet 平滑
Pr(t):单词t的先验概率
u:通常1000到2500
7.Probabilistic similarity
1)Kullback–Leibler (KL) Divergence 相对熵
KL散度:用来衡量两个分布的差异
原概率分布为P(x),近似概率分布为Q(x)
性质:Dkl >= 0 非负。如果Dkl =0,则说明两个概率分布相同
但是应用到两个document的相似度会出现问题:
对于frog来说,它没出现在d2中,所以会变成log2(无穷大),所以需要smoothing
2)Jensen-Shannon (JS) Divergence JS散度
计算d1对d2的KL散度和d2对d1的KL散度,分别配上不同权重
8.为什么用language model
检验how likely is it that two documents come from the same distribution,而并非计算两个docyment的consine similarity