Transformer Architecture Deep Dive: The Engine of AI | Transformer 架构详解:AI 的引擎¶
The Revolution Before 2017, AI was like a student reading a book word-by-word (RNNs). It often forgot the beginning of the sentence by the time it reached the end. Then came the Transformer. It reads the entire book at once. 革命 2017 年之前,AI 就像一个逐字阅读的学生(RNN)。读到句尾时,它经常忘记句首。 然后 Transformer 出现了。它一次性读完整个句子。
1. The Core Magic: Self-Attention | 核心魔法:自注意力机制¶
1.1 The "Cocktail Party" Effect | “鸡尾酒会”效应¶
Imagine you are at a noisy party. 想象你在一个嘈杂的派对上。 - You hear 100 people talking. - But when your friend says "Hey, look at that cat!", your brain instantly focuses on the word cat and links it to the furry animal in the corner. - You ignore the background noise.
Self-Attention does exactly this. When the model reads the word "Bank", it looks at the whole sentence to decide if it means "River Bank" (河岸) or "Money Bank" (银行). 自注意力就是做这个的。当模型读到“Bank”这个词时,它会看整个句子来决定它是指“河岸”还是“银行”。
1.2 The Mechanics: Q, K, V | 机制:Q、K、V¶
How does it know what to focus on? It uses a "Dating App" system. 它怎么知道关注什么?它使用一套“相亲应用”系统。
Every word has 3 vectors (Profiles): 每个词有 3 个向量(档案):
- Query (Q): What I am looking for. (e.g., "I am a pronoun looking for my noun").
查询:我在找什么。(例如,“我是代词,我在找我的名词”)。 - Key (K): What I can offer. (e.g., "I am a male noun").
键:我能提供什么。(例如,“我是阳性名词”)。 - Value (V): My actual content. (e.g., "The word is 'King'").
值:我的实际内容。(例如,“这个词是 King”)。
The Process: 1. Word A broadcasts its Query. 2. It checks the Keys of all other words. 3. If Q and K match (High Score), it absorbs the Value.
2. Multi-Head Attention: The "Council of Wizards" | 多头注意力:“巫师议会”¶
Why settle for one perspective? 为什么要满足于一个视角?
- Head 1 (The Grammarian): Focuses on grammar (Subject -> Verb).
- Head 2 (The Historian): Focuses on time (Yesterday -> Was).
- Head 3 (The Poet): Focuses on rhyme and emotion.
The Transformer has many heads (e.g., GPT-3 has 96 heads!). They look at the sentence from different angles and combine their wisdom. Transformer 有很多头(例如 GPT-3 有 96 个头!)。它们从不同角度看句子并汇聚智慧。
graph TD
Input[Input: 'The animal didn't cross the street because it was too tired']
Head1[Head 1: Focuses on 'it' -> 'animal']
Head2[Head 2: Focuses on 'tired' -> 'street' (Low score)]
Head3[Head 3: Focuses on 'tired' -> 'animal' (High score)]
Head1 --> Concat[Combine Insights]
Head2 --> Concat
Head3 --> Concat
Concat --> Output[Understanding: 'it' refers to 'animal']
3. Positional Encoding: The "Ticket Number" | 位置编码:“票号”¶
Unlike humans, the Transformer sees all words at the exact same time. It doesn't know that "Man bites Dog" is different from "Dog bites Man". 与人类不同,Transformer 同时看到所有单词。它不知道“人咬狗”和“狗咬人”的区别。
Solution: We give each word a ticket number. 解决方案:我们给每个词一个票号。 - "Man" + [Position 1] - "Bites" + [Position 2] - "Dog" + [Position 3]
Now the math changes depending on the order! 现在数学运算会根据顺序而改变!
4. The Architecture: Encoder & Decoder | 架构:编码器与解码器¶
4.1 Encoder (The Reader) | 编码器(阅读者)¶
- Job: Read the input and understand it perfectly.
- Analogy: A detective reading a crime scene and taking notes.
- Used in: BERT (Search engines, Classification).
4.2 Decoder (The Writer) | 解码器(写作者)¶
- Job: Generate the next word based on the notes.
- Analogy: A storyteller writing the next chapter.
- Used in: GPT (Chatbots, Writing).
5. Scientist's Corner | 科学家角落¶
Why is Transformer better than RNN? (Parallelism) - RNN: Must read step 1 before step 2. Like a relay race. Slow. - Transformer: Can process all words at once. Like a flash mob. Fast.
The Cost: $O(N^2)$ Complexity Because every word looks at every other word, if you double the text length, the computation goes up 4x. This is why LLMs have a "Context Window" limit. Reading a whole book at once is expensive! 代价:$O(N^2)$ 复杂度 因为每个词都要看其他所有词,如果你把文本长度加倍,计算量会增加 4 倍。 这就是为什么 LLM 有“上下文窗口”限制。一次读完一整本书是很贵的!
6. Practice Mission: Be the Attention Mechanism | 练习任务:扮演注意力机制¶
Objective (目标): Manually calculate which word the AI should focus on. 目标:手动计算 AI 应该关注哪个词。
Scenario (场景): You are the AI. You are reading the sentence: "The animal didn't cross the street because it was too tired." 你是 AI。你正在读这句话: "The animal didn't cross the street because it was too tired."
Task (任务): You are currently processing the word "it". You need to figure out what "it" refers to. 你正在处理单词 "it"。你需要弄清楚 "it" 指的是什么。
- Identify Candidates (识别候选者):
- Candidate A: "animal"
- Candidate B: "street"
- Context Clue (上下文线索):
- The adjective is "tired".
- Logic Check (逻辑检查):
- Can an "animal" be tired? (Yes/No)
- Can a "street" be tired? (Yes/No)
- Assign Attention Scores (分配注意力分数):
- Give a score (0-100) to "animal".
- Give a score (0-100) to "street".
Answer Key (答案): 1. Logic: Animals get tired. Streets do not. 2. Scores: * Attention(it -> animal) = 99% (High Attention) * Attention(it -> street) = 1% (Low Attention) 3. Result: The vector for "it" will be updated with the meaning of "animal". * Note: If the sentence was "because it was too wide", the attention would flip to "street"!