In this tutorial we’re going to build a seq2seq model in TensorFlow. We’re going to have some toy data. We’re going to give it some sequence of inputs and try to predict same sequence. We’re going to predict that same input sequence and in the process learn how memory works in sequence to sequence model.

The sequence to Sequence model is used for a whole bunch of different stuff everything from chatbots to speech to text to dialogue systems to Q&A to image captioning.

Sequences preserve the order of the inputs. It allows processing information that has an order element to it and preserves information that couldn’t be done via normal neural networks.

Two main components to a sequence to sequence model. An encoder, which basically takes info in time steps creates a hidden state and then sets it up to be passed to the decoder and then your decoder takes that hidden state and then uses that to start predicting things. Another thing is crucial with this kind of models is you need a lot of data.

The key ideas behind this are that the aim is to convert a sequence into a fixed sized feature vector that encodes only the important information in the sequence while losing the unnecessary information.

Prepare Dataset

We’ll use a language dataset provided by text file and convert characters into integers.

Create Input Function

In this tutorial, we use TensorFlow DataSet API to feed data into the model. We initialize a Dataset from a generator, this is useful when we have an array of different elements length like sequences.

Create a Model

You can separate the entire model into 2 part. The first part is Encoder and the second part is Decoder. Encoder akes a raw input text data just like any other RNN architectures. In the end, Encoder outputs a neural representation. The output of Encoder is going to be the input data for the Decoder.

The embedding layer allows us to map each token ID to a vector representation. The token embeddings are feed to the encoder.

Training Model

After defining the model, we run training steps by passing in batched inputs. In this tutorial, we use TensorFlow Estimator API to train model.


Download this Project from GitHub