データの前処理
ここでは、2 つの入力と 1 つの出力を持つ新しいモデル用にデータを処理します。2 つの入力は、one-hot エンコードした英語の単語列と、末尾の単語を除いた one-hot エンコードのフランス語の単語列です。
出力は、先頭の単語を除いた one-hot エンコードのフランス語の単語列になります。つまり、デコーダでは各フランス語の入力語に対して、その「次の単語」が出力になります。ここではその実装方法を学びます。
sents2seqs() 関数、en_text、fr_text が用意されています。
この演習はコースの一部です
Kerasで学ぶMachine Translation
演習の手順
sents2seqs()関数を使って、エンコーダ入力のバッチ(iからi+bsize。one-hot エンコードかつ逆順)を取得します。sents2seqs()関数を使って、デコーダ入力と出力のバッチ(iからi+bsize。one-hot エンコード)を取得します。- 時間次元でスライスして、
de_xyからデコーダ入力(フランス語の最後の単語以外すべて)を分離します。 de_xyからデコーダ出力(フランス語の最初の単語以外すべて)を分離します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
bsize = 250
for i in range(0, len(en_text), bsize):
# Get the encoder inputs using the sents2seqs() function
en_x = ____('source', ____[____:____], onehot=True, reverse=____)
# Get the decoder inputs/outputs using the sents2seqs() function
de_xy = sents2seqs('target', ____[____:____], onehot=True)
# Separate the decoder inputs from de_xy
de_x = de_xy[:,____,:]
# Separate the decoder outputs from de_xy
de_y = de_xy[:,____,:]
print("Data from ", i, " to ", i+bsize)
print("\tnp.argmax() => en_x[0]: ", np.argmax(en_x[0], axis=-1))
print("\tnp.argmax() => de_x[0]: ", np.argmax(de_x[0], axis=-1))
print("\tnp.argmax() => de_y[0]: ", np.argmax(de_y[0], axis=-1))