- Let’s first talk about what problems they solve
- Let’s talk about what the structure looks like
- Let’s talk about how to implement it in Transformer
- Finally, let’s talk about which parts of BERT and GPT are used respectively.
1. Why do we need Encoder and Decoder?
When doing "machine translation" in the early days, researchers faced a problem:
How to change one sentence into another sentence?
For example:
Chinese:
I like learning artificial intelligence
English:
I like studying artificial intelligence
This is actually a two-step process:
1️⃣ First “understand” the original sentence 2️⃣ Then "generate" the target sentence
So there is:
Encoder → 负责理解
Decoder → 负责生成2. What is Encoder?
✅ Encoder = understanding module
Its mission is:
Turn the input sentence into a set of "semantic representations"
For example, enter:
我 喜 欢 学 习Encoder will output:
[向量1] [向量2] [向量3] [向量4]These vectors are not ordinary numbers.
They are:
Semantic encoding containing contextual information
means:
- "Like" will know that the subject is "I"
- "Learning" will know that the preceding word is "like"
Features of Encoder
- Enter all sentences
- Every word can see all words (Self-Attention)
- Output a set of "context enhancement vectors"
Simply put:
Encoder is responsible for "understanding the entire sentence"
3. What is Decoder?
✅ Decoder = Generate module
Its mission is:
Generate output word by word based on existing information
For example, when translating:
The first step generates:
IThe second step generates:
I likeThe third step generates:
I like studyingKeep generating until the end of the sentence.
Features of Decoder
- Only words that have been generated previously can be seen
- Can't see the future
- Predict the next word at each step
This is called:
Auto-regressive generation (Auto-regressive)
4. Encoder/Decoder in Transformer
Representative papers:
Attention Is All You Need
The original structure of Transformer is:
Encoder 堆叠 N 层
Decoder 堆叠 N 层Each layer of Encoder contains:
- 1️⃣ Self-Attention
- 2️⃣ Feedforward neural network
Function:
Establish relationships between words
Each layer of Decoder contains:
- 1️⃣ Masked Self-Attention (can only see the front)
- 2️⃣ Cross-Attention (see Encoder output)
- 3️⃣ Feedforward neural network
Here comes the point:
What is Cross-Attention?
When the Decoder is generated, it will:
- Look at the content you have generated
- Also refer to the "Understanding Results" given by Encoder
So:
Encoder provides understanding Decoder is responsible for expressing
5. Three common structural types
Now we explain the three variations.
1️⃣ Encoder-only model
Representative:
BERT: Pre-training of Deep Bidirectional Transformers
Structure:
只用 EncoderPurpose:
- Text classification
- sentiment analysis
- Search sorting
Why?
Because it only requires:
Understand, no need to generate
2️⃣ Decoder-only model
Representative:
GPT-4 From: OpenAI
Structure:
只用 DecoderWhy?
Because it treats "input content" as generated content.
For example:
Input:
问题:什么是AI?
回答:Then start generating.
Its essence is:
Always predicting the next word
So GPT does not require an Encoder.
3️⃣ Encoder-Decoder model
Representative:
T5
From: Google
Structure:
完整 TransformerCommonly used for:
- Translate
- Summary
- Complex text conversion
6. An intuitive analogy
Imagine you hire a translator:
Encoder is:
After listening to Chinese, I completely understand the meaning.
Decoder is:
Re-express it in English.
If it is BERT:
There are only "understanders".
If it's GPT:
There are only "writers".
If it is T5:
Write after listening.
7. Let’s talk about another key point: Why does GPT not need an Encoder?
Because what GPT does is:
conditional language generation
The input content is already in front.
For example:
翻译:我爱你 →The model only needs to predict:
I love youSo:
The input itself is equivalent to "implicit encoding"
8. Summarize in one sentence
Encoder:
Responsible for understanding global semantics
Decoder:
Responsible for generating text in sequence
BERT:
only understand
GPT:
Only generate
T5:
Understand + generate