当前位置: 首页 > news >正文

研读论文《Attention Is All You Need》(7)

原文 14

3.2 Attention

An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key.

翻译

注意力函数可以被描述为将查询和一组键值对映射到输出的过程,其中查询、键、值和输出均为向量。输出的计算方式为值的加权和,而每个值对应的权重由查询与相应键的兼容性函数计算得出。

重点句子解析

  1. An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors.

【解析】

句子的结构是:主句+非限制性定语从句。主句采用了被动语态,其主干是:An attention function can be described as mapping A to B。其中,A代表a query and a set of key-value pairs,其中的a query和a set of key-value pairs是由and连接的两个并列名词短语;a set of(一组,一套) 和key-value都修饰pairs;B代表an output。be described as表示:被描述为,mapping A to B表示:把A映射到B。主句后边是where引导的非限制性定语从句,其中,where可以理解为“在此情况下”,指代前文描述的映射过程。从句属于“主系表”结构,其中的the query, keys, values, and output 是由逗号和and连接的四个并列名词,共同做主语。

【参考翻译】

注意力函数可以被描述为将查询和一组键值对映射到输出的过程,其中查询、键、值和输出均为向量。

  1. The output is computed as a weighted sum of the values, where the weight assigned to each value is computed by a compatibility function of the query with the corresponding key.

【解析】

句子的结构是:主句+非限制性定语从句。主句是一个较为简单的被动句,其中,is computed as的字面意思是“被计算为”,也可以活译为“计算方式为”;a weighted sum表示“加权和”;介词短语of the values做后置定语,意为:这些值的。where引导定语从句,并且在从句中做状语,可以理解为:在前边所述的计算过程中;从句的主干是:the weight is computed by a compatibility function. 意思是说:权重通过兼容性函数来计算。原句中的过去分词短语assigned to each value做后置定语,修饰the weight,字面意思是:被赋予每个值的;介词短语of the query with the corresponding key做后置定语,修饰function。其中的另一个介词with表示前后两者有关联,可以译为“与…”。

【参考翻译】

输出的计算方式为值的加权和,其中,每个值对应的权重由查询与相应键的兼容性函数计算得出。

原文 15

3.2.1 Scaled Dot-Product Attention

We call our particular attention “Scaled Dot-Product Attention” (Figure 2). The input consists of queries and keys of dimension d k d_k dk , and values of dimension d v d_v dv. We compute the dot products of the query with all keys, divide each by d k \sqrt{d_k} dk , and apply a softmax function to obtain the weights on the values.

在这里插入图片描述

Figure 2: (left) Scaled Dot-Product Attention. (right) Multi-Head Attention consists of several attention layers running in parallel.
图2:(左)缩放点积注意力;(右)多头注意力机制由多个并行运行的注意力层组成。

翻译

3.2.1 缩放点积注意力

我们将这种特定的注意力称为"缩放点积注意力"(图2)。输入包含维度为 d k d_k dk 的查询(queries)和键(keys),以及维度为 d v d_v dv 的值(values)。我们首先计算查询与所有键的点积,然后将每个点积结果除以 d k \sqrt{d_k} dk ,最后应用softmax 函数得到值的权重分布。

重点句子解析

  1. We call our particular attention “Scaled Dot-Product Attention” .

【解析】

这个句子的结构是“主语+谓语+宾语+宾补”。谓语动词call后边的our particular attention是宾语,"Scaled Dot-Product Attention"是宾语补足语(简称宾补),对宾语进行补充说明。我们可以把宾语our particular attention看作A,把宾补Scaled Dot-Product Attention看作B,从而把整个句子简化为:We call A B(我们把A称为B)。

【参考翻译】

我们将这种特定的注意力称为“缩放点积注意力”。

  1. The input consists of queries and keys of dimension d k d_k dk, and values of dimension d v d_v dv.

【解析】

这个一个“主谓宾”结构的简单句,只不过用作谓语的动词短语consists of(包含;由……组成) 后边有两个宾语,一个是queries and keys of dimension dk,另一个是values of dimension dv。如果我们把第一个宾语queries and keys of dimension dk看作A,把第二个宾语values of dimension dv看作B,就可以把整个句子简化为:The input consists of A and B(输入包含A和B)。再来看queries and keys of dimension dk,这里的of dimension dk是后置定语,修饰前边的并列名词queries and keys;在values of dimension dv中也有一个介词短语of dimension dv充当后置定语。

【参考翻译】

输入包含维度为 d k d_k dk 的查询和键,以及维度为 d v d_v dv 的值。

  1. We compute the dot products of the query with all keys, divide each by d k \sqrt{d_k} dk , and apply a softmax function to obtain the weights on the values.

解析:

句子的主语“We”后边跟了三个并列的动宾结构,可以简化为:We compute the dot products, divide each, and apply a softmax function. 原句中的介词短语of the query with all keys是后置定语,其中的另一个介词with体现了the query 和all keys之间的关联;介词短语by d k \sqrt{d_k} dk 是后置定语。结尾处的不定式短语 to obtain the weights on the values做目的状语,其中on the values是介词短语充当后置定语,修饰the weights。

【参考翻译】

我们首先计算查询与所有键的点积,然后将每个点积结果除以 d k \sqrt{d_k} dk ,最后应用softmax函数得到值的权重分布。

原文 16

In practice, we compute the attention function on a set of queries simultaneously, packed together into a matrix Q Q Q. The keys and values are also packed together into matrices K K K and V V V . We compute the matrix of outputs as:

Attention ( Q , K , V ) = s o f t m a x ( Q K T d k ) V (1) \text{Attention}(Q, K, V) = softmax(\frac{QK^T}{\sqrt{d_k}})V\tag{1} Attention(Q,K,V)=softmax(dk QKT)V(1)

The two most commonly used attention functions are additive attention, and dot-product (multi-plicative) attention. Dot-product attention is identical to our algorithm, except for the scaling factor of 1 d k \frac{1}{\sqrt{d_k}} dk 1 . Additive attention computes the compatibility function using a feed-forward network with a single hidden layer. While the two are similar in theoretical complexity, dot-product attention is much faster and more space-efficient in practice, since it can be implemented using highly optimized matrix multiplication code.

翻译

在实际操作中,我们会同时对一组查询进行注意力函数的计算,这些查询被一起打包成矩阵 Q Q Q。同样,键和值也分别被一起打包成矩阵 K K K V V V。我们对输出的矩阵按以下公式计算:

Attention ( Q , K , V ) = s o f t m a x ( Q K T d k ) V (1) \text{Attention}(Q, K, V) = softmax(\frac{QK^T}{\sqrt{d_k}})V\tag{1} Attention(Q,K,V)=softmax(dk QKT)V(1)

最常用的两种注意力函数是加性注意力和点积(乘法)注意力。点积注意力与我们的算法相同,只是有一个缩放因子 1 d k \frac{1}{\sqrt{d_k}} dk 1 的差别。加性注意力使用含有单个隐藏层的前馈网络计算兼容性函数。虽然二者理论复杂度相似,但实践中点积注意力速度更快且空间利用率更高,因其可通过高度优化的矩阵乘法代码实现。

相关文章:

  • Axure难点解决分享:垂直菜单展开与收回(4大核心问题与专家级解决方案)
  • LeetCode 35 搜索插入位置题解
  • 力扣HOT100之二叉树:230. 二叉搜索树中第 K 小的元素
  • python-leetcode 67.寻找两个正序数组中的中位数
  • MySQL数据库基础 -- SQL 语句的分类,存储引擎
  • Django框架的前端部分使用Ajax请求一
  • 【数根】2022-1-24
  • Vue 中 v-model 的三种使用方式对比与实践
  • C++23 std::mdspan:多维数组处理新利器
  • 【第二届帕鲁杯】第二届帕鲁杯 主环境 基本完整wp
  • linux安装conda环境-ubuntu
  • ECharts-饼图
  • 深入理解 ZAB:ZooKeeper 原子广播协议的工作原理
  • 游戏开发实战(一):Python复刻「崩坏星穹铁道」嗷呜嗷呜事务所---源码级解析该小游戏背后的算法与设计模式【纯原创】
  • 云计算简介:从“水电”到“数字引擎”的技术革命
  • 使用docker——10分钟内 完成一个高可用的 MongoDB 副本集部署
  • 【HTML】【面试提问】HTML面试提问总结
  • UE5在C++项目中判断不同平台
  • Docker项目部署深度解析:从基础命令到复杂项目部署
  • HarmonyOS实战:自定义时间选择器
  • 减负举措如何助力基层干部轻装上阵?记者一线调查
  • LPR名副其实吗?如果有所偏离又该如何调整?
  • AG600“鲲龙”批生产首架机完成生产试飞
  • 女排奥运冠军宋妮娜:青少年保持身心健康才能走得更远
  • 穆迪下调美国主权信用评级
  • 南宁一学校发生伤害案件,警方通报:嫌疑人死亡,2人受伤