Visualizing A Neural Machine Translation Model (Mechanics of Seq2seq Models With Attention)
Watch: MIT’s lecture referencing this post
May 25th update:开心时时彩计划软件下载 New graphics (RNN animation, word embedding graph), color coding, elaborated on the final attention example.
Note: The animations below are videos. Touch or hover on them (if you’re using a mouse) to get play controls so you can pause if needed.
sequence-to-sequence models are deep learning models that have achieved a lot of success in tasks like machine translation, text summarization, and image captioning. google translate started such a model in production in late 2016. these models are explained in the two pioneering papers (, ).
i found, however, that understanding the model well enough to implement it requires unraveling a series of concepts that build on top of each other. i thought that a bunch of these ideas would be more accessible if expressed visually. that’s what i aim to do in this post. you’ll need some previous understanding of deep learning to get through this post. i hope it can be a useful companion to reading the papers mentioned above (and the attention papers linked later in the post).
a sequence-to-sequence model is a model that takes a sequence of items (words, letters, features of an images…etc) and outputs another sequence of items. a trained model would work like this:
in neural machine translation, a sequence is a series of words, processed one after another. the output is, likewise, a series of words:
Looking under the hood
Under the hood, the model is composed of an encoder and a decoder.
The encoder processes each item in the input sequence, it compiles the information it captures into a vector (called the context). After processing the entire input sequence, the encoder send the context over to the decoder开心时时彩计划软件下载, which begins producing the output sequence item by item.
开心时时彩计划软件下载the same applies in the case of machine translation.
The context is a vector (an array of numbers, basically) in the case of machine translation. The encoder and decoder tend to both be recurrent neural networks (Be sure to check out Luis Serrano’s for an intro to RNNs).
You can set the size of the context vector when you set up your model. It is basically the number of hidden units in the encoder RNN. These visualizations show a vector of size 4, but in real world applications the context开心时时彩计划软件下载 vector would be of a size like 256, 512, or 1024.
by design, a rnn takes two inputs at each time step: an input (in the case of the encoder, one word from the input sentence), and a hidden state. the word, however, needs to be represented by a vector. to transform a word into a vector, we turn to the class of methods called “” algorithms. these turn words into vector spaces that capture a lot of the meaning/semantic information of the words (e.g. ).
now that we’ve introduced our main vectors/tensors, let’s recap the mechanics of an rnn and establish a visual language to describe these models:
the next rnn step takes the second input vector and hidden state #1 to create the output of that time step. later in the post, we’ll use an animation like this to describe the vectors inside a neural machine translation model.
In the following visualization, each pulse for the encoder or decoder is that RNN processing its inputs and generating an output for that time step. Since the encoder and decoder are both RNNs, each time step one of the RNNs does some processing, it updates its hidden state开心时时彩计划软件下载 based on its inputs and previous inputs it has seen.
Let’s look at the hidden states for the encoder. Notice how the last hidden state is actually the context we pass along to the decoder.
The decoder also maintains a hidden states that it passes from one time step to the next. We just didn’t visualize it in this graphic because we’re concerned with the major parts of the model for now.
Let’s now look at another way to visualize a sequence-to-sequence model. This animation will make it easier to understand the static graphics that describe these models. This is called an “unrolled” view where instead of showing the one decoder, we show a copy of it for each time step. This way we can look at the inputs and outputs of each time step.
Let’s Pay Attention Now
The context开心时时彩计划软件下载 vector turned out to be a bottleneck for these types of models. It made it challenging for the models to deal with long sentences. A solution was proposed in and . These papers introduced and refined a technique called “Attention”, which highly improved the quality of machine translation systems. Attention allows the model to focus on the relevant parts of the input sequence as needed.
开心时时彩计划软件下载let’s continue looking at attention models at this high level of abstraction. an attention model differs from a classic sequence-to-sequence model in two main ways:
First, the encoder passes a lot more data to the decoder. Instead of passing the last hidden state of the encoding stage, the encoder passes all the hidden states to the decoder:
Second, an attention decoder does an extra step before producing its output. In order to focus on the parts of the input that are relevant to this decoding time step, the decoder开心时时彩计划软件下载 does the following:
- Look at the set of encoder hidden states it received – each encoder hidden states is most associated with a certain word in the input sentence
- Give each hidden states a score (let’s ignore how the scoring is done for now)
- Multiply each hidden states by its softmaxed score, thus amplifying hidden states with high scores, and drowning out hidden states with low scores
This scoring exercise is done at each time step on the decoder side.
开心时时彩计划软件下载let us now bring the whole thing together in the following visualization and look at how the attention process works:
- The attention decoder RNN takes in the embedding of the initial decoder hidden state. token, and an
- The RNN processes its inputs, producing an output and a new hidden state vector (h4). The output is discarded.
- Attention Step: We use the encoder hidden states and the h4 vector to calculate a context vector (C4) for this time step.
- We concatenate h4 and C4 into one vector.
- We pass this vector through a feedforward neural network (one trained jointly with the model).
- The output of the feedforward neural networks indicates the output word of this time step.
- Repeat for the next time steps
开心时时彩计划软件下载this is another way to look at which part of the input sentence we’re paying attention to at each decoding step:
开心时时彩计划软件下载note that the model isn’t just mindless aligning the first word at the output with the first word from the input. it actually learned from the training phase how to align words in that language pair (french and english in our example). an example for how precise this mechanism can be comes from the attention papers listed above:
if you feel you’re ready to learn the implementation, be sure to check tensorflow’s .
i hope you’ve found this useful. these visuals are early iterations of a lesson on attention that is part of the udacity . we go into more details in the lesson, including discussing applications and touching on more recent attention methods like the transformer model from .
check out the trailer of the :
i’ve also created a few lessons as a part of udacity’s . the lessons i’ve created cover unsupervised learning, as well as a jupyter notebook on movie recommendations using collaborative filtering.
i’d love any feedback you may have. please reach me at .