Transcript 3.统计语言模型
统计语言模型 戴新宇 2015-10 2016/8/5 1 Language Model 计算一句话的概率 2016/8/5 2 Language Model 机器翻译 小朋友们在公园里玩。 The kids play in the park. √ The kids park play. On park play the kids. 语言模型 arg max P(E _ words | C _ words) = English P(C _ words | E _ words) ´ P(E _ words) arg max English P(C _ words) arg max P(C _ words | E _ words) ´ P(E _ words) English 翻译模型 2016/8/5 3 Language Model 语音识别 e.g. 听歌曲,识别歌词 语言模 型 argmax P(words | acoustics) = wordsequence P(acoustics | words) ´ P(words) argmax wordsequence P(acoustics) argmax P(acoustics | words) ´ P(words) wordsequence 声学模型 2016/8/5 4 Statistical Language Model 定义: 语言模型是用来刻画一个句子(词串序列)存在可能性的概率模型 P(W)=P(w1w2…wn) 如何计算P(W),不同的语言模型有不同的算法 例子: P(“我在打电话”) ≈ 0.001 P(“我在打苹果”) ≈ 0.000001 P(“苹果在我打”) ≈ 0.000000001 P(电话|我,在,打) ≈0.05 5 最简单的统计语言模型 - N-Gram P(W)=P(w1w2…wn)=P(w1)*P(w2|w1)* P(w3|w1w2)…P(wn|w1w2…wn-1) History-based Model: predict following things from past things Shannon Game: Impractical to condition on everything before P(??? | Turn to page 134 and look at the picture of the) ? 6 马尔可夫假设(Markov Assumption) 有限视野假设(Limited Horizon) 当前词出现的概率只和它前面的k个词相关 P(W ) P( w1w2 ...wn ) P( wi | wi k ...wi 1 ) i 1..n 上式我们称之为k阶马尔可夫链 e.g. 1阶马尔可夫链 P(W ) P( w1w2 ...wn ) P(w1 ) P(wi | wi 1 ) 2阶马尔可夫链 i 2..n P(W ) P( w1w2 ...wn ) P( w1 ) P( w2 | w1 ) P( wi | wi 2 wi 1 ) i 3..n 7 N-Gram N-1阶马尔可夫链我们称之为N元语言模型 (N-Gram Language Model) 2元模型 P(W ) P( w1w2 ...wn ) P(w1 ) P(wi | wi 1 ) 3元模型 i 2..n Each word is predicted according to a conditional distribution based on a limited context P(W ) P( w1w2 ...wn ) P( w1 ) P( w2 | w1 ) P( wi | wi 1wi 2 ) i 3..n 8 N-Gram 简单的线性图模型 词是随机变量,箭头表示词之间的依存关系 Simple model Amazing success. 9 N-Gram模型参数 参数数量:n越大,参数越多, 假设词汇量为20000 2元模型(1阶Markov)参数数量:200002 3元模型(2阶Markov)参数数量:200003 … N元模型(N-1阶Markov)参数数量:20000n 10 N-Gram参数估计 计算每一组的P(w|h) (2元为例) P(我想吃意大利空心粉) = P(我|<s>)*P(想|我)*P(吃| 想)*P(意大利|吃)*P(空心粉|意大利)= 0.25*0.32*0.26*0.002*0.6 = 0.000016 参数来源(训练数据):大量电子文本(汉语需分词) 训练数据:用于建立模型的给定语料 参数估计: P( wi | wi 1 ) P( wi 1wi ) Count ( wi 1wi ) Count ( wi 1wi ) P( wi 1 ) Count (wi1w) Count (wi1 ) w 相对频率(最大似然)估计 11 最大似然估计 - Maximum Likelihood Estimation A simple but important idea: Choose the alternative that maximize the probability of the observed outcome. (对训练数据的最佳估计) 假设:样本空间越大,最大似然估计值越接近于真实分布 统计学上称之为似然函数,之所以称为最大似然估计,是因为它选择的参 数值对于训练数据给出了最高的概率。Please refer to http://en.wikipedia.org/wiki/Maximum_likelihood 问题:相对频率 ≈ 最大似然估计 ? 12 13 14 Zipf Law f*r = k Zipf Law:如果以词频排序,词频和排位的乘积是一 个常数。 15 Zipf’s law for the Brown corpus 16 Zipf law for Chinese Corpus 17 Zipf Law(续) Zipf法则隐含的意义:大部分的词都稀有 语言中频繁出现的事件是有限的,不可能搜集到足够的 数据来得到稀有事件的完整概率分布。 词(一元)如此,对于二元、三元模型更加严重 Zero probability,零概率还会向下传播 一个2元或者3元文法的零概率,会导致整个句子的零概 率 However, 统计方法的一个特点是: Nothing is impossible. So, 数据稀疏问题永远存在 18 数据稀疏(零概率) 数据稀疏问题:没有足够的训练数据,对于未观测到的 数据,出现零概率现象 Balh等人的工作 用150万词的训练语料训练trigram模型 测试语料(同样来源)中,23%的trigram没有在训练库中出现过 对这23%未出现的trigram,利用MLE估计出的参数为0 解决方案 构造等价类 参数平滑 19 参数平滑 - Smoothing 平滑是指给没观察到的N元组合赋予一个概率值, 以保证词序列总能通过语言模型得到一个概率值。 思想:稍微减少已观察到的事件概率的大小,同时 把少量概率分配到没有看到过的事件上,折扣法, 使整个事件空间的概率分布曲线更加平滑。改进模 型的整体效果。 高概率调低点,小概率或者零概率调高点。 “劫富济贫” 约束:Anyway, ensure ∑X∈ΩP(X)=1 平滑算法的评估 - 通过语言模型评估 20 Smoothing 21 Smoothing Add counts, 简单,效果不好 Laplace smoothing/Dirichlet Prior 简单线性插值平滑 P( wi | wi 2 wi 1 ) 1P( wi | wi 2 wi 1 ) 2 P( wi | wi 1 ) 3 P( wi ) 1 2 3 1 Katz Smoothing, Kneser-Ney Smoothing …… 22 Add-One Smoothing 23 统计语言模型数据集分类 训练集Training Data 测试集Test Data 用来建立模型,获得模型参数 从训练集以外独立采样 反映系统面对真实世界的处理能力 交叉确认集Cross-Validation Data 从训练集和测试集以外独立采样 主要用来帮助做设计决策和参数设定 (hyperparameters) 24 Cross-Validation Set 又称为 held-out data、development data 帮助选择参数,优化模型的泛化能力 Test Data Test Data Hold-Out Data Training Data Training Data 25 模型评价 两个语言模型S1和S2(或者是经过两种不同的平滑方法得到的两 个模型S1和S2),哪个更好? 直接放入需要语言模型的任务中,如SMT,SR,看哪个效果更好? 困惑度(Perplexity):对测试集存在的概率 Perplexity is the probability of the test data, normalized by the number of words: 26 模型评价 熵(entropy): log likelihood per word in test data 熵与困惑度的关系 2016/8/5 27 Data >> Methods Having more data is better... ... but so is using a better estimator Another issue: N > 3 has huge costs in speech recognizers 2016/8/5 28 构建语言模型 Some available toolkits Big data CMU language model toolkit SRI Language model toolkit Lemur language model toolkit …… English Gigawords corpus: 2G tokens, 0.3G trigrams, 1.2G 5grams Google N-grams: 13M unigrams, 0.3G bigrams, ~1G 3-, 4-, 5grams Need to access entries very often, ideally in memory What do you do when language models get too big? Distributing LMs across machines Random hashing (e.g. Bloom filters) Suffix Array …… 2016/8/5 29 N-Gram model 在很多方面取得了成功(Chelba 1998, Charniak 2001) Speech Recognition OCR Context-sensitive spelling correction 从语言具有的特性看,显得过于简单和幼稚 Lexical No long distance dependencies No structure or syntactic dependency 30 其它的语言模型 (不同的计算P(W)的方法) 2016/8/5 31 其它的语言模型(2) (不同的计算P(W)的方法) Deep Learning 2016/8/5 Arisoy, Ebru, et al. "Deep neural network language models." Proceedings of the NAACLHLT 2012 Workshop: Will We Ever Really Replace the N-gram Model? On the Future of Language Modeling for HLT. Association for Computational Linguistics, 2012. 32 总结 Language Model Markov Assumption Estimation Smoothing Evaluation 2016/8/5 33