プリンキピア

第10章現代のリカレントニューラルネットワーク

10.7.3. Decoder10.7.3 デコーダ

Given a target output sequence y1,y2,…,yT′ for each time step t′ (we use t′ to differentiate from the input sequence time steps), the decoder assigns a predicted probability to each possible token occurring at step yt′+1 conditioned upon the previous tokens in the target y1,…,yt′ and the context variable 𝐜, i.e., P(yt′+1∣y1,…,yt′,𝐜).
英語のヒント
各時間ステップt′(入力系列の時間ステップと区別するためにt′を使う)について目標の出力系列y1,y2,…,yT′が与えられると、デコーダは、目標の中のそれまでのトークンy1,…,yt′と文脈変数𝐜を条件として、ステップyt′+1に現れうる各トークンに予測確率を割り当てる。つまりP(yt′+1∣y1,…,yt′,𝐜)である。
To predict the subsequent token t′+1 in the target sequence, the RNN decoder takes the previous step’s target token yt′, the hidden RNN state from the previous time step 𝐬t′−1, and the context variable 𝐜 as its input, and transforms them into the hidden state 𝐬t′ at the current time step. We can use a function g to express the transformation of the decoder’s hidden layer:
英語のヒント
目標系列の次のトークンt′+1を予測するため、RNNのデコーダは、前のステップの目標トークンyt′、前の時間ステップのRNNの隠れ状態𝐬t′−1、文脈変数𝐜を入力としてとり、それらを現在の時間ステップの隠れ状態𝐬t′に変換する。デコーダの隠れ層の変換を表すのに、関数gを使える。
𝐬t′=g(yt′−1,𝐜,𝐬t′−1).(10.7.3)
After obtaining the hidden state of the decoder, we can use an output layer and the softmax operation to compute the predictive distribution p(yt′+1∣y1,…,yt′,𝐜) over the subsequent output token t′+1.
英語のヒント
デコーダの隠れ状態を得た後、出力層とソフトマックス演算を使って、次の出力トークンt′+1についての予測分布p(yt′+1∣y1,…,yt′,𝐜)を計算できる。
Following Fig. 10.7.1, when implementing the decoder as follows, we directly use the hidden state at the final time step of the encoder to initialize the hidden state of the decoder. This requires that the RNN encoder and the RNN decoder have the same number of layers and hidden units. To further incorporate the encoded input sequence information, the context variable is concatenated with the decoder input at all the time steps. To predict the probability distribution of the output token, we use a fully connected layer to transform the hidden state at the final layer of the RNN decoder.
英語のヒント
図10.7.1に従い、以下のようにデコーダを実装するとき、エンコーダの最後の時間ステップの隠れ状態を直接使って、デコーダの隠れ状態を初期化する。そのためには、RNNのエンコーダとRNNのデコーダが同じ数の層と隠れユニットをもつ必要がある。符号化された入力系列の情報をさらに取り込むため、文脈変数はすべての時間ステップでデコーダの入力と連結される。出力トークンの確率分布を予測するため、全結合層を使ってRNNのデコーダの最後の層の隠れ状態を変換する。
class Seq2SeqDecoder(d2l.Decoder):
    """The RNN decoder for sequence to sequence learning."""
    def __init__(self, vocab_size, embed_size, num_hiddens, num_layers,
                 dropout=0):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_size)
        self.rnn = d2l.GRU(embed_size+num_hiddens, num_hiddens,
                           num_layers, dropout)
        self.dense = nn.LazyLinear(vocab_size)
        self.apply(init_seq2seq)

    def init_state(self, enc_all_outputs, *args):
        return enc_all_outputs

    def forward(self, X, state):
        # X shape: (batch_size, num_steps)
        # embs shape: (num_steps, batch_size, embed_size)
        embs = self.embedding(X.t().type(torch.int32))
        enc_output, hidden_state = state
        # context shape: (batch_size, num_hiddens)
        context = enc_output[-1]
        # Broadcast context to (num_steps, batch_size, num_hiddens)
        context = context.repeat(embs.shape[0], 1, 1)
        # Concat at the feature dimension
        embs_and_context = torch.cat((embs, context), -1)
        outputs, hidden_state = self.rnn(embs_and_context, hidden_state)
        outputs = self.dense(outputs).swapaxes(0, 1)
        # outputs shape: (batch_size, num_steps, vocab_size)
        # hidden_state shape: (num_layers, batch_size, num_hiddens)
        return outputs, [enc_output, hidden_state]
To illustrate the implemented decoder, below we instantiate it with the same hyperparameters from the aforementioned encoder. As we can see, the output shape of the decoder becomes (batch size, number of time steps, vocabulary size), where the final dimension of the tensor stores the predicted token distribution.
英語のヒント
実装したデコーダを示すため、以下では、前述のエンコーダと同じハイパーパラメータでそれをインスタンス化する。見てのとおり、デコーダの出力の形状は(バッチサイズ、時間ステップの数、語彙の大きさ)となり、テンソルの最後の次元に予測したトークンの分布が格納される。
decoder = Seq2SeqDecoder(vocab_size, embed_size, num_hiddens, num_layers)
state = decoder.init_state(encoder(X))
dec_outputs, state = decoder(X, state)
d2l.check_shape(dec_outputs, (batch_size, num_steps, vocab_size))
d2l.check_shape(state[1], (num_layers, batch_size, num_hiddens))
The layers in the above RNN encoder–decoder model are summarized in Fig. 10.7.2.
英語のヒント
上のRNNのエンコーダ・デコーダモデルの層を、図10.7.2にまとめる。
Fig. 10.7.2図10.7.2
Layers in an RNN encoder–decoder model.
英語のヒント
RNNのエンコーダ・デコーダモデルの層。