Encoder 与 Decoder 详解:从 Embedding 到 Seq2Seq 的完整数据流
本文用一个完整例子解释:输入"我爱你"后,每个词怎样经过 Embedding、Encoder,最后由 Decoder 生成 "I love you"。重点是区分词向量、上下文表示和生成过程。
文章目录
- [Encoder 与 Decoder 详解:从 Embedding 到 Seq2Seq 的完整数据流](#Encoder 与 Decoder 详解:从 Embedding 到 Seq2Seq 的完整数据流)
-
- 一、先说清楚几个容易混淆的词
-
- [1. Embedding 是什么?](#1. Embedding 是什么?)
- [2. Encoder 是什么?](#2. Encoder 是什么?)
- [3. Decoder 是什么?](#3. Decoder 是什么?)
- [4. Seq2Seq 是什么?](#4. Seq2Seq 是什么?)
- [二、Embedding、Encoder、Decoder 的关系](#二、Embedding、Encoder、Decoder 的关系)
- [三、经典 Seq2Seq 架构图](#三、经典 Seq2Seq 架构图)
- [四、完整数值例子:我爱你 → I love you](#四、完整数值例子:我爱你 → I love you)
-
- [4.1 第一步:给源语言做 Embedding](#4.1 第一步:给源语言做 Embedding)
- [4.2 第二步:Encoder 逐个读取](#4.2 第二步:Encoder 逐个读取)
- [4.3 第三步:Decoder 的目标词向量](#4.3 第三步:Decoder 的目标词向量)
- [4.4 Decoder 第一步:生成 I](#4.4 Decoder 第一步:生成 I)
- [4.5 Decoder 第二步:生成 love](#4.5 Decoder 第二步:生成 love)
- [4.6 Decoder 第三步:生成 you](#4.6 Decoder 第三步:生成 you)
- [4.7 Decoder 第四步:生成结束符](#4.7 Decoder 第四步:生成结束符)
- 五、把全部数字串起来
- [六、训练时为什么会出现 Teacher Forcing?](#六、训练时为什么会出现 Teacher Forcing?)
- [七、Attention 为什么重要?](#七、Attention 为什么重要?)
- [八、哪些任务需要 Decoder?](#八、哪些任务需要 Decoder?)
- 状态向量到底长什么样?
- [九、Encoder、Decoder 和 LSTM/GRU 的关系](#九、Encoder、Decoder 和 LSTM/GRU 的关系)
- 十、总结
一、先说清楚几个容易混淆的词
1. Embedding 是什么?
Embedding 的作用是把离散的词转换为数字向量:
文字 ⟶ 向量 \text{文字}\longrightarrow\text{向量} 文字⟶向量
例如:
text
我 → [1, 0]
爱 → [0, 1]
你 → [1, 1]
这一步可以理解成查一张"词到向量"的表。
Embedding 只回答:
这个词对应什么数字?
它还没有完整理解这个词在句子中的含义。
2. Encoder 是什么?
Encoder,也叫编码器,负责按顺序读取输入,并不断更新内部状态:
输入词向量序列 ⟶ 上下文表示 \text{输入词向量序列}\longrightarrow\text{上下文表示} 输入词向量序列⟶上下文表示
它回答:
这些词按照这个顺序放在一起,整体表达了什么?
3. Decoder 是什么?
Decoder,也叫解码器,负责根据 Encoder 的内部表示,一个词一个词生成输出:
上下文表示 ⟶ 输出词序列 \text{上下文表示}\longrightarrow\text{输出词序列} 上下文表示⟶输出词序列
它回答:
根据刚才理解的内容,目标语言应该怎样逐词说出来?
4. Seq2Seq 是什么?
Seq2Seq 是 Sequence-to-Sequence 的缩写,表示:
一个序列 ⟶ 另一个序列 \text{一个序列}\longrightarrow\text{另一个序列} 一个序列⟶另一个序列
例如:
text
中文序列:我 爱 你
英文序列:I love you
Encoder 和 Decoder 组合起来,就构成了经典的 Seq2Seq 架构。
二、Embedding、Encoder、Decoder 的关系
#mermaid-svg-OrvIpjSJI2OvnKC3{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-OrvIpjSJI2OvnKC3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-OrvIpjSJI2OvnKC3 .error-icon{fill:#552222;}#mermaid-svg-OrvIpjSJI2OvnKC3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-OrvIpjSJI2OvnKC3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .marker.cross{stroke:#333333;}#mermaid-svg-OrvIpjSJI2OvnKC3 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-OrvIpjSJI2OvnKC3 p{margin:0;}#mermaid-svg-OrvIpjSJI2OvnKC3 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .cluster-label text{fill:#333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .cluster-label span{color:#333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .cluster-label span p{background-color:transparent;}#mermaid-svg-OrvIpjSJI2OvnKC3 .label text,#mermaid-svg-OrvIpjSJI2OvnKC3 span{fill:#333;color:#333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .node rect,#mermaid-svg-OrvIpjSJI2OvnKC3 .node circle,#mermaid-svg-OrvIpjSJI2OvnKC3 .node ellipse,#mermaid-svg-OrvIpjSJI2OvnKC3 .node polygon,#mermaid-svg-OrvIpjSJI2OvnKC3 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-OrvIpjSJI2OvnKC3 .rough-node .label text,#mermaid-svg-OrvIpjSJI2OvnKC3 .node .label text,#mermaid-svg-OrvIpjSJI2OvnKC3 .image-shape .label,#mermaid-svg-OrvIpjSJI2OvnKC3 .icon-shape .label{text-anchor:middle;}#mermaid-svg-OrvIpjSJI2OvnKC3 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-OrvIpjSJI2OvnKC3 .rough-node .label,#mermaid-svg-OrvIpjSJI2OvnKC3 .node .label,#mermaid-svg-OrvIpjSJI2OvnKC3 .image-shape .label,#mermaid-svg-OrvIpjSJI2OvnKC3 .icon-shape .label{text-align:center;}#mermaid-svg-OrvIpjSJI2OvnKC3 .node.clickable{cursor:pointer;}#mermaid-svg-OrvIpjSJI2OvnKC3 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .arrowheadPath{fill:#333333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-OrvIpjSJI2OvnKC3 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-OrvIpjSJI2OvnKC3 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-OrvIpjSJI2OvnKC3 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-OrvIpjSJI2OvnKC3 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-OrvIpjSJI2OvnKC3 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-OrvIpjSJI2OvnKC3 .cluster text{fill:#333;}#mermaid-svg-OrvIpjSJI2OvnKC3 .cluster span{color:#333;}#mermaid-svg-OrvIpjSJI2OvnKC3 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-OrvIpjSJI2OvnKC3 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-OrvIpjSJI2OvnKC3 rect.text{fill:none;stroke-width:0;}#mermaid-svg-OrvIpjSJI2OvnKC3 .icon-shape,#mermaid-svg-OrvIpjSJI2OvnKC3 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-OrvIpjSJI2OvnKC3 .icon-shape p,#mermaid-svg-OrvIpjSJI2OvnKC3 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-OrvIpjSJI2OvnKC3 .icon-shape .label rect,#mermaid-svg-OrvIpjSJI2OvnKC3 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-OrvIpjSJI2OvnKC3 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-OrvIpjSJI2OvnKC3 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-OrvIpjSJI2OvnKC3 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 文字:我 爱 你
Embedding
词向量:1,0 0,1 1,1
Encoder:结合顺序和上下文
Context:整句话的内部表示
Decoder:逐步生成
I love you
可以把它们理解成三个步骤:
text
Embedding:把词变成数字
Encoder:读懂整句话
Decoder:把理解的内容重新说出来
它们不是重复工作。
例如"爱"这个词的初始向量可能始终是:
text
爱 → [0, 1]
但是在下面两句话中,它的上下文不同:
text
我 爱 你
你 爱 我
Encoder 会结合前后词和词序,得到不同的上下文状态。
三、经典 Seq2Seq 架构图
#mermaid-svg-ZgNIoecZBM89kD5o{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-ZgNIoecZBM89kD5o .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZgNIoecZBM89kD5o .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZgNIoecZBM89kD5o .error-icon{fill:#552222;}#mermaid-svg-ZgNIoecZBM89kD5o .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZgNIoecZBM89kD5o .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZgNIoecZBM89kD5o .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZgNIoecZBM89kD5o .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZgNIoecZBM89kD5o .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZgNIoecZBM89kD5o .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZgNIoecZBM89kD5o .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZgNIoecZBM89kD5o .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZgNIoecZBM89kD5o .marker.cross{stroke:#333333;}#mermaid-svg-ZgNIoecZBM89kD5o svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZgNIoecZBM89kD5o p{margin:0;}#mermaid-svg-ZgNIoecZBM89kD5o .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZgNIoecZBM89kD5o .cluster-label text{fill:#333;}#mermaid-svg-ZgNIoecZBM89kD5o .cluster-label span{color:#333;}#mermaid-svg-ZgNIoecZBM89kD5o .cluster-label span p{background-color:transparent;}#mermaid-svg-ZgNIoecZBM89kD5o .label text,#mermaid-svg-ZgNIoecZBM89kD5o span{fill:#333;color:#333;}#mermaid-svg-ZgNIoecZBM89kD5o .node rect,#mermaid-svg-ZgNIoecZBM89kD5o .node circle,#mermaid-svg-ZgNIoecZBM89kD5o .node ellipse,#mermaid-svg-ZgNIoecZBM89kD5o .node polygon,#mermaid-svg-ZgNIoecZBM89kD5o .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZgNIoecZBM89kD5o .rough-node .label text,#mermaid-svg-ZgNIoecZBM89kD5o .node .label text,#mermaid-svg-ZgNIoecZBM89kD5o .image-shape .label,#mermaid-svg-ZgNIoecZBM89kD5o .icon-shape .label{text-anchor:middle;}#mermaid-svg-ZgNIoecZBM89kD5o .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ZgNIoecZBM89kD5o .rough-node .label,#mermaid-svg-ZgNIoecZBM89kD5o .node .label,#mermaid-svg-ZgNIoecZBM89kD5o .image-shape .label,#mermaid-svg-ZgNIoecZBM89kD5o .icon-shape .label{text-align:center;}#mermaid-svg-ZgNIoecZBM89kD5o .node.clickable{cursor:pointer;}#mermaid-svg-ZgNIoecZBM89kD5o .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ZgNIoecZBM89kD5o .arrowheadPath{fill:#333333;}#mermaid-svg-ZgNIoecZBM89kD5o .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZgNIoecZBM89kD5o .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZgNIoecZBM89kD5o .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZgNIoecZBM89kD5o .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ZgNIoecZBM89kD5o .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZgNIoecZBM89kD5o .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ZgNIoecZBM89kD5o .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZgNIoecZBM89kD5o .cluster text{fill:#333;}#mermaid-svg-ZgNIoecZBM89kD5o .cluster span{color:#333;}#mermaid-svg-ZgNIoecZBM89kD5o 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-ZgNIoecZBM89kD5o .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZgNIoecZBM89kD5o rect.text{fill:none;stroke-width:0;}#mermaid-svg-ZgNIoecZBM89kD5o .icon-shape,#mermaid-svg-ZgNIoecZBM89kD5o .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZgNIoecZBM89kD5o .icon-shape p,#mermaid-svg-ZgNIoecZBM89kD5o .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ZgNIoecZBM89kD5o .icon-shape .label rect,#mermaid-svg-ZgNIoecZBM89kD5o .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZgNIoecZBM89kD5o .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZgNIoecZBM89kD5o .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZgNIoecZBM89kD5o :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 我
Encoder 单元
爱
Encoder 单元
你
Encoder 单元
h₁
h₂
h₃
Context
Decoder 单元
I
Decoder 单元
love
Decoder 单元
you
Decoder 单元
其中:
- :Begin Of Sentence,表示开始;
- :End Of Sentence,表示结束;
- Encoder 负责"读";
- Decoder 负责"写"。
LSTM 或 GRU 可以作为 Encoder 和 Decoder 内部的基本计算单元:
text
LSTM Encoder + LSTM Decoder = LSTM Seq2Seq
GRU Encoder + GRU Decoder = GRU Seq2Seq
四、完整数值例子:我爱你 → I love you
为了方便手算,下面使用二维向量和简化的循环公式。真实模型会使用更大的向量、学习得到的矩阵以及 LSTM/GRU 门控机制,但数据流完全一致。
4.1 第一步:给源语言做 Embedding
假设源语言词向量如下:
我 → 1 , 0 \text{我}\rightarrow1,0 我→1,0
爱 → 0 , 1 \text{爱}\rightarrow0,1 爱→0,1
你 → 1 , 1 \text{你}\rightarrow1,1 你→1,1
于是:
text
输入文字:我 爱 你
Embedding:[1,0] [0,1] [1,1]
这时模型只是把文字变成了数字。
4.2 第二步:Encoder 逐个读取
为了演示,假设 Encoder 使用:
h t = 0.5 h t − 1 + 0.5 e t h_t=0.5h_{t-1}+0.5e_t ht=0.5ht−1+0.5et
初始状态:
h 0 = 0 , 0 h_0=0,0 h0=0,0
读取"我"
e 1 = 1 , 0 e_1=1,0 e1=1,0
h 1 = 0.5 0 , 0 + 0.5 1 , 0 = 0.5 , 0 h_1=0.50,0+0.51,0=0.5,0 h1=0.50,0+0.51,0=0.5,0
此时 Encoder 只读到了"我"。
读取"爱"
e 2 = 0 , 1 e_2=0,1 e2=0,1
h 2 = 0.5 0.5 , 0 + 0.5 0 , 1 h_2=0.50.5,0+0.50,1 h2=0.50.5,0+0.50,1
h 2 = 0.25 , 0.5 h_2=0.25,0.5 h2=0.25,0.5
此时 h₂ 已经融合了"我"和"爱"。
读取"你"
e 3 = 1 , 1 e_3=1,1 e3=1,1
h 3 = 0.5 0.25 , 0.5 + 0.5 1 , 1 h_3=0.50.25,0.5+0.51,1 h3=0.50.25,0.5+0.51,1
h 3 = 0.125 , 0.25 + 0.5 , 0.5 h_3=0.125,0.25+0.5,0.5 h3=0.125,0.25+0.5,0.5
h 3 = 0.625 , 0.75 h_3=0.625,0.75 h3=0.625,0.75
最终 Context 是:
c = h 3 = 0.625 , 0.75 \boxed{c=h_3=0.625,0.75} c=h3=0.625,0.75
Encoder 的状态变化如下:
#mermaid-svg-0EpBvkmZuh7GTgRq{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-0EpBvkmZuh7GTgRq .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-0EpBvkmZuh7GTgRq .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-0EpBvkmZuh7GTgRq .error-icon{fill:#552222;}#mermaid-svg-0EpBvkmZuh7GTgRq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-0EpBvkmZuh7GTgRq .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-0EpBvkmZuh7GTgRq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-0EpBvkmZuh7GTgRq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-0EpBvkmZuh7GTgRq .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-0EpBvkmZuh7GTgRq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-0EpBvkmZuh7GTgRq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-0EpBvkmZuh7GTgRq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-0EpBvkmZuh7GTgRq .marker.cross{stroke:#333333;}#mermaid-svg-0EpBvkmZuh7GTgRq svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-0EpBvkmZuh7GTgRq p{margin:0;}#mermaid-svg-0EpBvkmZuh7GTgRq .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-0EpBvkmZuh7GTgRq .cluster-label text{fill:#333;}#mermaid-svg-0EpBvkmZuh7GTgRq .cluster-label span{color:#333;}#mermaid-svg-0EpBvkmZuh7GTgRq .cluster-label span p{background-color:transparent;}#mermaid-svg-0EpBvkmZuh7GTgRq .label text,#mermaid-svg-0EpBvkmZuh7GTgRq span{fill:#333;color:#333;}#mermaid-svg-0EpBvkmZuh7GTgRq .node rect,#mermaid-svg-0EpBvkmZuh7GTgRq .node circle,#mermaid-svg-0EpBvkmZuh7GTgRq .node ellipse,#mermaid-svg-0EpBvkmZuh7GTgRq .node polygon,#mermaid-svg-0EpBvkmZuh7GTgRq .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-0EpBvkmZuh7GTgRq .rough-node .label text,#mermaid-svg-0EpBvkmZuh7GTgRq .node .label text,#mermaid-svg-0EpBvkmZuh7GTgRq .image-shape .label,#mermaid-svg-0EpBvkmZuh7GTgRq .icon-shape .label{text-anchor:middle;}#mermaid-svg-0EpBvkmZuh7GTgRq .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-0EpBvkmZuh7GTgRq .rough-node .label,#mermaid-svg-0EpBvkmZuh7GTgRq .node .label,#mermaid-svg-0EpBvkmZuh7GTgRq .image-shape .label,#mermaid-svg-0EpBvkmZuh7GTgRq .icon-shape .label{text-align:center;}#mermaid-svg-0EpBvkmZuh7GTgRq .node.clickable{cursor:pointer;}#mermaid-svg-0EpBvkmZuh7GTgRq .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-0EpBvkmZuh7GTgRq .arrowheadPath{fill:#333333;}#mermaid-svg-0EpBvkmZuh7GTgRq .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-0EpBvkmZuh7GTgRq .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-0EpBvkmZuh7GTgRq .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0EpBvkmZuh7GTgRq .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-0EpBvkmZuh7GTgRq .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0EpBvkmZuh7GTgRq .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-0EpBvkmZuh7GTgRq .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-0EpBvkmZuh7GTgRq .cluster text{fill:#333;}#mermaid-svg-0EpBvkmZuh7GTgRq .cluster span{color:#333;}#mermaid-svg-0EpBvkmZuh7GTgRq 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-0EpBvkmZuh7GTgRq .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-0EpBvkmZuh7GTgRq rect.text{fill:none;stroke-width:0;}#mermaid-svg-0EpBvkmZuh7GTgRq .icon-shape,#mermaid-svg-0EpBvkmZuh7GTgRq .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0EpBvkmZuh7GTgRq .icon-shape p,#mermaid-svg-0EpBvkmZuh7GTgRq .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-0EpBvkmZuh7GTgRq .icon-shape .label rect,#mermaid-svg-0EpBvkmZuh7GTgRq .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0EpBvkmZuh7GTgRq .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-0EpBvkmZuh7GTgRq .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-0EpBvkmZuh7GTgRq :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 我 → 1,0
h₁ = 0.5,0
爱 → 0,1
h₂ = 0.25,0.5
你 → 1,1
h₃ = 0.625,0.75
Context
| 已读取的词 | 当前词向量 | Encoder 状态 |
|---|---|---|
| 我 | 1 , 0 1,0 1,0 | h 1 = 0.5 , 0 h_1=0.5,0 h1=0.5,0 |
| 我、爱 | 0 , 1 0,1 0,1 | h 2 = 0.25 , 0.5 h_2=0.25,0.5 h2=0.25,0.5 |
| 我、爱、你 | 1 , 1 1,1 1,1 | h 3 = 0.625 , 0.75 h_3=0.625,0.75 h3=0.625,0.75 |
4.3 第三步:Decoder 的目标词向量
Decoder 也需要向量作为输入。假设目标语言词向量为:
<BOS> → 1 , 0 \text{<BOS>}\rightarrow1,0 <BOS>→1,0
I → 0 , 1 \text{I}\rightarrow0,1 I→0,1
love → 1 , 1 \text{love}\rightarrow1,1 love→1,1
you → 1 , − 1 \text{you}\rightarrow1,-1 you→1,−1
注意:Decoder 第一步并不是直接把 I 作为输入,而是从 开始。
#mermaid-svg-4atTdmTkF2leSiBe{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-4atTdmTkF2leSiBe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-4atTdmTkF2leSiBe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-4atTdmTkF2leSiBe .error-icon{fill:#552222;}#mermaid-svg-4atTdmTkF2leSiBe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4atTdmTkF2leSiBe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-4atTdmTkF2leSiBe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4atTdmTkF2leSiBe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4atTdmTkF2leSiBe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-4atTdmTkF2leSiBe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4atTdmTkF2leSiBe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4atTdmTkF2leSiBe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4atTdmTkF2leSiBe .marker.cross{stroke:#333333;}#mermaid-svg-4atTdmTkF2leSiBe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4atTdmTkF2leSiBe p{margin:0;}#mermaid-svg-4atTdmTkF2leSiBe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-4atTdmTkF2leSiBe .cluster-label text{fill:#333;}#mermaid-svg-4atTdmTkF2leSiBe .cluster-label span{color:#333;}#mermaid-svg-4atTdmTkF2leSiBe .cluster-label span p{background-color:transparent;}#mermaid-svg-4atTdmTkF2leSiBe .label text,#mermaid-svg-4atTdmTkF2leSiBe span{fill:#333;color:#333;}#mermaid-svg-4atTdmTkF2leSiBe .node rect,#mermaid-svg-4atTdmTkF2leSiBe .node circle,#mermaid-svg-4atTdmTkF2leSiBe .node ellipse,#mermaid-svg-4atTdmTkF2leSiBe .node polygon,#mermaid-svg-4atTdmTkF2leSiBe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-4atTdmTkF2leSiBe .rough-node .label text,#mermaid-svg-4atTdmTkF2leSiBe .node .label text,#mermaid-svg-4atTdmTkF2leSiBe .image-shape .label,#mermaid-svg-4atTdmTkF2leSiBe .icon-shape .label{text-anchor:middle;}#mermaid-svg-4atTdmTkF2leSiBe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-4atTdmTkF2leSiBe .rough-node .label,#mermaid-svg-4atTdmTkF2leSiBe .node .label,#mermaid-svg-4atTdmTkF2leSiBe .image-shape .label,#mermaid-svg-4atTdmTkF2leSiBe .icon-shape .label{text-align:center;}#mermaid-svg-4atTdmTkF2leSiBe .node.clickable{cursor:pointer;}#mermaid-svg-4atTdmTkF2leSiBe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-4atTdmTkF2leSiBe .arrowheadPath{fill:#333333;}#mermaid-svg-4atTdmTkF2leSiBe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-4atTdmTkF2leSiBe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-4atTdmTkF2leSiBe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4atTdmTkF2leSiBe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-4atTdmTkF2leSiBe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4atTdmTkF2leSiBe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-4atTdmTkF2leSiBe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-4atTdmTkF2leSiBe .cluster text{fill:#333;}#mermaid-svg-4atTdmTkF2leSiBe .cluster span{color:#333;}#mermaid-svg-4atTdmTkF2leSiBe 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-4atTdmTkF2leSiBe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-4atTdmTkF2leSiBe rect.text{fill:none;stroke-width:0;}#mermaid-svg-4atTdmTkF2leSiBe .icon-shape,#mermaid-svg-4atTdmTkF2leSiBe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-4atTdmTkF2leSiBe .icon-shape p,#mermaid-svg-4atTdmTkF2leSiBe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-4atTdmTkF2leSiBe .icon-shape .label rect,#mermaid-svg-4atTdmTkF2leSiBe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-4atTdmTkF2leSiBe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-4atTdmTkF2leSiBe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-4atTdmTkF2leSiBe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} → 1,0
Decoder 第一步
输出 I
Embedding:0,1
Decoder 第二步
输出 love
Embedding:1,1
Decoder 第三步
输出 you
Embedding:1,-1
Decoder 第四步
输出
假设 Decoder 状态更新公式为:
s t = 0.6 c + 0.4 s t − 1 + 0.1 y t − 1 s_t=0.6c+0.4s_{t-1}+0.1y_{t-1} st=0.6c+0.4st−1+0.1yt−1
其中:
- c=0.625,0.75 是 Encoder 的 Context;
- s_t 是 Decoder 当前状态;
- y_{t-1} 是上一步输入词的 Embedding;
- s_0=c。
4.4 Decoder 第一步:生成 I
上一步输入是 :
y 0 = 1 , 0 y_0=1,0 y0=1,0
计算:
s 1 = 0.6 0.625 , 0.75 + 0.4 0.625 , 0.75 + 0.1 1 , 0 s_1=0.60.625,0.75+0.40.625,0.75+0.11,0 s1=0.60.625,0.75+0.40.625,0.75+0.11,0
s 1 = 0.725 , 0.75 s_1=0.725,0.75 s1=0.725,0.75
输出层把 s₁ 转换成每个候选词的分数。假设词表为:
text
<BOS>, I, love, you, <EOS>
输出 logits:
| 候选词 | logit |
|---|---|
| -2.0 | |
| I | 3.0 |
| love | 1.0 |
| you | 0.5 |
| -3.0 |
经过 Softmax 后:
| 候选词 | 概率 |
|---|---|
| 0.005 | |
| I | 0.815 |
| love | 0.110 |
| you | 0.067 |
| 0.002 |
最大概率是 I,因此:
Decoder 第一步输出 I \boxed{\text{Decoder 第一步输出 I}} Decoder 第一步输出 I
4.5 Decoder 第二步:生成 love
现在把 I 的向量作为下一步输入:
y 1 = 0 , 1 y_1=0,1 y1=0,1
s 2 = 0.6 c + 0.4 s 1 + 0.1 y 1 s_2=0.6c+0.4s_1+0.1y_1 s2=0.6c+0.4s1+0.1y1
代入:
s 2 = 0.6 0.625 , 0.75 + 0.4 0.725 , 0.75 + 0.1 0 , 1 s_2=0.60.625,0.75+0.40.725,0.75+0.10,1 s2=0.60.625,0.75+0.40.725,0.75+0.10,1
s 2 = 0.375 , 0.45 + 0.29 , 0.30 + 0 , 0.1 s_2=0.375,0.45+0.29,0.30+0,0.1 s2=0.375,0.45+0.29,0.30+0,0.1
s 2 = 0.665 , 0.85 s_2=0.665,0.85 s2=0.665,0.85
输出层得到:
| 候选词 | logit |
|---|---|
| -3.0 | |
| I | 0.5 |
| love | 3.0 |
| you | 0.8 |
| -2.0 |
Softmax 概率约为:
| 候选词 | 概率 |
|---|---|
| 0.002 | |
| I | 0.068 |
| love | 0.832 |
| you | 0.092 |
| 0.006 |
因此:
Decoder 第二步输出 love \boxed{\text{Decoder 第二步输出 love}} Decoder 第二步输出 love
4.6 Decoder 第三步:生成 you
把 love 的向量作为下一步输入:
y 2 = 1 , 1 y_2=1,1 y2=1,1
s 3 = 0.6 c + 0.4 s 2 + 0.1 y 2 s_3=0.6c+0.4s_2+0.1y_2 s3=0.6c+0.4s2+0.1y2
s 3 = 0.6 0.625 , 0.75 + 0.4 0.665 , 0.85 + 0.1 1 , 1 s_3=0.60.625,0.75+0.40.665,0.85+0.11,1 s3=0.60.625,0.75+0.40.665,0.85+0.11,1
s 3 = 0.375 , 0.45 + 0.266 , 0.34 + 0.1 , 0.1 s_3=0.375,0.45+0.266,0.34+0.1,0.1 s3=0.375,0.45+0.266,0.34+0.1,0.1
s 3 = 0.741 , 0.89 s_3=0.741,0.89 s3=0.741,0.89
输出层得到:
| 候选词 | logit |
|---|---|
| -3.0 | |
| I | 0.2 |
| love | 0.5 |
| you | 3.0 |
| -1.0 |
Softmax 概率约为:
| 候选词 | 概率 |
|---|---|
| 0.002 | |
| I | 0.052 |
| love | 0.071 |
| you | 0.860 |
| 0.016 |
因此:
Decoder 第三步输出 you \boxed{\text{Decoder 第三步输出 you}} Decoder 第三步输出 you
4.7 Decoder 第四步:生成结束符
Decoder 再读取 you 后,输出层可能得到:
| 候选词 | 概率 |
|---|---|
| 0.015 | |
| I | 0.046 |
| love | 0.051 |
| you | 0.046 |
| 0.841 |
最大概率是 ,所以生成结束。
完整输出:
text
I love you
五、把全部数字串起来
#mermaid-svg-u2Nm8tf1rJJzzo1R{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-u2Nm8tf1rJJzzo1R .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-u2Nm8tf1rJJzzo1R .error-icon{fill:#552222;}#mermaid-svg-u2Nm8tf1rJJzzo1R .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-u2Nm8tf1rJJzzo1R .marker{fill:#333333;stroke:#333333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .marker.cross{stroke:#333333;}#mermaid-svg-u2Nm8tf1rJJzzo1R svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-u2Nm8tf1rJJzzo1R p{margin:0;}#mermaid-svg-u2Nm8tf1rJJzzo1R .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .cluster-label text{fill:#333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .cluster-label span{color:#333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .cluster-label span p{background-color:transparent;}#mermaid-svg-u2Nm8tf1rJJzzo1R .label text,#mermaid-svg-u2Nm8tf1rJJzzo1R span{fill:#333;color:#333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .node rect,#mermaid-svg-u2Nm8tf1rJJzzo1R .node circle,#mermaid-svg-u2Nm8tf1rJJzzo1R .node ellipse,#mermaid-svg-u2Nm8tf1rJJzzo1R .node polygon,#mermaid-svg-u2Nm8tf1rJJzzo1R .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-u2Nm8tf1rJJzzo1R .rough-node .label text,#mermaid-svg-u2Nm8tf1rJJzzo1R .node .label text,#mermaid-svg-u2Nm8tf1rJJzzo1R .image-shape .label,#mermaid-svg-u2Nm8tf1rJJzzo1R .icon-shape .label{text-anchor:middle;}#mermaid-svg-u2Nm8tf1rJJzzo1R .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-u2Nm8tf1rJJzzo1R .rough-node .label,#mermaid-svg-u2Nm8tf1rJJzzo1R .node .label,#mermaid-svg-u2Nm8tf1rJJzzo1R .image-shape .label,#mermaid-svg-u2Nm8tf1rJJzzo1R .icon-shape .label{text-align:center;}#mermaid-svg-u2Nm8tf1rJJzzo1R .node.clickable{cursor:pointer;}#mermaid-svg-u2Nm8tf1rJJzzo1R .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .arrowheadPath{fill:#333333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-u2Nm8tf1rJJzzo1R .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-u2Nm8tf1rJJzzo1R .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-u2Nm8tf1rJJzzo1R .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-u2Nm8tf1rJJzzo1R .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-u2Nm8tf1rJJzzo1R .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-u2Nm8tf1rJJzzo1R .cluster text{fill:#333;}#mermaid-svg-u2Nm8tf1rJJzzo1R .cluster span{color:#333;}#mermaid-svg-u2Nm8tf1rJJzzo1R 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-u2Nm8tf1rJJzzo1R .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-u2Nm8tf1rJJzzo1R rect.text{fill:none;stroke-width:0;}#mermaid-svg-u2Nm8tf1rJJzzo1R .icon-shape,#mermaid-svg-u2Nm8tf1rJJzzo1R .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-u2Nm8tf1rJJzzo1R .icon-shape p,#mermaid-svg-u2Nm8tf1rJJzzo1R .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-u2Nm8tf1rJJzzo1R .icon-shape .label rect,#mermaid-svg-u2Nm8tf1rJJzzo1R .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-u2Nm8tf1rJJzzo1R .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-u2Nm8tf1rJJzzo1R .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-u2Nm8tf1rJJzzo1R :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 我
1,0
h₁=0.5,0
爱
0,1
h₂=0.25,0.5
你
1,1
h₃=0.625,0.75
Context=0.625,0.75
Decoder s₁=0.725,0.75
I
Decoder s₂=0.665,0.85
love
Decoder s₃=0.741,0.89
you
| 阶段 | 数值或结果 |
|---|---|
| 我经过 Embedding | 1 , 0 1,0 1,0 |
| 爱经过 Embedding | 0 , 1 0,1 0,1 |
| 你经过 Embedding | 1 , 1 1,1 1,1 |
| Encoder 读取我 | h 1 = 0.5 , 0 h_1=0.5,0 h1=0.5,0 |
| Encoder 读取爱 | h 2 = 0.25 , 0.5 h_2=0.25,0.5 h2=0.25,0.5 |
| Encoder 读取你 | h 3 = 0.625 , 0.75 h_3=0.625,0.75 h3=0.625,0.75 |
| Decoder 第一步 | 输出 I |
| Decoder 第二步 | 输出 love |
| Decoder 第三步 | 输出 you |
| Decoder 第四步 | 输出 |
六、训练时为什么会出现 Teacher Forcing?
训练时,Decoder 往往使用正确答案作为下一步输入:
#mermaid-svg-ekm0JzUqRYXfuYrS{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-ekm0JzUqRYXfuYrS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ekm0JzUqRYXfuYrS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ekm0JzUqRYXfuYrS .error-icon{fill:#552222;}#mermaid-svg-ekm0JzUqRYXfuYrS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ekm0JzUqRYXfuYrS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ekm0JzUqRYXfuYrS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ekm0JzUqRYXfuYrS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ekm0JzUqRYXfuYrS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ekm0JzUqRYXfuYrS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ekm0JzUqRYXfuYrS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ekm0JzUqRYXfuYrS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ekm0JzUqRYXfuYrS .marker.cross{stroke:#333333;}#mermaid-svg-ekm0JzUqRYXfuYrS svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ekm0JzUqRYXfuYrS p{margin:0;}#mermaid-svg-ekm0JzUqRYXfuYrS .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ekm0JzUqRYXfuYrS .cluster-label text{fill:#333;}#mermaid-svg-ekm0JzUqRYXfuYrS .cluster-label span{color:#333;}#mermaid-svg-ekm0JzUqRYXfuYrS .cluster-label span p{background-color:transparent;}#mermaid-svg-ekm0JzUqRYXfuYrS .label text,#mermaid-svg-ekm0JzUqRYXfuYrS span{fill:#333;color:#333;}#mermaid-svg-ekm0JzUqRYXfuYrS .node rect,#mermaid-svg-ekm0JzUqRYXfuYrS .node circle,#mermaid-svg-ekm0JzUqRYXfuYrS .node ellipse,#mermaid-svg-ekm0JzUqRYXfuYrS .node polygon,#mermaid-svg-ekm0JzUqRYXfuYrS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ekm0JzUqRYXfuYrS .rough-node .label text,#mermaid-svg-ekm0JzUqRYXfuYrS .node .label text,#mermaid-svg-ekm0JzUqRYXfuYrS .image-shape .label,#mermaid-svg-ekm0JzUqRYXfuYrS .icon-shape .label{text-anchor:middle;}#mermaid-svg-ekm0JzUqRYXfuYrS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ekm0JzUqRYXfuYrS .rough-node .label,#mermaid-svg-ekm0JzUqRYXfuYrS .node .label,#mermaid-svg-ekm0JzUqRYXfuYrS .image-shape .label,#mermaid-svg-ekm0JzUqRYXfuYrS .icon-shape .label{text-align:center;}#mermaid-svg-ekm0JzUqRYXfuYrS .node.clickable{cursor:pointer;}#mermaid-svg-ekm0JzUqRYXfuYrS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ekm0JzUqRYXfuYrS .arrowheadPath{fill:#333333;}#mermaid-svg-ekm0JzUqRYXfuYrS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ekm0JzUqRYXfuYrS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ekm0JzUqRYXfuYrS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ekm0JzUqRYXfuYrS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ekm0JzUqRYXfuYrS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ekm0JzUqRYXfuYrS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ekm0JzUqRYXfuYrS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ekm0JzUqRYXfuYrS .cluster text{fill:#333;}#mermaid-svg-ekm0JzUqRYXfuYrS .cluster span{color:#333;}#mermaid-svg-ekm0JzUqRYXfuYrS 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-ekm0JzUqRYXfuYrS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ekm0JzUqRYXfuYrS rect.text{fill:none;stroke-width:0;}#mermaid-svg-ekm0JzUqRYXfuYrS .icon-shape,#mermaid-svg-ekm0JzUqRYXfuYrS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ekm0JzUqRYXfuYrS .icon-shape p,#mermaid-svg-ekm0JzUqRYXfuYrS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ekm0JzUqRYXfuYrS .icon-shape .label rect,#mermaid-svg-ekm0JzUqRYXfuYrS .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ekm0JzUqRYXfuYrS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ekm0JzUqRYXfuYrS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ekm0JzUqRYXfuYrS :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Decoder
预测 I
正确答案 I
下一步 Decoder
预测 love
正确答案 love
下一步 Decoder
预测 you
训练时使用:
text
输入 <BOS> → 预测 I
输入 I → 预测 love
输入 love → 预测 you
预测时没有正确答案,只能使用模型自己的输出:
text
输入 <BOS> → 预测 I
输入 I → 预测 love
输入 love → 预测 you
这种训练技巧叫 Teacher Forcing。它可以让训练更稳定,但如果早期预测错了,预测阶段的错误可能会逐步累积。
七、Attention 为什么重要?
早期 Seq2Seq 只把 Encoder 最后的状态传给 Decoder:
text
我 爱 你 → 一个 Context 向量 → Decoder
句子很长时,一个向量可能难以保存所有细节。
Attention 允许 Decoder 在生成每个词时,查看 Encoder 的所有中间状态:
#mermaid-svg-8nIRQP3KIwbhO1jh{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-8nIRQP3KIwbhO1jh .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-8nIRQP3KIwbhO1jh .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-8nIRQP3KIwbhO1jh .error-icon{fill:#552222;}#mermaid-svg-8nIRQP3KIwbhO1jh .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8nIRQP3KIwbhO1jh .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-8nIRQP3KIwbhO1jh .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8nIRQP3KIwbhO1jh .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8nIRQP3KIwbhO1jh .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-8nIRQP3KIwbhO1jh .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8nIRQP3KIwbhO1jh .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8nIRQP3KIwbhO1jh .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8nIRQP3KIwbhO1jh .marker.cross{stroke:#333333;}#mermaid-svg-8nIRQP3KIwbhO1jh svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8nIRQP3KIwbhO1jh p{margin:0;}#mermaid-svg-8nIRQP3KIwbhO1jh .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-8nIRQP3KIwbhO1jh .cluster-label text{fill:#333;}#mermaid-svg-8nIRQP3KIwbhO1jh .cluster-label span{color:#333;}#mermaid-svg-8nIRQP3KIwbhO1jh .cluster-label span p{background-color:transparent;}#mermaid-svg-8nIRQP3KIwbhO1jh .label text,#mermaid-svg-8nIRQP3KIwbhO1jh span{fill:#333;color:#333;}#mermaid-svg-8nIRQP3KIwbhO1jh .node rect,#mermaid-svg-8nIRQP3KIwbhO1jh .node circle,#mermaid-svg-8nIRQP3KIwbhO1jh .node ellipse,#mermaid-svg-8nIRQP3KIwbhO1jh .node polygon,#mermaid-svg-8nIRQP3KIwbhO1jh .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8nIRQP3KIwbhO1jh .rough-node .label text,#mermaid-svg-8nIRQP3KIwbhO1jh .node .label text,#mermaid-svg-8nIRQP3KIwbhO1jh .image-shape .label,#mermaid-svg-8nIRQP3KIwbhO1jh .icon-shape .label{text-anchor:middle;}#mermaid-svg-8nIRQP3KIwbhO1jh .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-8nIRQP3KIwbhO1jh .rough-node .label,#mermaid-svg-8nIRQP3KIwbhO1jh .node .label,#mermaid-svg-8nIRQP3KIwbhO1jh .image-shape .label,#mermaid-svg-8nIRQP3KIwbhO1jh .icon-shape .label{text-align:center;}#mermaid-svg-8nIRQP3KIwbhO1jh .node.clickable{cursor:pointer;}#mermaid-svg-8nIRQP3KIwbhO1jh .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-8nIRQP3KIwbhO1jh .arrowheadPath{fill:#333333;}#mermaid-svg-8nIRQP3KIwbhO1jh .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-8nIRQP3KIwbhO1jh .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-8nIRQP3KIwbhO1jh .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8nIRQP3KIwbhO1jh .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-8nIRQP3KIwbhO1jh .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8nIRQP3KIwbhO1jh .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-8nIRQP3KIwbhO1jh .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-8nIRQP3KIwbhO1jh .cluster text{fill:#333;}#mermaid-svg-8nIRQP3KIwbhO1jh .cluster span{color:#333;}#mermaid-svg-8nIRQP3KIwbhO1jh 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-8nIRQP3KIwbhO1jh .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-8nIRQP3KIwbhO1jh rect.text{fill:none;stroke-width:0;}#mermaid-svg-8nIRQP3KIwbhO1jh .icon-shape,#mermaid-svg-8nIRQP3KIwbhO1jh .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8nIRQP3KIwbhO1jh .icon-shape p,#mermaid-svg-8nIRQP3KIwbhO1jh .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-8nIRQP3KIwbhO1jh .icon-shape .label rect,#mermaid-svg-8nIRQP3KIwbhO1jh .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8nIRQP3KIwbhO1jh .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-8nIRQP3KIwbhO1jh .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-8nIRQP3KIwbhO1jh :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 我
h₁
爱
h₂
你
h₃
生成 I 时的注意力
生成 love 时的注意力
生成 you 时的注意力
I
love
you
直观上可以理解为:
text
生成 I → 重点参考"我"
生成 love → 重点参考"爱"
生成 you → 重点参考"你"
因此,Attention 缓解了"所有信息必须压缩到最后一个 Context"的瓶颈。
八、哪些任务需要 Decoder?
| 任务 | 常见结构 | 是否通常需要 Decoder |
|---|---|---|
| 情感分类 | Embedding → Encoder → 分类器 | 通常不需要 |
| 命名实体识别 | Embedding → Encoder → 标签层 | 通常不需要 |
| 句子相似度 | Embedding → Encoder → 相似度计算 | 通常不需要 |
| 机器翻译 | Encoder → Decoder | 需要 |
| 文本摘要 | Encoder → Decoder | 通常需要 |
| 对话生成 | Decoder 或 Encoder-Decoder | 需要生成部分 |
| GPT 续写 | Decoder-only | 使用 Decoder 结构 |
所以,Embedding 并不能替代 Encoder 和 Decoder:
- 只做分类时,Embedding 加 Encoder 可能就够了;
- 要把一串输入转换成另一串输出时,通常需要 Decoder;
- GPT 这类模型没有经典的独立 Encoder,而是使用 Decoder-only 结构。
状态向量到底长什么样?
前面的 Encoder、Decoder 也会使用状态。为了避免把几个概念混在一起,可以把它们分成三层:
#mermaid-svg-ifcNKCWK2KVYEwYw{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-ifcNKCWK2KVYEwYw .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ifcNKCWK2KVYEwYw .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ifcNKCWK2KVYEwYw .error-icon{fill:#552222;}#mermaid-svg-ifcNKCWK2KVYEwYw .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ifcNKCWK2KVYEwYw .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ifcNKCWK2KVYEwYw .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ifcNKCWK2KVYEwYw .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ifcNKCWK2KVYEwYw .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ifcNKCWK2KVYEwYw .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ifcNKCWK2KVYEwYw .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ifcNKCWK2KVYEwYw .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ifcNKCWK2KVYEwYw .marker.cross{stroke:#333333;}#mermaid-svg-ifcNKCWK2KVYEwYw svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ifcNKCWK2KVYEwYw p{margin:0;}#mermaid-svg-ifcNKCWK2KVYEwYw .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ifcNKCWK2KVYEwYw .cluster-label text{fill:#333;}#mermaid-svg-ifcNKCWK2KVYEwYw .cluster-label span{color:#333;}#mermaid-svg-ifcNKCWK2KVYEwYw .cluster-label span p{background-color:transparent;}#mermaid-svg-ifcNKCWK2KVYEwYw .label text,#mermaid-svg-ifcNKCWK2KVYEwYw span{fill:#333;color:#333;}#mermaid-svg-ifcNKCWK2KVYEwYw .node rect,#mermaid-svg-ifcNKCWK2KVYEwYw .node circle,#mermaid-svg-ifcNKCWK2KVYEwYw .node ellipse,#mermaid-svg-ifcNKCWK2KVYEwYw .node polygon,#mermaid-svg-ifcNKCWK2KVYEwYw .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ifcNKCWK2KVYEwYw .rough-node .label text,#mermaid-svg-ifcNKCWK2KVYEwYw .node .label text,#mermaid-svg-ifcNKCWK2KVYEwYw .image-shape .label,#mermaid-svg-ifcNKCWK2KVYEwYw .icon-shape .label{text-anchor:middle;}#mermaid-svg-ifcNKCWK2KVYEwYw .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ifcNKCWK2KVYEwYw .rough-node .label,#mermaid-svg-ifcNKCWK2KVYEwYw .node .label,#mermaid-svg-ifcNKCWK2KVYEwYw .image-shape .label,#mermaid-svg-ifcNKCWK2KVYEwYw .icon-shape .label{text-align:center;}#mermaid-svg-ifcNKCWK2KVYEwYw .node.clickable{cursor:pointer;}#mermaid-svg-ifcNKCWK2KVYEwYw .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ifcNKCWK2KVYEwYw .arrowheadPath{fill:#333333;}#mermaid-svg-ifcNKCWK2KVYEwYw .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ifcNKCWK2KVYEwYw .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ifcNKCWK2KVYEwYw .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ifcNKCWK2KVYEwYw .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ifcNKCWK2KVYEwYw .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ifcNKCWK2KVYEwYw .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ifcNKCWK2KVYEwYw .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ifcNKCWK2KVYEwYw .cluster text{fill:#333;}#mermaid-svg-ifcNKCWK2KVYEwYw .cluster span{color:#333;}#mermaid-svg-ifcNKCWK2KVYEwYw 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-ifcNKCWK2KVYEwYw .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ifcNKCWK2KVYEwYw rect.text{fill:none;stroke-width:0;}#mermaid-svg-ifcNKCWK2KVYEwYw .icon-shape,#mermaid-svg-ifcNKCWK2KVYEwYw .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ifcNKCWK2KVYEwYw .icon-shape p,#mermaid-svg-ifcNKCWK2KVYEwYw .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ifcNKCWK2KVYEwYw .icon-shape .label rect,#mermaid-svg-ifcNKCWK2KVYEwYw .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ifcNKCWK2KVYEwYw .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ifcNKCWK2KVYEwYw .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ifcNKCWK2KVYEwYw :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 词:我
Embedding:初始词向量
Encoder 状态:结合上下文后的向量
Context:对整句的表示
Decoder 状态:生成目标词时的工作向量
例如,一个词经过 Embedding 后可能是:
text
我 → [1.0, 0.0]
当 Encoder 结合前后词和词序后,状态可能变成:
text
h₁ = [0.50, 0.00]
h₂ = [0.25, 0.50]
h₃ = [0.625, 0.75]
这里的 (h_3) 是读完整个"我爱你"后留下的状态。它不是原句的文字副本,而是一组供后续计算使用的数字。
Decoder 也有自己的状态,例如:
text
s₁ = [0.725, 0.750]
s₂ = [0.665, 0.850]
s₃ = [0.741, 0.890]
每一步生成一个目标词后,Decoder 会更新状态,再决定下一个词的概率。
需要注意:
- Embedding 是查表得到的初始向量;
- Encoder 状态包含输入序列的上下文影响;
- Context 是传给 Decoder 的句子级表示;
- Decoder 状态是生成目标序列时不断变化的工作记忆。
这些向量中的单个数字通常没有单独、固定、可读的语义。真正有意义的是整组数字经过后续矩阵运算后,对预测结果产生的影响。
九、Encoder、Decoder 和 LSTM/GRU 的关系
它们处在不同层次:
| 概念 | 负责什么 |
|---|---|
| Embedding | 把词转换成向量 |
| LSTM/GRU | 更新序列状态、保留历史信息 |
| Encoder | 读取并理解输入序列 |
| Decoder | 根据内部表示生成输出序列 |
| Seq2Seq | 整体的序列转换框架 |
例如:
text
我 → Embedding → LSTM Encoder → Context
Context → LSTM Decoder → I love you
也可以换成:
text
我 → Embedding → GRU Encoder → Context
Context → GRU Decoder → I love you
Embedding、Encoder 和 Decoder 不是互相替代的关系,而是流水线中的不同环节。
十、总结
把完整过程记成下面这条链:
文字 → Embedding → Encoder → Context → Decoder → Softmax → 输出文字 \boxed{ \text{文字} \rightarrow \text{Embedding} \rightarrow \text{Encoder} \rightarrow \text{Context} \rightarrow \text{Decoder} \rightarrow \text{Softmax} \rightarrow \text{输出文字} } 文字→Embedding→Encoder→Context→Decoder→Softmax→输出文字
对于例子"我爱你 → I love you":
text
我、爱、你
↓ Embedding
[1,0]、[0,1]、[1,1]
↓ Encoder
Context=[0.625,0.75]
↓ Decoder
I → love → you → <EOS>
最简单的理解是:
Embedding 负责把词数字化,Encoder 负责把输入读懂,Decoder 负责把结果逐词说出来,Seq2Seq 负责把一串输入转换成另一串输出。