Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

3.5 泛化与过拟合训练集

与许多统计模型一样,n 元模型依赖训练语料库。这意味着,它的概率往往会编码某个特定训练语料库的具体事实;也意味着随着 nn 增大,n 元语法会越来越好地建模训练语料库。

上一节的采样方法可以把这两点都可视化。为直观展示高阶 n 元语法不断增强的能力,图 3.4 给出从莎士比亚作品上训练的一元、二元、三元和 4 元模型中随机生成的句子。

上下文越长,句子就越连贯。一元句子的词语之间没有连贯关系,也没有句末标点。二元句子具有一定的局部逐词连贯性,尤其是在把标点也当作词时。三元句子开始很像莎士比亚,而 4 元句子似乎又像得过了头。It cannot be but so 直接出自《约翰王》。这并不是要贬低莎士比亚,而是因为与一般语料库相比,他的作品总量并不大(N=884,647N=884,647V=29,066V=29,066),所以 n 元概率矩阵极其稀疏。仅可能的二元语法就有 V2=844,000,000V^2=844,000,000 个,可能的 4 元语法则多达 V4=7×1017V^4=7\times10^{17} 个。因此,生成器选定最初的三元语法 It cannot be 后,第四个元素只有六种可能:but、I、that、thus、this 和句点。

1 元-To him swallowed confess hear both. Which. Of save on trail for are ay device and rote life have-Hill he late speaks; or! a more to leg less first you enter
2 元-Why dost stand forth thy canopy, forsooth; he is this palpable hit the King Henry. Live king. Follow.-What means, sir. I confess she? then all sorts, he is trim, captain.
3 元-Fly, and will rid me these news of price. Therefore the sadness of parting, as they say, 'tis done.-This shall forbid it should be branded, if renown made it empty.
4 元-King Henry. What! I will go seek the traitor Gloucester. Exeunt some of the watch. A great banquet serv'd in;-It cannot be but so.

图 3.4 从莎士比亚作品上计算的四个 n 元模型中随机生成的八个句子。所有字符先映射成小写,标点也被视为词。为提高可读性,输出的大小写经过人工修正。

为了了解模型对训练集的依赖,再考察在完全不同的语料库《华尔街日报》(WSJ)上训练的语言模型。莎士比亚作品与 WSJ 都使用英语,因此或许可以预期两个体裁的 n 元语法存在一定重叠。图 3.5 展示在 4000 万词 WSJ 语料上训练的一元、二元和三元模型所生成的句子。

1 元Months the my and issue of year foreign new exchange’s september were recession exchange new endorsed a acquire to six executives
2 元Last December through the way to preserve the Hudson corporation N. B. E. C. Taylor would seem to complete the major central planners one point five percent of U. S. E. has already old M. X. corporation of living on information such as more frequently fishing to keep her
3 元They also point to ninety nine point six billion dollars from two hundred four oh six three percent of the rates of interest stores as Mexico and Brazil on market conditions

图 3.5 从《华尔街日报》4000 万词上计算的三个 n 元模型中随机生成的三个句子;所有字符转换为小写,标点也被视为词。随后为提高可读性,人工修正了输出的大小写。

把这些例子与图 3.4 中仿莎士比亚的句子比较。尽管二者都在建模“类似英语的句子”,生成句子之间没有重叠,甚至短语也几乎不重叠。如果训练集与测试集像莎士比亚作品和 WSJ 一样差异巨大,统计模型作为预测器基本毫无用处。

构建 n 元模型时,应当如何处理这个问题?一个步骤是确保训练语料库的体裁与目标任务相似。要为法律文件翻译构建语言模型,需要法律文件训练语料库;要为问答系统构建语言模型,需要问题语料库。

采用适当方言或语言变体的训练数据同样重要,尤其是在处理社交媒体帖子或口语转写时。例如,一些推文会使用非裔美国人英语(AAE)的特征;AAE 是非裔美国人社群所使用的众多语言变体的统称(King, 2020)。这些特征可能包括 finna 等其他英语变体中没有的词;它是标记即刻将来时的助动词。也可能包括用 den 表示 then 等拼写,如下面这条推文(Blodgett and O’Connor, 2017):

(3.22) Bored af den my phone finna die!!!

而尼日利亚皮钦语等以英语为基础的语言,其词表和 n 元模式又与美国英语明显不同(Jurgens et al., 2017):

(3.23) @username R u a wizard or wat gan sef: in d mornin - u tweet, afternoon - u tweet, nyt gan u dey tweet. beta get ur IT placement wiv twitter

测试集中是否仍可能出现从未见过的词?如果 Jurafsky 从未出现在训练集中,却突然出现在测试集中,会发生什么?答案是:虽然词可能未见,NLP 算法通常并不在词上运行,而在子词词元上运行。使用第 2 章 BPE 等子词词元化方法,任何词都可以建模成已知的较小子词序列;必要时甚至可以用分别对应单个字母的词元序列。因此,尽管本章为方便起见一直使用“词”这一说法,语言模型词表通常是词元集合而不是词集合;通过这种方式,测试集永远不会包含未见词元。