第10章現代のリカレントニューラルネットワーク
10.7. Sequence-to-Sequence Learning for Machine Translation10.7 機械翻訳のための系列から系列への学習
In so-called sequence-to-sequence problems such as machine translation (as discussed in Section 10.5), where inputs and outputs each consist of variable-length unaligned sequences, we generally rely on encoder–decoder architectures (Section 10.6). In this section, we will demonstrate the application of an encoder–decoder architecture, where both the encoder and decoder are implemented as RNNs, to the task of machine translation (Cho et al., 2014, Sutskever et al., 2014).
英語のヒント
機械翻訳(10.5節で論じた)のような、いわゆる系列から系列への問題では、入力と出力がそれぞれ可変長の整列していない系列からなるので、一般にエンコーダ・デコーダ構造(10.6節)に頼る。この節では、エンコーダとデコーダをどちらもRNNとして実装したエンコーダ・デコーダ構造を、機械翻訳の課題に当てはめてみせる(Cho et al., 2014、Sutskever et al., 2014)。
Here, the encoder RNN will take a variable-length sequence as input and transform it into a fixed-shape hidden state. Later, in Section 11, we will introduce attention mechanisms, which allow us to access encoded inputs without having to compress the entire input into a single fixed-length representation.
英語のヒント
ここでは、エンコーダのRNNは可変長の系列を入力としてとり、それを固定された形状の隠れ状態に変換する。後の第11章では、入力全体を一つの固定長の表現に圧縮しなくても符号化された入力にアクセスできるようにする、注意機構を紹介する。
Then to generate the output sequence, one token at a time, the decoder model, consisting of a separate RNN, will predict each successive target token given both the input sequence and the preceding tokens in the output. During training, the decoder will typically be conditioned upon the preceding tokens in the official “ground truth” label. However, at test time, we will want to condition each output of the decoder on the tokens already predicted. Note that if we ignore the encoder, the decoder in a sequence-to-sequence architecture behaves just like a normal language model. Fig. 10.7.1 illustrates how to use two RNNs for sequence-to-sequence learning in machine translation.
英語のヒント
次に、出力の系列を一度に一つのトークンずつ生成するため、別のRNNからなるデコーダのモデルが、入力の系列と出力の中のそれまでのトークンの両方が与えられたときに、続く各目標トークンを予測する。訓練中は、デコーダはふつう、公式の「正解」ラベルの中のそれまでのトークンを条件とする。しかし、テスト時には、デコーダの各出力を、すでに予測したトークンを条件として行いたい。エンコーダを無視すれば、系列から系列への構造のデコーダは、通常の言語モデルとまったく同じようにふるまうことに注意してほしい。図10.7.1は、機械翻訳での系列から系列への学習に二つのRNNを使う方法を示している。
Fig. 10.7.1図10.7.1
Sequence-to-sequence learning with an RNN encoder and an RNN decoder.
英語のヒント
RNNのエンコーダとRNNのデコーダによる系列から系列への学習。
In Fig. 10.7.1, the special “<eos>” token marks the end of the sequence. Our model can stop making predictions once this token is generated. At the initial time step of the RNN decoder, there are two special design decisions to be aware of: First, we begin every input with a special beginning-of-sequence “<bos>” token. Second, we may feed the final hidden state of the encoder into the decoder at every single decoding time step (Cho et al., 2014). In some other designs, such as that of Sutskever et al. (2014), the final hidden state of the RNN encoder is used to initiate the hidden state of the decoder only at the first decoding step.
英語のヒント
図10.7.1では、特別な「<eos>」トークンが系列の終わりを示す。このトークンが生成されると、モデルは予測をやめられる。RNNのデコーダの最初の時間ステップでは、気をつけるべき特別な設計上の判断が二つある。第一に、すべての入力を、系列の始まりを表す特別な「<bos>」トークンで始める。第二に、エンコーダの最後の隠れ状態を、復号のすべての時間ステップでデコーダに入れることがある(Cho et al., 2014)。Sutskever et al. (2014)のようなほかの設計では、RNNのエンコーダの最後の隠れ状態は、最初の復号ステップでだけ、デコーダの隠れ状態を初期化するのに使われる。
import collections
import math
import torch
from torch import nn
from torch.nn import functional as F
from d2l import torch as d2l