[编码器 Encoder] ←→ [解码器 Decoder]
编码器:
- 输入:源语言序列
- 输出:每个词的上下文表示(embedding)
解码器: - 输入:目标语言序列+编码器输出
- 输出:下一个词的概率分布(目标句子生成)
inputs->inputs Embedding+Positional Encoding->N*encoder
output->outputs Embedding+Positional Encoding->N*decoder->Linear->softmax
Encoder Block and Decoder Block:
- 自注意力机制(论文中使用的是MultiHead Attention)
- 前馈网络
- 编解码注意力层
N*Encoder and N*Decoder:
编码器部分的流程:
Input Embedding + Positional Encoding↓
[Multi-Head Self-Attention] → [Add & Norm]↓
[Feed-Forward Network (FFN)] → [Add & Norm]↓
Output of this Encoder Layer
- 词嵌入,假设输入的是句子"The cat sat"
通过一个embedding得到向量 X X X - 位置编码(Positional Encodeing)
X i n p u t = X + P E X_{input}=X+PE Xinput=X+PE - 多头自注意力机制
- 每个头有自己的权重矩阵,可为 W i Q , W i K , Q i V W_i^Q,W_i^K,Q_i^V WiQ,WiK,QiV
- 自注意力指的是对于单个头的 Q , K , V Q,K,V Q,K,V矩阵以及输入 X X X是相等。
- 多头指的是多个注意力头,每个头有自己的 W i Q , W i K , Q i V W_i^Q,W_i^K,Q_i^V WiQ,WiK,QiV
- 自注意力机制计算:
对于每个注意力头 i = 1 , 2 , . . . , h i = 1, 2, ..., h i=1,2,...,h,定义线性变换矩阵:
W i Q ∈ R d model × d k , W i K ∈ R d model × d k , W i V ∈ R d model × d v W_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}, \quad W_i^K \in \mathbb{R}^{d_{\text{model}} \times d_k}, \quad W_i^V \in \mathbb{R}^{d_{\text{model}} \times d_v} WiQ∈Rdmodel×dk,WiK∈Rdmodel×dk,WiV∈Rdmodel×dv
其中:
- h h h:注意力头的数量
- d k d_k dk:Query 和 Key 的维度
- d v d_v dv:Value 的维度(通常 d k = d v = d model h d_k = d_v = \frac{d_{\text{model}}}{h} dk=dv=hdmodel)
Q i = X W i Q , K i = X W i K , V i = X W i V for i = 1 , 2 , . . . , h Q_i = X W_i^Q, \quad K_i = X W_i^K, \quad V_i = X W_i^V \quad \text{for } i = 1, 2, ..., h Qi=XWiQ,Ki=XWiK,Vi=XWiVfor i=1,2,...,h
这些 Q i , K i , V i Q_i,K_i,V_i Qi,Ki,Vi是不相等的,只在各个其权重矩阵相同的情况下相等。
Attention i = softmax ( Q i K i T d k ) V i \text{Attention}_i = \text{softmax}\left( \frac{Q_i K_i^T}{\sqrt{d_k}} \right) V_i Attentioni=softmax(dkQiKiT)Vi
结果为:
Attention i ∈ R n × d v \text{Attention}_i \in \mathbb{R}^{n \times d_v} Attentioni∈Rn×dv
Concat ( Attention 1 , Attention 2 , . . . , Attention h ) ∈ R n × ( h ⋅ d v ) = R n × d model \text{Concat}(\text{Attention}_1, \text{Attention}_2, ..., \text{Attention}_h) \in \mathbb{R}^{n \times (h \cdot d_v)} = \mathbb{R}^{n \times d_{\text{model}}} Concat(Attention1,Attention2,...,Attentionh)∈Rn×(h⋅dv)=Rn×dmodel
引入最终投影矩阵 W O ∈ R d model × d model W^O \in \mathbb{R}^{d_{\text{model}} \times d_{\text{model}}} WO∈Rdmodel×dmodel:
MultiHead ( X ) = Concat ( . . . ) ⋅ W O \text{MultiHead}(X) = \text{Concat}(...) \cdot W^O MultiHead(X)=Concat(...)⋅WO
结果为:
- MultiHead ( X ) ∈ R n × d model \text{MultiHead}(X) \in \mathbb{R}^{n \times d_{\text{model}}} MultiHead(X)∈Rn×dmodel
在多头注意力层后,是FFN(前馈网络层),在输入前需要进行残差连接和层归一化,伪代码如下:
给定输入:
X_input ∈ R^{n × d_model}1. 多头自注意力:for i in 1..h:Q_i = X_input * W_i^QK_i = X_input * W_i^KV_i = X_input * W_i^VAttention_i = softmax(Q_i K_i^T / sqrt(d_k)) * V_iConcat = Concat(Attention_1, ..., Attention_h)MultiHead = Concat * W^O2. 残差连接 + 层归一化:Output_SA = LayerNorm(MultiHead + X_input)3. 前馈网络 (FFN):FFN_Output = max(0, Output_SA * W1 + b1) * W2 + b24. 再次残差连接 + 层归一化:Final_Output = LayerNorm(FFN_Output + Output_SA)
一个完整的 Transformer 编码器由 N 个相同的 Encoder Layer 组成(通常是 6 层):
Input Embedding + Positional Encoding↓
Encoder Layer 1 → 输出 H1↓
Encoder Layer 2 → 输出 H2↓
...↓
Encoder Layer N → 最终输出 H_final ∈ R^{n × d_model}
每一层都会提取更高级的语义信息。
最终输出 H final H_{\text{final}} Hfinal 是一个包含上下文信息的序列表示。它的作用是:
- 作为解码器中 交叉注意力机制(Cross-Attention) 的 Key 和 Value 来源。
- 表示整个输入序列的语义信息,可用于下游任务(如分类、NER 等)。
Decoder部分的流程
Input Embedding + Positional Encoding↓
[Masked Multi-Head Self-Attention] → [Add & Norm]↓
[Multi-Head Cross-Attention] → [Add & Norm]↓
[Feed-Forward Network (FFN)] → [Add & Norm]↓
Output of this Decoder Layer
Q = Y input W Q , K = Y input W K , V = Y input W V Q = Y_{\text{input}} W^Q, \quad K = Y_{\text{input}} W^K, \quad V = Y_{\text{input}} W^V Q=YinputWQ,K=YinputWK,V=YinputWV
计算注意力分数时加入掩码(mask):
A = softmax ( Q K T d k + M ) V A = \text{softmax}\left( \frac{Q K^T}{\sqrt{d_k}} + M \right) V A=softmax(dkQKT+M)V
其中:
- $M$ 是一个三角形 mask 矩阵(上三角为 -∞),阻止当前位置看到后面的词最终输出:
$$
H_{\text{masked}} = \text{LayerNorm}( \text{MultiHead}(Y_{\text{input}}) + Y_{\text{input}} )
$$- Query 来自解码器当前层输出:$Q = H_{\text{masked}}$
- Key 和 Value 来自编码器最终输出:$K = V = H_{\text{encoder}}$
-
多头交叉注意力(Multi-Head Cross-Attention)
- Query 来自解码器当前层输出: Q = H masked Q = H_{\text{masked}} Q=Hmasked
- Key 和 Value 来自编码器最终输出: K = V = H encoder K = V = H_{\text{encoder}} K=V=Hencoder
Q = H masked W Q , K = H encoder W K , V = H encoder W V Q = H_{\text{masked}} W^Q, \quad K = H_{\text{encoder}} W^K, \quad V = H_{\text{encoder}} W^V Q=HmaskedWQ,K=HencoderWK,V=HencoderWV
计算注意力:
CrossAttn = softmax ( Q K T d k ) V \text{CrossAttn} = \text{softmax}\left( \frac{Q K^T}{\sqrt{d_k}} \right) V CrossAttn=softmax(dkQKT)V
然后做残差连接和归一化:
H cross = LayerNorm ( CrossAttn + H masked ) H_{\text{cross}} = \text{LayerNorm}( \text{CrossAttn} + H_{\text{masked}} ) Hcross=LayerNorm(CrossAttn+Hmasked)
4. FFN(Feed Forward)前馈网络
5. 上述结构多层堆叠:
和编码器类似,一个完整的 Transformer 解码器也由多个 Decoder Layer 组成:
Input: Target Sequence + Positional Encoding↓Decoder Layer 1 → 输出 H1↓Decoder Layer 2 → 输出 H2↓...↓Decoder Layer N → 最终输出 H_final ∈ R^{m × d_model}
解码器最后一层的输出 H final ∈ R m × d model H_{\text{final}} \in \mathbb{R}^{m \times d_{\text{model}}} Hfinal∈Rm×dmodel 将被送入一个线性层 + softmax,用于预测下一个词的概率分布:
P ( y t ) = softmax ( H final W output + b output ) P(y_t) = \text{softmax}( H_{\text{final}} W_{\text{output}} + b_{\text{output}} ) P(yt)=softmax(HfinalWoutput+boutput)
其中:
- W output ∈ R d model × ∣ V ∣ W_{\text{output}} \in \mathbb{R}^{d_{\text{model}} \times |V|} Woutput∈Rdmodel×∣V∣
- ∣ V ∣ |V| ∣V∣:目标语言词汇表大小
Transformer 解码器通过掩码自注意力防止未来信息泄露,通过交叉注意力关注编码器输出,结合前馈网络逐层提取信息,最终生成目标语言的词概率分布。
Encoder Output (H_encoder)↘
Target Input → [Masked Self-Attention] → [Cross-Attention] → [FFN] → Logits↗
H_encoder(提供 Key/Value)