AI Dictionary of Terms

Encoder-Decoder

A neural network architecture consisting of two main components — an Encoder that processes input data into a compressed representation, and a Decoder that generates output from that representation — widely used for sequence-to-sequence tasks like translation, summarization, and question answering.

The Simple Version

Imagine you’re translating a book from English to French. You need two skills:

  1. Understanding the English text (reading comprehension)
  2. Writing the French translation (generation)

An encoder-decoder architecture works the same way. The Encoder reads and understands the input (like English text), compressing it into a compact representation. The Decoder then takes that representation and generates the output (like French text).

This architecture is perfect for tasks where you need to transform one sequence into another: translate languages, summarize documents, convert speech to text, or answer questions.

Detailed Explanation

The encoder-decoder architecture separates the tasks of understanding and generation, allowing each component to specialize.

The Encoder:

The Decoder:

Architecture Variants:

1. RNN-based Encoder-Decoder:

2. Transformer Encoder-Decoder:

3. Encoder-Only (BERT-style):

4. Decoder-Only (GPT-style):

Key Components:

Attention Mechanisms:

Training:

Applications:

Key Characteristics

Business Context

Encoder-decoder architectures power many enterprise AI applications:

Enterprise Applications:

Strategic Benefits:

Model Selection:

Popular Encoder-Decoder Models:

Real-World Analogy

A translator working at the United Nations. The translator listens to a speech in one language (encoder processes input), understands the meaning and context, then speaks the translation in another language (decoder generates output). The encoder and decoder work together seamlessly, with the translator’s understanding (the compressed representation) bridging the two languages.

Code Example

# Encoder-Decoder model using Hugging Face Transformers (T5)
from transformers import T5Tokenizer, T5ForConditionalGeneration

# Load pre-trained T5 model (encoder-decoder architecture)
model_name = "t5-small"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)

# Task 1: Translation (English to German)
input_text = "translate English to German: The house is wonderful."
inputs = tokenizer(input_text, return_tensors="pt")

outputs = model.generate(
    **inputs,
    max_new_tokens=50,
    num_beams=4,  # Beam search for better quality
    early_stopping=True
)
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Translation: {translation}")
# Output: "Das Haus ist wunderbar."

# Task 2: Summarization
input_text = """summarize: The tower is 324 metres (1,063 ft) tall, about the same height
as an 81-storey building, and the tallest structure in Paris. Its base is square,
measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower
surpassed the Washington Monument to become the tallest man-made structure in the world."""

inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
outputs = model.generate(
    **inputs,
    max_new_tokens=50,
    num_beams=4,
    early_stopping=True
)
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Summary: {summary}")
# Output: "The Eiffel Tower is 324 metres tall, the tallest structure in Paris."

# Task 3: Question Answering
input_text = "question: What is the capital of France? context: France is a country in Europe. Its capital is Paris, a major center for art, fashion, and cuisine."
inputs = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=20)
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Answer: {answer}")
# Output: "Paris"

Common Misconceptions

Sources & Further Reading