Neural Translation, Step by Step

Neural Translation, Step by Step

How a neural model translates: where its vectors come from, how the encoder reads a Galician sentence into one summary, and how the decoder writes English from it. The running example is the start of a sentence from the novel Speechwriter: Unha tía miña, que todo o solucionaba con ditos e refráns, dicía algo así como «un é escravo do que di e dono do que cala».

What you will learn on this page

  1. A translation model has an encoder, which reads the Galician words into a summary vector, and a decoder, which writes English words one at a time from its state. All its vectors are learned from translated pairs. → section 1
  2. Training raises the probability of each gold output, one update at a time; whether one pair per update or all pairs at once is faster depends on what you count. → section 1, parts A–B
  3. Development data, held out of training, show what the training data cannot: a combination never seen, and whether training longer helps. → section 1, parts C–D
  4. The summary is ½ old + ½ new word, so in a long sentence the early words fade. → section 2
  5. Inference: the decoder writes the most probable word, mixes it into its state, and goes on. → section 3
  6. A model writes what its training data says (“I had an aunt who…”); the probability of a whole translation shows when it prefers its own wrong translation. → section 4

1Training: where a translation model's vectors come from

A neural translation model has two parts. The encoder reads the Galician words into a summary, a vector; the decoder starts from that summary and writes the English words one at a time. Every word in this model is a 2-D vector in one of three tables, and all of them start random: before it can translate, the model has to learn them from examples, by training. Here the 16 pairs are taken one at a time, round after round, and after every pair the tables are updated a little. Section 2 shows the encoder's rule with simple made-up numbers; section 3 takes the trained encoder and decoder apart.

The training data: 16 pairs, each an input (a Galician phrase) → its gold output (the right output: its English translation, by a person)

after each output, what is mixed into the state:

colors, in the tables and on the plane:this pair's inputsits gold outputsthe highest probability nowsecond vectors mixed into the statethe summary, then the state (- - - its direction: farther along = higher score)faint: before the update · short dash: the direction of a move too small to see (zoom in; the rows pushed away move by η × p, about a tenth of the gold row's move)gold k: gold output k · ↑ₖ ↓ₖ: from output k: its gold output up, the highest other down

Encoding: the inputs are mixed, one by one, into one vector, the summary (new = ½ × old + ½ × vector of the input; section 2 shows the rule)

green: the probability of this pair's gold translation · red: of the translation the model writes · orange line: where the cycle is now. Green climbs because every update of this pair moves the tables toward its gold translation; the other 15 pairs' updates share words with it and mostly help too.

B. The whole training: the same procedure as part A, seen a round at a time (1 round = one pass over the 16 pairs); and, for comparison, the other procedure

Training

pairs shown:
(training always uses all 16)

the training data: an input (a Galician phrase) → its gold output (the right output: its English translation, by a person) · click a cell to follow that pair in part A, with the tables as they are after this round

one output at a time, the one with the highest probability, mixed into the state before the next · the gold output · a different one · click one to follow it in part A, translating with these tables

Galician inputEnglish possible outputaxes: the plane zooms out as the vectors grow

click a word: its path from the random start to this round

training changes only the numbers in the three tables; how they are used (summary, score, probability, what is mixed in) never changes

Inputs translated right, over the rounds

count:

C. Training data and development data: the same training again, with one pair held out

Same random start, same rule, same 3000 rounds as part B; only unha tía miña → an aunt of mine is left out. The 15 pairs trained on are the training data. The held-out pair is development data: the model never sees it in training, and we use it to check the model while we build it. A failure here is not the summary losing words: in 3 words, tía is still a quarter of it. It is a model that learned the pairs it was shown, not how to combine familiar words anew. The other weakness, words fading from the summary of a long sentence, is section 2's; the next page takes it up.

Training on 15
pairs shown:
(the development pair is never trained on)

each gold output filled by the probability the model gives it · grayed, with a tag: the development pair, held out of training

one output at a time, the most probable, mixed into the state before the next · the gold output · a different one · section 3 walks the development pair output by output, with either model

Galician inputEnglish possible outputaxes: the plane zooms out as the vectors grow

click a word: its path from the random start to this round

unha tía miña (development): gold vs written translation, over the rounds

D. Training longer, checked on the development pair

Part C stopped at 3000 rounds. Here the same training on the same 15 pairs goes on to 6000 rounds; unha tía miña is still never trained on. The rest of the page keeps the models of 3000 rounds.

Training on 15

unha tía miña (development): gold vs written translation, over 6000 rounds

green: the probability of the gold translation · red: of the translation the model writes · orange line: this round · one curve where the model writes the gold translation

What the marks show

2-D vectors; stochastic gradient descent, one pair per update, learning rate 0.2, 3000 rounds over the 16 pairs (part B can also train all 16 at once: the 16 gradients added up, learning rate 0.1, 3000 rounds, replayed by the page as well); torch.manual_seed(0); mt_s2s_train.py records the random start, and the page replays the training with the same rule, so that every single update can be shown.

2Inference, part 1: the encoder reads a sentence into one vector

From here on, inference: a trained model translating. No gold output, no update; the tables stay as they are (section 1's "when translating" switch showed this inside training).

A translation model starts by reading the Galician sentence. Each word has an embedding, a row in a table like p on the skip-gram page; here it has 2 dimensions, so every word is a point. The encoder reads the words in order and keeps a summary: one more vector, updated after every word. In this simplified encoder, each new word moves the summary halfway toward that word's point.

a word's embedding the encoder's summary

The update, with this step's numbers

How much of each word is in the summary now

The word vectors are made up for this example. Real encoders, recurrent neural networks, learn their update rule instead of always taking half and half, and they can learn to keep some things longer. But their summary is still one vector of a fixed size, whatever the length of the sentence.

3Inference, part 2: the decoder writes English, word by word

Still inference: a model from section 1 translates, word by word, as in section 1 part A's “when translating”. Choose the model and the phrase: a training pair, the development pair, or a test phrase neither model ever saw.

The model of 16 writes all its training phrases right; it fails on phrases it never saw (the test phrases; for the model of 15, also the development pair) and on long sentences, where early words fade from the summary (section 2; the next page shows this with other models, and the remedy, attention).

model:

a. One plane for both languages: how the next word is chosen

Every word's score, on one line

Galician wordEnglish wordthe state (- - - the summary's path)click a ring or a word on the line: its score worked out

b. How the state was recomputed

What is in the state now

c. The first word: what unha, only ⅛ of the summary of unha tía miña, adds to its score

⅛ unha¼ tía½ miñathe summary = state 1written first

In long sentences the first words fade far more (section 2: tía is 0.534 of the summary). Sutskever and colleagues (2014) fed sentences in reverse to keep the first words fresh; the next page shows the remedy that made the trick unnecessary.

d. Is “an aunt of mine” the best translation?

The toy model learned to write “an aunt of mine” for unha tía miña, and the real model Helsinki-NLP/opus-mt-gl-en writes the same (a slide in the lesson looks at why). It is correct and faithful: “an” keeps the new character that unha introduces. But English rarely says it. For every 100 “my …”, English books write “a … of mine” this often (log scale):

a friend of mine12.47
an aunt of mine0.36
a brother of mine0.08

“A … of mine” picks one out of several, which is natural for friends and marked for close family. A translator would rebuild the sentence: “I had an aunt who…”. No word of the Galician corresponds to “I had”, and our model never saw such a sentence in training, so it cannot write it. Section 4 trains a model that does. Counts from Google Books; the n-gram page, section 7, has the full chart and how it was counted.

4When the training data says “I had an aunt who…”

Same kind of model as sections 1–3, different training data. Here the English follows how often English books use each construction: “a friend of mine” stays, but for aunts and brothers the English is rebuilt the way a translator would, “I had an aunt who sang”. The decoder writes I, had and who, words with no Galician counterpart, because its training data has them.

A. The data: 8 patterns, each with three verbs (cantaba sang, bailaba danced, ría laughed): 24 pairs; part C holds one out as development data

kept as English books have it

rebuilt, as a translator would

sections 1–3, their training dataunha tía miña → an aunt of minethis model's dataunha tía miña cantaba → I had an aunt who sang held out in part C

B. Training on all 24 pairs

Both runs start from the same random vectors and train the same way: all their training pairs at once, with a refinement of gradient descent that adapts the size of each step (Adam), 2000 steps, every 25th recorded. Section 1 showed how one pair is decoded and how an update moves the vectors; here only the outcome.

Training
verb shown:
(training uses all 24)

one output at a time, the most probable · the gold output · a different one · click a sentence: its words on the plane

Galician wordEnglish wordlabeled: the gold output's words, and the words the model writes (red: written, not in the gold output)- - - the encoder's path to the summary · arrow: the state at the output explained on the left

C. Training on 23 pairs: unha tía miña cantaba held out as development data

The same training without one pair. The held-out sentence is development data: the model never sees it, and we check what it writes for it. Its bailaba and ría versions, and un irmán meu cantaba (I had a brother who sang), are still trained on.

Training
verb shown:
(training uses 23 of the 24: all but the development sentence)

one output at a time, the most probable · the gold output · a different one · click a sentence: its words on the plane

Galician wordEnglish wordlabeled: the gold output's words, and the words the model writes (red: written, not in the gold output)- - - the encoder's path to the summary · arrow: the state at the output explained on the left