パート2:GRUモデルを理解する
Keras モデルで任意サイズのバッチを受け付ける方法を見ていきます。任意サイズのバッチを受け付けられることは、多くの理由で重要です。例えば、モデル側を変更せずに、学習時に異なるバッチサイズを試せるようになります。
この演習では、keras と numpy(np として)がすでにインポートされています。
この演習はコースの一部です
Kerasで学ぶMachine Translation
演習の手順
- シーケンス長 3、入力サイズ 4 のデータに対して、任意サイズのバッチを受け付ける入力レイヤーを定義します。
- 10 個の隠れユニットを持つ GRU レイヤーを定義し、前段の入力を受け取り出力を生成します。
- 入力レイヤーを入力とし、GRU レイヤーの出力を出力とする
modelという Model を定義します。モデル定義にはkeras.models.Model(<argument>=<value>)の構文を使えます。 x1とx2の両方に対してモデル出力を予測します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define an input layer
inp = keras.layers.____(____=(____))
# Define a GRU layer that takes in the input
gru_out = keras.layers.____(____)(____)
# Define a model that outputs the GRU output
____ = keras.models.____(inputs=____, outputs=____)
x1 = np.random.normal(size=(2,3,4))
x2 = np.random.normal(size=(5,3,4))
# Get the output of the model and print the result
y1 = ____.____(____)
y2 = ____.____(____)
print("shape (y1) = ", y1.shape, " shape (y2) = ", y2.shape)