Self-Attention && Cross-Attention 区别

c40bdb17b6edc96df5466e3475718c69.jpeg

transformer的细节到底是怎么样的?Transformer 连环18问!

4.1 从功能角度,Transformer Encoder的核心作用是提取特征,也有使用Transformer Decoder来提取特征。例如,一个人学习跳舞,Encoder是看别人是如何跳舞的,Decoder是将学习到的经验和记忆,展现出来

4.2 从结构角度,如图5所示,Transformer Encoder = Embedding + Positional Embedding + N*(子Encoder block1 + 子Encoder block2);

子Encoder block1 = Multi head attention + ADD + Norm;

子Encoder block2 = Feed Forward + ADD + Norm;

4.3 从输入输出角度,N个Transformer Encoder block中的第一个Encoder block的输入为一组向量 X = (Embedding + Positional Embedding),向量维度通常为512*512,其他N个TransformerEncoder block的输入为上一个 Transformer Encoder block的输出,输出向量的维度也为512*512(输入输出大小相同)。

4.4 为什么是512*512?前者是指token的个数,如“我爱学习”是4个token,这里设置为512是为了囊括不同的序列长度,不够时padding。后者是指每一个token生成的向量维度,也就是每一个token使用一个序列长度为512的向量表示。人们常说,Transformer不能超过512,否则硬件很难支撑;其实512是指前者,也就是token的个数,因为每一个token要做self attention操作;但是后者的512不宜过大,否则计算起来也很慢。

ecd01c95deac94449104cee6ef61ac07.jpeg

 

5.1 从功能角度,相比于Transformer Encoder,Transformer Decoder更擅长做生成式任务,尤其对于自然语言处理问题。

5.2 从结构角度,如图6所示,Transformer Decoder = Embedding + Positional Embedding + N*(子Decoder block1 + 子Decoder block2 + 子Decoder block3)+ Linear + Softmax;

子Decoder block1 = Mask Multi head attention + ADD + Norm;

子Decoder block2 = Multi head attention + ADD + Norm;

子Decoder block3 = Feed Forward + ADD + Norm;

5.3 从(Embedding+Positional Embedding)(N个Decoder block)(Linear + softmax) 这三个每一个单独作用角度:

Embedding + Positional Embedding :以机器翻译为例,输入“Machine Learning”,输出“机器学习”;这里的Embedding是把“机器学习”也转化成向量的形式。

N个Decoder block:特征处理和传递过程。

Linear + softmax:softmax是预测下一个词出现的概率,如图7所示,前面的Linear层类似于分类网络(ResNet18)最后分类层前接的MLP层。

 

5e3a1d20a4cae00bf0bd10a8f20f7777.jpeg

6. Transformer Encoder和Transformer

Decoder有哪些不同?

6.1 作用上,Transformer Encoder常用来提取特征,Transformer Decoder常用于生成式任务。Transformer Encoder和Transformer Decoder是两条不同的技术路线,Bert采用的前者,GPT系列模型采用的是后者。

6.2 结构上,Transformer Decoder block包括了3个子Decoder block,而Transformer Encoder block 包括2个子Encoder block,且Transformer Decoder中使用了Mask multi-head Attention。

6.3 从二者的输入输出角度,N个Transformer Encoder运算完成之后,它的输出才正式输入进Transformer Decoder,作为QKV中的K和V,给Transformer Decoder使用。那么TransformerEncoder最后层的输出是如何送给Decoder呢?

7. 什么是Embedding?

7.1 Embedding在Transformer架构中的位置如图13所示。

7.2 提出背景: 计算机无法直接处理一个单词或者一个汉字,需要把一个token转化成计算机可以识别的向量,这也就是embedding过程。

7.3 实现方式: 最简单的embedding操作就是one hot vector,但one hot vector有一个弊端就是没有考虑词语前后之间的关系,后来也就产生了WordEmbedding,如图13。

What is cross-attention, and how does it differ from self-attention?

 

In self-attention, we work with the same input sequence. In cross-attention, we mix or combine two different input sequences. In the case of the original transformer architecture above, that’s the sequence returned by the encoder module on the left and the input sequence being processed by the decoder part on the right.

Note that in cross-attention, the two input sequences \(\mathbf{x}_1\) and \(\mathbf{x}_2\) can have different numbers of elements. However, their embedding dimensions must match.

The figure below illustrates the concept of cross-attention. If we set \(\mathbf{x}_1 = \mathbf{x}_2\), this is equivalent to self-attention.

c0a04bc2928c0d203222c0a3931afc6f.png

下面是Cross-Attention

 7d0013613f14efe0b03f8b151efa2914.png

 

(Note that the queries usually come from the decoder, and the keys and values usually come from the encoder.)

 

### Cross-Attention Self-Attention区别及应用 #### 定义与机制 Self-Attention 是一种注意力机制,它允许序列中的每个位置关注整个序列的信息。通过计算输入序列中各个部分的相关性权重,Self-Attention 能够捕捉到全局依赖关系并生成上下文感知的表示[^1]。 Cross-Attention 则用于两个不同的序列之间建立关联。具体来说,给定一个查询(Query)序列键值对(Key-Value Pair)序列,Cross-Attention 可以让查询序列基于另一个序列的内容来调整其表示[^2]。 #### 计算过程对比 在 Self-Attention 中,查询 (Q)、键 (K) 值 (V) 都来源于同一个输入序列。这意味着模型会学习如何将同一序列的不同部分联系起来,从而增强局部或远程的关系建模能力。 而在 Cross-Attention 设置下,查询通常来自于源序列或者目标序列之一,而键值则来自另一方。这种设计使得模型能够有效地跨域或多模态数据间传递信息,比如文本与图像间的交互分析。 #### 应用场景比较 - **自然语言处理领域** - 自回归解码器架构常采用 Self-Attention 来捕获句子内部单词之间的长期依存关系,在机器翻译任务中有广泛应用。 - 对于涉及多模态的任务如视觉问答(VQA),Cross-Attention 将被用来融合图片特征问题描述,以便更好地理解两者之间的语义关联。 - **计算机视觉方面** - 卷积神经网络(CNNs)虽然擅长提取空间上的局部模式,但当面对较大范围内的对象识别时可能表现不佳;引入 Transformer 结构及其核心组件——Self-Attention 后可以改善这一情况。 - 当需要结合其他类型的传感器数据(例如雷达信号)来进行环境感知时,则可利用 Cross-Attention 实现异质数据的有效集成。 - **时间序列预测** - 在某些情况下,历史观测值本身就可以提供足够的线索完成未来趋势估计工作,此时仅需依靠 Self-Attention 即可实现良好效果。 - 如果还存在外部辅助变量影响最终结果的话,则应该考虑加入 Cross-Attention 层次进一步提升准确性。 ```python import torch from transformers import BertModel, BertTokenizer tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') model = BertModel.from_pretrained('bert-base-uncased') text = "Replace me by any text you'd like." encoded_input = tokenizer(text, return_tensors='pt') output = model(**encoded_input) print(output.last_hidden_state[:,0]) # Output contains embeddings enriched via self-attention. ``` 上述代码片段展示了 BERT 模型的应用实例,其中包含了典型的 Self-Attention 运作方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值