
Attention 机制作为近年来自然处理领域最重要的理念之一,在许多 NLP 子领域中得到了广泛应用。以注意力机制为核心的 Transformer、BERT、GPT 等模型也在各类任务 benchmark 不断屠榜。其中,Luong Attention 和 Bahdanau Attention 是最经典的两种注意力机制。二者在理念上大致相同,但在实现细节上存在许多区别。

简单来说,Luong Attention 相较 Bahdanau Attention 主要有以下几点区别:
- 注意力的计算方式不同
在 Luong Attention 机制中,第 t 步的注意力是由 decoder 第 t 步的 hidden state
与 encoder 中的每一个 hidden state
加权计算得出的。而在 Bahdanau Attention 机制中,第 t 步的注意力
是由 decoder 第 t-1 步的 hidden state
与 encoder 中的每一个 hidden state
加权计算得出的。
- decoder 的输入输出不同
在 Bahdanau Attention 机制中,decoder 在第 t 步时,输入是由注意力与前一步的 hidden state
拼接(concatenate)得出的,得到第 t 步的 hidden state
并直接输出
。而 Luong Attention 机制在 decoder 部分建立了一层额外的网络结构,以注意力
与原 decoder 第 t 步的 hidden state
拼接作为输入,得到第 t 步的 hidden state
并输出
。

总结一下,Bahdanau Attention 机制的计算流程为
此外,Bahdanau Attention 机制的论文中只尝试了使用 concat 作为对齐函数,而 Luong Attention 机制的论文在多种对齐函数上做了实验。
根据计算区域的大小,可以将对齐函数分为全局注意力(Global Attention)和局部注意力(Local Attention)两种机制。全局注意力模式会将 encoder 的每一步 hidden state 进行注意力计算,而局部注意力模式则计算部分范围的 hidden state。
根据所用信息,可以分为基于内容的对齐和基于位置的对齐。前者同时考虑了 encoder 的 hidden state

参考:
- Neural Machine Translation by Jointly Learning to Align and Translate
- Effective Approaches to Attention-based Neural Machine Translation
- Attention Variants
- BahdanauAttention与LuongAttention注意力机制简介-CSDN博客
- 胡文星:seq2seq中的两种attention机制(图+公式)