Sequence to sequence

Encoder–decoder · pattern · Text, Token sequence → Text, Token sequence

An encoder reads the whole input sequence; a decoder writes an output sequence of a different length from it.

Also called Seq2seq, Encoder–decoder RNN, Translation model

Figure 1. The Sequence to sequence as a schematic: the parts it is built from and how data flows between them. Select a part to read about it.

What it does

Sequence-to-sequence models map one sequence to another when the two have different lengths, such as a Chinese sentence to an English one. The encoder reads the source and produces either a final state (in the original recurrent version) or a vector per source token. The decoder starts from a start token and generates one output token at a time, conditioned on what it has generated and on the encoder output; attention lets it look back at the relevant source tokens at each step. Modern versions use transformer encoders and decoders, but the two-part structure is the same.

Formula

M = Encoder(x1, …, xS)p(yt | y<t, x) = softmax(W · Decoder(y<t, M) + b)stop when yt = end-of-sentence

Shapes and parameters

Shapes
Source ids [N, S] → memory [N, S, D] or a state [N, H]; target ids [N, T] → logits [N, T, V].
Parameters
Two embedding tables, the encoder, the decoder and the vocabulary projection. The recurrent example has about 13.4 million, mostly in the 8,000-token tables and projection.
Used for
The OPUS Chinese-to-English translator in the catalog (as a transformer encoder and decoder exported as two ONNX files), summarization, and older speech recognizers.

In WeightSpace

The translation model is edited at the decoder’s per-token output bias: making a standalone “is”, an apostrophe or the end-of-sentence token more or less likely changes word choice and sentence length.