Luong Attention2022-05-07
Attention 架构
Luong Attention分为Local和Global两种,本文主要分析Global Attention。下图为Global Attention的架构图:

符号解释:
- :encoder_output
- :decoder_output
- :attn_weights
- :Context vector
- :attentional hidden state
以中英文翻译场景为例,根据该架构图,分为如下计算步骤:
attn_weights计算
通过encoder_output和decoder_output计算得到attn_weights,即
Context vector计算
通过encoder_output和计算得到加权encoder_output,即
attentional hidden state计算
将和decoder_output进行合并,经过和变换处理得到attentional hidden state,即
预测
根据进行预测,得到最终预测结果。
Attention计算流程
Global Attention计算流程如下图所示:

计算步骤如下:
Encoder
step1:对原始输入,通过处理,得到encoder_output:
step2:为了能够使得encoder_output和decoder_output做运算,需要进行处理
step3:transpose(1,2)
Decoder
RNN
step4:对Decoder端的输入,通过处理,得到decoder_output:
Attention
step5:基于和,进行打分计算:
step6: 对打分结果通过计算,得到attn_weights
step7: 基于,对encoder_output求加权平均
Attentional hidden state
step8:将加权encoder_output通过操作”融入”到原始的decoder_output
为了方面后续的变换,需要对其shape进行调整,结果如下:
step9:对进行变换
step10:变换
step11:将二维展开到三维,得到最终的attentional hidden state
Predict
step12:对进行变换
step13:通过运算,得到最终预测概率结果
参考资料
- 第七课 Seq2Seq与Attention (julyedu.com)