モデルの学習
2017 年には、Google 翻訳が毎日 5 億人以上のユーザーに利用されていたことをご存じですか?
ここでは、最初の Teacher Forcing を用いたモデルを学習します。Teacher Forcing は、ニューラル機械翻訳のような sequence-to-sequence モデルで性能を高めるためによく使われます。
この演習では、sents2seqs() 関数、英語の文 en_text、フランス語の文 fr_text が提供されています。
この演習はコースの一部です
Kerasで学ぶMachine Translation
演習の手順
- 各系列の最後の単語を除く、フランス語単語のワンホット符号化系列を含むデコーダ入力を取得します。
- 各系列の最初の単語を除く、フランス語単語のワンホット符号化系列を含むデコーダ出力を取得します。
- 単一バッチのデータでモデルを学習します。
- 学習データ
en_x,de_x,de_yに対する評価指標(loss と accuracy)を取得します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
n_epochs, bsize = 3, 250
for ei in range(n_epochs):
for i in range(0,data_size,bsize):
en_x = sents2seqs('source', en_text[i:i+bsize], onehot=True, reverse=True)
de_xy = sents2seqs('target', fr_text[i:i+bsize], onehot=True)
# Separate the decoder inputs from de_xy
de_x = ____[:,____,:]
# Separate the decoder outputs from de_xy
de_y = ____[____]
# Train the model on a single batch of data
nmt_tf.____([____,____], ____)
# Obtain the eval metrics for the training data
res = _____.evaluate([____,____], ____, batch_size=bsize, verbose=0)
print("{} => Train Loss:{}, Train Acc: {}".format(ei+1,res[0], res[1]*100.0))