Attention 总结(全)

1.self attention

[深度应用]·Keras实现Self-Attention文本分类(机器如何读懂人心)

 Self attention 在NLP中有很多的应用,对于它的作用,个人觉得是通过attention score,能够区分出文本的不同部分对最终的任务有不同的重要性,比如,对于文本的分类任务,不同的字/词对于任务是有不同的重要程度,Self Attention从《Attention Is All You Need》提出。

具体过程可以参考《The Illustrated Transformer




 

对于计算出来的Z交给后面的任务前,有两种办法对tensor进行‘拉平’

  • 1. K.sum 

K.sum(weighted_input, axis=1)

  • 2. Pooling layer

input_seq = Self_Attention(128)(embeddings)
       input_seq = GlobalAveragePooling1D()(input_seq)

  • 3. 注:如果要使用attention则需要让LSTM每一步都有返回,因为每一步都要计算attention(如上1,2),model.add(Bidirectional(LSTM(64, return_sequences=True)))

其它与self attention有关的可以参考:

2.encode-decode attention

出自:Neural Machine Translation by Jointly Learning to Align and Translate

出自:《attention各种形式总结》-Encode-Decode attention

2.1 Bahdanau Attention(Hard attention)

2.2 LuongAttention - Global(Soft attention)

global-attention 其实就是 soft-attention

3. Multi-head Attention



下面出自:《为什么需要Multi-head Attention》10分钟带你深入理解Transformer原理及实现

可以类比CNN中同时使用多个滤波器的作用,直观上讲,多头的注意力有助于网络捕捉到更丰富的特征/信息。

论文中是这么说的:

Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions.

关于different representation subspaces,举一个不一定妥帖的例子:当你浏览网页的时候,你可能在颜色方面更加关注深色的文字,而在字体方面会去注意大的、粗体的文字。这里的颜色和字体就是两个不同的表示子空间。同时关注颜色和字体,可以有效定位到网页中强调的内容。使用多头注意力,也就是综合利用各方面的信息/特征

我觉得也可以把多头注意力看作是一种ensemble,模型内部的集成。不过另外的答主也提到了,多头注意力的机理还不是很清楚。事实上,注意力机制本身如何工作,这个可解释性工作也还没有完成,目前的一些解释都还只是intuition,除了seq2seq中起到一种alignment的作用外,在许多模型中加入注意力以后如何起作用,还是有一点争议的。

Multi-Head其实不是必须的,去掉一些头效果依然有不错的效果(而且效果下降可能是因为参数量下降),这是因为在头足够的情况下,这些头已经能够有关注位置信息、关注语法信息、关注罕见词的能力了,再多一些头,无非是一种enhance或noise而已。

4. attention的各种细节

Attention首先分为两大类:Hard Attention 与 Soft Attention, 两者的区别在于 Hard Attention 关注一个很小的区域,而soft Attention 关注的相对要发散。 

在 soft attention阵营中,很快又划分为两大阵营: Glocal attention 与 Local attention, 二者的区别在于关注的范围大小不同, 其中,Global attention 关注全部的文字序列,而 Local attention 关注的是固定的窗口中的所有文字序列

比较二者, Global attention 的计算量要比 Local Attention 要大,尤其是对于长句子而言,效率变得很低; 而 Local Attention 只与窗口内的文字相关,因此窗口的大小就显得至关重要了,且在local attention 中多了一个预测中心词的过程,这有可能会忽略一些重要的词。 而对于窗口的设置,论文中采用高斯分布来实现。

由于Global Attention考虑的信息较多,因此从原理上讲要更好一些,毕竟local attention 可能会忽略对当前输出很重要的词,且 Local Attention 的表现与窗口的大小密切相关,如果设置小了,可能会导致效果变得很差。而考虑到NLP中问题的复杂性(如句子长短不一,句子之间可能有很强的相关性),因此后来的很多论文中很少考虑采用 Local Attention 方法,且我自己在做阅读理解任务时,也基本不会考虑Local Attention, 毕竟窗口大小的设置实在太考验人了。

 

  • 1. 乘法VS加法attention

乘法是Self Attention中的Attention,下面是加法Attention:

  • 2. hard attention/soft attention

《Hard&Soft Attention》

《soft/hard attention-->multi-head attention》

《NLP中的Attention机制》

  • Bahdanau et al., Neural Machine Translation by Jointly Learning to Align and Translate, at 2015 ICLR
  • Luong et al., Effective Approaches to Attention Based Neural Machine Translation, at 2015 EMNLP

这两种算法在 Tensorflow attention wrapper 里都可以找到

https://github.com/tensorflow/nmt#background-on-the-attention-mechanism

Luong 的计算方法也被称为 “Soft Attention“, Bahdanau 的算法被称为 “Hard Attention". 两者主要区别在于 attention 计算的时候是否可以 access 所有的数据,比如在给图像配文字的任务中(Xu et al, 2015, Show, Attend and Tell)先把图像切成了不同的 batch, 就是典型的 CNN 了,在生成文本描述时,attention 是基于特定的 batch 的表征, 还是同时可见所有 batch 的表征。

https://lilianweng.github.io/lil-log/2018/06/24/attention-attention.html#soft-vs-hard-attention

  • 3. Local/Global attention

Global-attention 其实就是 soft-attention;

Local-attention 是 hard-attention 和 soft-attention 的折衷;

Global-attention: 全局注意力顾名思义对整个feature mapping进行注意力加权。

  • 4. multi-head attention

按Attention的计算方式

 

 

 

参考:

1.  《attention各种形式总结

2.   《self attention

3.   《NLP中的 Attention 机制

文献:

[1] Show, Attend and Tell: Neural Image Caption Generation with Visual Attention -- 不推荐

[2] Effective Approaches to Attention-based Neural Machine Translation

[3] Neural Machine Translation by Jointly Learning to Align and Translate

[4] Neural Responding Machine for Short-Text Conversation

[5] Attention is all you need

[6] 深度学习中的注意力机制

[7] Attention机制详解(二)——Self-Attention与Transformer

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值