デコーダーの定義

Kerasで学ぶMachine Translation

Thushan Ganegedara

Data Scientist and Author

エンコーダ・デコーダーモデル

  • エンコーダは英単語を順に処理
  • 最後にコンテキストベクトルを生成
  • デコーダーはそのベクトルを初期状態として受け取る
  • デコーダーは仏語の単語を順に生成

エンコーダ・デコーダーモデル

Kerasで学ぶMachine Translation

デコーダーの入力

  • デコーダーは Keras の GRU レイヤーで実装
  • GRU モデルは2つの入力が必要
    • 時系列入力(???)
    • 隠れ状態

エンコーダ・デコーダーモデル

Kerasで学ぶMachine Translation

デコーダーの入力

エンコーダのコンテキストベクトルを N 回繰り返す

  • 10語の仏語文を生成するなら、コンテキストベクトルを10回繰り返す

RepeatVector による繰り返し

Kerasで学ぶMachine Translation

RepeatVector レイヤーの理解

RepeatVector レイヤー:

  • 出力の系列長を定める引数を1つ取る
  • 入力は (batch_size, input size)(例: 2 x 3
  • 出力は (batch_size, sequence length, input size)(例: 2 x 5 x 3

RepeatVector の機能

Kerasで学ぶMachine Translation

RepeatVector レイヤーの定義

from tensorflow.keras.layers import RepeatVector
rep = RepeatVector(5)
r_inp = Input(shape=(3,))
r_out = rep(r_inp)
repeat_model = Model(inputs=r_inp, outputs=r_out)
  • 次の2つは同等です
rep = RepeatVector(5)
r_out = rep(r_inp)
r_out = RepeatVector(5)(r_inp)
Kerasで学ぶMachine Translation

モデルで予測する

モデルで予測する

x = np.array([[0,1,2],[3,4,5]])
y = repeat_model.predict(x)
print('x.shape = ',x.shape,'\ny.shape = ',y.shape)
x.shape =  (2, 3) 
y.shape =  (2, 5, 3)
Kerasで学ぶMachine Translation

デコーダーの実装

デコーダーの定義

de_inputs = RepeatVector(fr_len)(en_state)
decoder_gru = GRU(hsize, return_sequences=True)

デコーダーの初期状態を固定する

gru_outputs = decoder_gru(de_inputs, initial_state=en_state)
Kerasで学ぶMachine Translation

モデルの定義

enc_dec = Model(inputs=en_inputs, outputs=gru_outputs)
Kerasで学ぶMachine Translation

練習しましょう!

Kerasで学ぶMachine Translation

Preparing Video For Download...