encoder-only和decoder-only 图解
时间: 2025-03-30 12:02:51 浏览: 60
### Transformer 中 Encoder-Only 和 Decoder-Only 架构的工作机制比较
#### 1. **Encoder-Only 架构**
Transformer 的原始设计中包含了 Encoder 和 Decoder 部分,其中 Encoder 负责编码输入序列的信息。在某些应用场景下(如 BERT),仅使用 Encoder 部分来构建模型。
- **工作机制**
Encoder 是一种多层堆叠的结构,每一层由 Self-Attention 子层和前馈神经网络 (Feed Forward Network) 组成[^2]。通过多次叠加这些子层,模型能够捕捉到输入序列中的复杂模式。
- 输入经过嵌入层 (Embedding Layer),将 token 映射为向量表示。
- 使用位置编码 (Positional Encoding) 来引入顺序信息,因为纯注意力机制无法感知序列的位置关系。
- 多头自注意力机制允许模型关注不同位置上的上下文信息。
- 前馈神经网络进一步处理特征提取后的数据。
- **特点**
- 主要用于理解任务,例如分类、命名实体识别等。
- 并行化程度高,在训练过程中可以一次性处理整个输入序列[^3]。
```python
class EncoderLayer(nn.Module):
def __init__(self, d_model, num_heads, d_ff, dropout=0.1):
super(EncoderLayer, self).__init__()
self.self_attention = MultiHeadAttention(d_model, num_heads)
self.feed_forward = PositionWiseFeedForward(d_model, d_ff)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x, mask=None):
attn_output = self.self_attention(x, x, x, mask)
out1 = self.norm1(x + self.dropout(attn_output))
ff_output = self.feed_forward(out1)
output = self.norm2(out1 + self.dropout(ff_output))
return output
```
---
#### 2. **Decoder-Only 架构**
Decoder-Only 结构去除了传统的 Encoder 部分,专注于生成任务。这种架构特别适合于自回归生成场景,例如 GPT 系列模型。
- **工作机制**
解码器同样是一个多层堆叠的结构,每层包含三个主要组件:Self-Attention 层、交叉注意力层以及前馈神经网络[^4]。然而,在 Decoder-Only 模型中,交叉注意力被移除,只剩下 Self-Attention 和 FFN。
- 输入通常是从左至右逐步生成的目标序列的一部分。
- Masked Self-Attention 确保当前时刻的预测不会看到未来的时间步信息。
- 输出的概率分布基于 Softmax 计算得出。
- **特点**
- 更加注重生成能力,适用于文本生成、对话系统等领域。
- 利用了因果掩码 (Causal Masking),使得模型能够在不泄露未来信息的情况下完成逐词生成[^1]。
```python
class DecoderLayer(nn.Module):
def __init__(self, d_model, num_heads, d_ff, dropout=0.1):
super(DecoderLayer, self).__init__()
self.masked_self_attention = MultiHeadAttention(d_model, num_heads)
self.ffn = PositionWiseFeedForward(d_model, d_ff)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x, mask=None):
masked_attn_output = self.masked_self_attention(x, x, x, mask)
out1 = self.norm1(x + self.dropout(masked_attn_output))
ffn_output = self.ffn(out1)
output = self.norm2(out1 + self.dropout(ffn_output))
return output
```
---
#### 3. **图解对比**
| 特性 | Encoder-Only | Decoder-Only |
|---------------------|---------------------------------------|--------------------------------------|
| **核心功能** | 序列理解 | 文本生成 |
| **组成模块** | Self-Attention + FFN | Masked Self-Attention + FFN |
| **并行化支持** | 完全支持 | 受限于自回归特性 |
| **典型应用** | 分类、NER | 对话系统、文章摘要 |

> 注:上述链接仅为示意,请替换为实际可用资源或自行绘制图表。
---
#### 4. **总结**
Encoder-Only 和 Decoder-Only 各有侧重,前者擅长理解和分析静态输入,后者则更适配动态生成任务。两者的设计理念均源于经典的 Encoder-Decoder 框架,但在具体实现上进行了针对性优化以满足特定需求。
---
阅读全文
相关推荐

















