Sequence-to-sequence model

A sequence-to-sequence model, commonly abbreviated as seq2seq, is a class of machine-learning model that maps an input sequence to an output sequence. The two sequences can differ in length, and their elements need not belong to the same representational domain. Sequence-to-sequence modeling became a central framework for neural machine translation and subsequently influenced systems for document summarization, conversational response generation, speech recognition, and structured prediction.

The defining property of the framework is conditional sequence generation. Given an input sequence (x=(x_1,\ldots,x_m)), a model assigns a probability to an output sequence (y=(y_1,\ldots,y_n)):

[ p(y\mid x)=\prod_{t=1}^{n}p(y_t\mid y_{<t},x). ]

This factorization represents output generation as a succession of conditional predictions. Each prediction depends on the input and on the portion of the output that precedes it. Early neural implementations divided this computation between an encoder, which represented the input, and a decoder, which generated the output.

Historical development

Statistical approaches to sequence transduction preceded neural sequence-to-sequence models. Hidden Markov models, finite-state transducers, and phrase-based translation systems represented correspondences through explicitly structured latent variables or separately estimated components. Neural language models established that distributed representations could instead support probability estimation over sequences without requiring discrete phrase tables.

In 2014, Ilya Sutskever, Oriol Vinyals, Quoc V. Le, and You Watanabe presented an encoder–decoder system based on multilayer long short-term memory networks. Their model encoded a source sentence into a fixed-dimensional state and decoded a target sentence from that state. The study also examined reversal of source-word order, which reduced the effective distance between corresponding early elements of the source and target sequences during optimization. This architecture demonstrated that a comparatively uniform neural system could perform large-vocabulary translation without the modular pipeline used by contemporary phrase-based systems.

A related architecture introduced by Kyunghyun Cho, Bart van Merriënboer, Dzmitry Bahdanau, and Yoshua Bengio employed a gated recurrent encoder and decoder. That work introduced the gated recurrent unit and analyzed the use of continuous sentence representations within a statistical translation system. Both lines of research established the encoder–decoder formulation as a general mechanism for conditional generation.

The fixed-state interface created an information bottleneck because the complete input had to be represented by a single vector. Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio addressed this limitation with an alignment mechanism that computed a separate context vector at each decoding step. This mechanism became known as neural attention. Rather than relying exclusively on the encoder’s final state, the decoder assigned variable weights to encoder states according to their relevance to the current prediction.

Subsequent sequence-to-sequence systems increasingly replaced recurrence with attention-based computation. The Transformer, introduced by Ashish Vaswani and colleagues in 2017, retained the encoder–decoder organization while using self-attention to represent dependencies within each sequence. Transformers later became the predominant architecture for large-scale sequence modeling, although decoder-only and encoder-only variants no longer conform to the complete original seq2seq arrangement.

Encoder–decoder formulation

In a recurrent sequence-to-sequence model, the encoder updates a hidden state as it processes the input:

[ h_i=f_{\mathrm{enc}}(h_{i-1},x_i). ]

A basic fixed-context model derives a context vector (c) from the final encoder state. The decoder then updates its state according to the preceding output representation and the context:

[ s_t=f_{\mathrm{dec}}(s_{t-1},y_{t-1},c). ]

A normalized output layer converts the decoder state into a probability distribution over the output vocabulary:

[ p(y_t\mid y_{<t},x)=\operatorname{softmax}(Ws_t+b). ]

This formulation separates the representation of the observed sequence from the autoregressive construction of the predicted sequence. The separation permits the input and output to follow different tokenization systems. It also permits their lengths to vary without requiring a predetermined element-by-element correspondence.

The decoder continues until it generates a designated end-of-sequence symbol. The model therefore defines a distribution over both output content and output length. Length is not represented by an independent regression variable; it emerges from the probability assigned to continuation and termination at each decoding step.

Attention and alignment

An attention-based decoder replaces the single context vector with a time-dependent context (c_t). For each decoder step, an alignment function produces a score between the current decoder state and each encoder state:

[ e_{t,i}=a(s_{t-1},h_i). ]

Normalization across input positions produces attention weights:

[ \alpha_{t,i}= \frac{\exp(e_{t,i})} {\sum_j\exp(e_{t,j})}. ]

The corresponding context vector is a weighted combination of encoder states:

[ c_t=\sum_i\alpha_{t,i}h_i. ]

The weights form a differentiable alignment between source positions and decoder steps. They are computed as internal model variables rather than supplied as externally labeled correspondences. Attention reduces the dependence on a single fixed-dimensional summary, but it does not guarantee that the resulting weights constitute a unique causal explanation of a prediction.

In Transformer encoders, self-attention relates each input position to other positions in the same sequence. Transformer decoders apply masked self-attention so that a position cannot access later output tokens during autoregressive training. Cross-attention then connects decoder representations to encoder representations. Positional encoding supplies information about order because attention itself does not impose recurrence.

Learning

Sequence-to-sequence models are generally trained by minimizing the negative log-likelihood of reference outputs:

[ \mathcal{L}(\theta)

-\sum_{(x,y)} \sum_{t=1}^{n} \log p_\theta(y_t\mid y_{<t},x). ]

During this computation, the decoder conditions on the reference prefix rather than on its own earlier predictions. This convention is known as teacher forcing. It permits parallel evaluation of decoder positions in Transformer systems and provides direct likelihood gradients for every reference token.

At inference time, the reference prefix is unavailable, so each generated token becomes part of the conditioning context for later predictions. The difference between the training and inference contexts can propagate an early error through the remainder of the output. This phenomenon is commonly described as exposure bias, although likelihood training remains the standard probabilistic formulation for autoregressive sequence models.

Optimization uses backpropagation, with recurrent architectures applying the procedure through the sequence of hidden states. Long short-term memory units and gated recurrent units moderate the vanishing-gradient behavior of elementary recurrent networks. The source-reversal experiments associated with Sutskever and collaborators addressed the related problem of long computational paths between corresponding source and target elements.

Transformer models shorten these paths through direct attention connections. Their training cost instead depends strongly on the number of pairwise position interactions, which grows quadratically with sequence length under full attention. Sparse-attention and linear-attention methods alter this interaction structure, although they preserve the broader conditional sequence-modeling objective.

Decoding

Exact maximization of (p(y\mid x)) is generally intractable because the number of possible output sequences grows exponentially with length. Autoregressive systems consequently use approximate search. Greedy decoding selects the highest-probability token at each step, while beam search retains several partial hypotheses and repeatedly extends them.

The highest-likelihood sequence under an imperfect model does not necessarily correspond to the output preferred under a task-specific evaluation measure. Beam search can also favor sequences whose termination probabilities interact with accumulated log-probability in undesirable ways. Length normalization modifies the comparison among hypotheses by accounting for the number of generated tokens, while coverage-based terms represent whether relevant input positions have received sufficient attention.

Sampling provides a different approximation to the conditional distribution. Instead of retaining only high-scoring continuations, it draws each token from a model-derived distribution. Temperature scaling changes the concentration of that distribution, and restricted sampling methods remove portions of its low-probability tail. These procedures alter decoding behavior without changing the parameters learned by the underlying model.

Applications and evaluation

Neural machine translation provides the canonical sequence-to-sequence setting because a sentence in one language is mapped to a sentence in another language. The framework also applies to abstractive summarization, where a long document conditions a shorter textual representation, and to speech recognition, where a sequence of acoustic observations conditions a sequence of linguistic symbols. In each case, the model learns a conditional relationship rather than a fixed correspondence between positions.

Evaluation depends on the structure of the application. Translation systems have commonly used BLEU, which measures modified n-gram precision with a brevity penalty. Summarization research has frequently used ROUGE, which measures overlap with reference summaries. Likelihood-based evaluation instead measures how much probability the model assigns to observed sequences, but it does not directly determine whether a generated output satisfies semantic or task-level criteria.

Sequence-to-sequence performance depends on the representation of sequence elements. Subword tokenization reduces the need for a fixed vocabulary containing every complete word, while byte-level representations provide a more general alphabet at the cost of longer sequences. The chosen representation changes sequence length, the distribution of rare elements, and the computational burden placed on the encoder and decoder.

Limitations

Autoregressive decoding imposes a sequential dependency between generated positions, even when the internal architecture supports parallel computation during training. This dependency increases inference latency for long outputs. Non-autoregressive models generate multiple positions simultaneously, but they must represent uncertainty about output length and about dependencies among target elements through alternative mechanisms.

A sequence-to-sequence model can assign high probability to fluent output that is not supported by the input. This behavior follows from the interaction between conditional evidence and patterns learned from the output distribution. In summarization and dialogue systems, unsupported content is often categorized as hallucination. Architectural attention does not by itself eliminate this behavior because attention weights constrain information flow only through learned numerical interactions.

Long inputs also create representational and computational difficulties. Recurrent models compress earlier information through repeated state transitions, whereas full-attention models construct pairwise relationships among positions at substantial memory cost. Hierarchical encoders, retrieval-augmented systems, and restricted-attention architectures modify the context mechanism, but each changes which dependencies can be represented directly.

See also