Implementing the full encoder decoder model

Machine Translation with Keras

Thushan Ganegedara

Data Scientist and Author

What you implemented so far

  • The encoder consumes the English (i.e. source) input
  • The encoder produces the context vector
  • The decoder consumes a repeated set of context vectors
  • The decoder outputs GRU output sequence

Encoder decoder model repeat vector

Machine Translation with Keras

Top part of the decoder

  • Implemented with TimeDistributed and Dense layers.

Encoder decoder with TimeDistributed

Machine Translation with Keras

Implementing the full model

  • Encoder

    en_inputs = Input(shape=(en_len, en_vocab))
    en_gru = GRU(hsize, return_state=True)
    en_out, en_state = en_gru(en_inputs)
    
  • Decoder

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

Implementing the full model

  • The softmax prediction layer
de_dense = keras.layers.Dense(fr_vocab, activation='softmax')
de_dense_time = keras.layers.TimeDistributed(de_dense)
de_pred = de_seq_dense(de_out)
Machine Translation with Keras

Compiling the model

Defining the full model

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

Compiling the model

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

Let's practice!

Machine Translation with Keras

Preparing Video For Download...