注意力机制与Transformer架构解析
1. 注意力机制基础
注意力机制在自然语言处理和计算机视觉等领域有着广泛的应用。其基本公式如下:
[
\mathbf{c} t = \sum {i} \alpha_{t, i} \mathbf{h} i
]
其中,
[
\alpha {t, i} = \frac{\exp(e_{t, i})}{\sum_{i’} \exp(e_{t, i’})}
]
而 ( e_{t, i} ) 有多种计算方式:
- 点积形式 :(e_{t, i} = \mathbf{s} t^{\top} \mathbf{h}_i)
- 一般形式 :(e {t, i} = \mathbf{s} t^{\top} \mathbf{W}_a \mathbf{h}_i)
- 拼接形式 :(e {t, i} = \mathbf{v}_a^{\top} \tanh(\mathbf{W}_s \mathbf{s}_t ; \mathbf{W}_h \mathbf{h}_i))
在TensorFlow中,我们可以使用TensorFlow Addons为编码器 - 解码器模型添加Luong注意力机制,具体步骤如下:
1. 定义注意力机制:
import tensorflow_addons as tfa
i
超级会员免费看
订阅专栏 解锁全文
1196

被折叠的 条评论
为什么被折叠?



