第10章現代のリカレントニューラルネットワーク
10.7.7. Prediction10.7.7 予測
To predict the output sequence at each step, the predicted token from the previous time step is fed into the decoder as an input. One simple strategy is to sample whichever token that has been assigned by the decoder the highest probability when predicting at each step. As in training, at the initial time step the beginning-of-sequence (“<bos>”) token is fed into the decoder. This prediction process is illustrated in Fig. 10.7.3. When the end-of-sequence (“<eos>”) token is predicted, the prediction of the output sequence is complete.
英語のヒント
各ステップで出力系列を予測するため、前の時間ステップで予測したトークンが入力としてデコーダに入れられる。単純な戦略の一つは、各ステップで予測するときにデコーダが最も高い確率を割り当てたトークンをどれでも取り出すことである。訓練と同じように、最初の時間ステップでは、系列の始まりを表す(「<bos>」)トークンがデコーダに入れられる。この予測の過程を図10.7.3に示す。系列の終わりを表す(「<eos>」)トークンが予測されると、出力系列の予測は完了する。
Fig. 10.7.3図10.7.3
Predicting the output sequence token by token using an RNN encoder–decoder.
英語のヒント
RNNのエンコーダ・デコーダを使って、出力系列をトークンごとに予測する。
In the next section, we will introduce more sophisticated strategies based on beam search (Section 10.8).
英語のヒント
次の節では、ビームサーチ(10.8節)にもとづく、もっと洗練された戦略を紹介する。
@d2l.add_to_class(d2l.EncoderDecoder) #@save
def predict_step(self, batch, device, num_steps,
save_attention_weights=False):
batch = [a.to(device) for a in batch]
src, tgt, src_valid_len, _ = batch
enc_all_outputs = self.encoder(src, src_valid_len)
dec_state = self.decoder.init_state(enc_all_outputs, src_valid_len)
outputs, attention_weights = [tgt[:, (0)].unsqueeze(1), ], []
for _ in range(num_steps):
Y, dec_state = self.decoder(outputs[-1], dec_state)
outputs.append(Y.argmax(2))
# Save attention weights (to be covered later)
if save_attention_weights:
attention_weights.append(self.decoder.attention_weights)
return torch.cat(outputs[1:], 1), attention_weights