NLP自然语言处理|RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)

整理各种模型的公式,以后面试复习用
RNN NLP自然语言处理|RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
文章图片

公式:
h t = f ( W ? [ h t ? 1 , x t ] + b ) h_{t}=f\left(W \cdot\left[h_{t-1}, x_{t}\right]+b\right) ht?=f(W?[ht?1?,xt?]+b)
LSTM NLP自然语言处理|RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
文章图片

公式:
遗忘门: f t = σ ( W f ? [ h t ? 1 , x t ] + b f ) f_{t}=\sigma\left(W_{f} \cdot\left[h_{t-1}, x_{t}\right]+b_{f}\right) ft?=σ(Wf??[ht?1?,xt?]+bf?)
输入门: i t = σ ( W i ? [ h t ? 1 , x t ] + b i ) i_{t}=\sigma\left(W_{i} \cdot\left[h_{t-1}, x_{t}\right]+b_{i}\right) it?=σ(Wi??[ht?1?,xt?]+bi?)
细胞状态: C ~ t = tanh ? ( W C ? [ h t ? 1 , x t ] + b C ) \tilde{C}_{t}=\tanh \left(W_{C} \cdot\left[h_{t-1}, x_{t}\right]+b_{C}\right) C~t?=tanh(WC??[ht?1?,xt?]+bC?)
细胞更新: C t = f t ? C t ? 1 + i t ? C ~ t C_{t}=f_{t} * C_{t-1}+i_{t} * \tilde{C}_{t} Ct?=ft??Ct?1?+it??C~t?
输出门: o t = σ ( W o [ h t ? 1 , x t ] + b o ) o_{t}=\sigma\left(W_{o}\left[h_{t-1}, x_{t}\right]+b_{o}\right) ot?=σ(Wo?[ht?1?,xt?]+bo?)
输出: h t = o t ? tanh ? ( C t ) h_{t}=o_{t} * \tanh \left(C_{t}\right) ht?=ot??tanh(Ct?)
GRU NLP自然语言处理|RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
文章图片

公式:
更新门: z t = σ ( W z ? [ h t ? 1 , x t ] ) z_{t}=\sigma\left(W_{z} \cdot\left[h_{t-1}, x_{t}\right]\right) zt?=σ(Wz??[ht?1?,xt?])
重置门: r t = σ ( W r ? [ h t ? 1 , x t ] ) r_{t}=\sigma\left(W_{r} \cdot\left[h_{t-1}, x_{t}\right]\right) rt?=σ(Wr??[ht?1?,xt?])
当前状态: h ~ t = tanh ? ( W ? [ r t ? h t ? 1 , x t ] ) \tilde{h}_{t}=\tanh \left(W \cdot\left[r_{t} * h_{t-1}, x_{t}\right]\right) h~t?=tanh(W?[rt??ht?1?,xt?])
更新: h t = ( 1 ? z t ) ? h t ? 1 + z t ? h ~ t h_{t}=\left(1-z_{t}\right) * h_{t-1}+z_{t} * \tilde{h}_{t} ht?=(1?zt?)?ht?1?+zt??h~t?
Attention机制 NLP自然语言处理|RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
文章图片

Attention有很多计算方法,下面的公式只是比较常用的一种,计算方法和transformer中的qkv类似,下面公式以解码器第一个状态为例,Encoder输入长度为m, W \mathrm{W} W 为参数,自动学习获得。
公式:
计算q \mathrm{q} q: q 0 = W Q ? s 0 \mathrm{q}_{0}=\mathbf{W}_{Q} \cdot \mathrm{s}_{0} q0?=WQ??s0?
计算k \mathrm{k} k: k i = W K ? h i , \mathrm{k}_{i}=\mathbf{W}_{K} \cdot \mathbf{h}_{i}, ki?=WK??hi?, fori = 1 i=1 i=1 tom m m
计算每个位置得分: α ~ i = k i T q 0 , \tilde{\alpha}_{i}=\mathrm{k}_{i}^{T} \mathrm{q}_{0}, α~i?=kiT?q0?, fori = 1 i=1 i=1 tom m m
softmax归一化: [ α 1 , ? ? , α m ] = Softmax ? ( [ α ~ 1 , ? ? , α ~ m ] ) \left[\alpha_{1}, \cdots, \alpha_{m}\right]=\operatorname{Softmax}\left(\left[\tilde{\alpha}_{1}, \cdots, \tilde{\alpha}_{m}\right]\right) [α1?,?,αm?]=Softmax([α~1?,?,α~m?])(softmax公式想必很熟了)
最后,计算得到当前的 context vector: c 0 = α 1 h 1 + ? + α m h m c_{0}=\alpha_{1} \mathbf{h}_{1}+\cdots+\alpha_{m} \mathbf{h}_{m} c0?=α1?h1?+?+αm?hm?
Transformer NLP自然语言处理|RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
文章图片

transformer的公式不太好写,下面只给出几个关键公式
公式:
计算 Q 、 K 、 V Q、K、V Q、K、V: Q = W Q ? X Q=W^{Q} * X Q=WQ?X, K = W K ? X K=W^{K} * X K=WK?X, V = W V ? X V=W^{V} * X V=WV?X
计算self Attention:Attention( Q , K , V ) = softmax ? ( Q K T d k ) V (Q, K, V)=\operatorname{softmax}\left(\frac{Q K^{T}}{\sqrt{d_{k}}}\right) V (Q,K,V)=softmax(dk? ?QKT?)V
前馈网络层: FFN ? ( Z ) = max ? ( 0 , Z W 1 + b 1 ) W 2 + b 2 \operatorname{FFN}(Z)=\max \left(0, Z W_{1}+b_{1}\right) W_{2}+b_{2} FFN(Z)=max(0,ZW1?+b1?)W2?+b2?
位置编码:
P E ( p o s , 2 i ) = sin ? ( p o s 1000 0 2 i d m o d e l ) P E(p o s, 2 i)=\sin \left(\frac{p o s}{10000^{\frac{2 i}{d_{m o d e l}}}}\right) PE(pos,2i)=sin(10000dmodel?2i?pos?), P E ( p o s , 2 i + 1 ) = cos ? ( p o s 1000 0 2 i d m o d e l ) P E(p o s, 2 i+1)=\cos \left(\frac{p o s}{10000^{\frac{2 i}{d_{m o d e l}}}}\right) PE(pos,2i+1)=cos(10000dmodel?2i?pos?)
上面公式只是一部分,其中还有一些细节 ,比如mutil-attention、残差&layer norm、decoder中的mask等。
transformer的核心组件就是Attention,代码实现是用上述矩阵乘的方式,为方便理解下面简述单个单词的Attention计算流程:

  1. 根据embeding得到q , k , v q, \quad k, \quad v q,k,v 三个向量;
  2. 用当前单词的 q q q为其它每个单词计算一个score:\quadscore= q ? k =q \cdot k =q?k;
  3. 为了避免score分布尖锐, 进行数值缩放, 即除以d k \sqrt{d_{k}} dk? ?
  4. 对score进行softmax归一化;
  5. 加权求和得到当前词的context vectorz : z = ∑ α i v i z: \quad z=\sum \alpha_{i}v_{i} z:z=∑αi?vi?
仔细观察,和上一节的Attention计算方式几乎一样。
【NLP自然语言处理|RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)】持续更新中……

    推荐阅读