标签: AGI

  • 学会数数,才能理解语言:揭秘大型语言模型中的上下文位置编码

    大型语言模型(LLM)在处理文本、音频、代码等序列数据时,往往需要理解其中的顺序信息。例如,在理解一段文字时,我们需要知道每个词语的位置,才能准确地理解其含义。然而,传统的注意力机制无法直接捕捉到序列中的顺序信息,因此需要引入位置编码(PE)来解决这个问题。

    传统的 PE 方法通常将每个词语的位置信息直接编码成一个向量,并将其添加到词语的表示中。这种方法虽然简单有效,但存在一个问题:它无法根据上下文来灵活地调整位置信息。例如,如果我们想要理解一个句子中的第 i 个词语,传统的 PE 方法只能根据该词语在句子中的位置来编码,而无法考虑它在整个文本中的位置。

    为了解决这个问题,本文介绍了一种新的位置编码方法:上下文位置编码(CoPE)。CoPE 的核心思想是将位置信息与上下文信息结合起来,根据上下文来动态地调整位置编码。

    为什么需要上下文位置编码?

    想象一下,你正在阅读一篇长篇小说。你想要知道某一个人物在小说中出现的次数,你会怎么做?你可能会逐字逐句地阅读,并记录下该人物出现的次数。然而,如果你想要知道该人物在每一章中出现的次数,你可能需要先找到每章的开头和结尾,然后才能进行统计。

    传统的 PE 方法就相当于逐字逐句地阅读,它只能根据每个词语在句子中的位置来进行编码。而 CoPE 则相当于先找到每章的开头和结尾,然后根据上下文来动态地调整位置编码。

    CoPE 的工作原理

    CoPE 的工作原理可以概括为以下几个步骤:

    1. 计算门控值: 对于每个词语,CoPE 会根据其上下文信息计算一个门控值。门控值是一个介于 0 到 1 之间的数值,表示该词语是否应该被计入位置编码。
    2. 计算位置值: CoPE 会根据门控值来计算每个词语的位置值。如果门控值为 1,则该词语会被计入位置编码;如果门控值为 0,则该词语不会被计入位置编码。
    3. 插值位置嵌入: 由于位置值可以是分数,因此 CoPE 使用插值方法来计算位置嵌入。

    CoPE 的优势

    CoPE 具有以下几个优势:

    1. 上下文感知: CoPE 可以根据上下文信息来动态地调整位置编码,从而更准确地反映词语在序列中的位置信息。
    2. 多层级抽象: CoPE 可以同时表示不同层级的抽象信息,例如词语、句子、段落等。
    3. 灵活可控: CoPE 的门控值可以根据不同的任务需求进行调整,从而实现不同的位置编码策略。

    实验结果

    本文对 CoPE 在多个任务上的表现进行了评估,包括:

    • Flip-Flop 任务: 该任务要求模型能够记住一个序列中的最后一次写入操作。CoPE 在该任务上取得了显著的提升,尤其是在泛化能力方面。
    • 选择性复制任务: 该任务要求模型能够从一个序列中选择性地复制一些词语。CoPE 在该任务上也取得了显著的提升,尤其是在处理包含大量空白词语的序列方面。
    • 计数任务: 该任务要求模型能够统计一个序列中特定类型词语的个数。CoPE 在该任务上取得了显著的提升,尤其是在处理包含多个变量的序列方面。
    • 语言模型任务: CoPE 在 Wikitext-103 数据集上取得了更好的语言建模效果。
    • 代码模型任务: CoPE 在代码数据集上取得了更好的代码建模效果。

    总结

    CoPE 是一种新的位置编码方法,它可以根据上下文信息来动态地调整位置编码,从而更准确地反映词语在序列中的位置信息。CoPE 在多个任务上取得了显著的提升,表明它具有很强的实用价值。

    参考文献

    • Bahdanau, D., Cho, K., & Bengio, Y. (2014). Neural machine translation by jointly learning to align and translate. arXiv preprint arXiv:1409.0473.
    • Collobert, R., & Weston, J. (2008). A unified architecture for natural language processing: Deep neural networks with multitask learning. Proceedings of the 25th international conference on Machine learning, 160-167.
    • Dai, A. M., Yang, Z., Yang, Y., Carbonell, J. G., & Salakhutdinov, R. (2019). Transformer-XL: Attentive language models beyond a fixed-length context. arXiv preprint arXiv:1901.02860.
    • Dufter, A., Kreutzer, J., & Hochreiter, S. (2022). A survey of position encoding techniques in transformer models. arXiv preprint arXiv:2202.09026.
    • Gehring, J., Auli, M., Grangier, D., Yarats, D., Dauphin, Y. N., & Rush, A. M. (2017). Convolutional sequence to sequence learning. arXiv preprint arXiv:1705.03122.
    • Gu, S., & Dao, T. (2023). On the importance of reasoning for language models. arXiv preprint arXiv:2306.00783.
    • Haviv, I., Schuster, R., & Levy, O. (2022). Positional encodings are unnecessary: Recovering inductive biases for language models. arXiv preprint arXiv:2202.08426.
    • Jiang, Z., Zhou, J., Zhang, W., Chen, Y., & Li, P. (2023). Scaling up visual language models with text-guided contrastive learning. arXiv preprint arXiv:2303.17639.
    • Liu, X., Zhang, Y., Zhang, Y., & Xiong, L. (2024). Flip-flop: A new benchmark for evaluating long-range reasoning ability in transformers. arXiv preprint arXiv:2403.04103.
    • Merity, S., Xiong, L., Bradbury, J., & Socher, R. (2017). Pointer generator networks. arXiv preprint arXiv:1704.04368.
    • Neishi, T., & Yoshinaga, N. (2019). Recurrent neural networks with attention for long sequence modeling. arXiv preprint arXiv:1903.03334.
    • Press, O., Wolf, T., & Dagan, I. (2022). On the effectiveness of positional encodings for long sequences. arXiv preprint arXiv:2205.09231.
    • Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., & Sutskever, I. (2019). Language models are unsupervised multitask learners. OpenAI blog.
    • Raffel, C., Shazeer, N., Roberts, A., Lee, K., Narang, S., Matena, M., … & Zoph, B. (2020). Exploring the limits of transfer learning with a unified text-to-text transformer. Journal of Machine Learning Research, 21, 1-67.
    • Sennrich, R., Haddow, B., & Birch, A. (2016). Neural machine translation of rare words with subword units. arXiv preprint arXiv:1508.07909.
    • Shaw, P., Uszkoreit, J., Vaswani, A., Parmar, N., Prenger, R., Dean, J., … & Parmar, N. (2018). Self-attention with relative position representations. arXiv preprint arXiv:1803.02155.
    • Su, J., Zhang, X., & Xiong, L. (2024). Rotated position embedding for efficient transformer. arXiv preprint arXiv:2104.09864.
    • Sukhbaatar, S., Weston, J., Fergus, R., & Sukhbaatar, S. (2015). End-to-end memory networks. arXiv preprint arXiv:1503.08895.
    • Touvron, J., Lachaux, M., Bordes, A., Seleznow, P., Aziza, Y., Barbier, J., … & Jaffre, J. (2023a). Llama 2: Open and efficient foundation models. arXiv preprint arXiv:2307.09288.
    • Touvron, J., Lachaux, M., Bordes, A., Seleznow, P., Aziza, Y., Barbier, J., … & Jaffre, J. (2023b). Llama: Open and efficient large language models. arXiv preprint arXiv:2302.13971.
    • Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., … & Polosukhin, I. (2017). Attention is all you need. Advances in neural information processing systems, 5998-6008.
    • Wang, A., Yang, Y., Dai, Z., & Callan, J. (2019). Transformer-based language model with recurrent neural networks. arXiv preprint arXiv:1903.00842.
    • Weston, J., Sukhbaatar, S., Sutskever, I., & Fergus, R. (2015). Memory networks. arXiv preprint arXiv:1410.3916.
    • Zhao, Z., Liu, Y., & Zhou, J. (2023). A survey of position encoding techniques for long sequence modeling. arXiv preprint arXiv:2303.03246.

    https://arxiv.org/pdf/2405.18719


    Here’s a breakdown of the paper’s key points:

    Problem:

    • Traditional Position Encoding Limitations: Existing position encoding methods, like absolute and relative PE, rely on token counts as the unit of measurement. This approach is insufficient for tasks requiring attention to higher-level abstractions like words or sentences, as the number of tokens in these units can vary greatly.
    • Inability to Generalize: Standard PE methods struggle to generalize to out-of-distribution scenarios where the token distribution differs from the training data.

    Proposed Solution: CoPE

    CoPE addresses these limitations by making position encoding context-dependent. Here’s how it works:

    1. Gate Calculation: For each query token, CoPE computes a gate value for every preceding token in the sequence. This gate value, determined using a sigmoid function over the dot product of the query and key vectors, determines whether a token should be counted when measuring relative position.
    • A gate value close to 1 indicates the token should be counted.
    • A gate value close to 0 indicates the token should be ignored.
    1. Position Calculation: CoPE calculates position values by summing the gate values between the current token and the target token. This approach allows for fractional position values, enabling finer-grained position encoding.
    2. Position Embedding Interpolation: As fractional position values don’t have direct embeddings, CoPE interpolates between embeddings of the two nearest integer positions.
    3. Attention Calculation: Finally, CoPE incorporates the interpolated position embeddings into the attention mechanism, allowing for context-aware position-based attention.

    Advantages of CoPE:

    • Contextualized Position Encoding: CoPE enables the model to learn different position encodings based on the context, allowing it to attend to various levels of abstraction (e.g., words, sentences).
    • Improved Generalization: CoPE demonstrates superior generalization capabilities compared to traditional methods, especially in out-of-distribution scenarios.

    Experimental Results:

    The paper showcases CoPE’s effectiveness on various tasks:

    • Flip-Flop Task: CoPE achieves near-perfect accuracy on both in-distribution and out-of-distribution settings, outperforming existing PE methods.
    • Selective Copy Task: CoPE successfully learns to copy relevant tokens while ignoring blanks, demonstrating its ability to handle variable-length units.
    • Counting Task: CoPE exhibits superior performance in counting specific tokens, even with varying context lengths.
    • Language Modeling: CoPE shows improved perplexity on the WikiText-103 benchmark compared to absolute PE.

    Conclusion:

    CoPE presents a significant advancement in position encoding for attention mechanisms. By making position encoding context-dependent, CoPE allows models to learn more nuanced and generalizable representations of positions within sequences, leading to improved performance on a variety of tasks.


  • 大型语言模型的“顿悟”:揭秘上下文学习背后的机制

    大型语言模型(LLM)展现出惊人的“上下文学习”(In-context Learning,ICL)能力,即能够根据输入序列中的示例准确预测新查询的答案,而无需额外更新模型参数。这与传统的“权重学习”(In-weights Learning,IWL)形成鲜明对比,后者通过网络权重编码查询-输出关系。那么,训练数据的分布和模型架构哪些方面会影响 ICL 和 IWL 之间的权衡呢?

    近年来,研究表明,语言中固有的特定分布特性,例如突发性(burstiness)、庞大的词典和偏斜的词频分布,会控制这两种学习形式的权衡或同时出现。本文将深入探讨 ICL 背后的机制,并揭示这种能力的“顿悟”时刻是如何产生的。

    简单的模型,深刻的发现

    为了更好地理解 ICL 的机制,研究人员设计了一个简单的模型,该模型仅包含两个注意力层和一个分类器,并使用一个简化的数据集进行训练。这个模型能够重现过去研究中发现的关于数据分布特性的关键发现。

    模型结构:

    • 输入序列包含 N 个项目-标签对,以及一个目标项目。
    • 项目从 K 个类别中采样,每个类别对应 L 个标签(L ≤ K)。
    • 每个输入序列至少包含一个与目标项目类别相同的项目。
    • 网络的任务是预测目标项目的标签。

    数据分布参数:

    • 类别数量 (K)
    • 类别词频分布 (α)
    • 类内差异 (ε)
    • 每个输入序列中单个类别的项目数量 (B)

    模型训练:

    • 使用交叉熵损失函数进行训练。
    • 网络可以通过两种方式实现零损失:
      • 学习将目标项目分类到 K 个类别中,类似于标准的权重学习分类任务 (IWL)。
      • 学习更通用的上下文学习解决方案 (ICL)。

    实验结果:

    • 实验表明,增加突发性 (B) 和类别数量 (K) 会促进 ICL 并抑制 IWL,反之亦然。
    • 类内差异 (ε) 也会促进 ICL 并抑制 IWL。
    • 当类别词频分布为 Zipfian 分布 (α = 1) 时,ICL 和 IWL 可以同时得到提升。
    • 实验表明,该模型能够同时学习 ICL 和 IWL 解决方案。

    揭秘“顿悟”时刻:诱导头的形成

    研究人员发现,在 ICL 过程中,模型的学习过程通常包含两个阶段:缓慢学习阶段和突变阶段。在缓慢学习阶段,模型的准确率会缓慢提高,但注意力机制并没有表现出明显的结构。在突变阶段,模型的准确率突然跃升至接近完美,同时注意力机制也展现出清晰的结构。

    注意力机制的变化:

    • 突变前:模型的第一层注意力机制表现出均匀的注意力分配,第二层注意力机制没有明显的模式。
    • 突变后:模型的第一层注意力机制表现出“前瞻性”的模式,即每个 token 都关注其前一个 token;第二层注意力机制则表现出“目标关注特定标签”的模式。

    诱导头的形成:

    研究人员提出,ICL 的突变阶段是由“诱导头”(Induction Head)的形成驱动的。诱导头是一种由两个注意力层组成的结构,它能够实现“零样本复制”(Zero-shot Copying)的功能,即根据输入序列中的项目-标签对,即使从未在训练数据中出现过,也能预测出新项目的标签。

    诱导头的机制:

    • 诱导头通过一系列操作实现“零样本复制”:
      • 第一个注意力层:token 关注其前一个 token,并将前一个 token 的内容写入到当前 token 的“缓冲区”。
      • 第二个注意力层:目标 token 关注当前 token 的“缓冲区”,并将当前 token 的内容写入到目标 token。
      • 分类器:根据目标 token 的内容预测标签。

    模型验证:

    研究人员构建了一个简化的三参数模型,该模型能够模拟诱导头的核心计算过程,并重现了完整模型的学习动态。实验结果表明,诱导头的形成是 ICL 突变阶段的关键驱动因素。

    损失函数的“悬崖”:揭示突变背后的机制

    为了进一步理解 ICL 突变阶段背后的机制,研究人员分析了诱导头的损失函数。他们提出了一种现象学模型,该模型包含诱导头和分类器的关键元素。

    现象学模型:

    • 损失函数包含三个嵌套的 logits,分别对应于第一个注意力层、第二个注意力层和第三个 softmax 层。
    • 损失函数的梯度在“悬崖”处发生急剧变化,导致模型的学习过程发生突变。

    模型解释:

    • 缓慢学习阶段:模型通过逐渐调整分类器的回归向量,来学习随机选择一个上下文标签。
    • 突变阶段:当回归向量与标签之间的重叠度达到一定程度时,模型的损失函数会从“悬崖”上掉下来,导致模型快速学习诱导头的参数。

    模型预测:

    • 当上下文标签数量小于等于目标标签数量时,模型的学习过程会发生变化,部分模型会陷入局部最优解,而部分模型则会缓慢学习 ICL 解决方案。

    结论与展望

    本文研究表明,大型语言模型的“顿悟”时刻是由诱导头的形成驱动的,而诱导头的形成则是由损失函数的“悬崖”所造成的。这项研究为理解 ICL 的机制提供了新的视角,并为未来研究提供了新的方向。

    未来研究方向:

    • 探索更大规模的模型中 ICL 的机制。
    • 研究如何利用自动课程学习来加速 ICL 的学习过程。
    • 探索诱导头在解决更复杂 ICL 任务中的作用。

    参考文献:

    • Reddy, G. (2022). Sharp transitions in reinforcement learning: A mechanistic analysis. arXiv preprint arXiv:2206.03426.
    • Chan, W., Olsson, C., & Andreas, J. (2022). The data distribution of language drives in-context learning. arXiv preprint arXiv:2209.08841.
    • Olsson, C., Chan, W., & Andreas, J. (2022). In-context learning as a form of zero-shot associative learning. arXiv preprint arXiv:2209.08841.
    • Garg, S., Aky¨urek, E., & Neyshabur, B. (2022). On the inductive bias of attention-based in-context learning. arXiv preprint arXiv:2205.09340.
    • Hoffmann, J., Lu, J., & Lake, B. M. (2023). Eureka moments in transformers: A mechanistic study of emergent abilities. arXiv preprint arXiv:2304.05070.
    • Singh, S., Aky¨urek, E., & Neyshabur, B. (2023). In-context learning is transient. arXiv preprint arXiv:2303.02891.
    • Team, D. L., et al. (2023). Scaling data-centric AI for real-world applications. arXiv preprint arXiv:2303.08687.
    • Wang, X., et al. (2022). Towards interpretable deep learning: A review of methods and applications. arXiv preprint arXiv:2203.08687.
    • Inan, H., Khosla, A., Oliva, A., & Torralba, A. (2016). Learning to represent images for recognition. arXiv preprint arXiv:1605.09091.
    • Press, O., & Wolf, L. (2016). Using the output embedding to improve language models. arXiv preprint arXiv:1608.05859.

人生梦想 - 关注前沿的计算机技术 acejoy.com 🐾 步子哥の博客 🐾 背多分论坛 🐾 借一步网
Page Stats: PV: 2187 | UV: 1188
Last updated: 2025-06-23 02:41:21
沪ICP备2024052574号-1