Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Encoder-Decoder Architecture: Understanding the Bottleneck

Introduction

In our previous lesson, we explored the mechanics of LSTMs and GRUs, establishing how their gating mechanisms create a "gradient highway" that successfully mitigates the vanishing gradient problem. This allows them to capture dependencies over much longer sequences than vanilla RNNs. With these powerful sequence modeling tools in our arsenal, we can now move from analyzing single sequences to tackling tasks that involve mapping one sequence to another.

This lesson introduces the encoder-decoder architecture, a foundational framework for such sequence-to-sequence (seq2seq) problems. We will explore how this model uses two separate recurrent networks—an encoder and a decoder—to handle inputs and outputs of variable lengths, a common requirement in tasks like machine translation or text summarization. Our primary focus will be to construct the architecture conceptually and mathematically, and then to identify its principal theoretical weakness: the information bottleneck, a limitation that directly motivates the more advanced mechanisms we will study next.

What are Sequence-to-Sequence Tasks?

Many important problems in AI and NLP can be framed as transforming an input sequence into a new output sequence. Critically, the input and output sequences often have different lengths and complex structural relationships, making a simple one-to-one mapping impossible.

Examples of such tasks include:

  • Machine Translation: Translating a sentence from a source language (e.g., English) to a target language (e.g., Spanish). The sentence lengths and word orders will almost certainly differ.
  • Text Summarization: Condensing a long article (input sequence) into a short headline or abstract (output sequence).
  • Dialogue Systems: Taking a user's query (input sequence) and generating a relevant response (output sequence).
  • Code Generation: Converting a natural language description (input sequence) into a piece of source code (output sequence).

The challenge is to create a model that can read and "understand" the entire input sequence before it begins generating the output. The encoder-decoder framework was the first highly successful neural approach to this problem.

10Machine Translation and Encoder-Decoder Models

The introductory section of this chapter from Jurafsky and Martin's textbook provides excellent context for why seq2seq models are necessary.

Please read the first section of the chapter, up to the start of Section 10.2. Focus on the examples given, such as the English-to-Japanese translation, which illustrate the non-trivial reordering and structural changes that a model must handle. This motivates the need for an architecture more sophisticated than a simple sequence labeler.

The Encoder-Decoder Architecture

The core idea of the seq2seq model is to use two RNNs (typically LSTMs or GRUs) working in concert: one to process the input and another to generate the output.

  1. The Encoder: Its job is to read the entire input sequence, one token at a time, and compress it into a single, fixed-size vector representation. This vector is often called the context vector or "thought vector."
  2. The Decoder: Its job is to take the context vector from the encoder and generate the output sequence, one token at a time. It functions as a conditional language model—it generates text conditioned on the initial context it received.

The following video from StatQuest provides a clear, step-by-step visual breakdown of this entire process.

Sequence-to-Sequence (seq2seq) Encoder-Decoder Neural Networks, Clearly Explained!!!

This StatQuest video, "Sequence-to-Sequence Encoder-Decoder Neural Networks, Clearly Explained!!!", breaks down the model's components and their interactions.

Please watch the following segments: The introduction, which defines seq2seq problems and the challenge of variable-length inputs and outputs. The encoder section, which details how an input sentence is processed through embedding and LSTM layers to produce a final context vector. The decoder section, which shows how this context vector initializes a separate decoder network that generates the output sequence.

Mathematical Formulation

Let's formalize the architecture described in the video. Given your background, the precise mathematical flow will be illuminating.

The Encoder
The encoder is an RNN that processes an input sequence . At each time step , it updates its hidden state based on the current input and its previous hidden state :

After processing the entire sequence up to the final token , the encoder's final hidden state, , becomes the context vector, .

If using an LSTM, this context would consist of both the final hidden state and the final cell state, . For simplicity, we'll refer to this entire state package as .

The Decoder
The decoder is another RNN tasked with generating the output sequence . It's a conditional language model that predicts the next token given the context and the sequence of previously generated tokens.

  1. Initialization: The decoder's initial hidden state, , is set to the context vector from the encoder.
  2. Generation: At each time step , the decoder updates its hidden state based on its previous hidden state and the previously generated output token, . A common variant also feeds the context vector as an input at every step of the decoder, not just at the beginning, to prevent its influence from waning:
  3. Output: The output token is then generated by passing the decoder's current hidden state through a linear layer followed by a softmax function over the target vocabulary.

The process begins with a special <start> token as the initial input and continues until the decoder generates an <end> token.

The Information Bottleneck

This elegant architecture has a significant conceptual flaw. The encoder must compress the entire meaning, grammar, and nuance of the input sequence—regardless of its length—into a single, fixed-size context vector . This requirement creates a severe information bottleneck.

This diagram illustrates the core problem of the basic encoder-decoder model. The single context vector must encapsulate the entire source sequence, creating a bottleneck that limits the information available to the decoder.

Imagine trying to summarize a long, complex paragraph into a single, short sentence and then asking someone to reconstruct the original paragraph perfectly using only that summary. Information will inevitably be lost. For the seq2seq model, this means that for long input sequences, the context vector may fail to capture details from the beginning of the sequence, leading to a degraded translation or summary.

The Stanford NLP lecture in the following resource provides an excellent and concise explanation of this problem.

Stanford CS224N NLP with Deep Learning | Winter 2021 | Lecture 7 - Translation, Seq2Seq, Attention

This segment from Stanford's CS224N lecture series directly addresses the bottleneck problem as the primary motivation for developing the attention mechanism.

Please watch the clip from the beginning. The lecturer clearly explains why forcing all information into a single hidden state acts as a bottleneck and harms performance, especially on long sequences.

This limitation was the primary driver for the next major innovation in sequence modeling. While the basic encoder-decoder model was a huge step forward, its inability to effectively handle long-range dependencies across the input-output sequence pair was a critical issue to be solved.

10Machine Translation and Encoder-Decoder Models

The Jurafsky and Martin textbook provides a written account of this same bottleneck problem, which serves as a great reinforcement of the video's explanation.

Read the first few paragraphs of Section 10.4, specifically the text from the introduction to the attention mechanism. It uses the term "bottleneck" and explains why a static context vector is insufficient.

Conclusion

In this lesson, we have constructed the foundational encoder-decoder architecture for sequence-to-sequence tasks. We established how it leverages two separate RNNs to manage variable-length inputs and outputs, a crucial capability for applications like machine translation.

Here are the key takeaways:

  • Seq2Seq tasks involve mapping an input sequence of length to an output sequence of length , where and can differ.
  • The encoder-decoder architecture consists of an encoder RNN that reads the input and compresses it into a fixed-size context vector, and a decoder RNN that uses this context vector to generate the output sequence.
  • The decoder acts as a conditional language model, generating the output conditioned on the information provided by the encoder.
  • The primary limitation of this architecture is the information bottleneck: the single, fixed-size context vector must encode the entire source sentence, which is often impossible for long and complex inputs, leading to information loss.

This very problem—the bottleneck—sets the stage perfectly for our next lesson. How can we allow the decoder to access more information from the encoder? Instead of forcing all information through a single vector, what if we allowed the decoder to "look back" at the entire input sequence at every step of the generation process? This is the core idea behind the attention mechanism, the topic of our next discussion.

This diagram contrasts the classical seq2seq model with the "emerging idea" of attention. Instead of a single context vector, the decoder can selectively consult all of the encoder's hidden states, overcoming the information bottleneck.

Can't find a good explanation? Sign up and we'll make it for you

Sign up