實作完整的編碼器—解碼器模型

使用 Keras 進行機器翻譯

Thushan Ganegedara

Data Scientist and Author

目前你已完成的部分

  • 編碼器讀入英文(來源)輸入
  • 編碼器輸出情境向量
  • 解碼器讀入重複的情境向量
  • 解碼器輸出 GRU 序列

編碼器—解碼器模型(重複向量)

使用 Keras 進行機器翻譯

解碼器上半部

  • 使用 TimeDistributed 與 Dense 層實作。

含 TimeDistributed 的編碼器—解碼器

使用 Keras 進行機器翻譯

實作完整模型

  • 編碼器

    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 進行機器翻譯

實作完整模型

  • 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 進行機器翻譯

編譯模型

定義完整模型

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

編譯模型

nmt.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
使用 Keras 進行機器翻譯

一起來練習吧!

使用 Keras 進行機器翻譯

Preparing Video For Download...