.. _sec_seq2seq: 序列到序列学习(seq2seq) ========================= 正如我们在 :numref:`sec_machine_translation`\ 中看到的, 机器翻译中的输入序列和输出序列都是长度可变的。 为了解决这类问题,我们在 :numref:`sec_encoder-decoder`\ 中 设计了一个通用的”编码器-解码器“架构。 本节,我们将使用两个循环神经网络的编码器和解码器, 并将其应用于\ *序列到序列*\ (sequence to sequence,seq2seq)类的学习任务 :cite:`Sutskever.Vinyals.Le.2014,Cho.Van-Merrienboer.Gulcehre.ea.2014`\ 。 遵循编码器-解码器架构的设计原则, 循环神经网络编码器使用长度可变的序列作为输入, 将其转换为固定形状的隐状态。 换言之,输入序列的信息被\ *编码*\ 到循环神经网络编码器的隐状态中。 为了连续生成输出序列的词元, 独立的循环神经网络解码器是基于输入序列的编码信息 和输出序列已经看见的或者生成的词元来预测下一个词元。 :numref:`fig_seq2seq`\ 演示了 如何在机器翻译中使用两个循环神经网络进行序列到序列学习。 .. _fig_seq2seq: .. figure:: ../img/seq2seq.svg 使用循环神经网络编码器和循环神经网络解码器的序列到序列学习 在 :numref:`fig_seq2seq`\ 中, 特定的“”表示序列结束词元。 一旦输出序列生成此词元,模型就会停止预测。 在循环神经网络解码器的初始化时间步,有两个特定的设计决定: 首先,特定的“”表示序列开始词元,它是解码器的输入序列的第一个词元。 其次,使用循环神经网络编码器最终的隐状态来初始化解码器的隐状态。 例如,在 :cite:`Sutskever.Vinyals.Le.2014`\ 的设计中, 正是基于这种设计将输入序列的编码信息送入到解码器中来生成输出序列的。 在其他一些设计中 :cite:`Cho.Van-Merrienboer.Gulcehre.ea.2014`\ , 如 :numref:`fig_seq2seq`\ 所示, 编码器最终的隐状态在每一个时间步都作为解码器的输入序列的一部分。 类似于 :numref:`sec_language_model`\ 中语言模型的训练, 可以允许标签成为原始的输出序列, 从源序列词元“”“Ils”“regardent”“.” 到新序列词元 “Ils”“regardent”“.”“”来移动预测的位置。 下面,我们动手构建 :numref:`fig_seq2seq`\ 的设计, 并将基于 :numref:`sec_machine_translation`\ 中 介绍的“英-法”数据集来训练这个机器翻译模型。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import collections import math from mxnet import autograd, gluon, init, np, npx from mxnet.gluon import nn, rnn from d2l import mxnet as d2l npx.set_np() .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import collections import math import torch from torch import nn from d2l import torch as d2l .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import collections import math import tensorflow as tf from d2l import tensorflow as d2l .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python import collections import warnings from d2l import paddle as d2l warnings.filterwarnings("ignore") import math import paddle from paddle import nn .. raw:: html
.. raw:: html
编码器 ------ 从技术上讲,编码器将长度可变的输入序列转换成 形状固定的上下文变量\ :math:`\mathbf{c}`\ , 并且将输入序列的信息在该上下文变量中进行编码。 如 :numref:`fig_seq2seq`\ 所示,可以使用循环神经网络来设计编码器。 考虑由一个序列组成的样本(批量大小是\ :math:`1`\ )。 假设输入序列是\ :math:`x_1, \ldots, x_T`\ , 其中\ :math:`x_t`\ 是输入文本序列中的第\ :math:`t`\ 个词元。 在时间步\ :math:`t`\ ,循环神经网络将词元\ :math:`x_t`\ 的输入特征向量 :math:`\mathbf{x}_t`\ 和\ :math:`\mathbf{h} _{t-1}`\ (即上一时间步的隐状态) 转换为\ :math:`\mathbf{h}_t`\ (即当前步的隐状态)。 使用一个函数\ :math:`f`\ 来描述循环神经网络的循环层所做的变换: .. math:: \mathbf{h}_t = f(\mathbf{x}_t, \mathbf{h}_{t-1}). 总之,编码器通过选定的函数\ :math:`q`\ , 将所有时间步的隐状态转换为上下文变量: .. math:: \mathbf{c} = q(\mathbf{h}_1, \ldots, \mathbf{h}_T). 比如,当选择\ :math:`q(\mathbf{h}_1, \ldots, \mathbf{h}_T) = \mathbf{h}_T`\ 时 (就像 :numref:`fig_seq2seq`\ 中一样), 上下文变量仅仅是输入序列在最后时间步的隐状态\ :math:`\mathbf{h}_T`\ 。 到目前为止,我们使用的是一个单向循环神经网络来设计编码器, 其中隐状态只依赖于输入子序列, 这个子序列是由输入序列的开始位置到隐状态所在的时间步的位置 (包括隐状态所在的时间步)组成。 我们也可以使用双向循环神经网络构造编码器, 其中隐状态依赖于两个输入子序列, 两个子序列是由隐状态所在的时间步的位置之前的序列和之后的序列 (包括隐状态所在的时间步), 因此隐状态对整个序列的信息都进行了编码。 现在,让我们实现循环神经网络编码器。 注意,我们使用了\ *嵌入层*\ (embedding layer) 来获得输入序列中每个词元的特征向量。 嵌入层的权重是一个矩阵, 其行数等于输入词表的大小(\ ``vocab_size``\ ), 其列数等于特征向量的维度(\ ``embed_size``\ )。 对于任意输入词元的索引\ :math:`i`\ , 嵌入层获取权重矩阵的第\ :math:`i`\ 行(从\ :math:`0`\ 开始)以返回其特征向量。 另外,本文选择了一个多层门控循环单元来实现编码器。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class Seq2SeqEncoder(d2l.Encoder): """用于序列到序列学习的循环神经网络编码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super(Seq2SeqEncoder, self).__init__(**kwargs) # 嵌入层 self.embedding = nn.Embedding(vocab_size, embed_size) self.rnn = rnn.GRU(num_hiddens, num_layers, dropout=dropout) def forward(self, X, *args): # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X) # 在循环神经网络模型中,第一个轴对应于时间步 X = X.swapaxes(0, 1) state = self.rnn.begin_state(batch_size=X.shape[1], ctx=X.ctx) output, state = self.rnn(X, state) # output的形状:(num_steps,batch_size,num_hiddens) # state的形状:(num_layers,batch_size,num_hiddens) return output, state .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class Seq2SeqEncoder(d2l.Encoder): """用于序列到序列学习的循环神经网络编码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super(Seq2SeqEncoder, self).__init__(**kwargs) # 嵌入层 self.embedding = nn.Embedding(vocab_size, embed_size) self.rnn = nn.GRU(embed_size, num_hiddens, num_layers, dropout=dropout) def forward(self, X, *args): # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X) # 在循环神经网络模型中,第一个轴对应于时间步 X = X.permute(1, 0, 2) # 如果未提及状态,则默认为0 output, state = self.rnn(X) # output的形状:(num_steps,batch_size,num_hiddens) # state的形状:(num_layers,batch_size,num_hiddens) return output, state .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class Seq2SeqEncoder(d2l.Encoder): """用于序列到序列学习的循环神经网络编码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super().__init__(*kwargs) # 嵌入层 self.embedding = tf.keras.layers.Embedding(vocab_size, embed_size) self.rnn = tf.keras.layers.RNN(tf.keras.layers.StackedRNNCells( [tf.keras.layers.GRUCell(num_hiddens, dropout=dropout) for _ in range(num_layers)]), return_sequences=True, return_state=True) def call(self, X, *args, **kwargs): # 输入'X'的形状:(batch_size,num_steps) # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X) output = self.rnn(X, **kwargs) state = output[1:] return output[0], state .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class Seq2SeqEncoder(d2l.Encoder): """用于序列到序列学习的循环神经网络编码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super(Seq2SeqEncoder, self).__init__(**kwargs) weight_ih_attr = paddle.ParamAttr(initializer=nn.initializer.XavierUniform()) weight_hh_attr = paddle.ParamAttr(initializer=nn.initializer.XavierUniform()) # 嵌入层 self.embedding = nn.Embedding(vocab_size, embed_size) self.rnn = nn.GRU(embed_size, num_hiddens, num_layers, dropout=dropout, time_major=True, weight_ih_attr=weight_ih_attr, weight_hh_attr=weight_hh_attr) def forward(self, X, *args): # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X) # 在循环神经网络模型中,第一个轴对应于时间步 X = X.transpose([1, 0, 2]) # 如果未提及状态,则默认为0 output, state = self.rnn(X) # PaddlePaddle的GRU层output的形状:(batch_size,time_steps,num_directions * num_hiddens), # 需设定time_major=True,指定input的第一个维度为time_steps # state[0]的形状:(num_layers,batch_size,num_hiddens) return output, state .. raw:: html
.. raw:: html
循环层返回变量的说明可以参考 :numref:`sec_rnn-concise`\ 。 下面,我们实例化上述编码器的实现: 我们使用一个两层门控循环单元编码器,其隐藏单元数为\ :math:`16`\ 。 给定一小批量的输入序列\ ``X``\ (批量大小为\ :math:`4`\ ,时间步为\ :math:`7`\ )。 在完成所有时间步后, 最后一层的隐状态的输出是一个张量(\ ``output``\ 由编码器的循环层返回), 其形状为(时间步数,批量大小,隐藏单元数)。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python encoder = Seq2SeqEncoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) encoder.initialize() X = np.zeros((4, 7)) output, state = encoder(X) output.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [07:41:36] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for CPU .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (7, 4, 16) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python encoder = Seq2SeqEncoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) encoder.eval() X = torch.zeros((4, 7), dtype=torch.long) output, state = encoder(X) output.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output torch.Size([7, 4, 16]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python encoder = Seq2SeqEncoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) X = tf.zeros((4, 7)) output, state = encoder(X, training=False) output.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output TensorShape([4, 7, 16]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python encoder = Seq2SeqEncoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) encoder.eval() X = paddle.zeros((4, 7), dtype=paddle.int64) output, state = encoder(X) output.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output W0818 09:37:57.233700 78318 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.8, Runtime API Version: 11.8 W0818 09:37:57.265334 78318 gpu_resources.cc:91] device: 0, cuDNN Version: 8.7. .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [7, 4, 16] .. raw:: html
.. raw:: html
由于这里使用的是门控循环单元, 所以在最后一个时间步的多层隐状态的形状是 (隐藏层的数量,批量大小,隐藏单元的数量)。 如果使用长短期记忆网络,\ ``state``\ 中还将包含记忆单元信息。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python len(state), state[0].shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (1, (2, 4, 16)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python state.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output torch.Size([2, 4, 16]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python len(state), [element.shape for element in state] .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (2, [TensorShape([4, 16]), TensorShape([4, 16])]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python state.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [2, 4, 16] .. raw:: html
.. raw:: html
.. _sec_seq2seq_decoder: 解码器 ------ 正如上文提到的,编码器输出的上下文变量\ :math:`\mathbf{c}` 对整个输入序列\ :math:`x_1, \ldots, x_T`\ 进行编码。 来自训练数据集的输出序列\ :math:`y_1, y_2, \ldots, y_{T'}`\ , 对于每个时间步\ :math:`t'`\ (与输入序列或编码器的时间步\ :math:`t`\ 不同), 解码器输出\ :math:`y_{t'}`\ 的概率取决于先前的输出子序列 :math:`y_1, \ldots, y_{t'-1}`\ 和上下文变量\ :math:`\mathbf{c}`\ , 即\ :math:`P(y_{t'} \mid y_1, \ldots, y_{t'-1}, \mathbf{c})`\ 。 为了在序列上模型化这种条件概率, 我们可以使用另一个循环神经网络作为解码器。 在输出序列上的任意时间步\ :math:`t^\prime`\ , 循环神经网络将来自上一时间步的输出\ :math:`y_{t^\prime-1}` 和上下文变量\ :math:`\mathbf{c}`\ 作为其输入, 然后在当前时间步将它们和上一隐状态 :math:`\mathbf{s}_{t^\prime-1}`\ 转换为 隐状态\ :math:`\mathbf{s}_{t^\prime}`\ 。 因此,可以使用函数\ :math:`g`\ 来表示解码器的隐藏层的变换: .. math:: \mathbf{s}_{t^\prime} = g(y_{t^\prime-1}, \mathbf{c}, \mathbf{s}_{t^\prime-1}). :label: eq_seq2seq_s_t 在获得解码器的隐状态之后, 我们可以使用输出层和softmax操作 来计算在时间步\ :math:`t^\prime`\ 时输出\ :math:`y_{t^\prime}`\ 的条件概率分布 :math:`P(y_{t^\prime} \mid y_1, \ldots, y_{t^\prime-1}, \mathbf{c})`\ 。 根据 :numref:`fig_seq2seq`\ ,当实现解码器时, 我们直接使用编码器最后一个时间步的隐状态来初始化解码器的隐状态。 这就要求使用循环神经网络实现的编码器和解码器具有相同数量的层和隐藏单元。 为了进一步包含经过编码的输入序列的信息, 上下文变量在所有的时间步与解码器的输入进行拼接(concatenate)。 为了预测输出词元的概率分布, 在循环神经网络解码器的最后一层使用全连接层来变换隐状态。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python class Seq2SeqDecoder(d2l.Decoder): """用于序列到序列学习的循环神经网络解码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super(Seq2SeqDecoder, self).__init__(**kwargs) self.embedding = nn.Embedding(vocab_size, embed_size) self.rnn = rnn.GRU(num_hiddens, num_layers, dropout=dropout) self.dense = nn.Dense(vocab_size, flatten=False) def init_state(self, enc_outputs, *args): return enc_outputs[1] def forward(self, X, state): # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X).swapaxes(0, 1) # context的形状:(batch_size,num_hiddens) context = state[0][-1] # 广播context,使其具有与X相同的num_steps context = np.broadcast_to(context, ( X.shape[0], context.shape[0], context.shape[1])) X_and_context = np.concatenate((X, context), 2) output, state = self.rnn(X_and_context, state) output = self.dense(output).swapaxes(0, 1) # output的形状:(batch_size,num_steps,vocab_size) # state的形状:(num_layers,batch_size,num_hiddens) return output, state .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python class Seq2SeqDecoder(d2l.Decoder): """用于序列到序列学习的循环神经网络解码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super(Seq2SeqDecoder, self).__init__(**kwargs) self.embedding = nn.Embedding(vocab_size, embed_size) self.rnn = nn.GRU(embed_size + num_hiddens, num_hiddens, num_layers, dropout=dropout) self.dense = nn.Linear(num_hiddens, vocab_size) def init_state(self, enc_outputs, *args): return enc_outputs[1] def forward(self, X, state): # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X).permute(1, 0, 2) # 广播context,使其具有与X相同的num_steps context = state[-1].repeat(X.shape[0], 1, 1) X_and_context = torch.cat((X, context), 2) output, state = self.rnn(X_and_context, state) output = self.dense(output).permute(1, 0, 2) # output的形状:(batch_size,num_steps,vocab_size) # state的形状:(num_layers,batch_size,num_hiddens) return output, state .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python class Seq2SeqDecoder(d2l.Decoder): """用于序列到序列学习的循环神经网络解码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super().__init__(**kwargs) self.embedding = tf.keras.layers.Embedding(vocab_size, embed_size) self.rnn = tf.keras.layers.RNN(tf.keras.layers.StackedRNNCells( [tf.keras.layers.GRUCell(num_hiddens, dropout=dropout) for _ in range(num_layers)]), return_sequences=True, return_state=True) self.dense = tf.keras.layers.Dense(vocab_size) def init_state(self, enc_outputs, *args): return enc_outputs[1] def call(self, X, state, **kwargs): # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X) # 广播context,使其具有与X相同的num_steps context = tf.repeat(tf.expand_dims(state[-1], axis=1), repeats=X.shape[1], axis=1) X_and_context = tf.concat((X, context), axis=2) rnn_output = self.rnn(X_and_context, state, **kwargs) output = self.dense(rnn_output[0]) # output的形状:(batch_size,num_steps,vocab_size) # state是一个包含num_layers个元素的列表,每个元素的形状:(batch_size,num_hiddens) return output, rnn_output[1:] .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python class Seq2SeqDecoder(d2l.Decoder): """用于序列到序列学习的循环神经网络解码器""" def __init__(self, vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs): super(Seq2SeqDecoder, self).__init__(**kwargs) self.embedding = nn.Embedding(vocab_size, embed_size) weight_attr = paddle.ParamAttr(initializer=nn.initializer.XavierUniform()) weight_ih_attr = paddle.ParamAttr(initializer=nn.initializer.XavierUniform()) weight_hh_attr = paddle.ParamAttr(initializer=nn.initializer.XavierUniform()) self.rnn = nn.GRU(embed_size + num_hiddens, num_hiddens, num_layers, dropout=dropout, time_major=True, weight_ih_attr=weight_ih_attr,weight_hh_attr=weight_hh_attr) self.dense = nn.Linear(num_hiddens, vocab_size,weight_attr=weight_attr) def init_state(self, enc_outputs, *args): return enc_outputs[1] def forward(self, X, state): # 输出'X'的形状:(batch_size,num_steps,embed_size) X = self.embedding(X).transpose([1, 0, 2]) # 广播context,使其具有与X相同的num_steps context = state[-1].tile([X.shape[0], 1, 1]) X_and_context = paddle.concat((X, context), 2) output, state = self.rnn(X_and_context, state) output = self.dense(output).transpose([1, 0, 2]) # output的形状:(batch_size,num_steps,vocab_size) # state[0]的形状:(num_layers,batch_size,num_hiddens) return output, state .. raw:: html
.. raw:: html
下面,我们用与前面提到的编码器中相同的超参数来实例化解码器。 如我们所见,解码器的输出形状变为(批量大小,时间步数,词表大小), 其中张量的最后一个维度存储预测的词元分布。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python decoder = Seq2SeqDecoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) decoder.initialize() state = decoder.init_state(encoder(X)) output, state = decoder(X, state) output.shape, len(state), state[0].shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output ((4, 7, 10), 1, (2, 4, 16)) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python decoder = Seq2SeqDecoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) decoder.eval() state = decoder.init_state(encoder(X)) output, state = decoder(X, state) output.shape, state.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (torch.Size([4, 7, 10]), torch.Size([2, 4, 16])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python decoder = Seq2SeqDecoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) state = decoder.init_state(encoder(X)) output, state = decoder(X, state, training=False) output.shape, len(state), state[0].shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output (TensorShape([4, 7, 10]), 2, TensorShape([4, 16])) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python decoder = Seq2SeqDecoder(vocab_size=10, embed_size=8, num_hiddens=16, num_layers=2) decoder.eval() state = decoder.init_state(encoder(X)) output, state = decoder(X, state) output.shape, state.shape .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output ([4, 7, 10], [2, 4, 16]) .. raw:: html
.. raw:: html
总之,上述循环神经网络“编码器-解码器”模型中的各层如 :numref:`fig_seq2seq_details`\ 所示。 .. _fig_seq2seq_details: .. figure:: ../img/seq2seq-details.svg 循环神经网络编码器-解码器模型中的层 损失函数 -------- 在每个时间步,解码器预测了输出词元的概率分布。 类似于语言模型,可以使用softmax来获得分布, 并通过计算交叉熵损失函数来进行优化。 回想一下 :numref:`sec_machine_translation`\ 中, 特定的填充词元被添加到序列的末尾, 因此不同长度的序列可以以相同形状的小批量加载。 但是,我们应该将填充词元的预测排除在损失函数的计算之外。 为此,我们可以使用下面的\ ``sequence_mask``\ 函数 通过零值化屏蔽不相关的项, 以便后面任何不相关预测的计算都是与零的乘积,结果都等于零。 例如,如果两个序列的有效长度(不包括填充词元)分别为\ :math:`1`\ 和\ :math:`2`\ , 则第一个序列的第一项和第二个序列的前两项之后的剩余项将被清除为零。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = np.array([[1, 2, 3], [4, 5, 6]]) npx.sequence_mask(X, np.array([1, 2]), True, axis=1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[1., 0., 0.], [4., 5., 0.]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def sequence_mask(X, valid_len, value=0): """在序列中屏蔽不相关的项""" maxlen = X.size(1) mask = torch.arange((maxlen), dtype=torch.float32, device=X.device)[None, :] < valid_len[:, None] X[~mask] = value return X X = torch.tensor([[1, 2, 3], [4, 5, 6]]) sequence_mask(X, torch.tensor([1, 2])) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[1, 0, 0], [4, 5, 0]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def sequence_mask(X, valid_len, value=0): """在序列中屏蔽不相关的项""" maxlen = X.shape[1] mask = tf.range(start=0, limit=maxlen, dtype=tf.float32)[ None, :] < tf.cast(valid_len[:, None], dtype=tf.float32) if len(X.shape) == 3: return tf.where(tf.expand_dims(mask, axis=-1), X, value) else: return tf.where(mask, X, value) X = tf.constant([[1, 2, 3], [4, 5, 6]]) sequence_mask(X, tf.constant([1, 2])) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def sequence_mask(X, valid_len, value=0): """在序列中屏蔽不相关的项""" maxlen = X.shape[1] mask = paddle.arange((maxlen), dtype=paddle.float32)[None, :] < valid_len[:, None] Xtype = X.dtype X = X.astype(paddle.float32) X[~mask] = float(value) return X.astype(Xtype) X = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) sequence_mask(X, paddle.to_tensor([1, 2])) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Tensor(shape=[2, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[1, 0, 0], [4, 5, 0]]) .. raw:: html
.. raw:: html
我们还可以使用此函数屏蔽最后几个轴上的所有项。如果愿意,也可以使用指定的非零值来替换这些项。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = np.ones((2, 3, 4)) npx.sequence_mask(X, np.array([1, 2]), True, value=-1, axis=1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([[[ 1., 1., 1., 1.], [-1., -1., -1., -1.], [-1., -1., -1., -1.]], [[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [-1., -1., -1., -1.]]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = torch.ones(2, 3, 4) sequence_mask(X, torch.tensor([1, 2]), value=-1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([[[ 1., 1., 1., 1.], [-1., -1., -1., -1.], [-1., -1., -1., -1.]], [[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [-1., -1., -1., -1.]]]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = tf.ones((2,3,4)) sequence_mask(X, tf.constant([1, 2]), value=-1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python X = paddle.ones([2, 3, 4]) sequence_mask(X, paddle.to_tensor([1, 2]), value=-1) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Tensor(shape=[2, 3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[[ 1., 1., 1., 1.], [-1., -1., -1., -1.], [-1., -1., -1., -1.]], [[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [-1., -1., -1., -1.]]]) .. raw:: html
.. raw:: html
现在,我们可以通过扩展softmax交叉熵损失函数来遮蔽不相关的预测。 最初,所有预测词元的掩码都设置为1。 一旦给定了有效长度,与填充词元对应的掩码将被设置为0。 最后,将所有词元的损失乘以掩码,以过滤掉损失中填充词元产生的不相关预测。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class MaskedSoftmaxCELoss(gluon.loss.SoftmaxCELoss): """带遮蔽的softmax交叉熵损失函数""" # pred的形状:(batch_size,num_steps,vocab_size) # label的形状:(batch_size,num_steps) # valid_len的形状:(batch_size,) def forward(self, pred, label, valid_len): # weights的形状:(batch_size,num_steps,1) weights = np.expand_dims(np.ones_like(label), axis=-1) weights = npx.sequence_mask(weights, valid_len, True, axis=1) return super(MaskedSoftmaxCELoss, self).forward(pred, label, weights) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class MaskedSoftmaxCELoss(nn.CrossEntropyLoss): """带遮蔽的softmax交叉熵损失函数""" # pred的形状:(batch_size,num_steps,vocab_size) # label的形状:(batch_size,num_steps) # valid_len的形状:(batch_size,) def forward(self, pred, label, valid_len): weights = torch.ones_like(label) weights = sequence_mask(weights, valid_len) self.reduction='none' unweighted_loss = super(MaskedSoftmaxCELoss, self).forward( pred.permute(0, 2, 1), label) weighted_loss = (unweighted_loss * weights).mean(dim=1) return weighted_loss .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class MaskedSoftmaxCELoss(tf.keras.losses.Loss): """带遮蔽的softmax交叉熵损失函数""" def __init__(self, valid_len): super().__init__(reduction='none') self.valid_len = valid_len # pred的形状:(batch_size,num_steps,vocab_size) # label的形状:(batch_size,num_steps) # valid_len的形状:(batch_size,) def call(self, label, pred): weights = tf.ones_like(label, dtype=tf.float32) weights = sequence_mask(weights, self.valid_len) label_one_hot = tf.one_hot(label, depth=pred.shape[-1]) unweighted_loss = tf.keras.losses.CategoricalCrossentropy( from_logits=True, reduction='none')(label_one_hot, pred) weighted_loss = tf.reduce_mean((unweighted_loss*weights), axis=1) return weighted_loss .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save class MaskedSoftmaxCELoss(nn.CrossEntropyLoss): """带遮蔽的softmax交叉熵损失函数""" # pred的形状:(batch_size,num_steps,vocab_size) # label的形状:(batch_size,num_steps) # valid_len的形状:(batch_size,) def forward(self, pred, label, valid_len): weights = paddle.ones_like(label) weights = sequence_mask(weights, valid_len) self.reduction='none' unweighted_loss = super(MaskedSoftmaxCELoss, self).forward( pred, label) weighted_loss = (unweighted_loss * weights).mean(axis=1) return weighted_loss .. raw:: html
.. raw:: html
我们可以创建三个相同的序列来进行代码健全性检查, 然后分别指定这些序列的有效长度为\ :math:`4`\ 、\ :math:`2`\ 和\ :math:`0`\ 。 结果就是,第一个序列的损失应为第二个序列的两倍,而第三个序列的损失应为零。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python loss = MaskedSoftmaxCELoss() loss(np.ones((3, 4, 10)), np.ones((3, 4)), np.array([4, 2, 0])) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([2.3025851, 1.1512926, 0. ]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python loss = MaskedSoftmaxCELoss() loss(torch.ones(3, 4, 10), torch.ones((3, 4), dtype=torch.long), torch.tensor([4, 2, 0])) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output tensor([2.3026, 1.1513, 0.0000]) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python loss = MaskedSoftmaxCELoss(tf.constant([4, 2, 0])) loss(tf.ones((3,4), dtype = tf.int32), tf.ones((3, 4, 10))).numpy() .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([2.3025851, 1.1512926, 0. ], dtype=float32) .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python loss = MaskedSoftmaxCELoss() loss(paddle.ones([3, 4, 10]), paddle.ones((3, 4), dtype=paddle.int64), paddle.to_tensor([4, 2, 0])) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, [2.30258512, 1.15129256, 0. ]) .. raw:: html
.. raw:: html
.. _sec_seq2seq_training: 训练 ---- 在下面的循环训练过程中,如 :numref:`fig_seq2seq`\ 所示, 特定的序列开始词元(“”)和 原始的输出序列(不包括序列结束词元“”) 拼接在一起作为解码器的输入。 这被称为\ *强制教学*\ (teacher forcing), 因为原始的输出序列(词元的标签)被送入解码器。 或者,将来自上一个时间步的\ *预测*\ 得到的词元作为解码器的当前输入。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device): """训练序列到序列模型""" net.initialize(init.Xavier(), force_reinit=True, ctx=device) trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': lr}) loss = MaskedSoftmaxCELoss() animator = d2l.Animator(xlabel='epoch', ylabel='loss', xlim=[10, num_epochs]) for epoch in range(num_epochs): timer = d2l.Timer() metric = d2l.Accumulator(2) # 训练损失求和,词元数量 for batch in data_iter: X, X_valid_len, Y, Y_valid_len = [ x.as_in_ctx(device) for x in batch] bos = np.array([tgt_vocab['']] * Y.shape[0], ctx=device).reshape(-1, 1) dec_input = np.concatenate([bos, Y[:, :-1]], 1) # 强制教学 with autograd.record(): Y_hat, _ = net(X, dec_input, X_valid_len) l = loss(Y_hat, Y, Y_valid_len) l.backward() d2l.grad_clipping(net, 1) num_tokens = Y_valid_len.sum() trainer.step(num_tokens) metric.add(l.sum(), num_tokens) if (epoch + 1) % 10 == 0: animator.add(epoch + 1, (metric[0] / metric[1],)) print(f'loss {metric[0] / metric[1]:.3f}, {metric[1] / timer.stop():.1f} ' f'tokens/sec on {str(device)}') .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device): """训练序列到序列模型""" def xavier_init_weights(m): if type(m) == nn.Linear: nn.init.xavier_uniform_(m.weight) if type(m) == nn.GRU: for param in m._flat_weights_names: if "weight" in param: nn.init.xavier_uniform_(m._parameters[param]) net.apply(xavier_init_weights) net.to(device) optimizer = torch.optim.Adam(net.parameters(), lr=lr) loss = MaskedSoftmaxCELoss() net.train() animator = d2l.Animator(xlabel='epoch', ylabel='loss', xlim=[10, num_epochs]) for epoch in range(num_epochs): timer = d2l.Timer() metric = d2l.Accumulator(2) # 训练损失总和,词元数量 for batch in data_iter: optimizer.zero_grad() X, X_valid_len, Y, Y_valid_len = [x.to(device) for x in batch] bos = torch.tensor([tgt_vocab['']] * Y.shape[0], device=device).reshape(-1, 1) dec_input = torch.cat([bos, Y[:, :-1]], 1) # 强制教学 Y_hat, _ = net(X, dec_input, X_valid_len) l = loss(Y_hat, Y, Y_valid_len) l.sum().backward() # 损失函数的标量进行“反向传播” d2l.grad_clipping(net, 1) num_tokens = Y_valid_len.sum() optimizer.step() with torch.no_grad(): metric.add(l.sum(), num_tokens) if (epoch + 1) % 10 == 0: animator.add(epoch + 1, (metric[0] / metric[1],)) print(f'loss {metric[0] / metric[1]:.3f}, {metric[1] / timer.stop():.1f} ' f'tokens/sec on {str(device)}') .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device): """训练序列到序列模型""" optimizer = tf.keras.optimizers.Adam(learning_rate=lr) animator = d2l.Animator(xlabel="epoch", ylabel="loss", xlim=[10, num_epochs]) for epoch in range(num_epochs): timer = d2l.Timer() metric = d2l.Accumulator(2) # 训练损失总和,词元数量 for batch in data_iter: X, X_valid_len, Y, Y_valid_len = [x for x in batch] bos = tf.reshape(tf.constant([tgt_vocab['']] * Y.shape[0]), shape=(-1, 1)) dec_input = tf.concat([bos, Y[:, :-1]], 1) # 强制教学 with tf.GradientTape() as tape: Y_hat, _ = net(X, dec_input, X_valid_len, training=True) l = MaskedSoftmaxCELoss(Y_valid_len)(Y, Y_hat) gradients = tape.gradient(l, net.trainable_variables) gradients = d2l.grad_clipping(gradients, 1) optimizer.apply_gradients(zip(gradients, net.trainable_variables)) num_tokens = tf.reduce_sum(Y_valid_len).numpy() metric.add(tf.reduce_sum(l), num_tokens) if (epoch + 1) % 10 == 0: animator.add(epoch + 1, (metric[0] / metric[1],)) print(f'loss {metric[0] / metric[1]:.3f}, {metric[1] / timer.stop():.1f} ' f'tokens/sec on {str(device)}') .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device): """训练序列到序列模型""" optimizer = paddle.optimizer.Adam(learning_rate=lr, parameters=net.parameters()) loss = MaskedSoftmaxCELoss() net.train() animator = d2l.Animator(xlabel='epoch', ylabel='loss', xlim=[10, num_epochs]) for epoch in range(num_epochs): timer = d2l.Timer() metric = d2l.Accumulator(2) # 训练损失总和,词元数量 for batch in data_iter: optimizer.clear_grad() X, X_valid_len, Y, Y_valid_len = [paddle.to_tensor(x, place=device) for x in batch] bos = paddle.to_tensor([tgt_vocab['']] * Y.shape[0]).reshape([-1, 1]) dec_input = paddle.concat([bos, Y[:, :-1]], 1) # 强制教学 Y_hat, _ = net(X, dec_input, X_valid_len.squeeze()) l = loss(Y_hat, Y, Y_valid_len.squeeze()) l.backward() # 损失函数的标量进行“反向传播” d2l.grad_clipping(net, 1) num_tokens = Y_valid_len.sum() optimizer.step() with paddle.no_grad(): metric.add(l.sum(), num_tokens) if (epoch + 1) % 10 == 0: animator.add(epoch + 1, (metric[0] / metric[1],)) print(f'loss {metric[0] / metric[1]:.3f}, {metric[1] / timer.stop():.1f} ' f'tokens/sec on {str(device)}') .. raw:: html
.. raw:: html
现在,在机器翻译数据集上,我们可以 创建和训练一个循环神经网络“编码器-解码器”模型用于序列到序列的学习。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python embed_size, num_hiddens, num_layers, dropout = 32, 32, 2, 0.1 batch_size, num_steps = 64, 10 lr, num_epochs, device = 0.005, 300, d2l.try_gpu() train_iter, src_vocab, tgt_vocab = d2l.load_data_nmt(batch_size, num_steps) encoder = Seq2SeqEncoder(len(src_vocab), embed_size, num_hiddens, num_layers, dropout) decoder = Seq2SeqDecoder(len(tgt_vocab), embed_size, num_hiddens, num_layers, dropout) net = d2l.EncoderDecoder(encoder, decoder) train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss 0.024, 5031.7 tokens/sec on gpu(0) .. figure:: output_seq2seq_13725e_168_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python embed_size, num_hiddens, num_layers, dropout = 32, 32, 2, 0.1 batch_size, num_steps = 64, 10 lr, num_epochs, device = 0.005, 300, d2l.try_gpu() train_iter, src_vocab, tgt_vocab = d2l.load_data_nmt(batch_size, num_steps) encoder = Seq2SeqEncoder(len(src_vocab), embed_size, num_hiddens, num_layers, dropout) decoder = Seq2SeqDecoder(len(tgt_vocab), embed_size, num_hiddens, num_layers, dropout) net = d2l.EncoderDecoder(encoder, decoder) train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss 0.019, 12745.1 tokens/sec on cuda:0 .. figure:: output_seq2seq_13725e_171_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python embed_size, num_hiddens, num_layers, dropout = 32, 32, 2, 0.1 batch_size, num_steps = 64, 10 lr, num_epochs, device = 0.005, 300, d2l.try_gpu() train_iter, src_vocab, tgt_vocab = d2l.load_data_nmt(batch_size, num_steps) encoder = Seq2SeqEncoder(len(src_vocab), embed_size, num_hiddens, num_layers, dropout) decoder = Seq2SeqDecoder(len(tgt_vocab), embed_size, num_hiddens, num_layers, dropout) net = d2l.EncoderDecoder(encoder, decoder) train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss 0.029, 1026.6 tokens/sec on .. figure:: output_seq2seq_13725e_174_1.svg .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python embed_size, num_hiddens, num_layers, dropout = 32, 32, 2, 0.1 batch_size, num_steps = 64, 10 lr, num_epochs, device = 0.005, 300, d2l.try_gpu() train_iter, src_vocab, tgt_vocab = d2l.load_data_nmt(batch_size, num_steps) encoder = Seq2SeqEncoder(len(src_vocab), embed_size, num_hiddens, num_layers, dropout) decoder = Seq2SeqDecoder(len(tgt_vocab), embed_size, num_hiddens, num_layers, dropout) net = d2l.EncoderDecoder(encoder, decoder) train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output loss 0.020, 6844.7 tokens/sec on Place(gpu:0) .. figure:: output_seq2seq_13725e_177_1.svg .. raw:: html
.. raw:: html
预测 ---- 为了采用一个接着一个词元的方式预测输出序列, 每个解码器当前时间步的输入都将来自于前一时间步的预测词元。 与训练类似,序列开始词元(“”) 在初始时间步被输入到解码器中。 该预测过程如 :numref:`fig_seq2seq_predict`\ 所示, 当输出序列的预测遇到序列结束词元(“”)时,预测就结束了。 .. _fig_seq2seq_predict: .. figure:: ../img/seq2seq-predict.svg 使用循环神经网络编码器-解码器逐词元地预测输出序列。 我们将在 :numref:`sec_beam-search`\ 中介绍不同的序列生成策略。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, device, save_attention_weights=False): """序列到序列模型的预测""" src_tokens = src_vocab[src_sentence.lower().split(' ')] + [ src_vocab['']] enc_valid_len = np.array([len(src_tokens)], ctx=device) src_tokens = d2l.truncate_pad(src_tokens, num_steps, src_vocab['']) # 添加批量轴 enc_X = np.expand_dims(np.array(src_tokens, ctx=device), axis=0) enc_outputs = net.encoder(enc_X, enc_valid_len) dec_state = net.decoder.init_state(enc_outputs, enc_valid_len) # 添加批量轴 dec_X = np.expand_dims(np.array([tgt_vocab['']], ctx=device), axis=0) output_seq, attention_weight_seq = [], [] for _ in range(num_steps): Y, dec_state = net.decoder(dec_X, dec_state) # 我们使用具有预测最高可能性的词元,作为解码器在下一时间步的输入 dec_X = Y.argmax(axis=2) pred = dec_X.squeeze(axis=0).astype('int32').item() # 保存注意力权重(稍后讨论) if save_attention_weights: attention_weight_seq.append(net.decoder.attention_weights) # 一旦序列结束词元被预测,输出序列的生成就完成了 if pred == tgt_vocab['']: break output_seq.append(pred) return ' '.join(tgt_vocab.to_tokens(output_seq)), attention_weight_seq .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, device, save_attention_weights=False): """序列到序列模型的预测""" # 在预测时将net设置为评估模式 net.eval() src_tokens = src_vocab[src_sentence.lower().split(' ')] + [ src_vocab['']] enc_valid_len = torch.tensor([len(src_tokens)], device=device) src_tokens = d2l.truncate_pad(src_tokens, num_steps, src_vocab['']) # 添加批量轴 enc_X = torch.unsqueeze( torch.tensor(src_tokens, dtype=torch.long, device=device), dim=0) enc_outputs = net.encoder(enc_X, enc_valid_len) dec_state = net.decoder.init_state(enc_outputs, enc_valid_len) # 添加批量轴 dec_X = torch.unsqueeze(torch.tensor( [tgt_vocab['']], dtype=torch.long, device=device), dim=0) output_seq, attention_weight_seq = [], [] for _ in range(num_steps): Y, dec_state = net.decoder(dec_X, dec_state) # 我们使用具有预测最高可能性的词元,作为解码器在下一时间步的输入 dec_X = Y.argmax(dim=2) pred = dec_X.squeeze(dim=0).type(torch.int32).item() # 保存注意力权重(稍后讨论) if save_attention_weights: attention_weight_seq.append(net.decoder.attention_weights) # 一旦序列结束词元被预测,输出序列的生成就完成了 if pred == tgt_vocab['']: break output_seq.append(pred) return ' '.join(tgt_vocab.to_tokens(output_seq)), attention_weight_seq .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, save_attention_weights=False): """序列到序列模型的预测""" src_tokens = src_vocab[src_sentence.lower().split(' ')] + [ src_vocab['']] enc_valid_len = tf.constant([len(src_tokens)]) src_tokens = d2l.truncate_pad(src_tokens, num_steps, src_vocab['']) # 添加批量轴 enc_X = tf.expand_dims(src_tokens, axis=0) enc_outputs = net.encoder(enc_X, enc_valid_len, training=False) dec_state = net.decoder.init_state(enc_outputs, enc_valid_len) # 添加批量轴 dec_X = tf.expand_dims(tf.constant([tgt_vocab['']]), axis=0) output_seq, attention_weight_seq = [], [] for _ in range(num_steps): Y, dec_state = net.decoder(dec_X, dec_state, training=False) # 我们使用具有预测最高可能性的词元,作为解码器在下一时间步的输入 dec_X = tf.argmax(Y, axis=2) pred = tf.squeeze(dec_X, axis=0) # 保存注意力权重 if save_attention_weights: attention_weight_seq.append(net.decoder.attention_weights) # 一旦序列结束词元被预测,输出序列的生成就完成了 if pred == tgt_vocab['']: break output_seq.append(pred.numpy()) return ' '.join(tgt_vocab.to_tokens(tf.reshape(output_seq, shape = -1).numpy().tolist())), attention_weight_seq .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python #@save def predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, device, save_attention_weights=False): """序列到序列模型的预测""" # 在预测时将net设置为评估模式 net.eval() src_tokens = src_vocab[src_sentence.lower().split(' ')] + [ src_vocab['']] enc_valid_len = paddle.to_tensor([len(src_tokens)], place=device) src_tokens = d2l.truncate_pad(src_tokens, num_steps, src_vocab['']) # 添加批量轴 enc_X = paddle.unsqueeze( paddle.to_tensor(src_tokens, dtype=paddle.int64, place=device), axis=0) enc_outputs = net.encoder(enc_X, enc_valid_len) dec_state = net.decoder.init_state(enc_outputs, enc_valid_len) # 添加批量轴 dec_X = paddle.unsqueeze(paddle.to_tensor( [tgt_vocab['']], dtype=paddle.int64, place=device), axis=0) output_seq, attention_weight_seq = [], [] for _ in range(num_steps): Y, dec_state = net.decoder(dec_X, dec_state) # 我们使用具有预测最高可能性的词元,作为解码器在下一时间步的输入 dec_X = Y.argmax(axis=2) pred = dec_X.squeeze(axis=0).astype(paddle.int32).item() # 保存注意力权重(稍后讨论) if save_attention_weights: attention_weight_seq.append(net.decoder.attention_weights) # 一旦序列结束词元被预测,输出序列的生成就完成了 if pred == tgt_vocab['']: break output_seq.append(pred) return ' '.join(tgt_vocab.to_tokens(output_seq)), attention_weight_seq .. raw:: html
.. raw:: html
预测序列的评估 -------------- 我们可以通过与真实的标签序列进行比较来评估预测序列。 虽然 :cite:`Papineni.Roukos.Ward.ea.2002` 提出的BLEU(bilingual evaluation understudy) 最先是用于评估机器翻译的结果, 但现在它已经被广泛用于测量许多应用的输出序列的质量。 原则上说,对于预测序列中的任意\ :math:`n`\ 元语法(n-grams), BLEU的评估都是这个\ :math:`n`\ 元语法是否出现在标签序列中。 我们将BLEU定义为: .. math:: \exp\left(\min\left(0, 1 - \frac{\mathrm{len}_{\text{label}}}{\mathrm{len}_{\text{pred}}}\right)\right) \prod_{n=1}^k p_n^{1/2^n}, :label: eq_bleu 其中\ :math:`\mathrm{len}_{\text{label}}`\ 表示标签序列中的词元数和 :math:`\mathrm{len}_{\text{pred}}`\ 表示预测序列中的词元数, :math:`k`\ 是用于匹配的最长的\ :math:`n`\ 元语法。 另外,用\ :math:`p_n`\ 表示\ :math:`n`\ 元语法的精确度,它是两个数量的比值: 第一个是预测序列与标签序列中匹配的\ :math:`n`\ 元语法的数量, 第二个是预测序列中\ :math:`n`\ 元语法的数量的比率。 具体地说,给定标签序列\ :math:`A`\ 、\ :math:`B`\ 、\ :math:`C`\ 、\ :math:`D`\ 、\ :math:`E`\ 、\ :math:`F` 和预测序列\ :math:`A`\ 、\ :math:`B`\ 、\ :math:`B`\ 、\ :math:`C`\ 、\ :math:`D`\ , 我们有\ :math:`p_1 = 4/5`\ 、\ :math:`p_2 = 3/4`\ 、\ :math:`p_3 = 1/3`\ 和\ :math:`p_4 = 0`\ 。 根据 :eq:`eq_bleu`\ 中BLEU的定义, 当预测序列与标签序列完全相同时,BLEU为\ :math:`1`\ 。 此外,由于\ :math:`n`\ 元语法越长则匹配难度越大, 所以BLEU为更长的\ :math:`n`\ 元语法的精确度分配更大的权重。 具体来说,当\ :math:`p_n`\ 固定时,\ :math:`p_n^{1/2^n}` 会随着\ :math:`n`\ 的增长而增加(原始论文使用\ :math:`p_n^{1/n}`\ )。 而且,由于预测的序列越短获得的\ :math:`p_n`\ 值越高, 所以 :eq:`eq_bleu`\ 中乘法项之前的系数用于惩罚较短的预测序列。 例如,当\ :math:`k=2`\ 时,给定标签序列\ :math:`A`\ 、\ :math:`B`\ 、\ :math:`C`\ 、\ :math:`D`\ 、\ :math:`E`\ 、\ :math:`F` 和预测序列\ :math:`A`\ 、\ :math:`B`\ ,尽管\ :math:`p_1 = p_2 = 1`\ , 惩罚因子\ :math:`\exp(1-6/2) \approx 0.14`\ 会降低BLEU。 BLEU的代码实现如下。 .. raw:: latex \diilbookstyleinputcell .. code:: python def bleu(pred_seq, label_seq, k): #@save """计算BLEU""" pred_tokens, label_tokens = pred_seq.split(' '), label_seq.split(' ') len_pred, len_label = len(pred_tokens), len(label_tokens) score = math.exp(min(0, 1 - len_label / len_pred)) for n in range(1, k + 1): num_matches, label_subs = 0, collections.defaultdict(int) for i in range(len_label - n + 1): label_subs[' '.join(label_tokens[i: i + n])] += 1 for i in range(len_pred - n + 1): if label_subs[' '.join(pred_tokens[i: i + n])] > 0: num_matches += 1 label_subs[' '.join(pred_tokens[i: i + n])] -= 1 score *= math.pow(num_matches / (len_pred - n + 1), math.pow(0.5, n)) return score 最后,利用训练好的循环神经网络“编码器-解码器”模型, 将几个英语句子翻译成法语,并计算BLEU的最终结果。 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python engs = ['go .', "i lost .", 'he\'s calm .', 'i\'m home .'] fras = ['va !', 'j\'ai perdu .', 'il est calme .', 'je suis chez moi .'] for eng, fra in zip(engs, fras): translation, attention_weight_seq = predict_seq2seq( net, eng, src_vocab, tgt_vocab, num_steps, device) print(f'{eng} => {translation}, bleu {bleu(translation, fra, k=2):.3f}') .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output go . => entre ., bleu 0.000 i lost . => j'ai gagné ., bleu 0.000 he's calm . => j'ai gagné ., bleu 0.000 i'm home . => je suis chez moi !, bleu 0.719 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python engs = ['go .', "i lost .", 'he\'s calm .', 'i\'m home .'] fras = ['va !', 'j\'ai perdu .', 'il est calme .', 'je suis chez moi .'] for eng, fra in zip(engs, fras): translation, attention_weight_seq = predict_seq2seq( net, eng, src_vocab, tgt_vocab, num_steps, device) print(f'{eng} => {translation}, bleu {bleu(translation, fra, k=2):.3f}') .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output go . => va !, bleu 1.000 i lost . => j'ai perdu ., bleu 1.000 he's calm . => il est riche ., bleu 0.658 i'm home . => je suis en retard ?, bleu 0.447 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python engs = ['go .', "i lost .", 'he\'s calm .', 'i\'m home .'] fras = ['va !', 'j\'ai perdu .', 'il est calme .', 'je suis chez moi .'] for eng, fra in zip(engs, fras): translation, attention_weight_seq = predict_seq2seq( net, eng, src_vocab, tgt_vocab, num_steps) print(f'{eng} => {translation}, bleu {bleu(translation, fra, k=2):.3f}') .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output go . => va !, bleu 1.000 i lost . => j'ai perdu ., bleu 1.000 he's calm . => il est riche ., bleu 0.658 i'm home . => je suis chez moi partie ., bleu 0.803 .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python engs = ['go .', "i lost .", 'he\'s calm .', 'i\'m home .'] fras = ['va !', 'j\'ai perdu .', 'il est calme .', 'je suis chez moi .'] for eng, fra in zip(engs, fras): translation, attention_weight_seq = predict_seq2seq( net, eng, src_vocab, tgt_vocab, num_steps, device) print(f'{eng} => {translation}, bleu {bleu(translation, fra, k=2):.3f}') .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output go . => va !, bleu 1.000 i lost . => j'ai perdu ., bleu 1.000 he's calm . => il est riche homme feu fort venez venez court venez, bleu 0.258 i'm home . => je suis riche ., bleu 0.512 .. raw:: html
.. raw:: html
小结 ---- - 根据“编码器-解码器”架构的设计, 我们可以使用两个循环神经网络来设计一个序列到序列学习的模型。 - 在实现编码器和解码器时,我们可以使用多层循环神经网络。 - 我们可以使用遮蔽来过滤不相关的计算,例如在计算损失时。 - 在“编码器-解码器”训练中,强制教学方法将原始输出序列(而非预测结果)输入解码器。 - BLEU是一种常用的评估方法,它通过测量预测序列和标签序列之间的\ :math:`n`\ 元语法的匹配度来评估预测。 练习 ---- 1. 试着通过调整超参数来改善翻译效果。 2. 重新运行实验并在计算损失时不使用遮蔽,可以观察到什么结果?为什么会有这个结果? 3. 如果编码器和解码器的层数或者隐藏单元数不同,那么如何初始化解码器的隐状态? 4. 在训练中,如果用前一时间步的预测输入到解码器来代替强制教学,对性能有何影响? 5. 用长短期记忆网络替换门控循环单元重新运行实验。 6. 有没有其他方法来设计解码器的输出层? .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html
`Discussions `__ .. raw:: html
.. raw:: html