Transformer 与大语言模型:第2章 Transformer 总体架构

第2章 Transformer 总体架构

本章目标:

从整体上理解 Transformer 的结构,知道每个模块负责什么,以及为什么 GPT 只有 Decoder,BERT 只有 Encoder。


2.1 Transformer 到底是什么?

Transformer 是一种神经网络架构(不是某个具体模型),最初用于机器翻译。

例如:

text 复制代码
输入:I love AI
输出:我爱AI

Transformer 的核心思想只有一句话:

不要让信息一步一步传递,而是让每个 Token 直接从所有 Token 中拉取需要的信息。


2.2 为什么叫 Transformer?

Transformer 的名字来自它的功能:

Transform(变换)一个序列的表示。

输入是一串 Token 的向量,输出也是一串向量,但每个向量已经包含了整个句子的上下文信息。


2.3 Transformer 架构演进时间线

#mermaid-svg-cyl6IfqbgGW8f1eW{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-cyl6IfqbgGW8f1eW .error-icon{fill:#552222;}#mermaid-svg-cyl6IfqbgGW8f1eW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cyl6IfqbgGW8f1eW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cyl6IfqbgGW8f1eW .marker.cross{stroke:#333333;}#mermaid-svg-cyl6IfqbgGW8f1eW svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cyl6IfqbgGW8f1eW p{margin:0;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge{stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .section--1 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section--1 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section--1 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section--1 text{fill:#ffffff;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth--1{stroke-width:17;}#mermaid-svg-cyl6IfqbgGW8f1eW .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:#ffffff;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-0 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-0 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-0 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-0 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-0{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-0{stroke-width:14;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-1 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-1 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-1 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-1 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-1{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-1{stroke-width:11;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-2 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-2 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-2 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-2 text{fill:#ffffff;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-2{stroke-width:8;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:#ffffff;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-3 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-3 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-3 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-3 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-3{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-3{stroke-width:5;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-4 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-4 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-4 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-4 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-4{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-4{stroke-width:2;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-5 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-5 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-5 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-5 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-5{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-5{stroke-width:-1;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-6 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-6 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-6 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-6 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-6{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-6{stroke-width:-4;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-7 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-7 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-7 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-7 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-7{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-7{stroke-width:-7;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-8 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-8 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-8 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-8 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-8{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-8{stroke-width:-10;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-9 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-9 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-9 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-9 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-9{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-9{stroke-width:-13;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-10 rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-10 path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-10 circle,#mermaid-svg-cyl6IfqbgGW8f1eW .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-10 text{fill:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .node-icon-10{font-size:40px;color:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .edge-depth-10{stroke-width:-16;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cyl6IfqbgGW8f1eW .lineWrapper line{stroke:black;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled circle,#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:lightgray;}#mermaid-svg-cyl6IfqbgGW8f1eW .disabled text{fill:#efefef;}#mermaid-svg-cyl6IfqbgGW8f1eW .section-root rect,#mermaid-svg-cyl6IfqbgGW8f1eW .section-root path,#mermaid-svg-cyl6IfqbgGW8f1eW .section-root circle{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-cyl6IfqbgGW8f1eW .section-root text{fill:#ffffff;}#mermaid-svg-cyl6IfqbgGW8f1eW .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-cyl6IfqbgGW8f1eW .edge{fill:none;}#mermaid-svg-cyl6IfqbgGW8f1eW .eventWrapper{filter:brightness(120%);}#mermaid-svg-cyl6IfqbgGW8f1eW :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 2017 Google 发布原始Transformer Encoder-Decoder架构 用于机器翻译 2018 OpenAI 发布 GPT-1(Decoder-Only) Google 发布 BERT(Encoder-Only) 2019 GPT-2 (1.5B 参数) T5(Encoder-Decoder回归) 2020 GPT-3 (175B 参数,96层) 2023 Llama 1/2 (Meta) Qwen (阿里巴巴) 2024 Llama 3, Qwen2,DeepSeek-V2 全部 Decoder-Only +MoE Transformer 架构演进

趋势:2018年之后,生成类任务(对话、写作)统一走向 Decoder-Only;理解类任务(搜索、分类)仍然使用 Encoder-Only。


2.4 Transformer 的整体结构

原始 Transformer(2017 年论文)由两部分组成:
#mermaid-svg-bOPzajaNonCz8xrf{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-bOPzajaNonCz8xrf .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-bOPzajaNonCz8xrf .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-bOPzajaNonCz8xrf .error-icon{fill:#552222;}#mermaid-svg-bOPzajaNonCz8xrf .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bOPzajaNonCz8xrf .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-bOPzajaNonCz8xrf .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bOPzajaNonCz8xrf .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bOPzajaNonCz8xrf .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-bOPzajaNonCz8xrf .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bOPzajaNonCz8xrf .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bOPzajaNonCz8xrf .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bOPzajaNonCz8xrf .marker.cross{stroke:#333333;}#mermaid-svg-bOPzajaNonCz8xrf svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bOPzajaNonCz8xrf p{margin:0;}#mermaid-svg-bOPzajaNonCz8xrf .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-bOPzajaNonCz8xrf .cluster-label text{fill:#333;}#mermaid-svg-bOPzajaNonCz8xrf .cluster-label span{color:#333;}#mermaid-svg-bOPzajaNonCz8xrf .cluster-label span p{background-color:transparent;}#mermaid-svg-bOPzajaNonCz8xrf .label text,#mermaid-svg-bOPzajaNonCz8xrf span{fill:#333;color:#333;}#mermaid-svg-bOPzajaNonCz8xrf .node rect,#mermaid-svg-bOPzajaNonCz8xrf .node circle,#mermaid-svg-bOPzajaNonCz8xrf .node ellipse,#mermaid-svg-bOPzajaNonCz8xrf .node polygon,#mermaid-svg-bOPzajaNonCz8xrf .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-bOPzajaNonCz8xrf .rough-node .label text,#mermaid-svg-bOPzajaNonCz8xrf .node .label text,#mermaid-svg-bOPzajaNonCz8xrf .image-shape .label,#mermaid-svg-bOPzajaNonCz8xrf .icon-shape .label{text-anchor:middle;}#mermaid-svg-bOPzajaNonCz8xrf .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-bOPzajaNonCz8xrf .rough-node .label,#mermaid-svg-bOPzajaNonCz8xrf .node .label,#mermaid-svg-bOPzajaNonCz8xrf .image-shape .label,#mermaid-svg-bOPzajaNonCz8xrf .icon-shape .label{text-align:center;}#mermaid-svg-bOPzajaNonCz8xrf .node.clickable{cursor:pointer;}#mermaid-svg-bOPzajaNonCz8xrf .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-bOPzajaNonCz8xrf .arrowheadPath{fill:#333333;}#mermaid-svg-bOPzajaNonCz8xrf .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-bOPzajaNonCz8xrf .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-bOPzajaNonCz8xrf .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bOPzajaNonCz8xrf .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-bOPzajaNonCz8xrf .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bOPzajaNonCz8xrf .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-bOPzajaNonCz8xrf .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-bOPzajaNonCz8xrf .cluster text{fill:#333;}#mermaid-svg-bOPzajaNonCz8xrf .cluster span{color:#333;}#mermaid-svg-bOPzajaNonCz8xrf div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-bOPzajaNonCz8xrf .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-bOPzajaNonCz8xrf rect.text{fill:none;stroke-width:0;}#mermaid-svg-bOPzajaNonCz8xrf .icon-shape,#mermaid-svg-bOPzajaNonCz8xrf .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bOPzajaNonCz8xrf .icon-shape p,#mermaid-svg-bOPzajaNonCz8xrf .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-bOPzajaNonCz8xrf .icon-shape .label rect,#mermaid-svg-bOPzajaNonCz8xrf .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bOPzajaNonCz8xrf .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-bOPzajaNonCz8xrf .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-bOPzajaNonCz8xrf :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 输入序列

I love AI
Encoder
Context Vectors

上下文向量
Decoder
目标序列

我爱AI
输出序列

我 爱 AI

  • Encoder:理解输入句子
  • Decoder:根据理解,生成输出句子

这张图画的是训练阶段,不是推理阶段。Transformer 有两种完全不同的工作模式。

"目标序列"不就是已经翻译好的中文吗?既然已经知道答案了,为什么还要 Decoder 去翻译?


2.5 Transformer 的数据入口:文字是如何变成向量的?

在讲 Encoder 的内部结构之前,我们必须先回答一个问题:

Transformer 的第一层到底吃进去的是什么?

答案是:不是文字,不是 Token ID,而是向量矩阵。

完整的入口流程:
#mermaid-svg-LyFqGaD4cyBiYkCJ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-LyFqGaD4cyBiYkCJ .error-icon{fill:#552222;}#mermaid-svg-LyFqGaD4cyBiYkCJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-LyFqGaD4cyBiYkCJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .marker.cross{stroke:#333333;}#mermaid-svg-LyFqGaD4cyBiYkCJ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-LyFqGaD4cyBiYkCJ p{margin:0;}#mermaid-svg-LyFqGaD4cyBiYkCJ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .cluster-label text{fill:#333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .cluster-label span{color:#333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .cluster-label span p{background-color:transparent;}#mermaid-svg-LyFqGaD4cyBiYkCJ .label text,#mermaid-svg-LyFqGaD4cyBiYkCJ span{fill:#333;color:#333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .node rect,#mermaid-svg-LyFqGaD4cyBiYkCJ .node circle,#mermaid-svg-LyFqGaD4cyBiYkCJ .node ellipse,#mermaid-svg-LyFqGaD4cyBiYkCJ .node polygon,#mermaid-svg-LyFqGaD4cyBiYkCJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-LyFqGaD4cyBiYkCJ .rough-node .label text,#mermaid-svg-LyFqGaD4cyBiYkCJ .node .label text,#mermaid-svg-LyFqGaD4cyBiYkCJ .image-shape .label,#mermaid-svg-LyFqGaD4cyBiYkCJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-LyFqGaD4cyBiYkCJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-LyFqGaD4cyBiYkCJ .rough-node .label,#mermaid-svg-LyFqGaD4cyBiYkCJ .node .label,#mermaid-svg-LyFqGaD4cyBiYkCJ .image-shape .label,#mermaid-svg-LyFqGaD4cyBiYkCJ .icon-shape .label{text-align:center;}#mermaid-svg-LyFqGaD4cyBiYkCJ .node.clickable{cursor:pointer;}#mermaid-svg-LyFqGaD4cyBiYkCJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .arrowheadPath{fill:#333333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-LyFqGaD4cyBiYkCJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-LyFqGaD4cyBiYkCJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-LyFqGaD4cyBiYkCJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-LyFqGaD4cyBiYkCJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-LyFqGaD4cyBiYkCJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-LyFqGaD4cyBiYkCJ .cluster text{fill:#333;}#mermaid-svg-LyFqGaD4cyBiYkCJ .cluster span{color:#333;}#mermaid-svg-LyFqGaD4cyBiYkCJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-LyFqGaD4cyBiYkCJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-LyFqGaD4cyBiYkCJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-LyFqGaD4cyBiYkCJ .icon-shape,#mermaid-svg-LyFqGaD4cyBiYkCJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-LyFqGaD4cyBiYkCJ .icon-shape p,#mermaid-svg-LyFqGaD4cyBiYkCJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-LyFqGaD4cyBiYkCJ .icon-shape .label rect,#mermaid-svg-LyFqGaD4cyBiYkCJ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-LyFqGaD4cyBiYkCJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-LyFqGaD4cyBiYkCJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-LyFqGaD4cyBiYkCJ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 'I love AI'

原始文本
Tokenizer 分词

101, 2054, 993

Token IDs(3个整数)
Embedding Layer 查表
3, 512 矩阵

每个Token变成512维向量

  • Position Encoding
    3, 512 矩阵

包含位置信息
进入 Encoder Block 1

第一步:Tokenizer

把文字切成 Token,每个 Token 对应一个整数 ID:

text 复制代码
"I"    → 101
"love" → 2054
"AI"   → 993

Token ID 只是编号,没有数学意义(101 不代表"比 100 大一点")。

第二步:Embedding Layer

一个查找表,把每个整数 ID 映射到一个 d_model 维的向量:

text 复制代码
Embedding 矩阵形状:[vocab_size, d_model]
                    例如 [30000, 512]

Token ID = 2054 → 取第 2054 行 → 得到一个 512 维向量

经过 Embedding 后,Shape 变化:

text 复制代码
[3]        →    [3, 512]
(3个整数)       (3个512维向量)

第三步:Position Encoding

Transformer 的 Attention 是"所有 Token 同时计算"的,它不知道谁在前谁在后

如果不加位置信息:

text 复制代码
"I love AI"  和  "AI love I"
→ 经过 Embedding 后,得到相同的三个向量(只是顺序不同)
→ Attention 会给出完全相同的结果!

所以必须加上位置信息,让 Transformer 知道每个 Token 在第几个位置:

text 复制代码
最终输入 = Token Embedding + Position Encoding
Shape: [3, 512] + [3, 512] = [3, 512]

📌 第3章会详细讲 Tokenizer 的种类(BPE/WordPiece)、Position Encoding 的数学公式、以及为什么现代模型用 RoPE。这里只需要知道:进入 Encoder/Decoder Block 的是一个 seq_len, d_model 的矩阵

两种 Embedding 的区分(避免后续混淆)

本书会出现两种"Embedding",它们完全不同:

名称 位置 输入 输出 作用
Token Embedding Layer Transformer 内部第一层 Token ID(整数) 向量(512维) 把词变成向量
Sentence Embedding Embedding Model 的最终输出 一句话 一个语义向量(768维) 用于 RAG 检索

第16章会详细讲 Sentence Embedding。现在只需要记住:这里讲的 Embedding 是 Token 级别的查表操作。


2.6 Encoder 的结构

Encoder 由 N 个相同的 Block 堆叠而成(原始论文 N=6)。

⚠️ 6 层不是固定的。原始论文用 6 层是因为当时的计算资源和实验结果。现代模型远超这个数字:BERT 用 12/24 层,GPT-3 用 96 层,Llama 3 70B 用 80 层。层数是一个可调的超参数。

Block 在 Encoder 中的位置

text 复制代码
┌─────────────────────────┐
│        Encoder          │
├─────────────────────────┤
│   ┌─────────────────┐   │
│   │   Block 6       │   │
│   ├─────────────────┤   │
│   │   Block 5       │   │
│   ├─────────────────┤   │
│   │   Block 4       │   │
│   ├─────────────────┤   │
│   │   Block 3       │   │
│   ├─────────────────┤   │
│   │   Block 2       │   │
│   ├─────────────────┤   │
│   │   Block 1       │   │
│   └─────────────────┘   │
│           ↑             │
│    Embedding + Pos      │
└─────────────────────────┘

📌 第4章就是展开讲一个 Block 的内部结构。 当你读到第4章时,记住:你看到的那个 Block,就是上图中重复堆叠的基本单元。
#mermaid-svg-PQjhu3WUy1xqjTT4{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-PQjhu3WUy1xqjTT4 .error-icon{fill:#552222;}#mermaid-svg-PQjhu3WUy1xqjTT4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-PQjhu3WUy1xqjTT4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .marker.cross{stroke:#333333;}#mermaid-svg-PQjhu3WUy1xqjTT4 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-PQjhu3WUy1xqjTT4 p{margin:0;}#mermaid-svg-PQjhu3WUy1xqjTT4 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .cluster-label text{fill:#333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .cluster-label span{color:#333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .cluster-label span p{background-color:transparent;}#mermaid-svg-PQjhu3WUy1xqjTT4 .label text,#mermaid-svg-PQjhu3WUy1xqjTT4 span{fill:#333;color:#333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .node rect,#mermaid-svg-PQjhu3WUy1xqjTT4 .node circle,#mermaid-svg-PQjhu3WUy1xqjTT4 .node ellipse,#mermaid-svg-PQjhu3WUy1xqjTT4 .node polygon,#mermaid-svg-PQjhu3WUy1xqjTT4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-PQjhu3WUy1xqjTT4 .rough-node .label text,#mermaid-svg-PQjhu3WUy1xqjTT4 .node .label text,#mermaid-svg-PQjhu3WUy1xqjTT4 .image-shape .label,#mermaid-svg-PQjhu3WUy1xqjTT4 .icon-shape .label{text-anchor:middle;}#mermaid-svg-PQjhu3WUy1xqjTT4 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-PQjhu3WUy1xqjTT4 .rough-node .label,#mermaid-svg-PQjhu3WUy1xqjTT4 .node .label,#mermaid-svg-PQjhu3WUy1xqjTT4 .image-shape .label,#mermaid-svg-PQjhu3WUy1xqjTT4 .icon-shape .label{text-align:center;}#mermaid-svg-PQjhu3WUy1xqjTT4 .node.clickable{cursor:pointer;}#mermaid-svg-PQjhu3WUy1xqjTT4 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .arrowheadPath{fill:#333333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-PQjhu3WUy1xqjTT4 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-PQjhu3WUy1xqjTT4 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-PQjhu3WUy1xqjTT4 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-PQjhu3WUy1xqjTT4 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-PQjhu3WUy1xqjTT4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-PQjhu3WUy1xqjTT4 .cluster text{fill:#333;}#mermaid-svg-PQjhu3WUy1xqjTT4 .cluster span{color:#333;}#mermaid-svg-PQjhu3WUy1xqjTT4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-PQjhu3WUy1xqjTT4 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-PQjhu3WUy1xqjTT4 rect.text{fill:none;stroke-width:0;}#mermaid-svg-PQjhu3WUy1xqjTT4 .icon-shape,#mermaid-svg-PQjhu3WUy1xqjTT4 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-PQjhu3WUy1xqjTT4 .icon-shape p,#mermaid-svg-PQjhu3WUy1xqjTT4 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-PQjhu3WUy1xqjTT4 .icon-shape .label rect,#mermaid-svg-PQjhu3WUy1xqjTT4 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-PQjhu3WUy1xqjTT4 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-PQjhu3WUy1xqjTT4 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-PQjhu3WUy1xqjTT4 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Input Embedding + Position Encoding
Encoder Block 1
Encoder Block 2
...
Encoder Block N
Context Vectors

图中顶部的 "Input Embedding + Position Encoding" 就是 2.5 节讲的数据入口------经过 Tokenizer → Embedding → Position Encoding 之后得到的 seq_len, d_model 矩阵。

每个 Encoder Block 包含:
#mermaid-svg-mGRLTOzooec9K56s{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-mGRLTOzooec9K56s .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-mGRLTOzooec9K56s .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-mGRLTOzooec9K56s .error-icon{fill:#552222;}#mermaid-svg-mGRLTOzooec9K56s .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-mGRLTOzooec9K56s .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-mGRLTOzooec9K56s .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-mGRLTOzooec9K56s .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-mGRLTOzooec9K56s .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-mGRLTOzooec9K56s .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-mGRLTOzooec9K56s .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-mGRLTOzooec9K56s .marker{fill:#333333;stroke:#333333;}#mermaid-svg-mGRLTOzooec9K56s .marker.cross{stroke:#333333;}#mermaid-svg-mGRLTOzooec9K56s svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-mGRLTOzooec9K56s p{margin:0;}#mermaid-svg-mGRLTOzooec9K56s .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-mGRLTOzooec9K56s .cluster-label text{fill:#333;}#mermaid-svg-mGRLTOzooec9K56s .cluster-label span{color:#333;}#mermaid-svg-mGRLTOzooec9K56s .cluster-label span p{background-color:transparent;}#mermaid-svg-mGRLTOzooec9K56s .label text,#mermaid-svg-mGRLTOzooec9K56s span{fill:#333;color:#333;}#mermaid-svg-mGRLTOzooec9K56s .node rect,#mermaid-svg-mGRLTOzooec9K56s .node circle,#mermaid-svg-mGRLTOzooec9K56s .node ellipse,#mermaid-svg-mGRLTOzooec9K56s .node polygon,#mermaid-svg-mGRLTOzooec9K56s .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-mGRLTOzooec9K56s .rough-node .label text,#mermaid-svg-mGRLTOzooec9K56s .node .label text,#mermaid-svg-mGRLTOzooec9K56s .image-shape .label,#mermaid-svg-mGRLTOzooec9K56s .icon-shape .label{text-anchor:middle;}#mermaid-svg-mGRLTOzooec9K56s .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-mGRLTOzooec9K56s .rough-node .label,#mermaid-svg-mGRLTOzooec9K56s .node .label,#mermaid-svg-mGRLTOzooec9K56s .image-shape .label,#mermaid-svg-mGRLTOzooec9K56s .icon-shape .label{text-align:center;}#mermaid-svg-mGRLTOzooec9K56s .node.clickable{cursor:pointer;}#mermaid-svg-mGRLTOzooec9K56s .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-mGRLTOzooec9K56s .arrowheadPath{fill:#333333;}#mermaid-svg-mGRLTOzooec9K56s .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-mGRLTOzooec9K56s .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-mGRLTOzooec9K56s .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mGRLTOzooec9K56s .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-mGRLTOzooec9K56s .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mGRLTOzooec9K56s .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-mGRLTOzooec9K56s .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-mGRLTOzooec9K56s .cluster text{fill:#333;}#mermaid-svg-mGRLTOzooec9K56s .cluster span{color:#333;}#mermaid-svg-mGRLTOzooec9K56s div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-mGRLTOzooec9K56s .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-mGRLTOzooec9K56s rect.text{fill:none;stroke-width:0;}#mermaid-svg-mGRLTOzooec9K56s .icon-shape,#mermaid-svg-mGRLTOzooec9K56s .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mGRLTOzooec9K56s .icon-shape p,#mermaid-svg-mGRLTOzooec9K56s .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-mGRLTOzooec9K56s .icon-shape .label rect,#mermaid-svg-mGRLTOzooec9K56s .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mGRLTOzooec9K56s .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-mGRLTOzooec9K56s .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-mGRLTOzooec9K56s :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 输入 x
Multi-Head Self-Attention
Add & LayerNorm
Feed Forward Network
Add & LayerNorm
输出


2.7 Encoder 输出什么?

Encoder 的输出是一组向量,每个 Token 对应一个向量

I love AI(3个Token)为例,Encoder 输出:

text 复制代码
[向量_I, 向量_love, 向量_AI]

每个向量的维度是 d_model(原始论文是 512)。

⚠️ 重要:Encoder 输出不是"一个向量"

这是初学者最容易搞错的地方:

text 复制代码
❌ 错误理解:Encoder 把整句话压缩成一个 512 维向量
✅ 正确理解:Encoder 输出 N 个 512 维向量(N = Token 数量)
输入 Token 数 Encoder 输出 Shape
I love AI 3 3, 512
I love deep learning 4 4, 512
Hello 1 1, 512

这些向量不是简单的词向量,而是包含了整个句子上下文信息的表示。经过 6 层 Attention 之后,"I" 的向量已经"知道"了 "love" 和 "AI" 的信息。

💡 这个 Shape 非常重要:后面的 Embedding Model(第16章)输出的就是对这些向量做 Mean Pooling(平均)或取 CLS Token,得到一个句子级别的向量。


2.8 Decoder 的结构

Decoder 也由 N 个 Block 堆叠,但每个 Block 比 Encoder 多了一个 Cross-Attention 模块(用于从 Encoder 输出中拉取源句子的信息)。

Block 在 Decoder 中的位置

text 复制代码
┌─────────────────────────────┐
│         Decoder             │
├─────────────────────────────┤
│   ┌─────────────────────┐   │
│   │   Block 6           │   │
│   ├─────────────────────┤   │
│   │   Block 5           │   │
│   ├─────────────────────┤   │
│   │   Block 4           │   │
│   ├─────────────────────┤   │
│   │   Block 3           │   │
│   ├─────────────────────┤   │
│   │   Block 2           │   │
│   ├─────────────────────┤   │
│   │   Block 1           │   │  ← 接收 Encoder 输出
│   └─────────────────────┘   │
│            ↑                │
│     Embedding + Pos         │
│     (目标语言Token)          │
└─────────────────────────────┘

对比:

text 复制代码
Encoder Block = Self-Attention → FFN            (2个子模块)
Decoder Block = Masked Self-Attention → Cross-Attention → FFN (3个子模块)
                                        ↑
                                    这是多出来的

#mermaid-svg-wcw3yhOKiMGjPDSZ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wcw3yhOKiMGjPDSZ .error-icon{fill:#552222;}#mermaid-svg-wcw3yhOKiMGjPDSZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wcw3yhOKiMGjPDSZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .marker.cross{stroke:#333333;}#mermaid-svg-wcw3yhOKiMGjPDSZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wcw3yhOKiMGjPDSZ p{margin:0;}#mermaid-svg-wcw3yhOKiMGjPDSZ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .cluster-label text{fill:#333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .cluster-label span{color:#333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .cluster-label span p{background-color:transparent;}#mermaid-svg-wcw3yhOKiMGjPDSZ .label text,#mermaid-svg-wcw3yhOKiMGjPDSZ span{fill:#333;color:#333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .node rect,#mermaid-svg-wcw3yhOKiMGjPDSZ .node circle,#mermaid-svg-wcw3yhOKiMGjPDSZ .node ellipse,#mermaid-svg-wcw3yhOKiMGjPDSZ .node polygon,#mermaid-svg-wcw3yhOKiMGjPDSZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wcw3yhOKiMGjPDSZ .rough-node .label text,#mermaid-svg-wcw3yhOKiMGjPDSZ .node .label text,#mermaid-svg-wcw3yhOKiMGjPDSZ .image-shape .label,#mermaid-svg-wcw3yhOKiMGjPDSZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-wcw3yhOKiMGjPDSZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wcw3yhOKiMGjPDSZ .rough-node .label,#mermaid-svg-wcw3yhOKiMGjPDSZ .node .label,#mermaid-svg-wcw3yhOKiMGjPDSZ .image-shape .label,#mermaid-svg-wcw3yhOKiMGjPDSZ .icon-shape .label{text-align:center;}#mermaid-svg-wcw3yhOKiMGjPDSZ .node.clickable{cursor:pointer;}#mermaid-svg-wcw3yhOKiMGjPDSZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .arrowheadPath{fill:#333333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wcw3yhOKiMGjPDSZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wcw3yhOKiMGjPDSZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wcw3yhOKiMGjPDSZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wcw3yhOKiMGjPDSZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wcw3yhOKiMGjPDSZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wcw3yhOKiMGjPDSZ .cluster text{fill:#333;}#mermaid-svg-wcw3yhOKiMGjPDSZ .cluster span{color:#333;}#mermaid-svg-wcw3yhOKiMGjPDSZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wcw3yhOKiMGjPDSZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wcw3yhOKiMGjPDSZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-wcw3yhOKiMGjPDSZ .icon-shape,#mermaid-svg-wcw3yhOKiMGjPDSZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wcw3yhOKiMGjPDSZ .icon-shape p,#mermaid-svg-wcw3yhOKiMGjPDSZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wcw3yhOKiMGjPDSZ .icon-shape .label rect,#mermaid-svg-wcw3yhOKiMGjPDSZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wcw3yhOKiMGjPDSZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wcw3yhOKiMGjPDSZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wcw3yhOKiMGjPDSZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 目标序列 Embedding + Position Encoding
Masked Multi-Head Self-Attention
Add & LayerNorm
Cross-Attention

与 Encoder 输出交互
Encoder 输出
Add & LayerNorm
Feed Forward Network
Add & LayerNorm
输出

Decoder Block 的三个子模块:

  1. Masked Self-Attention:只看已经生成的 Token,不能偷看未来
  2. Cross-Attention:与 Encoder 的输出交互,获取源句子信息(第13章详细讲解公式和实现)
  3. Feed Forward:逐位置的全连接网络

什么是 Masked Self-Attention?

Encoder 的 Self-Attention 是"双向"的------每个 Token 可以看到所有其他 Token。

但 Decoder 不行。因为生成第 3 个字的时候,第 4、5 个字还没有生成,不能偷看。

所以 Decoder 用一个 Mask(掩码) 把未来的 Token 遮住:

text 复制代码
生成"爱"时能看到的 Token:

  我  爱  AI
我 [✓   ✗   ✗]   ← "我"只能看到自己
爱 [✓   ✓   ✗]   ← "爱"能看到"我"和自己,看不到"AI"
AI [✓   ✓   ✓]   ← "AI"能看到所有(它是最后一个)

这就是"Masked"的含义------用掩码把右上角(未来)遮住。也叫 Causal Attention(因果注意力),因为只有"过去能影响未来",不允许"未来影响过去"。

📌 第13章会详细讲 Causal Mask 的实现和数学原理。这里只需要记住:Masked = 遮住未来。


2.9 为什么 Decoder 需要 Cross-Attention?

假设我们要翻译 I love AI我爱AI。Decoder 正在生成第二个字"爱"。

如果没有 Cross-Attention

Decoder 只看到自己已经生成的"我",它怎么知道下一个字是"爱"而不是"是"或"想"?它根本不知道源句子里有 "love" 这个词!

有了 Cross-Attention

Decoder 生成"爱"的时候,通过 Cross-Attention 去 Encoder 输出中查找:

text 复制代码
Decoder(Q):"我已经生成了'我',下一个字应该是什么?让我看看源句子..."
Encoder(K,V):"源句子有 I、love、AI 三个词的表示,请自取。"
Decoder:找到 'love' 的相关度最高 → 生成 '爱'

生活类比

Cross-Attention 就像同声传译员的耳机。翻译员(Decoder)一边说中文,一边通过耳机(Cross-Attention)听源语言(Encoder输出)。如果拔掉耳机,翻译员就是在瞎编------只能根据已经说过的中文来猜下一个字。
📌 Cross-Attention 的数学公式和 Self-Attention 完全相同(都是 Q·K^T → Softmax → ×V),唯一区别是 Q 来自 Decoder、K/V 来自 Encoder。第6章讲 Self-Attention 公式,第13章讲 Cross-Attention 的具体实现和 Q/K/V 角色。


2.10 数据流:一句话是如何被翻译的?

以翻译 I love AI我爱AI 为例:
输出: 我 爱 AI Decoder Encoder 输入: I love AI 输出: 我 爱 AI Decoder Encoder 输入: I love AI #mermaid-svg-IfLZJnBTv5Szp4gD{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IfLZJnBTv5Szp4gD .error-icon{fill:#552222;}#mermaid-svg-IfLZJnBTv5Szp4gD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IfLZJnBTv5Szp4gD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IfLZJnBTv5Szp4gD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IfLZJnBTv5Szp4gD .marker.cross{stroke:#333333;}#mermaid-svg-IfLZJnBTv5Szp4gD svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IfLZJnBTv5Szp4gD p{margin:0;}#mermaid-svg-IfLZJnBTv5Szp4gD .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-IfLZJnBTv5Szp4gD text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-IfLZJnBTv5Szp4gD .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-IfLZJnBTv5Szp4gD .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-IfLZJnBTv5Szp4gD .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-IfLZJnBTv5Szp4gD .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-IfLZJnBTv5Szp4gD #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-IfLZJnBTv5Szp4gD .sequenceNumber{fill:white;}#mermaid-svg-IfLZJnBTv5Szp4gD #sequencenumber{fill:#333;}#mermaid-svg-IfLZJnBTv5Szp4gD #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-IfLZJnBTv5Szp4gD .messageText{fill:#333;stroke:none;}#mermaid-svg-IfLZJnBTv5Szp4gD .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-IfLZJnBTv5Szp4gD .labelText,#mermaid-svg-IfLZJnBTv5Szp4gD .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-IfLZJnBTv5Szp4gD .loopText,#mermaid-svg-IfLZJnBTv5Szp4gD .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-IfLZJnBTv5Szp4gD .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-IfLZJnBTv5Szp4gD .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-IfLZJnBTv5Szp4gD .noteText,#mermaid-svg-IfLZJnBTv5Szp4gD .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-IfLZJnBTv5Szp4gD .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-IfLZJnBTv5Szp4gD .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-IfLZJnBTv5Szp4gD .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-IfLZJnBTv5Szp4gD .actorPopupMenu{position:absolute;}#mermaid-svg-IfLZJnBTv5Szp4gD .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-IfLZJnBTv5Szp4gD .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-IfLZJnBTv5Szp4gD .actor-man circle,#mermaid-svg-IfLZJnBTv5Szp4gD line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-IfLZJnBTv5Szp4gD :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Token Embedding + Position Encoding6层 Self-Attention + FFNContext Vectors (每个Token的上下文表示)Masked Self-Attention (已生成的Token)Cross-Attention (与Encoder输出交互)逐个生成 Token

注意:上图展示的是推理时的流程(Decoder 逐个生成 Token)。训练时 Decoder 的工作方式不同------会一次性输入完整目标序列,利用 Mask 防止偷看未来。这涉及 Teacher Forcing 和自回归生成的区别,详见第13章。


2.11 完整数据流:Tensor Shape 如何变化?

I love AI我爱AI 为例,追踪完整的 Shape 变化:
#mermaid-svg-Y15Oly530DJwVANM{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Y15Oly530DJwVANM .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Y15Oly530DJwVANM .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Y15Oly530DJwVANM .error-icon{fill:#552222;}#mermaid-svg-Y15Oly530DJwVANM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Y15Oly530DJwVANM .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Y15Oly530DJwVANM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Y15Oly530DJwVANM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Y15Oly530DJwVANM .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Y15Oly530DJwVANM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Y15Oly530DJwVANM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Y15Oly530DJwVANM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Y15Oly530DJwVANM .marker.cross{stroke:#333333;}#mermaid-svg-Y15Oly530DJwVANM svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Y15Oly530DJwVANM p{margin:0;}#mermaid-svg-Y15Oly530DJwVANM .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Y15Oly530DJwVANM .cluster-label text{fill:#333;}#mermaid-svg-Y15Oly530DJwVANM .cluster-label span{color:#333;}#mermaid-svg-Y15Oly530DJwVANM .cluster-label span p{background-color:transparent;}#mermaid-svg-Y15Oly530DJwVANM .label text,#mermaid-svg-Y15Oly530DJwVANM span{fill:#333;color:#333;}#mermaid-svg-Y15Oly530DJwVANM .node rect,#mermaid-svg-Y15Oly530DJwVANM .node circle,#mermaid-svg-Y15Oly530DJwVANM .node ellipse,#mermaid-svg-Y15Oly530DJwVANM .node polygon,#mermaid-svg-Y15Oly530DJwVANM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Y15Oly530DJwVANM .rough-node .label text,#mermaid-svg-Y15Oly530DJwVANM .node .label text,#mermaid-svg-Y15Oly530DJwVANM .image-shape .label,#mermaid-svg-Y15Oly530DJwVANM .icon-shape .label{text-anchor:middle;}#mermaid-svg-Y15Oly530DJwVANM .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Y15Oly530DJwVANM .rough-node .label,#mermaid-svg-Y15Oly530DJwVANM .node .label,#mermaid-svg-Y15Oly530DJwVANM .image-shape .label,#mermaid-svg-Y15Oly530DJwVANM .icon-shape .label{text-align:center;}#mermaid-svg-Y15Oly530DJwVANM .node.clickable{cursor:pointer;}#mermaid-svg-Y15Oly530DJwVANM .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Y15Oly530DJwVANM .arrowheadPath{fill:#333333;}#mermaid-svg-Y15Oly530DJwVANM .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Y15Oly530DJwVANM .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Y15Oly530DJwVANM .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Y15Oly530DJwVANM .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Y15Oly530DJwVANM .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Y15Oly530DJwVANM .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Y15Oly530DJwVANM .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Y15Oly530DJwVANM .cluster text{fill:#333;}#mermaid-svg-Y15Oly530DJwVANM .cluster span{color:#333;}#mermaid-svg-Y15Oly530DJwVANM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Y15Oly530DJwVANM .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Y15Oly530DJwVANM rect.text{fill:none;stroke-width:0;}#mermaid-svg-Y15Oly530DJwVANM .icon-shape,#mermaid-svg-Y15Oly530DJwVANM .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Y15Oly530DJwVANM .icon-shape p,#mermaid-svg-Y15Oly530DJwVANM .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Y15Oly530DJwVANM .icon-shape .label rect,#mermaid-svg-Y15Oly530DJwVANM .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Y15Oly530DJwVANM .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Y15Oly530DJwVANM .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Y15Oly530DJwVANM :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 输入文本: I love AI
Tokenizer

'I' 'love' 'AI'
Token IDs

3 (3个整数)
Embedding

3, 512

  • Position Encoding

3, 512

Encoder (6层)

3, 512\] → \[3, 512

Encoder 输出

3, 512

每个Token一个512维向量
Decoder (6层)

接收Encoder输出 + 已生成Token
Linear投影

vocab_size

Softmax

概率分布
输出Token: '我'

关键观察

阶段 Shape 说明
原始文本 字符串 "I love AI"
Tokenizer 后 3 3个Token ID
Embedding 后 3, 512 每个Token变成512维向量
Encoder 输出 3, 512 Shape不变!但语义变了
Decoder 每步输出 已生成长度, 512 逐步增长
Linear 投影 vocab_size 映射到词表大小
Softmax vocab_size 概率分布,取最大

为什么上图只生成了一个"我"?

因为 Decoder 不是一次生成整句话,而是循环多次、每次生成一个 Token

上面的 Shape 图展示的是"一次 Forward"的流程。完整的翻译过程需要多次循环:

text 复制代码
时间 →
───────────────────────────────────────────────────────────

Encoder(只执行一次):

  I love AI  →  [3, 512] Context Vectors  ✓ 完成

───────────────────────────────────────────────────────────

Decoder(循环执行 4 次):

  第1次 Forward
  输入: <BOS>           → 预测: 我

  第2次 Forward
  输入: <BOS> 我        → 预测: 爱

  第3次 Forward
  输入: <BOS> 我 爱     → 预测: AI

  第4次 Forward
  输入: <BOS> 我 爱 AI  → 预测: <EOS> ← 停止

───────────────────────────────────────────────────────────

每次 Forward 时,Decoder 都会通过 Cross-Attention 查看同一份 Encoder 输出(3, 512),但 Decoder 自身的输入逐步增长:

第几次 Decoder 输入长度 Decoder 输入 Shape 预测结果
1 1 1, 512
2 2 2, 512
3 3 3, 512 AI
4 4 4, 512 <EOS>

💡 这就是为什么 Decoder 推理比 Encoder 慢------Encoder 一次并行处理所有 Token,Decoder 必须串行循环生成。第15章的 KV Cache 就是为了减少这种重复计算。
📌 记住这张表。后面每一章,我们都会回来看:当前讲的模块,对应这张表的哪一步。


2.12 为什么 BERT 只有 Encoder?

BERT 的任务是:

理解一句话的含义(用于分类、问答、Embedding 等)。

这是一个双向理解的任务,需要同时看左边和右边的 Token。

所以 BERT 只保留 Encoder,去掉了 Decoder。
#mermaid-svg-qGQcIRlmwWZnEZKX{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-qGQcIRlmwWZnEZKX .error-icon{fill:#552222;}#mermaid-svg-qGQcIRlmwWZnEZKX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-qGQcIRlmwWZnEZKX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-qGQcIRlmwWZnEZKX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-qGQcIRlmwWZnEZKX .marker.cross{stroke:#333333;}#mermaid-svg-qGQcIRlmwWZnEZKX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-qGQcIRlmwWZnEZKX p{margin:0;}#mermaid-svg-qGQcIRlmwWZnEZKX .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-qGQcIRlmwWZnEZKX .cluster-label text{fill:#333;}#mermaid-svg-qGQcIRlmwWZnEZKX .cluster-label span{color:#333;}#mermaid-svg-qGQcIRlmwWZnEZKX .cluster-label span p{background-color:transparent;}#mermaid-svg-qGQcIRlmwWZnEZKX .label text,#mermaid-svg-qGQcIRlmwWZnEZKX span{fill:#333;color:#333;}#mermaid-svg-qGQcIRlmwWZnEZKX .node rect,#mermaid-svg-qGQcIRlmwWZnEZKX .node circle,#mermaid-svg-qGQcIRlmwWZnEZKX .node ellipse,#mermaid-svg-qGQcIRlmwWZnEZKX .node polygon,#mermaid-svg-qGQcIRlmwWZnEZKX .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-qGQcIRlmwWZnEZKX .rough-node .label text,#mermaid-svg-qGQcIRlmwWZnEZKX .node .label text,#mermaid-svg-qGQcIRlmwWZnEZKX .image-shape .label,#mermaid-svg-qGQcIRlmwWZnEZKX .icon-shape .label{text-anchor:middle;}#mermaid-svg-qGQcIRlmwWZnEZKX .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-qGQcIRlmwWZnEZKX .rough-node .label,#mermaid-svg-qGQcIRlmwWZnEZKX .node .label,#mermaid-svg-qGQcIRlmwWZnEZKX .image-shape .label,#mermaid-svg-qGQcIRlmwWZnEZKX .icon-shape .label{text-align:center;}#mermaid-svg-qGQcIRlmwWZnEZKX .node.clickable{cursor:pointer;}#mermaid-svg-qGQcIRlmwWZnEZKX .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-qGQcIRlmwWZnEZKX .arrowheadPath{fill:#333333;}#mermaid-svg-qGQcIRlmwWZnEZKX .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-qGQcIRlmwWZnEZKX .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-qGQcIRlmwWZnEZKX .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-qGQcIRlmwWZnEZKX .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-qGQcIRlmwWZnEZKX .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-qGQcIRlmwWZnEZKX .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-qGQcIRlmwWZnEZKX .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-qGQcIRlmwWZnEZKX .cluster text{fill:#333;}#mermaid-svg-qGQcIRlmwWZnEZKX .cluster span{color:#333;}#mermaid-svg-qGQcIRlmwWZnEZKX div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-qGQcIRlmwWZnEZKX .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-qGQcIRlmwWZnEZKX rect.text{fill:none;stroke-width:0;}#mermaid-svg-qGQcIRlmwWZnEZKX .icon-shape,#mermaid-svg-qGQcIRlmwWZnEZKX .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-qGQcIRlmwWZnEZKX .icon-shape p,#mermaid-svg-qGQcIRlmwWZnEZKX .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-qGQcIRlmwWZnEZKX .icon-shape .label rect,#mermaid-svg-qGQcIRlmwWZnEZKX .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-qGQcIRlmwWZnEZKX .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-qGQcIRlmwWZnEZKX .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-qGQcIRlmwWZnEZKX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} I
love
deep
learning

每个 Token 都可以和所有其他 Token 直接交互。


2.13 为什么 GPT 只有 Decoder?

GPT 的任务是:

给定前面的 Token,预测下一个 Token。

这是一个单向的任务,不需要"翻译"另一个句子,只需要"续写"。

GPT 不需要 Encoder 的历史脉络

#mermaid-svg-tn10cIKDoVrl0piN{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-tn10cIKDoVrl0piN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-tn10cIKDoVrl0piN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-tn10cIKDoVrl0piN .error-icon{fill:#552222;}#mermaid-svg-tn10cIKDoVrl0piN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-tn10cIKDoVrl0piN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-tn10cIKDoVrl0piN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-tn10cIKDoVrl0piN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-tn10cIKDoVrl0piN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-tn10cIKDoVrl0piN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-tn10cIKDoVrl0piN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-tn10cIKDoVrl0piN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-tn10cIKDoVrl0piN .marker.cross{stroke:#333333;}#mermaid-svg-tn10cIKDoVrl0piN svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-tn10cIKDoVrl0piN p{margin:0;}#mermaid-svg-tn10cIKDoVrl0piN .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-tn10cIKDoVrl0piN .cluster-label text{fill:#333;}#mermaid-svg-tn10cIKDoVrl0piN .cluster-label span{color:#333;}#mermaid-svg-tn10cIKDoVrl0piN .cluster-label span p{background-color:transparent;}#mermaid-svg-tn10cIKDoVrl0piN .label text,#mermaid-svg-tn10cIKDoVrl0piN span{fill:#333;color:#333;}#mermaid-svg-tn10cIKDoVrl0piN .node rect,#mermaid-svg-tn10cIKDoVrl0piN .node circle,#mermaid-svg-tn10cIKDoVrl0piN .node ellipse,#mermaid-svg-tn10cIKDoVrl0piN .node polygon,#mermaid-svg-tn10cIKDoVrl0piN .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-tn10cIKDoVrl0piN .rough-node .label text,#mermaid-svg-tn10cIKDoVrl0piN .node .label text,#mermaid-svg-tn10cIKDoVrl0piN .image-shape .label,#mermaid-svg-tn10cIKDoVrl0piN .icon-shape .label{text-anchor:middle;}#mermaid-svg-tn10cIKDoVrl0piN .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-tn10cIKDoVrl0piN .rough-node .label,#mermaid-svg-tn10cIKDoVrl0piN .node .label,#mermaid-svg-tn10cIKDoVrl0piN .image-shape .label,#mermaid-svg-tn10cIKDoVrl0piN .icon-shape .label{text-align:center;}#mermaid-svg-tn10cIKDoVrl0piN .node.clickable{cursor:pointer;}#mermaid-svg-tn10cIKDoVrl0piN .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-tn10cIKDoVrl0piN .arrowheadPath{fill:#333333;}#mermaid-svg-tn10cIKDoVrl0piN .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-tn10cIKDoVrl0piN .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-tn10cIKDoVrl0piN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tn10cIKDoVrl0piN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-tn10cIKDoVrl0piN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tn10cIKDoVrl0piN .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-tn10cIKDoVrl0piN .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-tn10cIKDoVrl0piN .cluster text{fill:#333;}#mermaid-svg-tn10cIKDoVrl0piN .cluster span{color:#333;}#mermaid-svg-tn10cIKDoVrl0piN div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-tn10cIKDoVrl0piN .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-tn10cIKDoVrl0piN rect.text{fill:none;stroke-width:0;}#mermaid-svg-tn10cIKDoVrl0piN .icon-shape,#mermaid-svg-tn10cIKDoVrl0piN .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tn10cIKDoVrl0piN .icon-shape p,#mermaid-svg-tn10cIKDoVrl0piN .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-tn10cIKDoVrl0piN .icon-shape .label rect,#mermaid-svg-tn10cIKDoVrl0piN .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tn10cIKDoVrl0piN .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-tn10cIKDoVrl0piN .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-tn10cIKDoVrl0piN :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 2017: Google

原始Transformer

Encoder+Decoder

任务: 翻译
2018: OpenAI

GPT-1

只用Decoder

任务: 续写文本
关键洞察:

续写不需要'源句子'

不需要Encoder

不需要Cross-Attention
2019-2024:

GPT-2/3/4, Qwen, Llama

全部Decoder-Only

为什么续写不需要 Encoder?

翻译任务:有一个"源句子"需要理解 → 需要 Encoder。

续写任务:没有"源句子",只有"前面已经说过的话" → 前文已经在 Decoder 自己的 Masked Self-Attention 中了。
#mermaid-svg-XXD1feU7vLEnekrE{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-XXD1feU7vLEnekrE .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-XXD1feU7vLEnekrE .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-XXD1feU7vLEnekrE .error-icon{fill:#552222;}#mermaid-svg-XXD1feU7vLEnekrE .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-XXD1feU7vLEnekrE .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-XXD1feU7vLEnekrE .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-XXD1feU7vLEnekrE .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-XXD1feU7vLEnekrE .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-XXD1feU7vLEnekrE .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-XXD1feU7vLEnekrE .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-XXD1feU7vLEnekrE .marker{fill:#333333;stroke:#333333;}#mermaid-svg-XXD1feU7vLEnekrE .marker.cross{stroke:#333333;}#mermaid-svg-XXD1feU7vLEnekrE svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-XXD1feU7vLEnekrE p{margin:0;}#mermaid-svg-XXD1feU7vLEnekrE .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-XXD1feU7vLEnekrE .cluster-label text{fill:#333;}#mermaid-svg-XXD1feU7vLEnekrE .cluster-label span{color:#333;}#mermaid-svg-XXD1feU7vLEnekrE .cluster-label span p{background-color:transparent;}#mermaid-svg-XXD1feU7vLEnekrE .label text,#mermaid-svg-XXD1feU7vLEnekrE span{fill:#333;color:#333;}#mermaid-svg-XXD1feU7vLEnekrE .node rect,#mermaid-svg-XXD1feU7vLEnekrE .node circle,#mermaid-svg-XXD1feU7vLEnekrE .node ellipse,#mermaid-svg-XXD1feU7vLEnekrE .node polygon,#mermaid-svg-XXD1feU7vLEnekrE .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-XXD1feU7vLEnekrE .rough-node .label text,#mermaid-svg-XXD1feU7vLEnekrE .node .label text,#mermaid-svg-XXD1feU7vLEnekrE .image-shape .label,#mermaid-svg-XXD1feU7vLEnekrE .icon-shape .label{text-anchor:middle;}#mermaid-svg-XXD1feU7vLEnekrE .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-XXD1feU7vLEnekrE .rough-node .label,#mermaid-svg-XXD1feU7vLEnekrE .node .label,#mermaid-svg-XXD1feU7vLEnekrE .image-shape .label,#mermaid-svg-XXD1feU7vLEnekrE .icon-shape .label{text-align:center;}#mermaid-svg-XXD1feU7vLEnekrE .node.clickable{cursor:pointer;}#mermaid-svg-XXD1feU7vLEnekrE .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-XXD1feU7vLEnekrE .arrowheadPath{fill:#333333;}#mermaid-svg-XXD1feU7vLEnekrE .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-XXD1feU7vLEnekrE .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-XXD1feU7vLEnekrE .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XXD1feU7vLEnekrE .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-XXD1feU7vLEnekrE .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XXD1feU7vLEnekrE .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-XXD1feU7vLEnekrE .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-XXD1feU7vLEnekrE .cluster text{fill:#333;}#mermaid-svg-XXD1feU7vLEnekrE .cluster span{color:#333;}#mermaid-svg-XXD1feU7vLEnekrE div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-XXD1feU7vLEnekrE .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-XXD1feU7vLEnekrE rect.text{fill:none;stroke-width:0;}#mermaid-svg-XXD1feU7vLEnekrE .icon-shape,#mermaid-svg-XXD1feU7vLEnekrE .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XXD1feU7vLEnekrE .icon-shape p,#mermaid-svg-XXD1feU7vLEnekrE .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-XXD1feU7vLEnekrE .icon-shape .label rect,#mermaid-svg-XXD1feU7vLEnekrE .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XXD1feU7vLEnekrE .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-XXD1feU7vLEnekrE .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-XXD1feU7vLEnekrE :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} I
love
AI
?

GPT 的 Decoder 只有 Masked Self-Attention + FFN,没有 Cross-Attention(因为没有 Encoder 可以交互)。

💡 一句话:翻译需要"听源语言"(Cross-Attention),续写只需要"记住自己说过什么"(Masked Self-Attention)。

你可能会追问:没有 Encoder,GPT 怎么还能翻译、理解多语言? 答案是:用户的整个 Prompt(包括源语言句子)对 Decoder 来说都是"已经可见的过去",所以它已经拥有了全部上下文来做理解。第14章会详细解释这个问题。


2.14 三种架构对比

架构 代表模型 有 Encoder 有 Decoder 适合任务
Encoder-Decoder 原始 Transformer, T5 翻译、摘要
Encoder Only BERT, RoBERTa, BGE 理解、分类、Embedding
Decoder Only GPT, Qwen, Llama 生成、对话

2.15 与 CNN / LSTM 的对比

Transformer 出现之前,处理序列数据主要有两种架构:

  • CNN(卷积神经网络):最初为图像设计,用固定大小的卷积核扫描数据。后来也被用于文本(如 TextCNN),优势是可以并行计算,但卷积核大小有限,一次只能看局部信息。
  • LSTM(长短期记忆网络):专门为序列设计,逐步读取 Token 并更新记忆。能捕捉上下文关系,但必须串行计算,无法充分利用 GPU。

Transformer 的出现正是为了同时解决 CNN 的局部感受野问题和 LSTM 的串行计算问题。下表展示了三者的核心区别:

内容 CNN LSTM Transformer
感受野 局部 顺序扩展 全局
是否并行
长距离依赖 一般 较好 很强
参数共享
主要机制 卷积 门控 Attention
位置信息 隐式(卷积顺序) 隐式(时间步) 显式(Position Encoding)

💡 第1章详细讲解了 RNN/LSTM/GRU 的原理和局限。这里只做架构层面的对比总结。


2.16 本章核心思想

Transformer 是一种架构,不是一个模型。

这句话非常重要。很多初学者认为 "Transformer = GPT",但实际上:

概念 是什么 类比
Transformer 一种架构设计 建筑图纸
GPT 基于 Transformer 的具体模型 按图纸建的一栋楼
BERT 基于 Transformer 的另一个模型 按同一图纸建的另一栋楼
Qwen 基于 Transformer 的又一个模型 又一栋楼

就像"卷积神经网络"是架构,ResNet、VGG、EfficientNet 是具体模型一样。


本章常见误区

误区 正确理解
❌ Transformer = GPT ✅ Transformer 是架构,GPT/BERT/Qwen 都是基于它的具体模型
❌ Encoder 输出一个向量代表整句话 ✅ Encoder 输出 N 个向量(N=Token数),每个 Token 一个
❌ Decoder 一定能聊天 ✅ 原始 Transformer 的 Decoder 只是做翻译,需要 RLHF 才能聊天
❌ Transformer 一定有 Encoder + Decoder ✅ 可以只用其中一个,GPT 只有 Decoder,BERT 只有 Encoder
❌ Encoder 和 Decoder 的 Block 数量必须是 6 ✅ 6 只是论文设置,现代模型用 12~96 层
❌ GPT 没有 Encoder 就无法理解文本 ✅ Masked Self-Attention 本身就在做理解,只是单向的

本章总结

#mermaid-svg-JOLkjSTZrHGNTxhw{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-JOLkjSTZrHGNTxhw .error-icon{fill:#552222;}#mermaid-svg-JOLkjSTZrHGNTxhw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-JOLkjSTZrHGNTxhw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-JOLkjSTZrHGNTxhw .marker.cross{stroke:#333333;}#mermaid-svg-JOLkjSTZrHGNTxhw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-JOLkjSTZrHGNTxhw p{margin:0;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge{stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .section--1 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section--1 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section--1 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section--1 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section--1 text{fill:#ffffff;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth--1{stroke-width:17;}#mermaid-svg-JOLkjSTZrHGNTxhw .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-0 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-0 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-0 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-0 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-0 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-0{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-0{stroke-width:14;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-1 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-1 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-1 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-1 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-1 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-1{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-1{stroke-width:11;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 text{fill:#ffffff;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-2{stroke-width:8;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-3 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-3 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-3 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-3 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-3 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-3{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-3{stroke-width:5;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-4 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-4 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-4 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-4 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-4 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-4{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-4{stroke-width:2;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-5 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-5 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-5 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-5 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-5 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-5{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-5{stroke-width:-1;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-6 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-6 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-6 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-6 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-6 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-6{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-6{stroke-width:-4;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-7 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-7 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-7 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-7 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-7 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-7{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-7{stroke-width:-7;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-8 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-8 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-8 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-8 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-8 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-8{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-8{stroke-width:-10;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-9 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-9 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-9 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-9 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-9 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-9{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-9{stroke-width:-13;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-10 rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-10 path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-10 circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-10 polygon,#mermaid-svg-JOLkjSTZrHGNTxhw .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-10 text{fill:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .node-icon-10{font-size:40px;color:black;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .edge-depth-10{stroke-width:-16;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled circle,#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:lightgray;}#mermaid-svg-JOLkjSTZrHGNTxhw .disabled text{fill:#efefef;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-root rect,#mermaid-svg-JOLkjSTZrHGNTxhw .section-root path,#mermaid-svg-JOLkjSTZrHGNTxhw .section-root circle,#mermaid-svg-JOLkjSTZrHGNTxhw .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-JOLkjSTZrHGNTxhw .section-root text{fill:#ffffff;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-root span{color:#ffffff;}#mermaid-svg-JOLkjSTZrHGNTxhw .section-2 span{color:#ffffff;}#mermaid-svg-JOLkjSTZrHGNTxhw .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-JOLkjSTZrHGNTxhw .edge{fill:none;}#mermaid-svg-JOLkjSTZrHGNTxhw .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-JOLkjSTZrHGNTxhw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Transformer
Encoder
理解输入
双向Attention
输出Context Vectors
代表: BERT
Decoder
生成输出
单向Attention
Cross-Attention
代表: GPT
Encoder-Decoder
翻译/摘要
代表: T5

三句话总结本章:

  1. Transformer = Encoder(理解)+ Decoder(生成)
  2. GPT 只要 Decoder,因为它只做生成
  3. BERT 只要 Encoder,因为它只做理解

本章思考题

  1. 为什么 Encoder 可以双向 Attention,而 Decoder 必须单向(Masked)?
  2. 如果你要做一个中英文翻译模型,你会选择哪种架构?为什么?
  3. Embedding Model(如 BGE)为什么选择 Encoder-Only 架构?
  4. GPT 的 Decoder 和原始 Transformer 的 Decoder 有什么区别?

下一章预告

下一章我们进入 输入是如何进入 Transformer 的

这一章将回答:

  • 文字是如何变成数字的?(Tokenizer)
  • 数字是如何变成向量的?(Embedding)
  • Transformer 为什么不认识顺序?(Position Encoding)
  • RoPE 是什么?为什么 Qwen 和 Llama 都用它?
相关推荐
焦虑的说说1 小时前
订单库多表合并重构:零停机、无感知与数据一致性保障实践
重构·架构
liangsheng_g1 小时前
微服务本地开发零配置隔离方案
java·微服务·架构
richard_first2 小时前
Transformer 与大语言模型:第10章 Residual (残差连接)
人工智能·深度学习·机器学习·transformer
@atweiwei2 小时前
用 Rust 构建 Agent 应用的高性能框架:langchainrust 架构全景
人工智能·架构·rust·langchain·llm·agent·ai编程
十三画者2 小时前
【文献分享】abCRISPR:基于深度学习的脱碱基gRNA理性设计框架
人工智能·深度学习·数据挖掘·数据分析·数据可视化
大鹏的NLP博客2 小时前
WSL2 资源治理:从 ONNX Runtime CUDA 编译 OOM 到 `.wslconfig` 防御配置
c++·深度学习·onnx runtime
sel_93 小时前
【OPD论文导读(二)】OPD(On-Policy Distillation)全景调研:十篇论文讲透“在自己生成的内容上学习“这件事
人工智能·python·深度学习·学习·算法·语言模型
北山小恐龙3 小时前
使用手势在电脑刷抖音
人工智能·python·深度学习·yolo·机器学习·计算机视觉
richard_first3 小时前
Transformer与大语言模型:第11章 Encoder 是如何理解句子的
深度学习·语言模型·transformer