Run this notebook online:\ |Binder| or Colab: |Colab| .. |Binder| image:: https://mybinder.org/badge_logo.svg :target: https://mybinder.org/v2/gh/deepjavalibrary/d2l-java/master?filepath=chapter_recurrent-neural-networks/language-models-and-dataset.ipynb .. |Colab| image:: https://colab.research.google.com/assets/colab-badge.svg :target: https://colab.research.google.com/github/deepjavalibrary/d2l-java/blob/colab/chapter_recurrent-neural-networks/language-models-and-dataset.ipynb .. _sec_language_model: 语言模型和数据集 ================ 在 :numref:`sec_text_preprocessing`\ 中, 我们了解了如何将文本数据映射为词元, 以及将这些词元可以视为一系列离散的观测,例如单词或字符。 假设长度为\ :math:`T`\ 的文本序列中的词元依次为\ :math:`x_1, x_2, \ldots, x_T`\ 。 于是,\ :math:`x_t`\ (\ :math:`1 \leq t \leq T`\ ) 可以被认为是文本序列在时间步\ :math:`t`\ 处的观测或标签。 在给定这样的文本序列时,\ *语言模型*\ (language model)的目标是估计序列的联合概率 .. math:: P(x_1, x_2, \ldots, x_T). 例如,只需要一次抽取一个词元\ :math:`x_t \sim P(x_t \mid x_{t-1}, \ldots, x_1)`\ , 一个理想的语言模型就能够基于模型本身生成自然文本。 与猴子使用打字机完全不同的是,从这样的模型中提取的文本 都将作为自然语言(例如,英语文本)来传递。 只需要基于前面的对话片断中的文本, 就足以生成一个有意义的对话。 显然,我们离设计出这样的系统还很遥远, 因为它需要“理解”文本,而不仅仅是生成语法合理的内容。 尽管如此,语言模型依然是非常有用的。 例如,短语“to recognize speech”和“to wreck a nice beach”读音上听起来非常相似。 这种相似性会导致语音识别中的歧义,但是这很容易通过语言模型来解决, 因为第二句的语义很奇怪。 同样,在文档摘要生成算法中, “狗咬人”比“人咬狗”出现的频率要高得多, 或者“我想吃奶奶”是一个相当匪夷所思的语句, 而“我想吃,奶奶”则要正常得多。 学习语言模型 ------------ 显而易见,我们面对的问题是如何对一个文档, 甚至是一个词元序列进行建模。 假设在单词级别对文本数据进行词元化, 我们可以依靠在 :numref:`sec_sequence`\ 中对序列模型的分析。 让我们从基本概率规则开始: .. math:: P(x_1, x_2, \ldots, x_T) = \prod_{t=1}^T P(x_t \mid x_1, \ldots, x_{t-1}). 例如,包含了四个单词的一个文本序列的概率是: .. math:: P(\text{deep}, \text{learning}, \text{is}, \text{fun}) = P(\text{deep}) P(\text{learning} \mid \text{deep}) P(\text{is} \mid \text{deep}, \text{learning}) P(\text{fun} \mid \text{deep}, \text{learning}, \text{is}). 为了训练语言模型,我们需要计算单词的概率, 以及给定前面几个单词后出现某个单词的条件概率。 这些概率本质上就是语言模型的参数。 这里,我们假设训练数据集是一个大型的文本语料库。 比如,维基百科的所有条目、 `古登堡计划 `__\ , 或者所有发布在网络上的文本。 训练数据集中词的概率可以根据给定词的相对词频来计算。 例如,可以将估计值\ :math:`\hat{P}(\text{deep})` 计算为任何以单词“deep”开头的句子的概率。 一种(稍稍不太精确的)方法是统计单词“deep”在数据集中的出现次数, 然后将其除以整个语料库中的单词总数。 这种方法效果不错,特别是对于频繁出现的单词。 接下来,我们可以尝试估计 .. math:: \hat{P}(\text{learning} \mid \text{deep}) = \frac{n(\text{deep, learning})}{n(\text{deep})}, 其中\ :math:`n(x)`\ 和\ :math:`n(x, x')`\ 分别是单个单词和连续单词对的出现次数。 不幸的是,由于连续单词对“deep learning”的出现频率要低得多, 所以估计这类单词正确的概率要困难得多。 特别是对于一些不常见的单词组合,要想找到足够的出现次数来获得准确的估计可能都不容易。 而对于三个或者更多的单词组合,情况会变得更糟。 许多合理的三个单词组合可能是存在的,但是在数据集中却找不到。 除非我们提供某种解决方案,来将这些单词组合指定为非零计数, 否则将无法在语言模型中使用它们。 如果数据集很小,或者单词非常罕见,那么这类单词出现一次的机会可能都找不到。 一种常见的策略是执行某种形式的\ *拉普拉斯平滑*\ (Laplace smoothing), 具体方法是在所有计数中添加一个小常量。 用\ :math:`n`\ 表示训练集中的单词总数,用\ :math:`m`\ 表示唯一单词的数量。 此解决方案有助于处理单元素问题,例如通过: .. math:: \begin{aligned} \hat{P}(x) & = \frac{n(x) + \epsilon_1/m}{n + \epsilon_1}, \\ \hat{P}(x' \mid x) & = \frac{n(x, x') + \epsilon_2 \hat{P}(x')}{n(x) + \epsilon_2}, \\ \hat{P}(x'' \mid x,x') & = \frac{n(x, x',x'') + \epsilon_3 \hat{P}(x'')}{n(x, x') + \epsilon_3}. \end{aligned} 其中,\ :math:`\epsilon_1,\epsilon_2`\ 和\ :math:`\epsilon_3`\ 是超参数。 以\ :math:`\epsilon_1`\ 为例:当\ :math:`\epsilon_1 = 0`\ 时,不应用平滑; 当\ :math:`\epsilon_1`\ 接近正无穷大时,\ :math:`\hat{P}(x)`\ 接近均匀概率分布\ :math:`1/m`\ 。 上面的公式是 :cite:`Wood.Gasthaus.Archambeau.ea.2011` 的一个相当原始的变形。 然而,这样的模型很容易变得无效,原因如下: 首先,我们需要存储所有的计数; 其次,这完全忽略了单词的意思。 例如,“猫”(cat)和“猫科动物”(feline)可能出现在相关的上下文中, 但是想根据上下文调整这类模型其实是相当困难的。 最后,长单词序列大部分是没出现过的, 因此一个模型如果只是简单地统计先前“看到”的单词序列频率, 那么模型面对这种问题肯定是表现不佳的。 马尔可夫模型与\ :math:`n`\ 元语法 --------------------------------- 在讨论包含深度学习的解决方案之前,我们需要了解更多的概念和术语。 回想一下我们在 :numref:`sec_sequence`\ 中对马尔可夫模型的讨论, 并且将其应用于语言建模。 如果\ :math:`P(x_{t+1} \mid x_t, \ldots, x_1) = P(x_{t+1} \mid x_t)`\ , 则序列上的分布满足一阶马尔可夫性质。 阶数越高,对应的依赖关系就越长。 这种性质推导出了许多可以应用于序列建模的近似公式: .. math:: \begin{aligned} P(x_1, x_2, x_3, x_4) &= P(x_1) P(x_2) P(x_3) P(x_4),\\ P(x_1, x_2, x_3, x_4) &= P(x_1) P(x_2 \mid x_1) P(x_3 \mid x_2) P(x_4 \mid x_3),\\ P(x_1, x_2, x_3, x_4) &= P(x_1) P(x_2 \mid x_1) P(x_3 \mid x_1, x_2) P(x_4 \mid x_2, x_3). \end{aligned} 通常,涉及一个、两个和三个变量的概率公式分别被称为 “一元语法”(unigram)、“二元语法”(bigram)和“三元语法”(trigram)模型。 下面,我们将学习如何去设计更好的模型。 自然语言统计 ------------ 我们看看在真实数据上如果进行自然语言统计。 根据 :numref:`sec_text_preprocessing`\ 中介绍的时光机器数据集构建词表, 并打印前\ :math:`10`\ 个最常用的(频率最高的)单词。 .. code:: java %load ../utils/djl-imports %load ../utils/plot-utils %load ../utils/PlotUtils.java %load ../utils/Accumulator.java %load ../utils/Animator.java %load ../utils/Functions.java %load ../utils/StopWatch.java %load ../utils/Training.java %load ../utils/timemachine/Vocab.java %load ../utils/timemachine/RNNModelScratch.java %load ../utils/timemachine/TimeMachine.java .. code:: java NDManager manager = NDManager.newBaseManager(); .. code:: java String[][] tokens = TimeMachine.tokenize(TimeMachine.readTimeMachine(), "word"); // 由于每一行文字不一定是一个句子或段落,因此我们把所有文本行拼接到一起 List corpus = new ArrayList<>(); for (int i = 0; i < tokens.length; i++) { for (int j = 0; j < tokens[i].length; j++) { if (tokens[i][j] != "") { corpus.add(tokens[i][j]); } } } Vocab vocab = new Vocab(new String[][] {corpus.toArray(new String[0])}, -1, new String[0]); for (int i = 0; i < 10; i++) { Map.Entry token = vocab.tokenFreqs.get(i); System.out.println(token.getKey() + ": " + token.getValue()); } .. parsed-literal:: :class: output the: 2261 i: 1267 and: 1245 of: 1155 a: 816 to: 695 was: 552 in: 541 that: 443 my: 440 正如我们所看到的,最常用的词看起来很并没有意义, 这些词通常被称为\ *停用词*\ (stop words),因此可以被过滤掉。 尽管如此,它们本身仍然是有意义的,我们仍然会在模型中使用它们。 此外,还有个明显的问题是词频衰减的速度相当地快。 例如,最常用单词的词频对比,第\ :math:`10`\ 个还不到第\ :math:`1`\ 个的\ :math:`1/5`\ 。 为了更好地理解,我们可以画出的词频图: .. code:: java int n = vocab.tokenFreqs.size(); double[] freqs = new double[n]; double[] x = new double[n]; for (int i = 0; i < n; i++) { freqs[i] = (double) vocab.tokenFreqs.get(i).getValue(); x[i] = (double) i; } PlotUtils.plotLogScale(new double[][] {x}, new double[][] {freqs}, new String[] {""}, "token: x", "frequency: n(x)"); .. raw:: html
通过此图我们可以发现:词频以一种明确的方式迅速衰减。 将前几个单词作为例外消除后,剩余的所有单词大致遵循双对数坐标图上的一条直线。 这意味着单词的频率满足\ *齐普夫定律*\ (Zipf's law), 即第\ :math:`i`\ 个最常用单词的频率\ :math:`n_i`\ 为: .. math:: n_i \propto \frac{1}{i^\alpha}, :label: eq_zipf_law 等价于 .. math:: \log n_i = -\alpha \log i + c, 其中\ :math:`\alpha`\ 是刻画分布的指数,\ :math:`c`\ 是常数。 这告诉我们想要通过计数统计和平滑来建模单词是不可行的, 因为这样建模的结果会大大高估尾部单词的频率,也就是所谓的不常用单词。 那么其他的词元组合,比如二元语法、三元语法等等,又会如何呢? 我们来看看二元语法的频率是否与一元语法的频率表现出相同的行为方式。 .. code:: java String[] bigramTokens = new String[corpus.size()-1]; for (int i = 0; i < bigramTokens.length; i++) { bigramTokens[i] = corpus.get(i) + " " + corpus.get(i+1); } Vocab bigramVocab = new Vocab(new String[][] {bigramTokens}, -1, new String[0]); for (int i = 0; i < 10; i++) { Map.Entry token = bigramVocab.tokenFreqs.get(i); System.out.println(token.getKey() + ": " + token.getValue()); } .. parsed-literal:: :class: output 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 这里值得注意:在十个最频繁的词对中,有九个是由两个停用词组成的, 只有一个与“the time”有关。 我们再进一步看看三元语法的频率是否表现出相同的行为方式。 .. code:: java String[] trigramTokens = new String[corpus.size()-2]; for (int i = 0; i < trigramTokens.length; i++) { trigramTokens[i] = corpus.get(i) + " " + corpus.get(i+1) + " " + corpus.get(i+2); } Vocab trigramVocab = new Vocab(new String[][] {trigramTokens}, -1, new String[0]); for (int i = 0; i < 10; i++) { Map.Entry token = trigramVocab.tokenFreqs.get(i); System.out.println(token.getKey() + ": " + token.getValue()); } .. parsed-literal:: :class: output the time traveller: 59 the time machine: 30 : 26 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 最后,我们直观地对比三种模型中的词元频率:一元语法、二元语法和三元语法。 .. code:: java n = bigramVocab.tokenFreqs.size(); double[] bigramFreqs = new double[n]; double[] bigramX = new double[n]; for (int i = 0; i < n; i++) { bigramFreqs[i] = (double) bigramVocab.tokenFreqs.get(i).getValue(); bigramX[i] = (double) i; } n = trigramVocab.tokenFreqs.size(); double[] trigramFreqs = new double[n]; double[] trigramX = new double[n]; for (int i = 0; i < n; i++) { trigramFreqs[i] = (double) trigramVocab.tokenFreqs.get(i).getValue(); trigramX[i] = (double) i; } PlotUtils.plotLogScale(new double[][] {x, bigramX, trigramX}, new double[][] {freqs, bigramFreqs, trigramFreqs}, new String[] {"unigram", "bigram", "trigram"}, "token: x", "frequency: n(x)"); .. raw:: html
这张图非常令人振奋!原因有很多: 首先,除了一元语法词,单词序列似乎也遵循齐普夫定律, 尽管公式 :eq:`eq_zipf_law`\ 中的指数\ :math:`\alpha`\ 更小 (指数的大小受序列长度的影响)。 其次,词表中\ :math:`n`\ 元组的数量并没有那么大,这说明语言中存在相当多的结构, 这些结构给了我们应用模型的希望。 第三,很多\ :math:`n`\ 元组很少出现,这使得拉普拉斯平滑非常不适合语言建模。 作为代替,我们将使用基于深度学习的模型。 .. _fig_timemachine_5gram: 读取长序列数据 -------------- 由于序列数据本质上是连续的,因此我们在处理数据时需要解决这个问题。 在 :numref:`sec_sequence`\ 中我们以一种相当特别的方式做到了这一点: 当序列变得太长而不能被模型一次性全部处理时, 我们可能希望拆分这样的序列方便模型读取。 在介绍该模型之前,我们看一下总体策略。 假设我们将使用神经网络来训练语言模型, 模型中的网络一次处理具有预定义长度 (例如\ :math:`n`\ 个时间步)的一个小批量序列。 现在的问题是如何随机生成一个小批量数据的特征和标签以供读取。 首先,由于文本序列可以是任意长的, 例如整本《时光机器》(\ *The Time Machine*\ ), 于是任意长的序列可以被我们划分为具有相同时间步数的子序列。 当训练我们的神经网络时,这样的小批量子序列将被输入到模型中。 假设网络一次只处理具有\ :math:`n`\ 个时间步的子序列。 :numref:`fig_timemachine_5gram`\ 画出了 从原始文本序列获得子序列的所有不同的方式, 其中\ :math:`n=5`\ ,并且每个时间步的词元对应于一个字符。 请注意,因为我们可以选择任意偏移量来指示初始位置,所以我们有相当大的自由度。 |分割文本时,不同的偏移量会导致不同的子序列| 因此,我们应该从 :numref:`fig_timemachine_5gram`\ 中选择哪一个呢? 事实上,他们都一样的好。 然而,如果我们只选择一个偏移量, 那么用于训练网络的、所有可能的子序列的覆盖范围将是有限的。 因此,我们可以从随机偏移量开始划分序列, 以同时获得\ *覆盖性*\ (coverage)和\ *随机性*\ (randomness)。 下面,我们将描述如何实现\ *随机采样*\ (random sampling)和 *顺序分区*\ (sequential partitioning)策略。 随机采样 ~~~~~~~~ 在随机采样中,每个样本都是在原始的长序列上任意捕获的子序列。 在迭代过程中,来自两个相邻的、随机的、小批量中的子序列不一定在原始序列上相邻。 对于语言建模,目标是基于到目前为止我们看到的词元来预测下一个词元, 因此标签是移位了一个词元的原始序列。 下面的代码每次可以从数据中随机生成一个小批量。 在这里,参数\ ``batch_size``\ 指定了每个小批量中子序列样本的数目, 参数\ ``num_steps``\ 是每个子序列中预定义的时间步数。 .. |分割文本时,不同的偏移量会导致不同的子序列| image:: https://d2l.ai/_images/timemachine-5gram.svg .. code:: java /** * 使用随机抽样生成一小批 子序列。 */ public ArrayList seqDataIterRandom(List corpus, int batchSize, int numSteps, NDManager manager) { // 从一个随机偏移量(包括'numSteps-1')开始到分区a // 序列 corpus = corpus.subList(new Random().nextInt(numSteps - 1), corpus.size()); // 减去1,因为我们需要考虑标签 int numSubseqs = (corpus.size() - 1) / numSteps; // `numSteps` 长度子序列的起始指数 List initialIndices = new ArrayList<>(); for (int i = 0; i < numSubseqs * numSteps; i += numSteps) { initialIndices.add(i); } // 在随机抽样中,来自两个相邻随机序列的子序列 // 迭代过程中的小批量不一定在 // 原始序列 Collections.shuffle(initialIndices); int numBatches = numSubseqs / batchSize; ArrayList pairs = new ArrayList(); for (int i = 0; i < batchSize * numBatches; i += batchSize) { // 这里,`initialIndices` 包含的是 // 子序列 List initialIndicesPerBatch = initialIndices.subList(i, i + batchSize); NDArray xNDArray = manager.create(new Shape(initialIndices.size(), numSteps), DataType.INT32); NDArray yNDArray = manager.create(new Shape(initialIndices.size(), numSteps), DataType.INT32); for (int j = 0; j < initialIndices.size(); j++) { ArrayList X = data(initialIndices.get(j), corpus, numSteps); xNDArray.set(new NDIndex(j), manager.create(X.stream().mapToInt(Integer::intValue).toArray())); ArrayList Y = data(initialIndices.get(j)+1, corpus, numSteps); yNDArray.set(new NDIndex(j), manager.create(Y.stream().mapToInt(Integer::intValue).toArray())); } NDList pair = new NDList(); pair.add(xNDArray); pair.add(yNDArray); pairs.add(pair); } return pairs; } ArrayList data(int pos, List corpus, int numSteps) { // 返回从`pos` 开始的长度为`numSteps` 的序列 return new ArrayList(corpus.subList(pos, pos + numSteps)); } 下面我们生成一个从\ :math:`0`\ 到\ :math:`34`\ 的序列。 假设批量大小为\ :math:`2`\ ,时间步数为\ :math:`5`\ ,这意味着可以生成 :math:`\lfloor (35 - 1) / 5 \rfloor= 6`\ 个“特征-标签”子序列对。 如果设置小批量大小为\ :math:`2`\ ,我们只能得到\ :math:`3`\ 个小批量。 .. code:: java List mySeq = new ArrayList<>(); for (int i = 0; i < 35; i++) { mySeq.add(i); } for (NDList pair : seqDataIterRandom(mySeq, 2, 5, manager)) { System.out.println("X:\n" + pair.get(0).toDebugString(50, 50, 50, 50, true)); System.out.println("Y:\n" + pair.get(1).toDebugString(50, 50, 50, 50, true)); } .. parsed-literal:: :class: output X: ND: (6, 5) gpu(0) int32 [[ 8, 9, 10, 11, 12], [13, 14, 15, 16, 17], [ 3, 4, 5, 6, 7], [28, 29, 30, 31, 32], [18, 19, 20, 21, 22], [23, 24, 25, 26, 27], ] Y: ND: (6, 5) gpu(0) int32 [[ 9, 10, 11, 12, 13], [14, 15, 16, 17, 18], [ 4, 5, 6, 7, 8], [29, 30, 31, 32, 33], [19, 20, 21, 22, 23], [24, 25, 26, 27, 28], ] X: ND: (6, 5) gpu(0) int32 [[ 8, 9, 10, 11, 12], [13, 14, 15, 16, 17], [ 3, 4, 5, 6, 7], [28, 29, 30, 31, 32], [18, 19, 20, 21, 22], [23, 24, 25, 26, 27], ] Y: ND: (6, 5) gpu(0) int32 [[ 9, 10, 11, 12, 13], [14, 15, 16, 17, 18], [ 4, 5, 6, 7, 8], [29, 30, 31, 32, 33], [19, 20, 21, 22, 23], [24, 25, 26, 27, 28], ] X: ND: (6, 5) gpu(0) int32 [[ 8, 9, 10, 11, 12], [13, 14, 15, 16, 17], [ 3, 4, 5, 6, 7], [28, 29, 30, 31, 32], [18, 19, 20, 21, 22], [23, 24, 25, 26, 27], ] Y: ND: (6, 5) gpu(0) int32 [[ 9, 10, 11, 12, 13], [14, 15, 16, 17, 18], [ 4, 5, 6, 7, 8], [29, 30, 31, 32, 33], [19, 20, 21, 22, 23], [24, 25, 26, 27, 28], ] 顺序分区 ~~~~~~~~ 除了对原始序列进行随机抽样外,我们还可以确保 来自两个相邻小批次的子序列 迭代期间 在原始序列上相邻。 当在小批量上迭代时,此策略保持分割子序列的顺序,因此称为顺序分区。 .. code:: java /** * 使用顺序分区生成一小批 子序列。 */ public ArrayList seqDataIterSequential(List corpus, int batchSize, int numSteps, NDManager manager) { // 从随机偏移量开始划分序列 int offset = new Random().nextInt(numSteps); int numTokens = ((corpus.size() - offset - 1) / batchSize) * batchSize; NDArray Xs = manager.create( corpus.subList(offset, offset + numTokens).stream().mapToInt(Integer::intValue).toArray()); NDArray Ys = manager.create( corpus.subList(offset + 1, offset + 1 + numTokens).stream().mapToInt(Integer::intValue).toArray()); Xs = Xs.reshape(new Shape(batchSize, -1)); Ys = Ys.reshape(new Shape(batchSize, -1)); int numBatches = (int) Xs.getShape().get(1) / numSteps; ArrayList pairs = new ArrayList(); for (int i = 0; i < numSteps * numBatches; i += numSteps) { NDArray X = Xs.get(new NDIndex(":, {}:{}", i, i + numSteps)); NDArray Y = Ys.get(new NDIndex(":, {}:{}", i, i + numSteps)); NDList pair = new NDList(); pair.add(X); pair.add(Y); pairs.add(pair); } return pairs; } 使用相同的设置, 让我们为通过顺序分区读取的每个小批量子序列打印特征\ ``X`` 和标签 ``Y`` 注意 来自两个相邻小批次的子序列 迭代期间 在原始序列上确实是相邻的。 .. code:: java for (NDList pair : seqDataIterSequential(mySeq, 2, 5, manager)) { System.out.println("X:\n" + pair.get(0).toDebugString(10, 10, 10, 10, true)); System.out.println("Y:\n" + pair.get(1).toDebugString(10, 10, 10, 10, true)); } .. parsed-literal:: :class: output X: ND: (2, 5) gpu(0) int32 [[ 2, 3, 4, 5, 6], [18, 19, 20, 21, 22], ] Y: ND: (2, 5) gpu(0) int32 [[ 3, 4, 5, 6, 7], [19, 20, 21, 22, 23], ] X: ND: (2, 5) gpu(0) int32 [[ 7, 8, 9, 10, 11], [23, 24, 25, 26, 27], ] Y: ND: (2, 5) gpu(0) int32 [[ 8, 9, 10, 11, 12], [24, 25, 26, 27, 28], ] X: ND: (2, 5) gpu(0) int32 [[12, 13, 14, 15, 16], [28, 29, 30, 31, 32], ] Y: ND: (2, 5) gpu(0) int32 [[13, 14, 15, 16, 17], [29, 30, 31, 32, 33], ] 现在,我们将上述两个采样函数封装到一个类中,以便以后可以将其用作数据迭代器。 .. code:: java public class SeqDataLoader implements Iterable { public ArrayList dataIter; public List corpus; public Vocab vocab; public int batchSize; public int numSteps; /** * 加载序列数据的迭代器 */ @SuppressWarnings("unchecked") public SeqDataLoader(int batchSize, int numSteps, boolean useRandomIter, int maxTokens) throws IOException, Exception { Pair, Vocab> corpusVocabPair = TimeMachine.loadCorpusTimeMachine(maxTokens); this.corpus = corpusVocabPair.getKey(); this.vocab = corpusVocabPair.getValue(); this.batchSize = batchSize; this.numSteps = numSteps; if (useRandomIter) { dataIter = seqDataIterRandom(corpus, batchSize, numSteps, manager); }else { dataIter = seqDataIterSequential(corpus, batchSize, numSteps, manager); } } @Override public Iterator iterator() { return dataIter.iterator(); } } 最后,我们定义了一个函数 ``loadDataTimeMachine`` ,它返回数据迭代器和词汇表。 .. code:: java /** * 返回时间机器数据集的迭代器和词汇表 */ public Pair, Vocab> loadDataTimeMachine(int batchSize, int numSteps, boolean useRandomIter, int maxTokens) throws IOException, Exception { SeqDataLoader seqData = new SeqDataLoader(batchSize, numSteps, useRandomIter, maxTokens); return new Pair(seqData.dataIter, seqData.vocab); // ArrayList, Vocab } 总结 ---- - 语言模型是自然语言处理的关键。 - :math:`n`\ 元语法通过截断相关性,为处理长序列提供了一种实用的模型。 - 长序列存在一个问题:它们很少出现或者从不出现。 - 齐普夫定律支配着单词的分布,这个分布不仅适用于一元语法,还适用于其他\ :math:`n`\ 元语法。 - 通过拉普拉斯平滑法可以有效地处理结构丰富而频率不足的低频词词组。 - 读取长序列的主要方式是随机采样和顺序分区。在迭代过程中,后者可以保证来自两个相邻的小批量中的子序列在原始序列上也是相邻的。 练习 ---- 1. 假设训练数据集中有\ :math:`100,000`\ 单词。四克需要存储多少字频率和多字相邻频率? 2. 你将如何模拟对话? 3. 估计单图、双图和三元图的齐普夫定律指数。 4. 对于读取长序列数据,您还能想到哪些其他方法? 5. 考虑我们用来读取长序列的随机偏移量。 1. 为什么有一个随机偏移是个好主意? 2. 它真的会导致文档序列上的完全均匀分布吗? 3. 你要怎么做才能使事情变得更加统一? 6. 如果我们希望序列示例是一个完整的句子,那么在小批量采样中会引入什么样的问题?我们如何解决这个问题?