← NLP Recommended Papers
NLP · Recommended Papers · Seq2Seq

Sequence to Sequence Learning with Neural Networks

Translating a sentence requires more than predicting one label. The output may have a different length, a different word order, and an endpoint that must be predicted. An encoder–decoder separates reading the source from writing that output.

The paper’s design. Separate deep LSTMs encode the source and generate the target through a fixed-dimensional source summary. The system reverses source words, leaves target order intact, and decodes with beam search. Original paper, §§2–3.

This companion follows red square → carré rouge. We use an original, hand-set probability table to make training and search inspectable. The numbers are not outputs from a fitted LSTM, and the example is not a reproduction of the paper’s translation system.

A fixed-width state is different from a fixed-length input

The feedforward language-model companion uses a fixed number of previous words. With a two-word window, “the red square” predicts its next word from the explicit inputs “red square”; “the” is outside that window. Reusing the predictor allows generation to continue, but does not enlarge its context window.

A recurrent network instead updates a state as each token arrives. For a source of length $T$, write $e(x_t)$ for the token embedding and $s_t$ for the recurrent state:

$$s_t=\operatorname{EncoderStep}(e(x_t),s_{t-1}).$$

The same encoder parameters process every source position. A longer input means more applications of that update, not a wider input vector. The final state has fixed size, but depends on the entire sequence of updates. That permits information from earlier positions to affect the result; it does not guarantee that every earlier detail is retained accurately.

The decoder has its own parameters and recurrent state. Initialize it from the encoder summary, consume a beginning-of-sequence marker, and predict the first target token. Continue with target-side inputs until the model produces <EOS>. Our diagrams use explicit <BOS> and <EOS> markers for this interface.

Figure 1 · Read the whole source, then write the target
Source words, in their ordinary orderred · square

The desired output is carré · rouge. Output order need not match input order.

Encoder · reuse one recurrent updatesquare → red → <EOS>

Update the encoder state after each input. There is no French prediction paired with each English step.

Source summary · fixed dimensionality

For an LSTM, recurrent state includes hidden activations and cell memory. The decoder receives source information through this interface.

Decoder · one distribution per target position<BOS> → score carré
carré → score rouge
rouge → score <EOS>

This is the reference-prefix path. During generation, the model supplies its own previous token instead.

The arrows represent computation and information flow, not word alignments. Source reversal changes only the order presented to the encoder. The target remains carré rouge. A longer target simply requires more decoder updates.
What is inside an LSTM state?

An LSTM carries a cell-memory vector $c_t$ and a hidden-output vector $h_t$. The forget, input and output gates $f_t,i_t,o_t$ control retention, writing and exposure; $g_t$ is a candidate update. For a standard LSTM,

$$c_t=f_t\odot c_{t-1}+i_t\odot g_t,\qquad h_t=o_t\odot\tanh(c_t).$$

The symbol $\odot$ means elementwise multiplication. Gates depend on the current input and previous hidden state. Stacked layers carry their own states. Thus “pass the state” is more precise than imagining one scalar, or discarding the cell memory when wiring an implementation. The PyTorch LSTM specification gives the complete update and state shapes.

Specify all the choices in a tiny decoder

Hold the source red square fixed. Let the following table stand in for the decoder’s next-token distributions. Omitted tokens have probability zero. After either two-word output, this toy always emits EOS; this restriction keeps every path finite and equally long.

Constructed conditional probabilities for one source
Target prefix already suppliedNext-token distribution
Empty prefixcarré: 0.40 · cercle: 0.60
carrérouge: 0.90 · bleu: 0.10
cerclerouge: 0.50 · bleu: 0.50
Any permitted two-word prefix<EOS>: 1.00

carré means square; cercle means circle. This model prefers the wrong first word locally. That lets us examine a real distinction: scoring a supplied correct translation is not the same operation as generating a translation from scratch.

For a target $y_1,\ldots,y_L$, including its final EOS, multiply the probability of each token conditioned on the source $x$ and the preceding target prefix:

$$p(y\mid x)=\prod_{t=1}^{L}p(y_t\mid x,y_{<t}).$$

The probability of the reference carré rouge <EOS> is 0.40 × 0.90 × 1.00 = 0.36. Its negative log probability is $-\ln(0.36)\approx1.0217$. Every factor is conditional on its own prefix; using the probability of rouge after cercle would score a different path.

Training supplies the reference prefix; generation does not

During teacher forcing, the decoder input is the previous reference token. To score the next token, use inputs [BOS, carré, rouge] and labels [carré, rouge, EOS]. The one-position shift matters: the current label is not fed into its own prediction.

At step 1 our model assigns only 0.40 to carré and prefers cercle. Nevertheless, the teacher-forced second step receives carré. A greedy generation run instead chooses cercle at step 1 and supplies cercle at step 2. The distribution for the next word changes with that prefix. The PyTorch decoder tutorial shows this input switch explicitly.

Figure 2 · Same probability table, different input histories
Step 1

PrefixEmpty

Input now<BOS>

Highest-probability tokencercle

Reference token scoredcarré · 0.40

Step 2

Prefixcarré

Input nowcarré

Highest-probability tokenrouge

Reference token scoredrouge · 0.90

Step 3

Prefixcarré rouge

Input nowrouge

Highest-probability token<EOS>

Reference token scored<EOS> · 1.00

Teacher forcing scores carré rouge <EOS> with probability 0.36. It does not choose the most probable first token.
No weights change when switching these views. Teacher forcing describes the prefix used to evaluate a supervised example; an optimizer would then use the resulting gradients. Greedy generation feeds back an argmax token. Ties in this toy choose rouge before bleu.

A training update differentiates the reference log probabilities through the decoder, the source summary, and the encoder. It does not need to differentiate through greedy choices. Evaluating reference likelihood also uses the known prefix, so it can score translations the model would not generate. Report generation quality separately.

Generation stops on EOS. A real implementation also needs a maximum length in case EOS never appears; reaching that cap means truncation, not a successfully predicted endpoint. Padded positions used to batch examples are bookkeeping and should not become supervised target words.

Beam search changes the search, not the learned probabilities

Greedy decoding keeps only cercle, because 0.60 exceeds 0.40. Its next choice is rouge under our tie rule, giving 0.60 × 0.50 = 0.30. It has already discarded the carré branch, whose best continuation would have scored 0.36.

A width-two beam keeps both first-word prefixes. Expand each with the next word, multiply prefix and conditional probabilities, then retain the two best extended prefixes. In a real decoder, each branch also needs its own recurrent state; sharing one state across different histories would evaluate the wrong distributions.

Figure 3 · A weaker first step can lead to a stronger full sequence

After the first word

cercle0.60 · kept
carré0.40 · kept

Expand the retained prefixes

carré rouge0.36 · kept
cercle rouge0.30 · kept
cercle bleu0.30 · pruned
carré bleu0.04 · pruned
Width 2 returns carré rouge <EOS> with probability 0.36. Every surviving path then adds EOS with probability 1.
Green borders mark retained prefixes; dashed borders mark discarded ones. Width 1 never expands carré, so its second-step candidate list is smaller. These are products from the stated table, not BLEU scores. All toy paths have equal length, so no length normalization is involved.

Use sums of log probabilities in a numerical implementation to avoid multiplying many small numbers. A finite beam remains an approximation: a promising prefix can be pruned before its strong continuation appears. Here width two finds the best of all four possible outputs because we can enumerate them and check; that is not a general guarantee.

Training fits the conditional distributions. Beam search explores those fitted distributions after training. Better search can increase model probability without improving the actual translation if the model ranks a bad sentence highly. Likewise, a larger beam cannot recover an unknown word missing from the decoder vocabulary.

Reverse the source, not the target or translation direction

For our example, source reversal feeds square red to the encoder while the target stays carré rouge. It does not mean French-to-English translation, reversing character spellings, or feeding target words backward. Apply the same source transformation during training and evaluation.

Why could it help? Consider a separate, simplified alignment example: four source words $x_1,\ldots,x_4$ correspond in order to four targets $y_1,\ldots,y_4$. Place their word-processing positions on one timeline. Ignore boundary markers for this calculation, and define a gap as target position minus source position.

Figure 4 · Earlier target words can get shorter pathsForward source x1 x2 x3 x4 followed by y1 y2 y3 y4 gives gaps 4, 4, 4, 4. Reversed source x4 x3 x2 x1 gives gaps 1, 3, 5, 7. Both have mean 4; the first pair becomes closer.
Original positional calculation, assuming one-to-one monotonic alignment. The orange arc highlights x1 → y1. The average gap stays 4 while the minimum falls from 4 to 1. Boundaries would add a common offset here; real translation alignments need not be monotonic.

Before reversal, $y_1$ is at position 5 and $x_1$ at position 1, so their gap is 4. After reversal, $x_1$ is at position 4, so the gap is 1. The later pairs become farther apart: the last gap rises to 7. “Every dependency becomes shorter” would therefore misdescribe even this simple example.

This diagram illustrates the short-dependency explanation discussed in the paper’s §3.3; it is not a gradient calculation or a proof of improved translation. A recurrent update is order-sensitive, so reversal also changes the states themselves. The idea needs evaluation for a particular model and dataset.

Keep direct translation separate from reranking

The following are the paper’s WMT’14 English→French test results, using its tokenized, cased BLEU setup. “Reversed” describes source order.

Selected results from Tables 1–2
System and decoding routeBLEU
Phrase-based SMT baseline33.30
Single forward LSTM · direct · beam 1226.17
Single reversed LSTM · direct · beam 1230.59
Five reversed LSTMs · direct · beam 1234.81
Five reversed LSTMs · rerank SMT’s 1,000-best list36.50

The 34.81 result uses five models, not one. The 36.50 result chooses among SMT-generated candidates by combining scores; it is not standalone neural generation. The single reversed model remains below the SMT baseline. Tables 1–2 and §§3.2, 3.6 provide the comparison.

What the fixed summary makes us ask next

In Figure 1, every source detail that influences decoding must pass through the final encoder state. The decoder cannot return to an earlier source position through a separate lookup. That makes a useful diagnostic question: when the output omits or confuses a detail, was the source information represented, retained and used?

The attention companion changes that interface by making source-position representations available during each decoding step. It still distinguishes supplied reference prefixes from generated prefixes. The subword companion addresses a separate issue: representing words outside a whole-word vocabulary. Neither change should be confused with widening a beam.

The standard-library reproduction script checks every constructed path, teacher-forced versus greedy inputs, both beam widths, and the reversal gaps. It writes the interactive figure data and the reversal SVG:

python3 blog/nlp/recommended-papers/sutskever_seq2seq_demo.py

To build an actual neural implementation, first verify the shifted decoder inputs, EOS handling, per-example state reset, source preprocessing, and padding mask on a tiny paired dataset. Then check whether it can fit that dataset and generate its targets without teacher forcing. Our probability table isolates the inference mechanics; it does not validate an encoder, LSTM gradients, or translation training.

References and further reading