Defining the decoder

Machine Translation with Keras

Thushan Ganegedara

Data Scientist and Author

Encoder-decoder model

  • Encoder consumes English words one-by-one
  • Finally produces the context vector
  • Decoder takes the context vector as the initial state
  • Decoder produces French words one-by-one

Encoder decoder model

Machine Translation with Keras

Input of the decoder

  • Decoder is implemented using a Keras GRU layer
  • GRU model require two inputs
    • A time-series input (???)
    • A hidden state

Encoder decoder model

Machine Translation with Keras

Input of the decoder

Repeat the context vector from the encoder N-many times

  • To produce a french sentence of 10 words, you repeat the context vector 10 times.

Encoder decoder model repeat vector

Machine Translation with Keras

Understanding the RepeatVector layer

RepeatVector layer:

  • Takes one argument which defines the sequence length of the required output
  • Takes in an input of (batch_size, input size) (e.g. Input of size 2 x 3)
  • Outputs data having shape (batch_size, sequence length, input size) (e.g. Output of size 2 x 3 x 3)

Repeat vector functionality

Machine Translation with Keras

Defining a RepeatVector layer

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)
  • Note that the following two are equivalent
rep = RepeatVector(5)
r_out = rep(r_inp)
r_out = RepeatVector(5)(r_inp)
Machine Translation with Keras

Predicting with the model

Predicting with the model

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)
Machine Translation with Keras

Implementing the decoder

Defining the decoder

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

Fixing the initial state of the decoder

gru_outputs = decoder_gru(de_inputs, initial_state=en_state)
Machine Translation with Keras

Defining the model

enc_dec = Model(inputs=en_inputs, outputs=gru_outputs)
Machine Translation with Keras

Let's practice!

Machine Translation with Keras

Preparing Video For Download...