エンコーダ・デコーダ完全モデルの実装

Kerasで学ぶMachine Translation

Thushan Ganegedara

Data Scientist and Author

これまでに実装したもの

  • エンコーダは英語(ソース)入力を受け取る
  • エンコーダはコンテキストベクトルを生成
  • デコーダはコンテキストベクトルを繰り返して受け取る
  • デコーダはGRUの出力系列を出力

エンコーダ・デコーダモデル(RepeatVector)

Kerasで学ぶMachine Translation

デコーダの上部

  • TimeDistributedDense レイヤーで実装

TimeDistributed を用いたエンコーダ・デコーダ

Kerasで学ぶMachine Translation

フルモデルの実装

  • エンコーダ

    en_inputs = Input(shape=(en_len, en_vocab))
    en_gru = GRU(hsize, return_state=True)
    en_out, en_state = en_gru(en_inputs)
    
  • デコーダ

    de_inputs = RepeatVector(fr_len)(en_state)
    de_gru = GRU(hsize, return_sequences=True)
    de_out = de_gru(de_inputs, initial_state=en_state)
    
Kerasで学ぶMachine Translation

フルモデルの実装

  • Softmax 予測レイヤー
de_dense = keras.layers.Dense(fr_vocab, activation='softmax')
de_dense_time = keras.layers.TimeDistributed(de_dense)
de_pred = de_seq_dense(de_out)
Kerasで学ぶMachine Translation

モデルのコンパイル

フルモデルの定義

nmt = keras.models.Model(inputs=en_inputs, outputs=de_pred)

モデルのコンパイル

nmt.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
Kerasで学ぶMachine Translation

Let's practice!

Kerasで学ぶMachine Translation

Preparing Video For Download...