在Transformer模型中解码器(Decoder)的详细解释(Attention Is All You Need)

在这里插入图片描述
在这里插入图片描述

解码器(Decoder)的详细解释

在Transformer模型中,解码器与编码器相似,由堆叠的 N = 6 N = 6 N=6个相同的层组成。但解码器比编码器多了一个子层:对编码器输出的多头注意力(Multi-Head Attention over the encoder’s output)。解码器的每一层有三个子层:

  1. 掩码多头自注意力机制(Masked Multi-Head Self-Attention Mechanism)
  2. 对编码器输出的多头注意力机制(Multi-Head Attention over the Encoder’s Output)
  3. 逐点全连接前馈神经网络(Positionwise Fully Connected Feed-Forward Network)

每个子层都使用残差连接,并在其后进行层归一化。这种设计确保了模型能够保持稳定的梯度,并能有效地进行训练。掩码机制和输出嵌入的位移确保了预测 i i i位置时,只能依赖于之前的位置。

解码器的结构和工作原理

1. 掩码多头自注意力机制(Masked Multi-Head Self-Attention Mechanism)

掩码多头自注意力机制确保在生成序列的过程中,当前时间步只关注之前的时间步,不会看到未来的信息。这通过掩码矩阵实现。

2. 对编码器输出的多头注意力机制(Multi-Head Attention over the Encoder’s Output)

这一子层使用编码器的输出作为键和值,解码器的自注意力输出作为查询。这允许解码器在生成每个输出时,参考编码器的所有隐藏状态,从而捕捉输入序列与输出序列之间的依赖关系。

3. 逐点全连接前馈神经网络(Positionwise Fully Connected Feed-Forward Network)

这一子层对每个位置独立应用两个线性变换和一个非线性激活函数,用于进一步特征提取和变换。

详细的处理步骤和示例

假设我们有一个输入序列 Input Sequence = [ 23.1 , 24.3 , 22.8 , 23.5 ] \text{Input Sequence} = [23.1, 24.3, 22.8, 23.5] Input Sequence=[23.1,24.3,22.8,23.5]以及一个部分输出序列 Generated Sequence = [ y 1 , y 2 , y 3 ] \text{Generated Sequence} = [y_1, y_2, y_3] Generated Sequence=[y1,y2,y3]

1. 输入嵌入和位置编码

对输入序列和部分输出序列进行嵌入和位置编码:

Input Embedding = [ 23.1 0 … 0 24.3 0 … 0 22.8 0 … 0 23.5 0 … 0 ] \text{Input Embedding} = \begin{bmatrix} 23.1 & 0 & \ldots & 0 \\ 24.3 & 0 & \ldots & 0 \\ 22.8 & 0 & \ldots & 0 \\ 23.5 & 0 & \ldots & 0 \end{bmatrix} Input Embedding= 23.124.322.823.500000000
Output Embedding = [ y 1 0 … 0 y 2 0 … 0 y 3 0 … 0 ] \text{Output Embedding} = \begin{bmatrix} y_1 & 0 & \ldots & 0 \\ y_2 & 0 & \ldots & 0 \\ y_3 & 0 & \ldots & 0 \end{bmatrix} Output Embedding= y1y2y3000000

位置编码:

P E input = [ 0 0 … 0 sin ⁡ ( 1 ) 0 … 0 sin ⁡ ( 2 ) 0 … 0 sin ⁡ ( 3 ) 0 … 0 ] PE_{\text{input}} = \begin{bmatrix} 0 & 0 & \ldots & 0 \\ \sin(1) & 0 & \ldots & 0 \\ \sin(2) & 0 & \ldots & 0 \\ \sin(3) & 0 & \ldots & 0 \end{bmatrix} PEinput= 0sin(1)sin(2)sin(3)00000000
P E output = [ 0 0 … 0 sin ⁡ ( 1 ) 0 … 0 sin ⁡ ( 2 ) 0 … 0 ] PE_{\text{output}} = \begin{bmatrix} 0 & 0 & \ldots & 0 \\ \sin(1) & 0 & \ldots & 0 \\ \sin(2) & 0 & \ldots & 0 \end{bmatrix} PEoutput= 0sin(1)sin(2)000000

将位置编码与嵌入表示相加:

Input + Position = [ 23.1 0 … 0 25.1415 0 … 0 23.7093 0 … 0 23.6411 0 … 0 ] \text{Input + Position} = \begin{bmatrix} 23.1 & 0 & \ldots & 0 \\ 25.1415 & 0 & \ldots & 0 \\ 23.7093 & 0 & \ldots & 0 \\ 23.6411 & 0 & \ldots & 0 \end{bmatrix} Input + Position= 23.125.141523.709323.641100000000
Output + Position = [ y 1 0 … 0 y 2 + sin ⁡ ( 1 ) 0 … 0 y 3 + sin ⁡ ( 2 ) 0 … 0 ] \text{Output + Position} = \begin{bmatrix} y_1 & 0 & \ldots & 0 \\ y_2 + \sin(1) & 0 & \ldots & 0 \\ y_3 + \sin(2) & 0 & \ldots & 0 \end{bmatrix} Output + Position= y1y2+sin(1)y3+sin(2)000000

2. 掩码多头自注意力机制

计算查询、键和值矩阵:

Q masked = K masked = V masked = Output + Position Q_{\text{masked}} = K_{\text{masked}} = V_{\text{masked}} = \text{Output + Position} Qmasked=Kmasked=Vmasked=Output + Position

应用掩码:

M = [ 0 − ∞ − ∞ 0 0 − ∞ 0 0 0 ] M = \begin{bmatrix} 0 & -\infty & -\infty \\ 0 & 0 & -\infty \\ 0 & 0 & 0 \end{bmatrix} M= 000000

计算注意力权重:

Attention Weights masked = softmax ( Q masked K masked T + M d model ) \text{Attention Weights}_{\text{masked}} = \text{softmax}\left(\frac{Q_{\text{masked}} K_{\text{masked}}^T + M}{\sqrt{d_{\text{model}}}}\right) Attention Weightsmasked=softmax(dmodel QmaskedKmaskedT+M)

加权求和:

Attention Output masked = Attention Weights masked ⋅ V masked \text{Attention Output}_{\text{masked}} = \text{Attention Weights}_{\text{masked}} \cdot V_{\text{masked}} Attention Outputmasked=Attention WeightsmaskedVmasked

3. 对编码器输出的多头注意力机制

计算查询、键和值矩阵:

Q = Attention Output masked , K = V = Encoder Output Q = \text{Attention Output}_{\text{masked}}, \quad K = V = \text{Encoder Output} Q=Attention Outputmasked,K=V=Encoder Output

计算注意力权重:

Attention Weights = softmax ( Q K T d model ) \text{Attention Weights} = \text{softmax}\left(\frac{Q K^T}{\sqrt{d_{\text{model}}}}\right) Attention Weights=softmax(dmodel QKT)

加权求和:

Attention Output = Attention Weights ⋅ V \text{Attention Output} = \text{Attention Weights} \cdot V Attention Output=Attention WeightsV

4. 逐点全连接前馈神经网络

计算前馈神经网络输出:

h = max ⁡ ( 0 , Attention Output ⋅ W 1 + b 1 ) h = \max(0, \text{Attention Output} \cdot W_1 + b_1) h=max(0,Attention OutputW1+b1)
y = h ⋅ W 2 + b 2 y = h \cdot W_2 + b_2 y=hW2+b2

5. 残差连接和层归一化

对于掩码多头自注意力子层:

Self-Attention Output = LayerNorm ( Output + Position + Attention Output masked ) \text{Self-Attention Output} = \text{LayerNorm}(\text{Output + Position} + \text{Attention Output}_{\text{masked}}) Self-Attention Output=LayerNorm(Output + Position+Attention Outputmasked)

对于对编码器输出的多头注意力子层:

Cross-Attention Output = LayerNorm ( Self-Attention Output + Attention Output ) \text{Cross-Attention Output} = \text{LayerNorm}(\text{Self-Attention Output} + \text{Attention Output}) Cross-Attention Output=LayerNorm(Self-Attention Output+Attention Output)

对于前馈神经网络子层:

FFN Output = LayerNorm ( Cross-Attention Output + y ) \text{FFN Output} = \text{LayerNorm}(\text{Cross-Attention Output} + y) FFN Output=LayerNorm(Cross-Attention Output+y)

总结

通过详细解释解码器的结构和每个子层的工作原理,我们可以看到解码器如何利用掩码多头自注意力机制、对编码器输出的多头注意力机制和逐点全连接前馈神经网络来生成输出序列。每个子层都使用残差连接和层归一化,以确保模型的稳定性和训练效果。这些步骤使得解码器能够在生成每个符号时,只依赖于之前生成的符号,同时参考编码器的输出,从而实现高效的序列生成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值