Sentence autocompletion using Encoder-Decoder

Natural Language Generation in Python

Biswanath Halder

Data Scientist

Encoder decoder architecture

  • Encoder

    • Summarizes input information is states.
    • Outputs ignored.
  • Decoder

    • Initial state - final state of the encoder.
    • Final states ignored.
    • Input during training - original target.
    • Input during inference - predicted target.

Encoder Decoder Block Diagram

Natural Language Generation in Python

Encoder decoder for sentence auto-completion

Encoder-Decoder Sentence Autocomplete Training

Natural Language Generation in Python

Encoder for sentence auto-completion

# Create the input layer of the encoder
encoder_input = Input(shape=(None, len(vocabulary)))
# Create LSTM Layer of size 256
encoder_LSTM = LSTM(256, return_state = True)
# Save encoder output, hidden and cell state
encoder_outputs, encoder_h, encoder_c = encoder_LSTM(encoder_input)
# Save encoder states
encoder_states = [encoder_h, encoder_c]
Natural Language Generation in Python

Decoder for sentence auto-completion

decoder_input = Input(shape=(None, len(vocabulary)))
decoder_LSTM = LSTM(256, return_sequences=True, return_state = True)
decoder_out, _ , _ = decoder_LSTM(decoder_input,
                                  initial_state=encoder_states)
decoder_dense = Dense(len(vocabulary), activation='softmax')
decoder_out = decoder_dense(decoder_out)
Natural Language Generation in Python

Combine the encoder and the decoder

  • Build the model.
model = Model(inputs=[encoder_input, decoder_input],
                          outputs=[decoder_out])
  • Check model summary.
model.summary()
Natural Language Generation in Python

Train the network

  • Compile the model.
model.compile(optimizer='adam', loss='categorical_crossentropy')
  • Train the model.
model.fit(x=[input_data_prefix, input_data_suffix], y=target_data,
                  batch_size=64, epochs=1, validation_split=0.2)
Natural Language Generation in Python

Let's practice!

Natural Language Generation in Python

Preparing Video For Download...