パート2: 文字列反転モデル - エンコーダ
ここでは、文字列反転モデルの残りのエンコーダ部分を実装します。エンコーダは、前の課題で実装した words2onehot() 関数が生成するワンホットベクトルを入力として受け取ります。
ここで実装するのは encoder() 関数です。encoder() 関数はワンホットベクトルの集合を受け取り、単語IDのリストへと変換します。
この演習では、words2onehot() 関数と、(We、like、dogs を含む)word2index 辞書が提供されています。
この演習はコースの一部です
Kerasで学ぶMachine Translation
演習の手順
np.argmax()関数を使ってonehotを単語IDの配列に変換し、その単語IDを返します。- 単語
We、like、dogsを含む単語リストを定義します。 words2onehot()関数を使って、単語リストをワンホットベクトルに変換します。words2onehot()は引数として単語リストと Python の辞書を受け取ることに注意してください。encoder()関数を使って、ワンホットベクトルのコンテキストベクトルを取得します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
def encoder(onehot):
# Get word IDs from onehot vectors and return the IDs
word_ids = np.____(____, axis=____)
return ____
# Define "We like dogs" as words
words = ____
# Convert words to onehot vectors using words2onehot
onehot = ____(____, ____)
# Get the context vector by using the encoder function
context = encoder(____)
print(context)