第10章現代のリカレントニューラルネットワーク
10.7.3. Decoder10.7.3 デコーダ
Given a target output sequence for each time step (we use to differentiate from the input sequence time steps), the decoder assigns a predicted probability to each possible token occurring at step conditioned upon the previous tokens in the target and the context variable , i.e., .
英語のヒント
各時間ステップ(入力系列の時間ステップと区別するためにを使う)について目標の出力系列が与えられると、デコーダは、目標の中のそれまでのトークンと文脈変数を条件として、ステップに現れうる各トークンに予測確率を割り当てる。つまりである。
To predict the subsequent token in the target sequence, the RNN decoder takes the previous step’s target token , the hidden RNN state from the previous time step , and the context variable as its input, and transforms them into the hidden state at the current time step. We can use a function to express the transformation of the decoder’s hidden layer:
英語のヒント
目標系列の次のトークンを予測するため、RNNのデコーダは、前のステップの目標トークン、前の時間ステップのRNNの隠れ状態、文脈変数を入力としてとり、それらを現在の時間ステップの隠れ状態に変換する。デコーダの隠れ層の変換を表すのに、関数を使える。
(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 over the subsequent output token .
英語のヒント
デコーダの隠れ状態を得た後、出力層とソフトマックス演算を使って、次の出力トークンについての予測分布を計算できる。
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のエンコーダ・デコーダモデルの層。