Transformer中Decoder的计算过程及各部分维度变化
创始人
2025-01-08 16:33:41
0

在Transformer模型中,解码器的计算过程涉及多个步骤,主要包括自注意力机制、编码器-解码器注意力和前馈神经网络。以下是解码器的详细计算过程及数据维度变化:

1. 输入嵌入和位置编码

解码器的输入首先经过嵌入层和位置编码:
Input d = Embedding ( x ) + PositionEncoding ( x ) \text{Input}_d = \text{Embedding}(x) + \text{PositionEncoding}(x) Inputd​=Embedding(x)+PositionEncoding(x)

  • 维度变化: x x x: 输入序列的标记,维度为 ( n , d m o d e l ) (n, d_{model}) (n,dmodel​) Embedding ( x ) \text{Embedding}(x) Embedding(x): 输出维度为 ( n , d m o d e l ) (n, d_{model}) (n,dmodel​) PositionEncoding ( x ) \text{PositionEncoding}(x) PositionEncoding(x): 输出维度为 ( n , d m o d e l ) (n, d_{model}) (n,dmodel​)

2. 自注意力机制

自注意力机制计算如下:
Q = Input d W Q , K = Input d W K , V = Input d W V Q = \text{Input}_d W_Q, \quad K = \text{Input}_d W_K, \quad V = \text{Input}_d W_V Q=Inputd​WQ​,K=Inputd​WK​,V=Inputd​WV​

  • 这里 W Q , W K , W V W_Q, W_K, W_V WQ​,WK​,WV​ 是参数矩阵,维度为 ( d m o d e l , d k ) (d_{model}, d_k) (dmodel​,dk​),假设 d k = d m o d e l d_k = d_{model} dk​=dmodel​。
  • 维度变化: Q , K , V Q, K, V Q,K,V: 输出维度为 ( n , d k ) (n, d_k) (n,dk​)
    自注意力的计算为:
    Attention ( Q , K , V ) = softmax ( Q K T d k + M ) V \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}} + M\right)V Attention(Q,K,V)=softmax(dk​​QKT​+M)V
  • 维度变化: Q K T QK^T QKT: 维度为 ( n , n ) (n, n) (n,n) softmax \text{softmax} softmax: 结果维度为 ( n , n ) (n, n) (n,n)最终输出的维度为 ( n , d v ) (n, d_v) (n,dv​)(假设 d v = d m o d e l d_v = d_{model} dv​=dmodel​)。

3. 残差连接与层归一化

自注意力的输出与输入相加,然后进行层归一化:
Output d ( l ) = LayerNorm ( Attention + Input d ) \text{Output}_d^{(l)} = \text{LayerNorm}(\text{Attention} + \text{Input}_d) Outputd(l)​=LayerNorm(Attention+Inputd​)

  • 维度变化:维度保持为 ( n , d m o d e l ) (n, d_{model}) (n,dmodel​)。

4. 编码器-解码器注意力

接下来,解码器会对编码器的输出进行注意力计算:
Q ′ = Output d ( l ) W Q ′ , K ′ = EncoderOutput W K ′ , V ′ = EncoderOutput W V ′ Q' = \text{Output}_d^{(l)} W_Q', \quad K' = \text{EncoderOutput} W_K', \quad V' = \text{EncoderOutput} W_V' Q′=Outputd(l)​WQ′​,K′=EncoderOutputWK′​,V′=EncoderOutputWV′​

  • 这里 W Q ′ , W K ′ , W V ′ W_Q', W_K', W_V' WQ′​,WK′​,WV′​ 的维度也是 ( d m o d e l , d k ) (d_{model}, d_k) (dmodel​,dk​)。
  • 编码器输出的维度为 ( T e , d m o d e l ) (T_e, d_{model}) (Te​,dmodel​)。
    注意力计算为:
    Attention ( Q ′ , K ′ , V ′ ) = softmax ( Q ′ K ′ T d k ) V ′ \text{Attention}(Q', K', V') = \text{softmax}\left(\frac{Q'K'^T}{\sqrt{d_k}}\right)V' Attention(Q′,K′,V′)=softmax(dk​​Q′K′T​)V′
  • 维度变化: Q ′ K ′ T Q'K'^T Q′K′T: 维度为 ( n , T e ) (n, T_e) (n,Te​)最终输出的维度为 ( n , d v ) (n, d_v) (n,dv​)。
    然后与自注意力的输出进行残差连接和层归一化:
    Output d ( l ) = LayerNorm ( EncoderDecoderAttention + Output d ( l ) ) \text{Output}_d^{(l)} = \text{LayerNorm}(\text{EncoderDecoderAttention} + \text{Output}_d^{(l)}) Outputd(l)​=LayerNorm(EncoderDecoderAttention+Outputd(l)​)

5. 前馈神经网络

接下来是前馈神经网络的处理:
FFN ( x ) = ReLU ( x W 1 + b 1 ) W 2 + b 2 \text{FFN}(x) = \text{ReLU}(xW_1 + b_1)W_2 + b_2 FFN(x)=ReLU(xW1​+b1​)W2​+b2​

  • W 1 W_1 W1​ 维度为 ( d m o d e l , d f f ) (d_{model}, d_{ff}) (dmodel​,dff​), W 2 W_2 W2​ 维度为 ( d f f , d m o d e l ) (d_{ff}, d_{model}) (dff​,dmodel​),其中 d f f d_{ff} dff​ 是前馈层的隐藏单元数。
  • 维度变化:输入维度为 ( n , d m o d e l ) (n, d_{model}) (n,dmodel​)输出维度为 ( n , d m o d e l ) (n, d_{model}) (n,dmodel​)。

6. 最终输出

在最后一步,再次进行残差连接和层归一化:
Output d ( l ) = LayerNorm ( FFN + Output d ( l ) ) \text{Output}_d^{(l)} = \text{LayerNorm}(\text{FFN} + \text{Output}_d^{(l)}) Outputd(l)​=LayerNorm(FFN+Outputd(l)​)
接下来,解码器的最终输出通过线性层和Softmax层生成词汇表的概率分布:
Logits = Output d ( l ) W o u t + b o u t \text{Logits} = \text{Output}_d^{(l)} W_{out} + b_{out} Logits=Outputd(l)​Wout​+bout​
Probabilities = softmax ( Logits ) \text{Probabilities} = \text{softmax}(\text{Logits}) Probabilities=softmax(Logits)

  • 维度变化: W o u t W_{out} Wout​ 维度为 ( d m o d e l , V ) (d_{model}, V) (dmodel​,V),其中 V V V 是词汇表的大小。 Logits \text{Logits} Logits 的维度为 ( n , V ) (n, V) (n,V), Probabilities \text{Probabilities} Probabilities 的维度同样为 ( n , V ) (n, V) (n,V),表示每个时间步上各个词汇的概率。
    通过这些步骤,解码器能够生成序列的下一个标记。

相关内容

热门资讯

透视实锤!werplan辅助软... 透视实锤!werplan辅助软件,wepoker辅助工具,项目教程(有挂工具)-哔哩哔哩1、进入到w...
透视透视挂!wepoker俱乐... 透视透视挂!wepoker俱乐部辅助(透视)都是真的有挂,力荐教程(有挂大厅)-哔哩哔哩1、下载好w...
6分钟详情!wejoker私人... 6分钟详情!wejoker私人辅助软件(透视)竟然是有挂,我来教教你(有人有挂)-哔哩哔哩1、wej...
透视了解!wepoker破解器... 透视了解!wepoker破解器激活码,wepoker破解是真的还是假的,总结教程(有挂头条)-哔哩哔...
透视辅助!wepoker底牌透... 透视辅助!wepoker底牌透视(透视)果然真的是有挂,2025新版教程(有挂代打ai)-哔哩哔哩1...
7分钟解谜!wepoker黑侠... 7分钟解谜!wepoker黑侠破解(透视)本来有挂,德州论坛(有挂详细)-哔哩哔哩1、wepoker...
透视了解!wejoker辅助脚... 透视了解!wejoker辅助脚本,steampokermaster辅助,指引教程(有挂教学)-哔哩哔...
透视软件!wepoker一直输... 透视软件!wepoker一直输的号能继续打吗(透视)切实是有挂,黑科技教程(有挂猫腻)-哔哩哔哩小薇...
7分钟解迷!wepoker透视... 7分钟解迷!wepoker透视破解版(透视)一直有挂,详细教程(确实有挂)-哔哩哔哩1、任何wepo...
透视app!hhpoker有没... 透视app!hhpoker有没有辅助,wpk有那种辅助吗,大纲教程(有人有挂)-哔哩哔哩暗藏猫腻,小...