注意力机制介绍(attention)

article/2025/10/29 12:06:37

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

在这里插入图片描述
人眼的视觉注意力允许我们以“高分辨率”关注某个特定区域(例如黄色框内的耳朵)同时以“低分辨率”处理周围的环境信息(例如下雪的背景),接下来我们转移关注点或者直接根据关注点作出相应的判断。给定一张图片的一些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 = [ x 1 , x 2 , … , x n ] y = [ y 1 , y 2 , … , y m ] \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}}_i h i 以及反向隐藏状态 h ← i \overleftarrow{\boldsymbol{h}}_i h i 的双向RNN(或者按照我们的选择设定其他种类的循环网络)。将这两个隐藏状态简单拼接表示编码器状态。其出发点是在处理某个单词时包括它前面以及后面单词的信息:

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

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

c t = ∑ i = 1 n α t , i h i ; Context vector for output  y t α t , i = align ( y t , x i ) ; How well two words  y t and  x i are aligned. = exp ⁡ ( score ( s t − 1 , h i ) ) ∑ i ′ = 1 n exp ⁡ ( score ( s t − 1 , h i ′ ) ) ; 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 output yt; How well two words yt and xi are aligned.; Softmax of some predefined alignment score..

alignment模型将一个分数 α t , i \alpha_{t,i} αt,i 分配给输入位置为 i i i 以及输出位置为 t t t 的一个组合,分数值取决于它们之间的匹配度。 { α 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 ( s t , h i ) = v a ⊤ tanh ⁡ ( W a [ s t ; h i ] ) \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])

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

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

在这里插入图片描述

Attention机制家族

待补充

总结

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

名字alignment score 函数引用
Content-base attention score ( s t , h i ) = cosine [ s t , h i ] \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 ( s t , h i ) = v a ⊤ tanh ⁡ ( W a [ s t ; h i ] ) \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 ( W a s t ) \alpha_{t,i} = \text{softmax}(\mathbf{W}_a \boldsymbol{s}_t) αt,i=softmax(Wast)(注意,这简化了softmax alignment,仅仅依赖于目标位置)Luong2015
General score ( s t , h i ) = s t ⊤ W a h i \text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \boldsymbol{s}_t^\top\mathbf{W}_a\boldsymbol{h}_i score(st,hi)=stWahi(这里 W a \mathbf{W}_a Wa是attention层的一个可训练权重参数矩阵Luong2015
Dot-Product score ( s t , h i ) = s t ⊤ h i \text{score}(\boldsymbol{s}_t, \boldsymbol{h}_i) = \boldsymbol{s}_t^\top\boldsymbol{h}_i score(st,hi)=sthiLuong2015
Scaled Dot-Product(^) score ( s t , h i ) = s t ⊤ h i n \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/SoftAttending to the entire input state space.Xu2015
Local/HardAttending 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), 两者的维度均为 n n n(输入的序列长度),在NMT文中,keys以及values都是编码器的隐藏状态。在decoder中,先前的输出被压缩为一个维度为m的query Q \mathbf{Q} Q,下一个输出通过将这个query以及对应的keys以及values进行mapping得到。

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

Attention ( Q , K , V ) = softmax ( Q K ⊤ 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? 😉 According to the paper, “multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions. With a single attention head, averaging inhibits this."


http://chatgpt.dhexx.cn/article/EiyobvPV.shtml

相关文章

什么是注意力机制及其应用(self attention)?

一、引言 注意力机制是自深度学习快速发展后广泛应用于自然语言处理、统计学习、图像检测、语音识别等领域的核心技术,例如将注意力机制与RNN结合进行图像分类,将注意力机制运用在自然语言处理中提高翻译精度,注意力机制本质上说就是实现信息…

自注意力(Self-Attention)

一、自注意力机制概述 循环神经网络由于信息传递的容量以及梯度消失问题,实际上也只能建立短距离依赖关系。 为了建立长距离的依赖关系,可以增加网络的层数或者使用全连接网络。但是全连接网络无法处理变长的输入序列,另外,不同的…

5、注意力机制和Transformer模型

1、人类的视觉注意力 从注意力模型的命名方式看,很明显其借鉴了人类的注意力机制,因此,我们首先简单介绍人类视觉的选择性注意力机制。 视觉注意力机制是人类视觉所特有的大脑信号处理机制。人类视觉通过快速扫描全局图像,获得需…

注意力机制原理及其模型发展和应用

点击上方“小白学视觉”,选择加"星标"或“置顶” 重磅干货,第一时间送达 Attention机制在近几年来在图像,自然语言处理等领域中都取得了重要的突破,被证明有益于提高模型的性能。Attention机制本身也是符合人脑和人眼的…

深度解析注意力模型(attention model)

前言attention的内部结构是什么? 前言 这里学习的注意力模型是我在研究image caption过程中的出来的经验总结,其实这个注意力模型理解起来并不难,但是国内的博文写的都很不详细或说很不明确,我在看了 attention-mechanism后才完全…

图解自注意力机制

写在最前边 这个文章是《图解GPT-2 | The Illustrated GPT-2 (Visualizing Transformer Language Models)》的一部分,因为篇幅太长我就单独拿出来了。 当然如果你只想了解自注意力机制可以只看本文的前半部分。 后半部分主要是讲Masked Self-attention在GPT-2中的应…

NeurIPS 2021 | Twins:重新思考高效的视觉注意力模型设计

Twins 是美团和阿德莱德大学合作提出的视觉注意力模型,相关论文已被 NeurIPS 2021 会议接收。本文主要讲述 Twins 解决的难点、设计和实现思路,以及在美团场景的探索落地,希望能对从事视觉算法研发的同学有所帮助和启发。 导读 Twins [1] 是美…

深度理解机器学习20-注意力机制模型

人类的注意力机制(Attention Mechanism)是从直觉中得到,它是人类利用有限的注意力资源从大量信息中快速筛选出高价值信息的手段。深度学习中的注意力机制借鉴了人类的注意力思维方式,被广泛的应用在自然语言处理(Natur…

人工智能之注意力模型

朋友们,如需转载请标明出处:人工智能AI技术的博客_CSDN博客-python系列教程,人工智能,程序人生领域博主 注意力模型 通过对教程中前面一些文章的学习,我们知道可以用上面的神经网络来实现机器翻译。假设要将一段法语句子翻译成英文句子。那么…

注意力之双线性模型注意力

本文主要针对两篇论文:双线性注意力网络模型和深度模块化注意力进行总结,加上自己对其的理解。若有不足,还望指出。 论文地址: 双线性注意力网络 深度模块化注意力 项目地址: 双线性注意力网络 深度模块化注意力 0. 写…

注意力模型CBAM

论文:CBAM: Convolutional Block Attention Module Convolutional Block Attention Module (CBAM) 表示卷积模块的注意力机制模块。是一种结合了空间(spatial)和通道(channel)的注意力机制模块。相比于senet只关注通道…

注意力模块

目前主流的注意力机制可以分为以下三种:通道注意力、空间注意力以及自注意力(Self-attention) 通道域旨在显示的建模出不同通道之间的相关性,通过网络学习的方式来自动获取到每个特征通道的重要程度,最后再为每个通道…

注意力机制学习

注意力机制学习 学习于博客https://blog.csdn.net/weixin_44791964/article/details/121371986 1.Channel Attention 1.1 SeNet 对于输入进来的特征层,关注其每一个通道的权重,让网络关注它最需要关注的通道。【channel不变,h,w变】 代表…

一般注意力模型

文章目录 一般注意力模型注意力输入注意力输出 一般注意力模型 描述一般注意力模型,首先要描述可以使用注意力的模型的一般特征。我们将这种模型称为任务模型,如图: 这个模型接受一个输入,执行指定的任务,然后产生所…

深度学习中的注意力机制模型及代码实现(SE Attention、CBAM Attention)

目录 常用的注意力机制模型 SE Attention CBAM Attention CBAM Attention 模型结构​ CBAM Attention 代码实现(Pytorch版): 注意力机制加到网络的哪里合适 常用的注意力机制模型 常用的注意力机制多为SE Attention和CBAM Attention。它…

深度学习笔记——Attention Model(注意力模型)学习总结

深度学习里的Attention model其实模拟的是人脑的注意力模型,举个例子来说,当我们观赏一幅画时,虽然我们可以看到整幅画的全貌,但是在我们深入仔细地观察时,其实眼睛聚焦的就只有很小的一块,这个时候人的大脑…

什么是注意力机制?

Attention机制在近几年来在图像,自然语言处理等领域中都取得了重要的突破,被证明有益于提高模型的性能。 Attention机制本身也是符合人脑和人眼的感知机制,这次我们主要以计算机视觉领域为例,讲述Attention机制的原理&#xff0c…

transformer 模型(self-attention自注意力)

transformer模型在《Attention is all you need》论文中提出 这篇论文主要亮点在于:1)不同于以往主流机器翻译使用基于RNN的seq2seq模型框架,该论文用attention机制代替了RNN搭建了整个模型框架。2)提出了多头注意力(…

深度学习中的注意力机制模型ECANet

目录 ECANet简介 ECA Module ECANet 性能对比 ECANet简介 Efficient Channel Attention Module 简称 ECA,2020年 Qilong Wang等人提出的一种 高效通道注意力(ECA)模块 ; 提出了一种 不降维的局部跨通道交互策略 ,有效避免了降维对于通道…

注意力模型直观理解(Attention Model Intuition)

来源:Coursera吴恩达深度学习课程 本周的课程我们都在使用这个编码解码的构架(a Encoder-Decoder architecture)来完成机器翻译。当你使用RNN读一个句子,于是另一个会输出一个句子。注意力模型(the Attention Model&a…