Notes: Neural Machine Translation by Jointly Learning to Align and Translate

Notes: Neural Machine Translation by Jointly Learning to Align and Translate

  • Previous approach was 2 networks, an encoder and decoder network
  • Required the construction of a fixed-length representation (vector) as an intermediate step between encoder and decoder
    • Hypothesized to be limiting, especially for long sequences, since a lot of information needs to be stored in a fixed size
  • This paper introduces the soft-attention approach which allows the model to automatically soft search for important parts of the source sentence, and pay special attention to these parts during the translation

Previous work had an encoder - it reads a source sentence, and encodes it into a fixed-length vector

The decoder reads the fixed-length vector and outputs a sequence that is the translated sentence. Training is done to maximize the log probability of the translated sentence conditioned on the source sentence.

Limitation : need o encode the entire representation into a fixed-length vector (making it harder to remember longer-term dependencies, which can occur in longer sentences)

The problem with long sequence is also seen if you make an analogy for what human translators do: they don’t just memorize an entire sentence and then split out the translation for it (especially not for longer sentences) - they read a part of the sentence, store some information, produce part of the translation, read the next portion of the sentence, update their information, produce more of the sentence, etc.


Overview of RNN - Instead of an ordinary hidden layer in a neural network that computes we instead maintain hidden states across a series of timesteps, and we pass in our inputs at each timestep:

(where can be the softmax function, for example)

Seq to seq encoder

Inputs: a sequence of vectors of variable length, where the overall sequence is the sentence, and each vector represents the encoded word, such as with word2vec.

Output: a fixed-length vector . Often computing by applying a function to the hidden states we have accumulated, i.e. , we further often just set , meaning that the final hidden state is the intermediate, fixed-length encoding representation.

Seq to seq decoder

  • Goal: predict next given the context vector and the previous ‘s. We define the probability of the translation as

  • In the decoder network, each of these conditional probabilities is defined using an RNN:

    i.e. each probability is computed as a function of the previous prediction at the previous timestep (we can also feed in the true label during training, which is known as teacher forcing), the current hidden state, and the intermediate context vector. We update the hidden state at each timestep similar to how we did it with the encoder RNN: where the initial could simply be , the function applied to the original context vector.

Learning to Align and Translate

Intuition for what attention is
  • Given that we have our input sequence, we run it through a (bidirectional in the paper) RNN to get hidden states for each in the sequence. (There are actually both forward and backward activations if we use a bidirectional RNN, but we can simply take the concatenation to get a single activation)
  • One of the goals of the attention model is to compute attention weights where indicates intuitively, when we’re generating word in the output translation, how much attention should we pay to word in the source sentence? These values are used to weight the hidden states in the source sentence. The below figure shows this:

  • We can continue this: after generating hidden state and producing output , we then need to repeat this process in order to get the second output:

    • we somehow (explained later) compute for all in the input sentence, and use this to calculate , and our next state is a function of the previous state , this new context , and our previous translation output . This is then used to compute a function for our next translation result which relies on the new state , the new context vector and the previous translation output.

    • Remember the interpretation for these values: for example, tells us how much attention we should pay to the first word in the input when generating output word 2. The goal is to get the model automatically identify which window of the input sentence it should pay special attention to when generating a certain output, so the model “attends to” specific windows while doing its translation.

  • It remains to show how exactly the context is defined at each timestep. Intuitively, for context , it should emphasize the region of the sentence that we should use when producing the output . It relies on and the encoder hidden states .

  • It also remains to show exactly how values are calculated, which indicate how much attention we should pay to a particular word when generating a particular output word. Each is influenced by the encoder RNN’s (i.e., the RNN that is reading the source sentence) hidden state at time , and the decoder RNN’s hidden state at time (which in turn comes from the previous context and values).

Calculating the time-specific context vectors and attention weights
  • The context at each timestep will be defined as a weighted sum of the activations, where the activations are weighted by the values.
  • To calculate the context vectors, first we take the concatenation of the bidirectional RNN activations:
  • Next, we have , where we sum across all in the input sequence, and in general
  • We once again have amount of attention that output should pay to the encoder hidden state , which is directly computed from the th input word.

  • This is done sequentially, so our above network becomes a standard RNN known as the decoder network:

Calculating the attention weights $\alpha_{ij} $
  • We remember that the term denotes the amount of attention that the output word at time should pay to the input word at time .

  • We have which is essentially a softmax over the vector . This ensures that the attention weights sum to and can be interpreted as probabilities.

  • Each is computed as a function of the decoder RNN’s previous hidden state activation (so the activation at time , note that we do not have access to the hidden state at the current timestep yet ) and the hidden state of the encoder RNN

  • Note that for a fixed such as , we can compute independently of each other (where is the size of our input sequence) (assuming that all the hidden states for our bidirectional encoder RNN have been computed) , but the computation of any of the ’s are blocked on the computation of the next hidden state (, for which we need the context vector for, for which we need all the previous ’s for). Therefore, for any , we need to have computed before computing

  • The function used to compute is a small, usually single-layer neural network with inputs (previous hidden state of our decoder RNN) and (th hidden state of our encoder network)

  • This network is jointly trained with the rest of the network with one giant backpropagation, meaning that the gradient of the cost function flows through it and it is not trained separately from the main task.

  • The below figure illustrates how the attenion weights are computed.

Complete model description

  • First, we need to produce what the authors call “annotations”. Each annotation is the concatenation of a bidirectional RNN’s backward and forward hidden states and at each timestep.

    • The bidirectional RNN is used because for the current word, we want to summarize not only the preceding words but also the following words. Since RNN’s tend to focus on recent inputs, the annotation will be focused in the window of words around .
  • We now have a sequence of annotations .

  • Next, we’d like to compute our first context vector , but for this we need our attention weights , so we need to calculate from our alignment model. We have which is the output of a simple one-layer neural network. Now, we can run softmax to compute our alpha values .

  • Now, we can compute our context vector: since we have as our encoder RNN output and as our attention weight, that tells us for each input word how much attention we should pay to it to produce the first word in our resulting translation.

  • Given this context vector, we can apply a function to compute the next state: where is an initial hidden state, is the initial word (such as a start token) and is the time-specific context we just created.

  • Using this new hidden state, we generate our output at this timestep :

  • We note that to compute in order to get the attention weights for the 2nd output, we need access to s1, so we must compute before being able to compute the next set of attention weights.

  • The general formulas are:

    , , and

Details on bidirectional RNN
  • BiRNN consistens of forward and backward RNNs - forward RNN reads the input sequence as it is given, and produces forward hidden states. The backwards RNN reads the sequence in revserse order, resulting in calculating a sequence of backwards hidden states
    • Result is concatenated to produce what the authors call “annotations”
Visualizing attention weights
  • The following figure shows the attention weights that were learned for some example sentence translation pairs. We see that in many cases the weights on the diagonal are more pronounced, since the source word at that index corresponds closely to the same index in the translated word
    • However, in the figure this is not always true. For example, French has a different order of adjectives and nouns, so we see the correct weighting when the system translates [European economic area] into [zone econonomique europeen]

  1. Neural Machine Translation by Jointly learning to align and translate:

  2. explanation of attention models: