Three years before "Attention Is All You Need", Dzmitry Bahdanau, Kyunghyun Cho and Yoshua Bengio added attention to an RNN translation model, and the attention in my Transformer post grew out of that idea. This post is just that one idea, with very little code. The next post reads the paper properly and builds it in PyTorch.

The question

In 2014, why did neural translation models get worse as sentences got longer?

The usual model then had two RNNs. The encoder read the English sentence one word at a time and ended with a single vector, a fixed list of numbers. The decoder wrote the French sentence using only that vector. A three-word sentence and a fifty-word sentence both had to fit into a vector of the same size.

The paper's guess was that this fixed-length vector was the bottleneck, and their experiments backed it up: the older model's scores fell sharply on longer sentences. So they kept the encoder and decoder and changed what the decoder gets to look at.

One vector vs one note per word

The analogy: closed-book vs open-book exam

In a closed-book exam you read the chapter, close the book, and answer from memory. With a short chapter that works. With a long one, details start to blur and you mix things up. That's the old encoder-decoder: everything has to survive inside one vector.

In an open-book exam you keep the book open. For each question, you flip back to the pages that matter and read them again. The 2014 model works like this. The encoder keeps a separate note for every source word (the paper calls them annotations), and before writing each French word, the decoder decides which notes to read.

The math, in three lines

For output word i and source word j:

eij​=v⊤tanh(Wsi−1​+Uhj​),αij​=∑k​exp(eik​)exp(eij​)​,ci​=j∑​αij​hj​.

si−1​ is the decoder's state, its memory of what it has written so far. hj​ is the note for source word j. The first equation gives each note a score, the second turns the scores into weights that add up to one (that's softmax), and the third mixes the notes using those weights. The mix, ci​ , is called the context vector, and the decoder uses it to pick the next word. Then it does all three steps again for the next word, with fresh weights.

The score adds two projected vectors before the tanh, which is why people call this additive attention. The Transformer later swapped this score for a scaled dot product, but the score, softmax and blend steps stayed the same.

A tiny example you can run

import torch torch.manual_seed(0) words = ["the", "cat", "sat"] h = torch.randn(3, 4) # encoder output, one note per word s = torch.randn(4) # decoder state before the next word W = torch.randn(4, 4) # these three get learned in a real model U = torch.randn(4, 4) v = torch.randn(4) scores = torch.tanh(s @ W + h @ U) @ v # one score per word weights = scores.softmax(dim=0) context = weights @ h # weighted mix of the notes for word, score, weight in zip(words, scores, weights): print(f"{word:>4} score {score:+.2f} weight {weight:.2f}") print("context:", [round(x, 2) for x in context.tolist()]) 

It prints:

 the score -1.60 weight 0.15 cat score -2.16 weight 0.08 sat score +0.07 weight 0.77 context: [-0.42, -0.47, -0.74, 0.29] 

The weights here are random, so "sat" winning means nothing. Three scores go in, three weights that add up to one come out, and the context is one vector the same size as a single note. Training adjusts W, U and v until the useful note wins.

Does it really help?

I trained two small models on a toy task (the next post has the details and the code). They were identical except for one thing: one model got a single fixed vector, and the other got attention.

Accuracy by sentence length

On short sentences both are fine. By 30 words, the fixed-vector model gets about one word in five right, while the attention model gets nearly all of them. The paper saw the same pattern on real English-to-French sentences.

Where the analogy stops working

With an open book, you flip to one page. The model reads a weighted blend of every note at once. In my toy model, the biggest single weight for an output word was often only around half, with most of the rest on a neighbouring word.

Nobody tells the model which page to read, either. There are no labels saying "French word 3 goes with English word 5". It learns where to look only because looking in the right place makes its translations less wrong, which is what the title means by jointly learning to align and translate. And the notes aren't the original words: a bidirectional RNN writes them after reading the sentence both ways, so each note already knows a bit about its neighbours. More like a book where every page has a summary of the pages around it.

Next post: the paper section by section, both models built from scratch in PyTorch, and a heatmap showing where the model looked for every word.

The paper: Bahdanau, Cho and Bengio, Neural Machine Translation by Jointly Learning to Align and Translate (2014).