2

漫谈注意力机制(三):全局注意力与局部注意力

 2 years ago
source link: https://allenwind.github.io/blog/9479/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Mr.Feng Blog

NLP、深度学习、机器学习、Python、Go

漫谈注意力机制(三):全局注意力与局部注意力

Attention中的全局注意力与局部注意力。

global Attention

全局注意力(global Attention)在整个向量序列中执行注意力机制,直白点就是说加权求和涉及整个序列。这个思路其实就是soft Attetnion,评分函数 ss 用于计算查询向量 qq 与每个 xixi​ 的相关性,然后归一化后对向量序列加权平均,

Attention(X,q)=n∑i=1p(z=i|X,q)xiAttention⁡(X,q)=∑i=1np(z=i|X,q)xi

由于归一化过程使用softmax,具有很好的梯度特点,没有优化上的问题。但是要在全序列上加权平均,当序列很长的时候,计算量较大。

local Attention

global Attention或者soft Attention关注整个序列,但是在模型情况下,对数据的局部性建模即可,于是只需要局部进行Attention即可,这个思路出发建模的Attention称为local Attention。说指标点就是,局部注意力则把加权求和的范围缩小到给定窗口大小的局部范围。于是,local Attention如何确定局部范围成为关键。这包括两点:

  • 窗口的大小
  • 窗口的位置

论文Effective Approaches to Attention-based Neural Machine Translation中提到local-p方法,以ptpt为中心的正态分布函数来调整对齐函数,

pt=S⋅sigmoid(v⊤ptanh(Wpht))at(s)=align(ht,¯¯¯¯hs)exp(−(s−pt)22σ2)pt=S⋅sigmoid⁡(vp⊤tanh⁡(Wpht))at(s)=align⁡(ht,h¯s)exp⁡(−(s−pt)22σ2)

局部注意力示意图(图片来自以上论文),

可以说local Attention介于global Attention和hard Attention的折中。

global Attention与local Attention都可以归类为soft Attention。

转载请包括本文地址:https://allenwind.github.io/blog/9479
更多文章请参考:https://allenwind.github.io/blog/archives/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK