整理各种模型的公式,以后面试复习用

RNN

RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
公式:
ht=f(W[ht1,xt]+b)h_{t}=f\left(W \cdot\left[h_{t-1}, x_{t}\right]+b\right)

LSTM

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

GRU

RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
公式:
更新门:zt=σ(Wz[ht1,xt])z_{t}=\sigma\left(W_{z} \cdot\left[h_{t-1}, x_{t}\right]\right)
重置门:rt=σ(Wr[ht1,xt])r_{t}=\sigma\left(W_{r} \cdot\left[h_{t-1}, x_{t}\right]\right)
当前状态:h~t=tanh(W[rtht1,xt])\tilde{h}_{t}=\tanh \left(W \cdot\left[r_{t} * h_{t-1}, x_{t}\right]\right)
更新:ht=(1zt)ht1+zth~th_{t}=\left(1-z_{t}\right) * h_{t-1}+z_{t} * \tilde{h}_{t}

Attention机制

RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
Attention有很多计算方法,下面的公式只是比较常用的一种,计算方法和transformer中的qkv类似,下面公式以解码器第一个状态为例,Encoder输入长度为m,W\mathrm{W} 为参数,自动学习获得。

公式:
计算 q\mathrm{q}q0=WQs0\mathrm{q}_{0}=\mathbf{W}_{Q} \cdot \mathrm{s}_{0}
计算 k\mathrm{k}ki=WKhi,\mathrm{k}_{i}=\mathbf{W}_{K} \cdot \mathbf{h}_{i}, for i=1i=1 to mm
计算每个位置得分:α~i=kiTq0,\tilde{\alpha}_{i}=\mathrm{k}_{i}^{T} \mathrm{q}_{0}, for i=1i=1 to mm
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)(softmax公式想必很熟了)
最后,计算得到当前的 context vector:c0=α1h1++αmhmc_{0}=\alpha_{1} \mathbf{h}_{1}+\cdots+\alpha_{m} \mathbf{h}_{m}

Transformer

RNN LSTM GRU Attention transformer公式整理总结(超详细图文公式)
transformer的公式不太好写,下面只给出几个关键公式

公式:
计算QKVQ、K、VQ=WQXQ=W^{Q} * XK=WKXK=W^{K} * XV=WVXV=W^{V} * X
计算self Attention:Attention (Q,K,V)=softmax(QKTdk)V(Q, K, V)=\operatorname{softmax}\left(\frac{Q K^{T}}{\sqrt{d_{k}}}\right) V
前馈网络层:FFN(Z)=max(0,ZW1+b1)W2+b2\operatorname{FFN}(Z)=\max \left(0, Z W_{1}+b_{1}\right) W_{2}+b_{2}
位置编码:
PE(pos,2i)=sin(pos100002idmodel)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+1)=cos(pos100002idmodel)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)

上面公式只是一部分,其中还有一些细节 ,比如mutil-attention、残差&layer norm、decoder中的mask等。

transformer的核心组件就是Attention,代码实现是用上述矩阵乘的方式,为方便理解下面简述单个单词的Attention计算流程:

  1. 根据embeding得到 q,k,vq, \quad k, \quad v 三个向量;
  2. 用当前单词的qq为其它每个单词计算一个score: \quad score =qk=q \cdot k;
  3. 为了避免score分布尖锐, 进行数值缩放, 即除以 dk\sqrt{d_{k}}
  4. 对score进行softmax归一化;
  5. 加权求和得到当前词的context vector z:z=αiviz: \quad z=\sum \alpha_{i}v_{i}

仔细观察,和上一节的Attention计算方式几乎一样。

持续更新中……

相关文章:

  • 2021-11-20
  • 2021-06-02
  • 2021-06-16
  • 2021-09-22
  • 2021-10-21
  • 2021-08-16
猜你喜欢
  • 2022-02-05
  • 2021-09-14
  • 2021-08-13
  • 2021-12-10
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案