LLM(3) | 自注意力机制 (self-attention mechanisms)

news2024/10/5 14:19:13

LLM(3) | 自注意力机制 (self-attention mechanisms)

self-attention 是 transformer 的基础, 而 LLMs 大语言模型也都是 transformer 模型, 理解 self-attention, 才能理解为什么 LLM 能够处理好上下文关联性。

本篇是对于 Must-Read Starter Guide to Mastering Attention Mechanisms in Machine Learning 的翻译。

文章目录

  • LLM(3) | 自注意力机制 (self-attention mechanisms)
    • 掌握机器学习中注意力机制的必读入门指南 Must-Read Starter Guide to Mastering Attention Mechanisms in Machine Learning
      • 从基础知识到自注意力的工作原理, 快速参考指南, 了解这篇标志性论文及其多年后的实际应用 (From the basics to how self-attention works, a quick-reference guide to understanding the iconic paper and its practical applications years later)
      • 机器学习中的 attention 是什么? (What is Attention in Machine Learning?)
      • 注意力机制是如何工作的? 有哪些常见的机制? (How Does It Work and What Are the Common Mechanisms?)
        • Soft Attention
        • Hard Attention
        • Self-Attention
        • Global-Attention
        • Local Attention
      • 有什么好处? (What are the Benefits?)
      • 什么时间、在哪里使用? (When and Where To Use)
    • 总结
    • 个人总结

掌握机器学习中注意力机制的必读入门指南 Must-Read Starter Guide to Mastering Attention Mechanisms in Machine Learning

从基础知识到自注意力的工作原理, 快速参考指南, 了解这篇标志性论文及其多年后的实际应用 (From the basics to how self-attention works, a quick-reference guide to understanding the iconic paper and its practical applications years later)

Originally inspired by the human mechanism of selectively focusing on certain aspects of information, attention mechanisms in machine learning models provide a way to weigh the importance of different elements in a given input. The concept first gained significant popularity and importance in the wake of the iconic paper “Attention is All You Need.”

受人类选择性关注(selectively focusing)信息的机制启发, 机器学习模型中的**注意力机制(attention mechanisms)**提供了一种衡量给定输入中不同元素重要性的方式。注意力机制(attention mechanisms) 这个概念是在标志性论文 “Attention is All You Need” 中首次获得重要的关注和重要性。

机器学习中的 attention 是什么? (What is Attention in Machine Learning?)

Attention is a mechanism in machine learning models that allows them to dynamically focus on specific parts of the input data, assigning varying levels of importance to different elements. It helps in resolving the limitations of fixed-length encoding in traditional neural networks, which can struggle to handle long sequences or complex patterns. By selectively focusing on certain elements of the input, attention mechanisms can improve the model’s overall performance in tasks like natural language processing, image recognition, and speech recognition.

注意力是机器学习模型中的一个机制, 允许他们动态的关注输入数据的特定部分, 对不同元素赋予不同的重要性。 传统神网络中存在固定长度编码(fixed-length encoding)的限制,在长序列、复杂模式下效果不好, 而注意力模型则解决了这个问题。通过选择性的关注输入的特定元素, attention 机制在自然语言处理、图像识别、语音识别等任务上, 可以提升模型的整体性能。

At its core, attention can be thought of as a way of assigning importance weights to different parts of the input, based on their relevance to the task at hand. For example, in natural language processing, attention can be used to determine which words in a sentence are most important for understanding its meaning, while in computer vision, attention can be used to focus on specific regions of an image that are most relevant for object detection or recognition.

核心层面, attention 可以被理解为:给输入的不同部分赋予不同的重要性权重的方法, 这是基于和手头任务的相关性来做到的。比如说, 在自然语言处理中, attention 可以被用于决定一个句子中的哪个单词对于理解(整个句子的)含义是最重要的, 而在计算机视觉中, attention 可以被用于关注图像上和目标检测、目标识别最相关的特定区域。

注意力机制是如何工作的? 有哪些常见的机制? (How Does It Work and What Are the Common Mechanisms?)

在这里插入图片描述

There are several common types of attention mechanisms used in machine learning, particularly in natural language processing tasks. These include soft attention, hard attention, self attention, global attention, and local attention. Let’s dive into each.

在机器学习中,尤其是自然语言处理任务中,有几种常见的注意力机制。具体包括: 软注意力机制(soft attention), 硬注意力机制(hard attention), 自注意力机制(self-attention), 全局注意力机制(global-attention), 局部注意力机制(local-attention)。

Soft Attention

Soft attention assigns continuous weights to input elements, allowing the model to attend to multiple elements simultaneously. Soft attention mechanisms are differentiable, which makes them easier to optimize using gradient-based techniques. These mechanisms have been widely adopted in various deep learning applications, such as neural machine translation, where the model learns to weigh the importance of each word in the input sentence when generating the target translation. Soft attention computes a weighted sum of input elements, where the weights are typically obtained by applying a softmax function to the attention scores.

软注意力机制,是给输入元素赋予了 连续的权重, 使模型能够同时关注(attend to)多个元素。 soft-attention 是可微分的, 这使得它们更容易使用基于梯度的技术进行优化。 这些机制在许多深度学习应用中广泛使用, 比如 NMT(neural machine translation, 神经机器翻译)里在生成目标翻译文本时, 模型学习给输入句子中的每个单词的重要性打分。 soft attention 计算输入元素的加权和, 其中权重通常是 attention 得分的 softmax 结果。

Hard Attention

Hard attention is an attention mechanism that selects a subset of input elements to focus on, while ignoring the rest. It works by learning to make discrete decisions on which input elements are most relevant for a given task. For instance, in image captioning, hard attention may select specific regions of the image that are most relevant for generating the caption. One of the main challenges with hard attention is that it is non-differentiable, which makes it difficult to optimize using gradient-based methods like backpropagation. To address this, researchers often resort to techniques like reinforcement learning or the use of surrogate gradients.

硬注意力(hard attention)是说, 选择输入元素中的一个子集, 没有选中的则被丢弃。它通过学习对于给定任务哪些输入元素最相关来做出离散决策。比如说在 image caption 任务中, hard attention 可以选择图像中和生成标题最相关的图像区域。 hard attention 的主要挑战之一是, 它是不可微的, 这使得使用像反向传播这样的梯度方法进行优化变得困难。 为了解决这个问题, 研究人员经常会采用强化学习 (reinforce-learning) 或者使用替代梯度 (surrogate gradients) 的技术。

Self-Attention

Self attention, also known as intra-attention, is an attention mechanism where the model attends to different parts of the input sequence itself, rather than attending to another sequence or a different modality. This mechanism surged in popularity through its use in the transformer architecture, which shows remarkable performance across various natural language processing tasks. In self attention, the model learns to relate different positions of the input sequence to compute a representation of the sequence. This allows the model to capture long-range dependencies and contextual information, leading to improved performance in tasks such as language modeling, machine translation, and text summarization.

自注意力 (self-attention), 也被叫做 “内部注意力” (intra-attention), 是说模型关注(attends)的是输入序列本身的不同部分, 而不是关注另一个序列或不同的模态。 这种机制在 transformer 架构中的应用中使它变得非常受欢迎, 在各种自然语言处理任务中表现出色。 在 self attention 中, 模型学习把输入序列中不同的位置之间建立联系, 从而计算出序列的一个表示。 这使得模型能够捕获长范围的依赖和上下文信息, 从而改善了语言模型、 机器翻译、 文本总结等任务的性能。

这段人工翻译的不够润,贴一段 ChatGPT4 的翻译:

自注意力,也称为内部注意力,是一种注意机制,模型关注的是输入序列本身的不同部分,而不是关注另一个序列或不同的模态。这种机制在transformer架构中的使用使其变得非常流行,该架构在各种自然语言处理任务中表现出色。在自我注意力中,模型学会了将输入序列的不同位置联系起来计算序列的表示。这使得模型能够捕捉长距离依赖性和上下文信息,从而提高了语言建模、机器翻译和文本摘要等任务的性能。

Global-Attention

Global attention mechanisms focus on all elements of the input sequence when computing attention weights. In this approach, the model computes a context vector by taking a weighted sum of all input elements, where the weights are determined by the attention scores. Global attention is widely used in sequence-to-sequence models, such as those used for machine translation, where the model must attend to all words in the input sentence to generate a coherent translation. One of the benefits of global attention is that it allows the model to capture long-range dependencies and global context, which can be crucial for understanding and generating complex structures.

全局注意力机制 (global attention) 是说, 在计算 attention 权重的时候, 关注输入序列的所有元素。 在这种方法中, 模型通过对所有输入元素进行加权求和来计算上下文向量, 其中权重由 attention 分数确定。 global attention 在 序列到序列 (sequence-to-sequence) 模型任务中广泛使用, 比如在机器翻译中使用的模型里, 模型必须关注输入序列中的所有单词来生成一个连贯 (coherent) 的翻译。 global attention 的一个好处是, 他可以帮助模型捕捉长距离的依赖关系 和 全局背景, 这对于理解和生成复杂结构至关重要。

Local Attention

Local attention mechanisms focus on a smaller, localized region of the input sequence when computing attention weights. Instead of attending to all input elements, the model learns to select a specific region or window around the current position, and computes attention weights only within that region. This can be advantageous in situations where the relevant information is located in a specific, local context, such as when processing time series data or text with strong locality properties. Local attention can also be computationally more efficient than global attention, as it reduces the number of attention computations required, making it suitable for applications with large input sequences or limited computational resources.

局部注意力机制 (local attention) 在计算 attention 权重时, 关注输入序列中较小的、 局部的区域。 local attention 模型不关注所有的输入, 而是学会选择当前位置周围的特定区域或窗口, 并仅在该区域内计算注意力权重。 这在模型情况下很有优势, 特别是当相关信息位于特定的本地环境中, 比如处理时间序列数据 或具有强烈局部性质的文本时。

local attention 在计算上也远比 global attention 更高效, 因为它减少(reduce)了所需的注意力计算次数, 适用于具有大量输入序列 或 有限计算资源的应用。

有什么好处? (What are the Benefits?)

Attention mechanisms have demonstrated a multitude of benefits in the realm of machine learning. One of the most notable advantages is the improved performance they bring to tasks such as natural language processing, image recognition, and speech recognition. By allowing the model to selectively focus on important elements, attention mechanisms enhance the accuracy and effectiveness of these tasks.

attention 机制在机器学习领域展示了许多好处 (multitude of benefits)。 其中最明显的优势之一是它们为自然语言处理、 图像识别, 以及语音识别等任务带来的性能提升。 通过允许模型有选择的关注重要元素, attention 机制提高了这些任务的准确性和有效性。

Moreover, attention helps overcome the limitations of fixed-length encoding, enabling models to work with longer input sequences and maintain relevant information. This ability is crucial when dealing with complex patterns or large amounts of data. Additionally, attention mechanisms often produce interpretable results, as the attention weights provide insights into which parts of the input the model considers important. This interpretability facilitates a deeper understanding of the model’s decision-making process, fostering trust and improving debugging efforts.

此外, attention 有助于克服固定长度编码 (fixed-length encoding) 的限制, 使模型能够处理更长的输入序列并保持相关信息。在处理复杂模式或大量数据时, 这种能力至关重要。 此外, attention 机制通常会产生可解释的结果, 因为注意力权重能够揭示模型认为输入的哪些部分是重要的。 这种可解释性有助于更深入的理解模型的决策过程, 增进信任并提升调试工作的效果。

Finally, attention mechanisms, particularly local attention, can improve computational efficiency by reducing the scope of the context that the model needs to consider. This reduction allows for faster processing times without sacrificing the quality of the output. Overall, attention mechanisms have contributed significantly to advancements in machine learning, leading to more robust, efficient, and interpretable models.

最后, 特别是 local attention 机制, 可以通过减少模型需要考虑的上下文范围来提高计算效率。 这种减少可以让处理速度更快, 而不会牺牲输出质量。 总的来说, attention 机制在机器学习的发展中发挥了重要作用, 使模型更加稳健、 高效和易于解释。

什么时间、在哪里使用? (When and Where To Use)

Attention mechanisms have proven valuable in a wide range of applications, but knowing when to use them is essential for optimizing their benefits. Here are some scenarios where incorporating attention mechanisms may be particularly advantageous:

注意力机制在各种应用中已经被证明是非常有价值的, 但关键是要知道何时使用它们以优化它们的好处。 以下是适合 attention 机制会带来明显好处的场景:

序列到序列的任务(Sequence-to-sequence tasks):

Attention is especially useful in sequence-to-sequence tasks, such as machine translation, speech recognition, and summarization. In these cases, attention allows the model to selectively focus on relevant parts of the input sequence, leading to better context-aware outputs.

attention 在序列到序列任务中尤其有用, 比如机器翻译, 语音识别, 摘要/总结。 在这些场景中, attention 使得模型选择性的关注输入序列中的相关部分, 提供更好的上下文相关的输出结果。

处理长序列(Handling long sequences):

When working with long input sequences or complex patterns, attention mechanisms can help overcome the limitations of traditional fixed-length encoding. By enabling the model to focus on specific elements, attention mechanisms can maintain critical information and improve overall performance.

当处理较长的输入序列, 或者复杂的输入模式时, attention 机制可以帮助克服传统的固定长度编码的问题。 通过使模型聚焦在特定元素上, attention 机制能够保持重要的信息, 并且提升整体的性能。

需要上下文感知来处理的任务(Tasks requiring context-aware processing):

Attention is beneficial in tasks that demand context-aware processing, such as natural language processing or image captioning. By weighing the importance of different elements within the input sequence, attention mechanisms allow models to make more informed decisions based on the broader context.

attention 对于需要上下文感知的计算是有好处的, 比如自然语言处理, image caption (图像描述)。通过对输入序列的不同部分赋予不同的重要性权重, attention 机制使得模型在广泛的上下文环境中做出更好的决策。

可解释性(Interpretability and explainability)

When it is essential to understand the decision-making process of a model, attention mechanisms can provide valuable insights. By highlighting which parts of the input the model focuses on, attention weights can offer a clearer understanding of how the model arrives at its conclusions.

当理解一个模型的决策过程是很重要的时候, attention 机制提供了有价值的权重。 通过高亮模型对于输入的哪部分着重处理, attention 权重可以提供模型是如何生成它的结论的清晰理解。

计算效率(Computational efficiency)

In cases where reducing computational requirements is a priority, local attention mechanisms can be employed. By focusing on a smaller, fixed-size window around the current element, local attention reduces the computational burden while still providing context-aware processing.

在减少计算开销的优先级比较高的情况下, local attention 机制可以派上用场。 通过关注一个小的、 当前元素周围固定长度的窗口, local attention 减少了计算开销, 同时保持了上下文感知的计算。

While attention mechanisms can be beneficial in these scenarios, it is essential to consider the specific requirements and constraints of the task at hand. Attention mechanisms can add complexity to a model, and in some cases, simpler architectures may suffice. Therefore, it is crucial to carefully evaluate the trade-offs between model complexity, computational demands, and the benefits of attention before incorporating these mechanisms into your machine learning models.

尽管 attention 机制在这些场景中很有用, 仔细思考手头上任务的特定需求和限制仍然是很重要的。 attention 机制会增加模型的复杂性, 并且在有些场景下, 更简单的架构就足够了。 因此, 在模型的复杂性、 计算开销、 使用 attention 带来的收益之间做出权衡是很重要的。

总结

Attention mechanisms have proved to be a valuable addition to the field of machine learning, enhancing model performance and interpretability in various tasks. By understanding the different types of attention and their benefits, researchers and practitioners can continue to push the boundaries of what machine learning models can achieve.

attention 机制被证明是机器学习领域里一个有价值的新鲜事物, 在多个任务上提升了模型的性能和可解释性。 通过理解不同类型的 attention 和它们的优点, 研究人员和参与这门可以继续推进机器学习模型能力的边界。

个人总结

  1. 论文 Attention Is All You Need 首次提出了 attension machanisms 注意力机制
  2. attention 机制解决了 fixed-length encoding 的限制
  3. 英文单词 attend 意思是 “关注”, attention 是它的名词形式
  4. attention 的几种类型:
    • attention 是说对于输入的每个元素, 学习一个权重
    • soft attention: 权重可微
    • hard attention: 忽略掉一部分输入. 权重不可微。
    • self attention: 对整个输入做分解, 对于分解出来的部分之间, 建立关注
    • global attention: 所有输入都处理, 不过仍然要各自学习出不同的权重
    • local attention: 只处理输入中的小部分的权重的学习
  5. attention 的好处
    • 能处理长范围的依赖
    • 能克服固定长度编码的限制
    • 能减少计算量
    • 给预测/生成的结果, 提供了可解释性
    • 以上的特点, 使得能够处理大数据
  6. 没有提到 generative。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1428572.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LISN究竟是什么?有什么作用?

在电子领域中,LISN是一个充满神秘感的工具,常常被用于电磁兼容性测试。本文将深入探讨LISN的本质是什么,以及它在电子领域中扮演的关键角色。 1. 认识LISN LISN,全称为Line Impedance Stabilization Network,即线路阻…

Jetpack Compose系列(1)-初识Jetpck

Jetpack Compose是什么 2019年的I/O大会上,Google宣布Kotlin成为Android开发首选语言(这次不是第一次说了),且后续会有新的Jetpack API和功能将在Kotlin中提供,并同时开源Jetpack Compose。 简介 Jetpack是一套库、…

骨传导耳机的原理是什么?骨传导耳机是如何传输声音的

随着生活质量的日益提升,可穿戴智能设备的种类和款式也愈发丰富多彩。特别是耳机产业的快速演变,不仅在类型上多样化,其外观设计也呈现出各式各样的趋势。然而,对于运动爱好者来说,真正合适的运动耳机却并不多见。在众…

Acwing---802.区间和

区间和 1.题目2.基本思想3.代码实现 1.题目 假定有一个无限长的数轴,数轴上每个坐标上的数都是 0。 现在,我们首先进行 n 次操作,每次操作将某一位置 x 上的数加 c。 接下来,进行 m次询问,每个询问包含两个整数 l 和…

Pycharm安装插件

经常用Pycharm写代码的话,时不时的就会接触到一些好用的插件,如何安装插件呢?经常使用的是两种方式,分别是在线安装和离线安装。 在线安装 在线安装比较简单,打开Pycharm,在左上角【文件】->【设置】-…

深度学习入门笔记(四)函数与优化方法

深度学习有三大部分 模型表征(包括模型设计、网络表示等)模型评估(上一篇文章提到的准确召回和损失函数等)优化算法(模型如何学习或更新)本节我们就来介绍模型是如何学习或更新的。 4.1 损失函数 模型的学习,实际上就是对参数的学习。参数学习的过程需要一系列的约束,…

构建LLM辅助生物威胁制造预警系统 人类越发展获取的超能力越大,破坏力越大,威胁越大。我们需要什么样的预警系统?既克服威胁又具有超能力 安全基础

https://openai.com/research/building-an-early-warning-system-for-llm-aided-biological-threat-creation 人类越发展获取的超能力越大,破坏力就越大,威胁越大。 人工智能就是为了赋予人人都能有超能力,而一旦被恶意或无意使用又威胁到人…

如何在Shopee平台上进行手机类目选品?

在Shopee平台上进行手机类目的选品是一个关键而复杂的任务。卖家需要经过一系列的策略和步骤,以确保选品的成功和销售业绩的提升。下面将介绍一些有效的策略,帮助卖家在Shopee平台上进行手机类目选品。 先给大家推荐一款shopee知虾数据运营工具知虾免费…

tqdm双进度条训练模型

效果图 from tqdm import tqdm import time pbar1 tqdm(total5, position0, desc"Epoch", leaveFalse, colourgreen, ncols100,unit"epoch") for i in range(5):pbar2 tqdm(total10, position1, desc"Iteration", leaveFalse, colourred, ncol…

宠物商业数据分析

一、宠物热销品类分布 欧睿国际更有一份数据表明,宠物食品在所有“它经济”中占比是最大,仅仅是猫狗食品就达到了59.1%,增速也远高于其他宠物用品。 2018年,中国就已经有超7700万独居成年人口,国内养宠主力军中90后占…

C语言——标准输出函数(printf、putchar和puts)

目录 1. 标准输入输函数出头文件2. printf2.1 函数申明2.2 基本用法2.3 占位符2.4 输出格式2.4.1 限定宽度2.4.2 总是显示正负号2.4.3 限定小数位数2.4.4 输出部分字符串 3. putchar3.1 函数申明3.2 基本用法 4. puts4.1 函数申明4.2 基本用法 1. 标准输入输函数出头文件 #inc…

应急响应事件处理过程参考手册

D盾介绍及下载 下载地址:http://www.d99net.net/ 使用步骤与杀毒软件类似,下面简要说一下 D盾使用步骤 更新规则库 点击检测更新,自动更新到最新的规则库 更新后在底部会显示当前规则库版本: 选择网站根目录,进行w…

Linux下grep命令详解

grep #文件内容过滤显示 #在指定的普通文件中查找并显示含有指定字符串的行,也可与管道符一起使用格式: grep-参数 查找条件 文件名 参数: 示例: [rootnode1 ~]# grep -n "root" /etc/passwd # -n&a…

OpenCV 14 - 自定义线性滤波

1 卷积 1-1概念 卷积是图像处理中一个操作,kernel在图像的每个像素上的操作。 Kernel本质上一个固定大小的矩阵数组,其中心点称为锚点 1-2 卷积如何工作 把kernel放到像素数组之上,求锚点周围覆盖的像素乘积之和(包括锚点),用来替换锚点覆盖下像素点值称为卷积处理。 …

【Java网络编程04】网络原理进阶(二)

1. 前言 在网络原理进阶(一)部分我们详细介绍了UDP/TCP两大协议及其相关特性,本章我们会讨论网络层、数据链路层、物理层相关协议。但是需要注意的是,如果有小伙伴们未来是想成为Java后端开发工程师的,那么未来工作中…

【机器学习】贝叶斯垃圾邮件识别

实验三:贝叶斯垃圾邮件识别 本次作业以垃圾邮件分类任务为基础,要求提取文本特征并使用朴素贝叶斯算法进行垃圾邮件识别(调用已有工具包或自行实现)。 1 任务介绍 ​ 电子邮件是互联网的一项重要服务,在大家的学习、…

Linux/Uinx 系统编程:进程管理(3)

Linux/Uinx 系统编程:进程管理(3) 本章来讲解进程管理的最后一部分内容。 文章目录 Linux/Uinx 系统编程:进程管理(3)I/O重定向原理FILE结构体的内部结构重定向的实现过程 scanf 与 printfscanfprintf 重定…

LDRA Testbed软件静态分析_Jenkins持续集成_(2)配置邮件自动发送静态分析结果

系列文章目录 LDRA Testbed软件静态分析_操作指南 LDRA Testbed软件静态分析_自动提取静态分析数据生成文档 LDRA Testbed软件静态分析_Jenkins持续集成_(1)自动进行静态分析的环境搭建 LDRA Testbed软件静态分析_Jenkins持续集成_(2)配置邮件自动发送静态分析结果 LDRA Testb…

10 排序的概念

目录 1.排序的概念和运用 2.排序的分类 1. 排序的概念及运用 1.1 排序的概念 排序: 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作 稳定性:假定在待排序的记录序列中,存在…

微调实操一: 增量预训练(Pretraining)

1、前言 《微调入门篇:大模型微调的理论学习》我们对大模型微调理论有了基本了解,这篇结合我们现实中常见的场景,进行大模型微调实操部分的了解和学习,之前我有写过类似的文章《实践篇:大模型微调增量预训练实践(二)》利用的MedicalGPT的源码在colab进行操作, 由于MedicalGPT代…