为什么在变形金刚中使用多头注意力?

Why use multi-headed attention in Transformers?

我想了解为什么 transformer 使用多个注意力头。我发现了以下 quote:

Instead of using a single attention function where the attention can be dominated by the actual word itself, transformers use multiple attention heads.

“注意力被单词本身支配”是什么意思,使用多个头如何解决这个问题?

由于观察到不同的词以不同的方式相互关联,因此引入了多头注意力。对于一个给定的词,句子中的其他词可以起到调节或否定意义的作用,但它们也可以表达继承(is a kind of)、占有(belongs to)等关系

我发现 this online 讲座很有帮助,因此提出了这个例子:

“餐厅不是太糟糕。”

注意'terrible'这个词的意思被'too'和'not'这两个词歪曲了(太:适度,不是:倒置)和'terrible'也与 'restaurant' 相关,因为它表示 属性.

Transformers 最初被提出,正如标题“Attention is All You Need”所暗示的那样,作为一种更有效的 seq2seq 模型消融了当时常用的 RNN 结构。

然而,在追求这种效率的过程中,与基于 RNN 的模型相比,单头注意力降低了描述能力。提出了多个头来缓解这种情况,允许模型学习多个 lower-scale 特征映射而不是一个 all-encompasing 映射:

In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions [...] This makes it more difficult to learn dependencies between distant positions [12]. In the Transformer this is reduced to a constant number of operations, albeit at the cost of reduced effective resolution due to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention...

因此,multiple attention heads 在变压器的单层中 类似于 CNN 中单层中的多个内核:它们具有相同的架构,并且在相同的 feature-space 上运行,但由于它们是分开的 'copies',具有不同的权重集,因此它们 'free' 学习不同的函数。

在CNN中这可能对应视觉特征的不同定义,在Transformer中这可能对应相关性的不同定义:1

例如:

Architecture Input (Layer 1)
Kernel/Head 1
(Layer 1)
Kernel/Head 2
CNN Image Diagonal edge-detection Horizontal edge-detection
Transformer Sentence Attends to next word Attends from verbs to their direct objects

备注:

  1. 不能保证这些是人类可解释的,但在许多流行的架构中,它们确实准确地映射到语言概念上:

    While no single head performs well at many relations, we find that particular heads correspond remarkably well to particular relations. For example, we find heads that find direct objects of verbs, determiners of nouns, objects of prepositions, and objects of possesive pronouns...