Skip to the content

fatehi.devx2Learning

Deep learning

Encoder-decoder networks

One network reads the whole input into a set of vectors; a second writes the output from them, one element at a time.

Input and output of different shapes

Some tasks turn one sequence into another of a different length: a German sentence into an English one, a recording into its words, a long article into a short summary. No position of the output lines up with a fixed position of the input. An encoder-decoder network splits such a task in two. An encoder reads the whole input and turns it into a representation, a set of vectors. A decoder then writes the output from that representation, one element at a time.

The decoder writes autoregressively: each element it writes is fed back in, so the choice of the next one can depend on everything written so far. It starts from a start mark, written <s>, and stops when it writes an end mark. At each step it gives a probability to every possible next element, usually with a softmax over a vocabulary of tokens, and one of them is chosen.

The same split works beyond text. An autoencoder encodes an input into a small code and decodes the input back from it; a U-Net encodes an image into coarse features and decodes them into a map of the same image, one label per pixel. This page follows the sequence case, where the idea was developed for machine translation.

The first neural translators

In 2014 two groups built translators from two recurrent networks. Sutskever, Vinyals and Le at Google used two LSTMs of four layers each; Cho and colleagues in Montreal used the gated cell now called the GRU. The encoder reads the source sentence word by word. Its final state, a single vector usually called c, the context, is handed to the decoder, which starts from it and writes the translation word by word, each step taking the word it wrote before.

A recurrent encoder-decoder translating a sentence. The encoder reads the six English words one at a time; its last state, the single vector c, is all that reaches the decoder. The decoder starts from c and writes the German words one at a time, each word fed back as its next input.

On English to French, five such networks together scored 34.8 BLEU, above a phrase-based statistical system on the same test (33.3). But the design has a narrow point: everything the decoder knows about the source sentence must pass through c, a vector of fixed size, whether the sentence has five words or fifty. Cho and colleagues found that their translations got much worse as sentences grew longer. Sutskever’s group found a trick that shows where the trouble lies: feeding the source sentence in reversed, so that its first words, which the decoder needs first, were the last the encoder read and the freshest in c. That alone raised one of their models from 25.9 to 30.6 BLEU.

Attention to the input

In 2014 Bahdanau, Cho and Bengio removed the narrow point. Their encoder keeps all its states, one per source word, and at each step the decoder decides afresh which of them to read. It compares its own current state with every encoder state, turns the comparison scores into weights with a softmax, and takes the weighted sum of the encoder states as its context for this step. Writing “Katze”, it can lean on the state of “cat”; writing “Matte”, on that of “mat”. The weights are learned with the rest of the network, from translations alone; nobody tells the model which words correspond.

Attention in a recurrent translator. The encoder keeps all six of its states. At each step the decoder gives every state a weight, shown by how far its gauge is filled and by the brightness of its signal, and reads their weighted sum, carried on one wire down to the cell that writes. (The weights are made up for the illustration: each German word leans mostly on the English word it translates.)

With attention the quality no longer collapsed on long sentences, and the weights themselves became readable: drawn as a table of target words against source words, they trace an alignment, mostly along the diagonal, with jumps where the two languages order their words differently. This mechanism, a step reading many vectors with weights it computes itself, is attention. Where the reader and the vectors it reads come from two different sequences, as here, it is called cross-attention.

Training and writing

An encoder-decoder is trained on pairs, an input and its correct output. During training the decoder is not fed its own guesses but the correct previous words, a practice called teacher forcing, and at every step it is scored on how much probability it gave the correct next word (the cross-entropy). So the training signal reaches every position of the output in one pass, even though writing will later go word by word.

When the trained model writes, it has no correct words to lean on. Taking the most probable word at each step (greedy decoding) is fast, but an early choice that looked best can lead into a poor sentence. Beam search keeps the few best partial sentences, the beam, at every step, extends each by every word, and keeps the best few of the results, ranked by their total log-probability; at the end it returns the best complete sentence. Beams of four to ten are common in translation.

Test yourself

  1. Why can the decoder not write the whole output at once, even after the encoder has read the whole input?

    Each output word depends on the words before it: which word fits fourth depends on the first three. So at writing time the decoder must choose them in order, feeding each choice back in.

  2. Why did reversing the source sentence help the first LSTM translators?

    Everything passed through the encoder’s final state. Reversed, the first source words, which the decoder needs first, were the last the encoder read and so the best kept in that state, and the gap between each source word and the target word it matches became shorter at the start of the sentence.

  3. With attention, what does the decoder read at each step instead of the single vector c?

    A weighted sum of all the encoder’s states, one per source word, with weights it computes at that step by comparing its own state with each of them and passing the scores through a softmax.

  4. Training uses teacher forcing; writing uses beam search. Why the difference?

    In training the correct previous words are known, so the decoder is fed them and every position is trained at once. When writing, the decoder only has its own earlier choices, so it must search: beam search keeps several promising partial sentences instead of committing to the most probable word at each step.

Where it leads

Kept in this browser only, never sent.

Paths through it