注意力机制是指我们将视觉注意力集中在图像的不同区域,或者将注意力集中在一句话中的某个词语,以下图为例:


人眼的视觉注意力允许我们以“高分辨率”关注某个特定区域(例如黄色框内的耳朵)同时以“低分辨率”处理周围的环境信息(例如下雪的背景),接下来我们转移关注点或者直接根据关注点作出相应的判断。给定一张图片的一些patch,其余部分的像素提供给我们patch所在区域是什么的信息。我们期望在黄框内看到一个耳朵,这是因为我们已经看到了一只狗鼻子、另外一个耳朵以及狗狗的眼睛(红框内的物体)。然而,毛衣和毯子对于判断狗狗特征是毫无帮助的。

类似地,我们可以解释某个句子中的单词之间的关系。当我们看见“吃”这个词时,我们希望马上遇到一个食物的单词。下面的”green“单词描述了食物,但是它没有和“吃”直接相关联:


简言之,深度学习中的注意力机制可以被广义地解释为表示重要性的权重向量:为了预测或者推断某个元素,例如图片中的一个像素,或者句子中的一个单词,我们使用注意力向量估计它与其他元素(在论文中这个过程被称为“attend to”)的相关性有多强,并将它们的值与注意力向量加权之后的值之和作为目标的近似值。

Seq2Seq模型存在的问题

seq2seq模型起源于语言处理模型(Sutskever, et al. 2014),广义来讲,它的目的是将输入序列转换为一个新的输出序列,两者都是任意长度的。转化任务的一个例子就是各种语言之间的翻译。

seq2seq模型通常来讲有一个编码-解码结构,由如下部分组成:

  • 编码器处理输入序列并将信息压缩为固定长度的context向量(这也被称为sentence embedding或者“thought”向量)。这种表达方式期望获取整个输入序列的一个较好的总结信息。
  • 解码器由context向量初始化,然后生成转化后的输出。早期工作仅使用解码网络的最后状态作为解码器的初始状态。

编码器和解码器都是循环神经网络,例如使用LSTM或者GRU单元,下图是seq2seq模型的示例:

固定长度的context向量的一个致命缺陷是缺乏记忆长句子的能力。通常它在处理完整个输入后会遗忘前面的信息。因此attention机制在2015年被提出来解决这个问题(Bahdanau et al., 2015)

attention机制的提出

attention机制提出是为了帮助记忆机器翻译(neural machine translation,NMT)中的长句子。attention机制通过一种独特的方式,创建了context向量以及整个输入间的shortcuts。The weights of these shortcut connections are customizable for each output element.

由于context向量可以访问整个输入序列,我们不需要担心遗忘的问题。The alignment between the source and target is learned and controlled by the context vector. Essentially the context vector consumes three pieces of information:

  • 编码器隐藏状态
  • 解码器隐藏状态
  • alignment between source and target

定义

接下来我们定义NMT中提出的attention机制。举例来说,我们有一个长度为n的输入序列x\mathbf{x}x并且想要输出一个长度为m的目标序列y\mathbf{y}y

x=[x1,x2,…,xn]y=[y1,y2,…,ym]\mathbf{x}=[x_1,x_2,\dots,x_n]\quad\mathbf{y}=[y_1,y_2,\dots,y_m]x=[x1,x2,,xn]y=[y1,y2,,ym]

注意加粗黑体表示是一个向量。

编码器是一个带有前向隐藏状态 h→i\overrightarrow{\boldsymbol{h}}_ih

i 以及反向隐藏状态 h←i\overleftarrow{\boldsymbol{h}}_ih

i
的双向RNN(或者按照我们的选择设定其他种类的循环网络)。将这两个隐藏状态简单拼接表示编码器状态。其出发点是在处理某个单词时包括它前面以及后面单词的信息:

hi=[h→i⊤;h←i⊤]⊤,i=1,…,n\boldsymbol{h}_i = [\overrightarrow{\boldsymbol{h}}_i^\top; \overleftarrow{\boldsymbol{h}}_i^\top]^\top, i=1,\dots,nhi=[h

i;h

i
],i=1,,n

解码器网络包含位置t(t=1,…,m)t (t=1,\dots,m)t(t=1,,m) 处的输出单词的隐藏状态 st=f(st−1,yt−1,ct)\boldsymbol{s}_t=f(\boldsymbol{s}_{t-1}, y_{t-1}, \mathbf{c}_t)st=f(st1,yt1,ct) ,这里context向量ct\mathbf{c}_tct is a sum of hidden states of the input sequence, weighted by alignment scores:

ct=∑i=1nαt,ihi; Context vector for output ytαt,i=align(yt,xi); How well two words ytand xiare aligned.=exp⁡(score(st−1,hi))∑i′=1nexp⁡(score(st−1,hi′)); Softmax of some predefined alignment score..\begin{aligned} \mathbf{c}_t &= \sum_{i=1}^n \alpha_{t,i} \boldsymbol{h}_i & \small{\text{; Context vector for output }y_t}\\ \alpha_{t,i} &= \text{align}(y_t, x_i) & \small{\text{; How well two words }y_t\text{ and }x_i\text{ are aligned.}}\\ &= \frac{\exp(\text{score}(\boldsymbol{s}_{t-1}, \boldsymbol{h}_i))}{\sum_{i'=1}^n \exp(\text{score}(\boldsymbol{s}_{t-1}, \boldsymbol{h}_{i'}))} & \small{\text{; Softmax of some predefined alignment score.}}. \end{aligned}ctαt,i=i=1nαt,ihi=align(yt,xi)=i=1nexp(score(st1,hi))exp(score(st1,hi)); Context vector for outputyt; How well two wordsytandxiare aligned.; Softmax of some predefined alignment score..

alignment模型将一个分数 αt,i\alpha_{t,i}αt,i 分配给输入位置为 iii 以及输出位置为 ttt 的一个组合,分数值取决于它们之间的匹配度。{αt,i}\{\alpha_{t, i}\}{αt,i} 集合是一组权重,表示how much of each source hidden state should be considered for each output. 在Bahdanau的论文中,alignment score α\alphaα 由一个单隐藏层的前馈神经网络进行参数化,并且这个网络和模型中的其他部分一起进行训练。score function因此选用如下格式(将tanh用作非线性激活函数):

score(st,hi)=va⊤tanh⁡(Wa[st;hi])\text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \mathbf{v}_a^\top \tanh(\mathbf{W}_a[\boldsymbol{s}_t; \boldsymbol{h}_i])score(st,hi)=vatanh(Wa[st;hi])

这里va\mathbf{v}_ava以及Wa\mathbf{W}_aWa都是alignment模型中要学习的权重矩阵。

alignment score矩阵能够明确显示源词和目标词之间的相关性。

Attention机制家族

待补充

总结

下表总结了几个流行的注意力机制以及它们对应的alignment score函数:

名字 alignment score 函数 引用
Content-base attention score(st,hi)=cosine[st,hi]\text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \text{cosine}[\boldsymbol{s}_t, \boldsymbol{h}_i]score(st,hi)=cosine[st,hi] Graves2014
Additive(*) score(st,hi)=va⊤tanh⁡(Wa[st;hi])\text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \mathbf{v}_a^\top \tanh(\mathbf{W}_a[\boldsymbol{s}_t; \boldsymbol{h}_i])score(st,hi)=vatanh(Wa[st;hi]) Bahdanau2015
Location-Base αt,i=softmax(Wast)\alpha_{t,i} = \text{softmax}(\mathbf{W}_a \boldsymbol{s}_t)αt,i=softmax(Wast)(注意,这简化了softmax alignment,仅仅依赖于目标位置) Luong2015
General score(st,hi)=st⊤Wahi\text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \boldsymbol{s}_t^\top\mathbf{W}_a\boldsymbol{h}_iscore(st,hi)=stWahi(这里Wa\mathbf{W}_aWa是attention层的一个可训练权重参数矩阵 Luong2015
Dot-Product score(st,hi)=st⊤hi\text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \boldsymbol{s}_t^\top\boldsymbol{h}_iscore(st,hi)=sthi Luong2015
Scaled Dot-Product(^) score(st,hi)=st⊤hin\text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \frac{\boldsymbol{s}_t^\top\boldsymbol{h}_i}{\sqrt{n}}score(st,hi)=n

sthi
(注意这和dot-product attention仅相差了一个缩放因子,这里n是source隐藏状态的维度
Vaswani2017

(*) Referred to as “concat” in Luong, et al., 2015 and as “additive attention” in Vaswani, et al., 2017.
(^) It adds a scaling factor (1/\sqrt{n}), motivated by the concern when the input is large, the softmax function may have an extremely small gradient, hard for efficient learning.

如下是广义类别的attention机制的一个总结:

名字 定义 引用
Self-Attention(&) Relating different positions of the same input sequence. 理论上讲self-attention可以适应上述任意类型的score functions,仅需要将目标序列变为输入序列 Cheng2016
Global/Soft Attending to the entire input state space. Xu2015
Local/Hard Attending to the part of input state space; i.e. a patch of the input image. Xu2015; Luong2015

(&) Also, referred to as “intra-attention” in Cheng et al., 2016 and some other papers.

Self-Attention

Self-attenion,同样被称为intra-attention,是一种将单个序列中不同位置关联起来以计算这个序列的某种表达方式的attention机制。这种attention已经被证明在machine reading、abstractive summarization以及image description generation领域十分有效。

long short-term memory网络论文中使用self-attention来进行machine reading。在下面的例子中,self-attention机制使得我们可以学习到当前单词以及句子中之前部分单词之间的相关性。

上图中当前单词用红色标注,蓝色阴影的大小表示activation level。

Soft以及Hard Attention

在 show, attend and tell 论文中,注意力机制用于生成captions. 图片首先被CNN处理来提取特征。接下来一个LSTM解码器使用卷积特征来每次逐个生成descriptive words,权重通过attention机制学习。The visualization of the attention weights clearly demonstrates which regions of the image the model is paying attention to so as to output a certain word.


上图中最终输出的句子是“A woman is throwing a frisbee in a park.”,图片来自于Xu et al. 2015中的图6b。

This paper first proposed the distinction between “soft” vs “hard” attention, based on whether the attention has access to the entire image or only a patch:

  • Soft Attention:alignment权重are learned and placed “softly” over all patches in the source image; essentially the same type of attention as in Bahdanau et al., 2015.
    优点:模型是平滑可微分的
    缺点:当输入很大时计算量大
  • Hard Attention:only selects one patch of the image to attend to at a time.
    优点:less calculation at the inference time.
    缺点:模型无法微分并且需要more complicated techniques such as variance reduction or reinforcement learning to train. (Luong, et al., 2015)

Global以及Local Attention对比

Luong, et al., 2015 proposed the “global” and “local” attention. The global attention is similar to the soft attention, while the local one is an interesting blend between hard and soft, an improvement over the hard attention to make it differentiable: the model first predicts a single aligned position for the current target word and a window centered around the source position is then used to compute a context vector.

Neural Turing Machines

Pointer网络

Transformer

“Attention is All you Need” (Vaswani, et al., 2017)是2017年最有影响力的工作之一。它针对soft attention提出了大量的改进,使得我们可以在没有循环网络单元的情况下进行seq2seq modeling。Transformer模型整体构建于self-attention机制,没有用到sequence-aligned recurrent 结构。

Key,Value以及Query

transformer中最主要的组成部分就是multi-head self-attention机制。transformer将输入的编码表示为一组键值对(K,V)(\mathbf{K}, \mathbf{V})(K,V), 两者的维度均为nnn(输入的序列长度),在NMT文中,keys以及values都是编码器的隐藏状态。在decoder中,先前的输出被压缩为一个维度为m的queryQ\mathbf{Q}Q,下一个输出通过将这个query以及对应的keys以及values进行mapping得到。

transformer采用scaled dot-product attention:输出是values的加权和,这里分配给每个value的权重通过query和所有keys的dot-product确定:

Attention(Q,K,V)=softmax(QK⊤n)V\text{Attention}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \text{softmax}(\frac{\mathbf{Q}\mathbf{K}^\top}{\sqrt{n}})\mathbf{V}Attention(Q,K,V)=softmax(n

QK)V

Multi-Head Self-Attention


Rather than only computing the attention once, the multi-head mechanism runs through the scaled dot-product attention multiple times in parallel. The independent attention outputs are simply concatenated and linearly transformed into the expected dimensions. I assume the motivation is because ensembling always helps?

注意力机制介绍(attention)相关推荐

  1. 在RNN模型中引入注意力机制(Attention)

    此前的文章介绍过Seq2seq模型,并将其用于机器翻译.Seq2seq模型的一个问题在于随着输入句子越来越长,更早输入的单词就很大可能会被忘掉.于是,随着输入句子中单词数变多,翻译质量就会很快劣化.改 ...

  2. 深入理解深度学习——注意力机制(Attention Mechanism):注意力评分函数(Attention Scoring Function)

    分类目录:<深入理解深度学习>总目录 相关文章: ·注意力机制(AttentionMechanism):基础知识 ·注意力机制(AttentionMechanism):注意力汇聚与Nada ...

  3. 深入理解注意力机制(Attention Mechanism)和Seq2Seq

    学习本部分默认大家对RNN神经网络已经深入理解了,这是基础,同时理解什么是时间序列,尤其RNN的常用展开形式进行画图,这个必须理解了. 这篇文章整理有关注意力机制(Attention Mechanis ...

  4. 注意力机制(Attention Mechanism)-ECANet

    引言 神经网络中的注意力机制(Attention Mechanism)是在计算能力有限的情况下,将计算资源分配给更重要的任务,同时解决信息超载问题的一种资源分配方案.在神经网络学习中,一般而言模型的参 ...

  5. 注意力机制(Attention Mechanism)-SENet

    引言 神经网络中的注意力机制(Attention Mechanism)是在计算能力有限的情况下,将计算资源分配给更重要的任务,同时解决信息超载问题的一种资源分配方案.在神经网络学习中,一般而言模型的参 ...

  6. 深入理解深度学习——注意力机制(Attention Mechanism):带掩码的多头注意力(Masked Multi-head Attention)

    分类目录:<深入理解深度学习>总目录 相关文章: ·注意力机制(AttentionMechanism):基础知识 ·注意力机制(AttentionMechanism):注意力汇聚与Nada ...

  7. 空间注意力机制sam_Attention注意力机制介绍

    什么是Attention机制 Attention机制通俗的讲就是把注意力集中放在重要的点上,而忽略其他不重要的因素.其中重要程度的判断取决于应用场景,拿个现实生活中的例子,比如1000个人眼中有100 ...

  8. 深度学习【注意力机制(Attention)原理和实现】

    文章目录 一 Attention的原理和实现 1. Attention的介绍 2. Attenion的实现机制 2.1 Attention的实现过程 2.2 不同Attention的介绍 2.2.1 ...

  9. 注意力机制(attention)学习记录(二)

    前面曾经记录过注意力机制的学习过程,今天则是在学习的过程中对其有了其他的理解,便将其记录下来. Attention Model 概述 深度学习里的Attention model其实模拟的是人脑的注意力 ...

  10. Transformer:注意力机制(attention)和自注意力机制(self-attention)的学习总结

    目录 前言 1. 注意力机制 1.1非自主提示和自主提示 1.2 查询,键和值 1.3 注意力机制的公式 1.3.1 平均汇聚 1.3.2 非参数的注意力汇聚(Nadaraya-Watson核回归) ...

最新文章

  1. 随手正则写的 CSDN【只看楼主】功能
  2. java kettle6_Java调用Kettle6的transaction和job
  3. PHP Module
  4. 深入mysql慢查询设置的详解
  5. python中带附件发送电子邮件_python发送带附件邮件
  6. 百度seo排名规则_百度关键词seo优化排名如何上首页
  7. ORACLE按用户名重建索引
  8. centos配置ssh免密码登录
  9. iOS 网络请求封装
  10. 百度网盘无提取码分享文件方法
  11. 数据结构--哈希(Hash)和代码实现(详解)
  12. Jmeter实战:零基础也能看懂的性能测试
  13. ICC图文流程——(六)可造性设计Chip finishing
  14. 物理建模钢琴-Arturia Piano V2 v2.5.0.3410 MacOSX
  15. c语言银行信用卡卡管理系统,自编自导多人多卡信用卡管理系统
  16. 点击按钮随机更换页面背景颜色
  17. 【Endnote】在论文中插入参考文献,并将插入参考文献的序号格式改为[1] [2] [3] 或1,2,3
  18. 领导合影站位图_领导座次安排图,没有人会教你这些,但非常重要!(建议收藏)...
  19. cocos2d-x 关于旋转和移动的一点小技巧
  20. 简单的stm32入门小程序(交通信号灯)STM32F103C8T6

热门文章

  1. 【数据、软件共享】年鉴,夜间灯光,土地数据(永久更新)
  2. Qt实现判断鼠标左右键信号
  3. “空天地”一体化的遥感农业保险简介
  4. Mysql字符集转换原理剖析及乱码原因
  5. 安卓自定义列表dialog
  6. 大厂的 404 页面都长啥样?看到最后一个,我笑了。。。
  7. 昨晚家里停网后,我动了邪念用Python破解了隔壁小姐姐的wifi密码
  8. 稀疏表示的合适字典_基于共振稀疏分解的滚动轴承早期微弱故障诊断
  9. mysql语句怎么替换_mysql怎么批量替换sql语句
  10. .net 导出excel_.NET Core一行代码导入导出Excel生成Word