Label Words are Anchors: An Information Flow Perspective for Understanding In-Context Learning
Label Words are Anchors: An Information Flow Perspective for Understanding In-Context Learning
标签词即锚点:从信息流视角理解上下文学习
Lean Wang Email: lean@pku.edu.cn Lei Li † Email: daidamai@pku.edu.cn Damai Dai † Email: xusun@pku.edu.cn Deli Chen § Hao Zhou § , Fandong Meng § , Jie Zhou § , Xu Sun † † National Key Laboratory for Multimedia Information Processing,School of Computer Science, Peking University § Pattern Recognition Center, WeChat AI, Tencent Inc., China Email: nlp.lilei@gmail.com Email: victorchen@deepseek.com Email: tuxzhou@tencent.com Email: fandongmeng@tencent.com Email: withtomzhou@tencent.com
Abstract
摘要
In-context learning (ICL) emerges as a promising capability of large language models (LLMs) by providing them with demonstration examples to perform diverse tasks. However, the underlying mechanism of how LLMs learn from the provided context remains under-explored. In this paper, we investigate the working mechanism of ICL through an information flow lens. Our findings reveal that label words in the demonstration examples function as anchors: (1) semantic information aggregates into label word representations during the shallow computation layers’ processing; (2) the consolidated information in label words serves as a reference for LLMs’ final predictions. Based on these insights, we introduce an anchor re-weighting method to improve ICL performance, a demonstration compression technique to expedite inference, and an analysis framework for diagnosing ICL errors in GPT2-XL. The promising applications of our findings again validate the uncovered ICL working mechanism and pave the way for future studies.[^1]
上下文学习(ICL)通过向大语言模型(LLM)提供演示示例以完成多样任务,成为一种有前景的能力。然而,LLM 如何从所给上下文中学习,其底层机制仍探索不足。本文从信息流视角考察 ICL 的工作机制。我们发现演示示例中的标签词充当锚点:(1)在浅层计算层处理过程中,语义信息聚合到标签词表示;(2)标签词中汇聚的信息成为 LLM 最终预测的参照。基于这些洞见,我们引入锚点重加权方法以提升 ICL 表现、演示压缩技术以加速推理,以及用于诊断 GPT2-XL 上 ICL 错误的分析框架。这些应用的积极结果再次验证了我们所揭示的 ICL 工作机制,并为后续研究铺路。[^1]
1 Introduction
1 引言
In-context Learning (ICL) has emerged as a powerful capability alongside the development of scaled-up large language models (LLMs) (Brown et al., 2020). By instructing LLMs using few-shot demonstration examples, ICL enables them to perform a wide range of tasks, such as text classification (Min et al., 2022a) and mathematical reasoning (Wei et al., 2022). Since ICL does not require updates to millions or trillions of model parameters and relies on human-understandable natural language instructions (Dong et al., 2023), it has become a promising approach for harnessing the full potentiality of LLMs. Despite its significance, the inner working mechanism of ICL remains an open question, garnering considerable interest from research communities (Xie et al., 2022; Dai et al., 2022; Akyürek et al., 2022; Li et al., 2023b).
随着规模化大语言模型(LLM)的发展,上下文学习(ICL)成为一种强大能力(Brown et al., 2020)。通过用少样本演示示例引导 LLM,ICL 使它们能完成广泛任务,例如文本分类(Min et al., 2022a)与数学推理(Wei et al., 2022)。由于 ICL 无需更新数百万乃至数万亿模型参数,并依赖人类可理解的自然语言指令(Dong et al., 2023),它已成为发挥 LLM 全部潜力的有前景途径。尽管意义重大,ICL 的内在工作机制仍是开放问题,并引起研究社区广泛兴趣(Xie et al., 2022; Dai et al., 2022; Akyürek et al., 2022; Li et al., 2023b)。
Figure 1: Visualization of the information flow in a GPT model performing ICL. The line depth reflects the significance of the information flow from the right word to the left. The flows involving label words are highlighted. Label words gather information from demonstrations in shallow layers, which is then extracted in deep layers for final prediction.
图 1:在执行 ICL 的 GPT 模型中可视化信息流。线条深浅反映从右侧词到左侧词的信息流重要性。涉及标签词的流被高亮。标签词在浅层从演示中收集信息,再在深层被提取用于最终预测。
图 2:我们假设的示意。在浅层,标签词从演示中收集信息,形成供更深处理的语义表示;在深层,模型从标签词提取并利用该信息以形成最终预测。
In this paper, we find that the label words serve as anchors that aggregate and distribute information in ICL. We first visualize the attention interactive pattern between tokens with a GPT model (Brown et al., 2020) on sentiment analysis (Figure 1). Initial observations suggest that label words aggregate information in shallow layers and distribute it in deep layers.[^2] To draw a clearer picture of this phenomenon, we design two metrics based on saliency scores to portray the information flow in ICL and further propose the following hypothesis:
本文发现,标签词在 ICL 中充当聚合与分发信息的锚点。我们首先在情感分析上用 GPT 模型(Brown et al., 2020)可视化词元间的注意力交互模式(图 1)。初步观察表明,标签词在浅层聚合信息,在深层分发信息。[^2] 为更清晰刻画这一现象,我们基于显著性分数设计两个指标来刻画 ICL 中的信息流,并进一步提出如下假设:
Two experiments are designed to validate the hypothesis using GPT2-XL (Radford et al., 2019) and GPT-J (Wang and Komatsuzaki, 2021) across several text classification benchmarks. (1) By blocking the information aggregation path to label words in certain layers, we find that such isolation in shallow layers significantly impairs model performance. This indicates that label words collect useful information during forward propagation in shallow layers. (2) We investigate the relationship between the attention distributions on the label words of the target position and the model’s final prediction. Our results illustrate a strong positive correlation, where a candidate label’s probability increases with more attention weight on its corresponding label token. In summary, these experimental findings suggest that our hypothesis holds well with large language models on real-world datasets.
我们设计两项实验,在若干文本分类基准上用 GPT2-XL(Radford et al., 2019)与 GPT-J(Wang and Komatsuzaki, 2021)验证该假设。(1)通过在特定层阻断到标签词的信息聚合路径,我们发现浅层中的此类隔离显著损害模型表现。这表明标签词在浅层前向传播中收集有用信息。(2)我们考察目标位置上对标签词的注意力分布与模型最终预测之间的关系。结果表明存在强正相关:候选标签的概率随其对应标签词元上注意力权重增大而升高。总之,这些实验发现表明,我们的假设与大语言模型在真实数据集上的行为相符。
Drawing on insights from the information flow perspective, we explore three approaches to enhance ICL’s effectiveness, efficiency, and interpretability. (1) An anchor re-weighting method is introduced, which employs a learnable vector to adjust the significance of different label words in demonstrations, leading to a 16.7% average accuracy boost compared to standard ICL baselines. (2) For quicker ICL inference, inputs are compressed into pre-calculated anchor representations since model predictions primarily rely on label word activations. Testing shows a 1.8 speedup in inference with only a minimal performance trade-off. (3) An error analysis of ICL on GPT2-XL demonstrates that the label confusion matrix aligns closely with the distance distribution of anchor key vectors, implying that errors might result from similar anchor representations. These promising applications further validate our hypothesis and shed light on future ICL studies for better transparency of LLMs.
从信息流视角出发,我们探索三条路径以增强 ICL 的有效性、效率与可解释性。(1)引入锚点重加权方法:用可学习向量调节演示中不同标签词的重要性,相对标准 ICL 基线平均准确率提升 16.7%。(2)为加速 ICL 推理,由于模型预测主要依赖标签词激活,可将输入压缩为预先计算的锚点表示。测试显示推理加速约 1.8 倍,性能损失很小。(3)对 GPT2-XL 上 ICL 的错误分析表明,标签混淆矩阵与锚点键向量的距离分布高度一致,暗示错误可能源于相似的锚点表示。这些有前景的应用进一步验证了我们的假设,并为未来更透明地理解 LLM 的 ICL 研究提供启发。
2 Label Words are Anchors
2 标签词即锚点
This section confirms the intuitive findings using two saliency score-based metrics as discussed in § 2.1. The quantitative results lead to a proposed hypothesis for the ICL working mechanism: : In shallow layers, label words aggregate information from demonstration examples to form semantic representations for later computations. : In deep layers, the model makes predictions by extracting information from label words. The validation for these hypotheses is presented in § 2.2 and § 2.3, respectively.
本节用第 2.1 节讨论的两个基于显著性分数的指标确认直观发现。定量结果引出对 ICL 工作机制的假设::在浅层,标签词从演示示例聚合信息,形成供后续计算的语义表示。:在深层,模型通过从标签词提取信息做出预测。对这些假设的验证分别见第 2.2 与 2.3 节。
2.1 Hypothesis Motivated by Saliency Scores
2.1 由显著性分数驱动的假设
This section aims to discover the inherent patterns in the attention interaction between tokens for a GPT model. The saliency technique Simonyan et al. (2013), a common interpretation tool, is employed for highlighting critical token interactions. Following common practice, we use the Taylor expansion Michel et al. (2019) to calculate the saliency score for each element of the attention matrix:
本节旨在发现 GPT 模型中词元间注意力交互的固有模式。显著性技术(Simonyan et al., 2013)作为常用解释工具,被用来高亮关键词元交互。遵循常见做法,我们用泰勒展开(Michel et al., 2019)计算注意力矩阵每个元素的显著性分数:
Here, is the value of the attention matrix of the -th attention head in the -th layer, is the input, and is the loss function of the task, e.g., the cross-entropy objective for a classification problem. We average all attention heads to obtain the saliency matrix for the -th layer.[^3] represents the significance of the information flow from the -th word to the -th word for ICL. By observing , we can get an intuitive impression that as the layer goes deeper, demonstration label words will become more dominant for the prediction, as depicted in Figure 1.
此处, 是第 层第 个注意力头的注意力矩阵取值, 是输入, 是任务损失函数,例如分类问题的交叉熵目标。我们对所有注意力头取平均,得到第 层的显著性矩阵 。[^3] 表示对 ICL 而言从第 个词到第 个词的信息流重要性。通过观察 ,可直观感到:层越深,演示标签词对预测越占主导,如图 1 所示。
To draw a clearer picture of this phenomenon, we propose three quantitative metrics based on . Our focus lies in three components: (i) the label words, such as “Negative” and “Positive” in Figure 2, denoted as , where represents the total number of label words;[^4] (ii) the target position, where the model generates prediction labels (i.e., the final token in the input), which we denote as ; and (iii) the text part, i.e., the tokens before label words in the demonstration.
为更清晰刻画这一现象,我们基于 提出三个定量指标。关注点落在三个组件:(i)标签词,例如图 2 中的「Negative」与「Positive」,记为 ,其中 为标签词总数;[^4](ii)目标位置,即模型生成预测标签之处(输入的最后一个词元),记为 ;(iii)文本部分,即演示中标签词之前的词元。
The definitions of the three quantitative metrics follow below.
三个定量指标的定义如下。
, the mean significance of information flow from the text part to label words:
,从文本部分到标签词的信息流平均重要性:
, the mean significance of information flow from label words to the target position:
,从标签词到目标位置的信息流平均重要性:
, the mean significance of the information flow amongst all words, excluding influences represented by and :
,除 与 所代表影响之外、所有词之间信息流的平均重要性:
, , and help assess different information flows in the model. indicates the intensity of information aggregation onto label words. A high demonstrates a strong information extraction from label words for final decision-making. assesses average information flow among words, serving as a benchmark to gauge the intensity of the patterns identified by and .
、 与 有助于评估模型中不同信息流。 指示信息向标签词聚合的强度。高 表明为最终决策从标签词提取信息的强度大。 评估词之间的平均信息流,作为衡量 与 所识别模式强度的基准。
Experimental Settings
We choose GPT2-XL from the GPT series Radford et al. (2019) as our primary model for investigation, due to its moderate model size (of 1.5B parameters) that is suitable for our hardware resource and its decent ICL performance (Dai et al., 2022). For datasets, we use Stanford Sentiment Treebank Binary (SST-2) Socher et al. (2013) for sentiment analysis, Text REtrieval Conference Question Classification (TREC) Li and Roth (2002); Hovy et al. (2001) for question type classification, AG’s news topic classification dataset (AGNews) Zhang et al. (2015) for topic classification, and EmoContext (EmoC) Chatterjee et al. (2019) for emotion classification. Templates for constructing demonstrations are provided in Appendix A. examples are sampled from the test set for evaluation, with one demonstration per class sampled from the training set. Experiments with more demonstrations yield similar outcomes (refer to Appendix F.1 for details). Results reflect averages from five random seeds.
实验设定
我们从 GPT 系列(Radford et al., 2019)中选择 GPT2-XL 作为主要研究对象,因其适中规模(15 亿参数)适合我们的硬件资源,且 ICL 表现不错(Dai et al., 2022)。数据集方面,我们用 Stanford Sentiment Treebank Binary(SST-2)(Socher et al., 2013)做情感分析,Text REtrieval Conference Question Classification(TREC)(Li and Roth, 2002; Hovy et al., 2001)做问题类型分类,AG’s news topic classification dataset(AGNews)(Zhang et al., 2015)做主题分类,以及 EmoContext(EmoC)(Chatterjee et al., 2019)做情绪分类。构造演示的模板见附录 A。从测试集采样 1000 个样本评估,每类从训练集采样一条演示。更多演示的实验得到相似结果(详见附录 F.1)。结果为五个随机种子的平均。
Results and Analysis
(a) Results on the SST-2 dataset
结果与分析
(a) SST-2 数据集上的结果
Figure 3 reveals that: (1) in shallow layers, , the significance of the information flow from label words to targeted positions, is low, while , the information flow from the text part to label words is high; (2) in deep layers, , the importance of information flow from label words to the targeted position becomes the dominant one. Notably, and usually surpass , suggesting that interactions involving label words outweigh others.
图 3 显示:(1)在浅层,(从标签词到目标位置的信息流重要性)较低,而 (从文本部分到标签词的信息流)较高;(2)在深层,(从标签词到目标位置的信息流重要性)成为主导。值得注意的是, 与 通常超过 ,表明涉及标签词的交互强于其他交互。
Proposed Hypothesis
Based on this, we propose the hypothesis that label words function as anchors in the ICL information flow. In shallow layers, label words gather information from demonstration examples to form semantic representations for deeper layers, while in deep layers, the model extracts the information from label words to form the final prediction. Figure 2 gives an illustration for our hypothesis.
所提假设
据此,我们提出假设:标签词在 ICL 信息流中充当锚点。在浅层,标签词从演示示例收集信息,形成供更深层使用的语义表示;在深层,模型从标签词提取信息以形成最终预测。图 2 给出该假设的示意。
2.2 Shallow Layers: Information Aggregation
2.2 浅层:信息聚合
In this part, we validate our hypothesis’ first component. We assume that the information aggregation in ICL relies on the information flow from the text part to label tokens, which is facilitated by the transformer’s attention mechanism. By manipulating the attention layer in the model to block this flow and examining the model behavior change, we validate the existence of the information aggregation process and its contribution to the final prediction.
本部分验证假设的第一部分。我们假定 ICL 中的信息聚合依赖从文本部分到标签词元的信息流,并由 Transformer 的注意力机制促成。通过操控模型注意力层以阻断该流并考察模型行为变化,我们验证信息聚合过程的存在及其对最终预测的贡献。
Experimental Settings
We retain the same test sample size of 1000 inputs as § 2.1. We use the same demonstration for a single random seed. To further validate our findings on larger models, we incorporate GPT-J (6B) (Wang and Komatsuzaki, 2021) in experiments, which exceeds GPT2-XL in model size and capacity.
实验设定
我们保留与第 2.1 节相同的 1000 个输入测试样本量。对单个随机种子使用相同演示。为在更大模型上进一步验证,实验中纳入规模与能力超过 GPT2-XL 的 GPT-J(60 亿)(Wang and Komatsuzaki, 2021)。
Implementation Details
To block the information flow to label words, we isolate label words by manipulating the attention matrix . Specifically, we set to 0 in the attention matrix of the -th layer, where represents label words and represents preceding words. Consequently, in the -th layer, label words cannot access information from the prior demonstration text.
实现细节
为阻断到标签词的信息流,我们通过操控注意力矩阵 隔离标签词。具体地,在第 层注意力矩阵 中把 置为 0,其中 表示标签词, 表示其前序词。因此在第 层,标签词无法访问先前演示文本中的信息。
Metrics
We use the following metrics to assess the impact of blocking information flow from the text part to label tokens: (1) Label Loyalty: measures the consistency of output labels with and without isolation. (2) Word Loyalty: employs the Jaccard similarity to compare the top-5 predicted words with and without isolation, capturing more subtle model output alterations (See Appendix C for details). Low loyalty indicates a profound impact of isolation on model predictions.
指标
我们用以下指标评估阻断从文本部分到标签词元信息流的影响:(1)标签忠诚度: 衡量隔离与否时输出标签的一致性。(2)词忠诚度: 用 Jaccard 相似度比较隔离与否时的 top-5 预测词,以捕捉更细微的模型输出变化(细节见附录 C)。低忠诚度表明隔离对模型预测影响深刻。
图 4:在前 5 层或后 5 层中隔离标签词相对随机隔离非标签词的影响。在前 5 层隔离标签词影响最大,凸显经标签词进行浅层信息聚合的重要性。
Results and Analysis
Figure 4 illustrates a notable influence on the model’s behavior when label words are isolated within the first 5 layers. Yet, this influence becomes inconsequential within the last 5 layers, or when random non-label words are used. This observation underlines the fundamental importance of shallow-layer information aggregation via label words in ICL. It also emphasizes the superiority of label words over non-label words. Further tests with variable numbers of layers reaffirm these findings (Appendix D). Moreover, similar results were obtained when testing ICL with semantically unrelated labels (refer to Appendix F.2).
结果与分析
图 4 表明:在前 5 层隔离标签词对模型行为有显著影响;而在后 5 层隔离,或随机隔离非标签词时,影响变得无关紧要。该观察强调浅层经标签词进行信息聚合在 ICL 中的根本重要性,也强调标签词相对非标签词的优越性。对不同隔离层数的进一步测试再次确认这些发现(附录 D)。此外,在语义无关标签的 ICL 测试中也得到相似结果(见附录 F.2)。
2.3 Deep Layers: Information Extraction
2.3 深层:信息提取
We proceed to validate the latter part of our hypothesis that the model extracts information from label words to form the final prediction. We denote the sum of the attention matrices in the -th layer as .[^5] In deeper layers, we find a strong correlation between the attention distributions on the label words of the target position, represented as , and the model’s final prediction, affirming our hypothesis. The experimental setup mirrors that discussed in § 2.2.
我们继续验证假设的后半部分:模型从标签词提取信息以形成最终预测。记第 层注意力矩阵之和为 。[^5] 在更深层,我们发现目标位置上对标签词的注意力分布 与模型最终预测之间存在强相关,从而确认我们的假设。实验设定与第 2.2 节所述相同。
2.3.1 Experiments
We utilize the AUC-ROC score to quantify the correlation between and model prediction, which we denote as for the -th layer. We prefer the AUC-ROC metric due to two primary reasons: (1) might differ from the probability of the model outputting label by a constant factor. As Kobayashi et al. (2020) points out, attention should be multiplied by the norm of the key vector to yield ’more interpretable attention’. The AUC-ROC metric can implicitly account for these factors, thus allowing us to uncover the correlation more effectively. (2) The proportion of different labels output by the model may be unbalanced. Using the AUC-ROC metric can help mitigate this issue, reducing disturbances caused by class imbalance.
2.3.1 实验
我们用 AUC-ROC 分数量化 与模型预测之间的相关性,记第 层为 。偏好 AUC-ROC 主要有两点原因:(1) 可能与模型输出标签 的概率相差一个常数因子。如 Kobayashi et al.(2020)所指出,注意力应乘以键向量范数才得到「更可解释的注意力」。AUC-ROC 可隐式考虑这些因素,从而更有效揭示相关性。(2)模型输出不同标签的比例可能不平衡。使用 AUC-ROC 有助于缓解该问题,减少类别不平衡带来的干扰。
Considering the residual mechanism of transformers, we can view each layer’s hidden state as the cumulative effect of all prior layer calculations. To quantify the accumulated contribution of the first layers to model prediction, we introduce :
考虑到 Transformer 的残差机制,可将每层隐状态视为所有先前层计算的累积效应。为量化前 层对模型预测的累积贡献,我们引入 :
This measure tracks the positive contribution above a baseline AUC-ROC threshold of 0.5. The value of signifies the proportional contribution of the first layers to the model prediction.
该度量跟踪高于 AUC-ROC 基线阈值 0.5 的正向贡献。 的值表示前 层对模型预测的比例贡献。
2.3.2 Results and Analysis
Figures 5a and 5b delineate correlation metrics for GPT2-XL and GPT-J, averaged across four datasets. The for deep layers approaches , illustrating a strong correlation between the attention distributions on label words of the target position and the model’s final prediction. Moreover, shallow layers show negligible cumulative contributions (), with a significant increase in middle and deep layers. These results signify the crucial role of deep layers for final prediction, validating that the model extracts information from label words in deep layers to form the final prediction.
2.3.2 结果与分析
图 5a 与 5b 给出 GPT2-XL 与 GPT-J 在四个数据集上平均的相关性指标。深层的 接近 ,表明目标位置上对标签词的注意力分布与模型最终预测之间存在强相关。此外,浅层的累积贡献()可忽略,在中层与深层显著增加。这些结果说明深层对最终预测至关重要,验证了模型在深层从标签词提取信息以形成最终预测。
(a) GPT2-XL (total 48 layers).
(a) GPT2-XL(共 48 层)。
2.4 Discussion of Our Hypothesis
2.4 对我们假设的讨论
In § 2.2, we have affirmed that the model’s shallow layers assemble information from demonstrations via label words to form semantic representations. In § 2.3, we verify that the aforementioned aggregated information on label words is then extracted to form the final prediction in the deep layers. Recognizing the crucial function of label words in this process, we have introduced the term “Anchors” to denote them. Given the considerable role these “anchors” fulfill, we find it intuitive to design ICL improvements based on them, as elaborated in § 3.
在第 2.2 节,我们确认模型浅层经标签词从演示组装信息以形成语义表示。在第 2.3 节,我们验证上述聚合在标签词上的信息随后在深层被提取以形成最终预测。认识到标签词在该过程中的关键作用,我们引入「锚点」一词指代它们。鉴于这些「锚点」的重要作用,我们觉得基于它们设计 ICL 改进是直观的,详见第 3 节。
3 Applications of Our Anchor-Based Understanding
3 基于锚点理解的应用
With insights from the validated hypothesis, we propose strategies to boost ICL’s accuracy and inference speed. We propose an anchor re-weighting method in § 3.1 to adjust the demonstrations’ contributions and improve accuracy. In § 3.2, we explore a context compression technique that reduces original demonstrations to anchor hidden states to speed up ICL inference. Besides, in § 3.3, we utilize anchor distances to perform an analysis to understand the errors ICL made in real-world scenarios. These approaches corroborate our hypothesis, pointing to potential paths for future ICL enhancements.
借由已验证假设的洞见,我们提出提升 ICL 准确率与推理速度的策略。第 3.1 节提出锚点重加权方法,以调节演示贡献并提升准确率。第 3.2 节探索把原始演示缩减为锚点隐状态以加速 ICL 推理的上下文压缩技术。此外,第 3.3 节利用锚点距离做分析,以理解 ICL 在真实场景中的错误。这些方法印证我们的假设,并指向未来 ICL 增强的潜在路径。
| Method | SST-2 | TREC | AGNews | EmoC | Average |
|---|---|---|---|---|---|
| Vanilla In-Context Learning ( 1-shot per class ) | |||||
| Vanilla In-Context Learning ( 5-shot per class ) | |||||
| Anchor Re-weighting (1-shot per class) | 90.07 | 60.92 | 81.94 | 41.64 | 68.64 |
Table 1: The effect after adding parameter . For AGNews, due to the length limit, we only use three demonstrations per class. Our Anchor Re-weighting method achieves the best performance overall tasks.
| Method | SST-2 | TREC | AGNews | EmoC | Average |
|---|---|---|---|---|---|
| Vanilla In-Context Learning ( 1-shot per class ) | |||||
| Vanilla In-Context Learning ( 5-shot per class ) | |||||
| Anchor Re-weighting (1-shot per class) | 90.07 | 60.92 | 81.94 | 41.64 | 68.64 |
表 1:加入参数 后的效果。对 AGNews,因长度限制每类仅用三条演示。我们的锚点重加权方法在全部任务上整体表现最佳。
3.1 Anchor Re-weighting
3.1 锚点重加权
Based on our analysis in § 2, we draw parallels between ICL and logistic regression and propose an approach to improve ICL’s accuracy by re-weighting label anchors.
基于第 2 节的分析,我们在 ICL 与逻辑回归之间建立类比,并提出通过重加权标签锚点来提升 ICL 准确率的方法。
3.1.1 Method
§ 2.3 illustrates a strong correlation between the model’s output category and the attention distribution on label words of the target position in deep layers. We can view the attention module as a classifier ,
3.1.1 方法
第 2.3 节表明,模型输出类别与深层中目标位置 上对标签词 的注意力分布 之间存在强相关。可将注意力模块视为分类器 ,
By setting and , we deduce:
令 且 ,可推出:
This approximates a logistic regression model where:
这近似一个逻辑回归模型,其中:
In this equation, and are parameters that can be learned, while is the input feature.
在该式中, 与 是可学习参数,而 是输入特征。
Inspired by the similarity between ICL and logistic regression, we’ve incorporated a learnable into Eq. (7), which is equivalent to adjusting the attention weights :
受 ICL 与逻辑回归相似性启发,我们把可学习的 并入式 (7),这等价于调节注意力权重 :
Each is a learnable parameter, set uniquely for different attention heads and layers. Refer to Appendix G for more details.
每个 是可学习参数,对不同注意力头与层分别设置。更多细节见附录 G。
To train the re-weighting vector , we utilize an auxiliary training set . Here, we perform ICL with normal demonstrations and optimize with respect to the classification loss on :
为训练重加权向量 ,我们使用辅助训练集 。在此用正常演示执行 ICL,并相对 上的分类损失 优化 :
This approach can be metaphorically described as "re-weighting the anchors," leading us to term it as Anchor Re-weighting. It can also be viewed as a modification of the demonstration contributions since demonstration information has been incorporated into the anchors as suggested by our prior analysis in § 2.2. Additionally, it can be interpreted as a unique adapter variant, introducing minimal parameters while preserving most of the original model. However, it is specifically designed based on our anchor hypothesis and requires fewer parameters than traditional adapters.
该方法可形象地描述为「重加权锚点」,因此我们称之为锚点重加权。按第 2.2 节先前分析,演示信息已并入锚点,故它也可视为对演示贡献的修改。此外,它可被解释为一种独特的适配器变体:引入极少参数同时保留大部分原模型。不过它专为我们的锚点假设设计,所需参数少于传统适配器。
3.1.2 Experiments
We choose one sample per class as normal demonstrations and choose four extra samples per class to form the auxiliary training set . The setup follows § 2.2, with results averaged over five random seeds. Owing to computational constraints, we employ GPT2-XL for evaluation, excluding GPT-J. The parameters are trained using gradient descent. More details can be found in Appendix H.
3.1.2 实验
我们每类选一个样本作为正常演示,再每类另选四个样本构成辅助训练集 。设定遵循第 2.2 节,结果对五个随机种子取平均。因计算限制,我们用 GPT2-XL 评估,排除 GPT-J。参数 用梯度下降训练。更多细节见附录 H。
We compare Anchoring Re-weighting with two baselines: (1) Vanilla ICL with the same demonstration (1-shot per class) (2) Vanilla ICL, where the auxiliary training set of is included as demonstrations (5-shot per class) for a fair comparison.
我们将锚点重加权与两个基线比较:(1)使用相同演示(每类 1-shot)的普通 ICL;(2)为公平比较,把 的辅助训练集也作为演示纳入的普通 ICL(每类 5-shot)。
3.1.3 Results
As Table 1 shows, the proposed anchor re-weighting significantly enhances ICL performance, particularly on the SST-2 and EmoC datasets. Besides, adding more demonstrations for vanilla ICL may not bring a stable accuracy boost due to the potential noise introduced, as discussed in Zhao et al. (2021). Different from vanilla ICL which utilizes the extra examples to form a demonstration, we train a re-weighting vector to modulate label anchor contributions. This shortens the input context and thus brings (almost) no extra cost to the inference speed. The consistent improvements of our method suggest that the re-weighting mechanism could be a better alternative to utilize demonstration examples. Furthermore, it reiterates the crucial role that anchors play in ICL.
3.1.3 结果
如表 1 所示,所提锚点重加权显著提升 ICL 表现,尤其在 SST-2 与 EmoC 数据集上。此外,为普通 ICL 增加更多演示未必带来稳定准确率提升,因为可能引入噪声,如 Zhao et al.(2021)所讨论。与把额外示例当作演示的普通 ICL 不同,我们训练重加权向量 来调节标签锚点贡献。这缩短输入上下文,从而几乎不给推理速度带来额外代价。我们方法的一致改进表明,重加权机制可能是利用演示示例的更好替代方案。此外,它再次强调锚点在 ICL 中的关键作用。
3.2 Anchor-Only Context Compression
3.2 仅锚点的上下文压缩
We further explore a context compression technique that reduces the full demonstration to anchor hidden states for accelerating ICL inference.
我们进一步探索一种上下文压缩技术:把完整演示缩减为锚点隐状态,以加速 ICL 推理。
3.2.1 Method
In § 2.3, we find that the model output heavily relies on the label words, which collect information from the demonstrations. Given the auto-regressive nature of GPT-like models, where hidden states of tokens depend solely on preceding ones, label words’ information aggregation process is independent of subsequent words. This allows for the calculation and caching of the label word hidden states ( is the -th layer’s hidden state of the -th label word in the demonstration). By concatenating at the front in each layer during inference, instead of using the full demonstration, we can speed up inference.
3.2.1 方法
在第 2.3 节,我们发现模型输出严重依赖从演示收集信息的标签词。鉴于类 GPT 模型的自回归性质——词元隐状态仅依赖其前序词元——标签词的信息聚合过程独立于后续词。这使得可以计算并缓存标签词隐状态 ( 是演示中第 个标签词在第 层的隐状态)。推理时在每层前端拼接 ,而不使用完整演示,即可加速推理。
In our preliminary experiments, concatenating hidden states of label words alone was inadequate for completing the ICL task.[^6] This might be due to the critical role of formatting information in helping the model to determine the output space at the target position,[^7] as highlighted in Min et al. (2022b). As a solution, we amalgamate the hidden states of both the formatting and the label words, a method we’ve termed Hidden.
在初步实验中,仅拼接标签词隐状态不足以完成 ICL 任务。[^6] 这可能是因为格式信息在帮助模型确定目标位置输出空间上起关键作用,[^7] 如 Min et al.(2022b)所强调。作为解决方案,我们合并格式与标签词的隐状态,称之为 Hidden。
3.2.2 Experiments
We follow the same experimental settings as § 2.2. We compare our Hidden input compression method with two equally efficient baselines.
3.2.2 实验
我们遵循与第 2.2 节相同的实验设定。将 Hidden 输入压缩方法与两个同等高效的基线比较。
Text: This method concatenates the formatting and label text with the input, as opposed to concatenating the hidden states at each layer.
Text:该方法把格式与标签文本同输入拼接,而不是在每层拼接隐状态。
Hidden: This approach concatenates the hidden states of formatting and randomly selected non-label words (equal in number to Hidden).
Hidden:该方法拼接格式与随机选取的非标签词的隐状态(数量与 Hidden 相等)。
Hidden: To establish a stronger baseline, we randomly select 20 sets of non-label words in Hidden and report the one with the highest label loyalty.
Hidden:为建立更强基线,我们在 Hidden 中随机选取 20 组非标签词,并报告标签忠诚度最高的一组。
The Text method is included to demonstrate that the effectiveness of Hidden is attributed to the aggregation of information in label words, rather than the mere text of label words. If we find that Hidden surpasses Text in performance, it solidifies the notion that the aggregated information within label words carries significant importance. The Hidden method is introduced to illustrate that anchor hidden states encapsulate most of the demonstration information among all hidden states.
纳入 Text 是为了表明 Hidden 的有效性归因于标签词中的信息聚合,而非标签词的纯文本。若发现 Hidden 表现超过 Text,即可巩固标签词内聚合信息具有重要意义的观点。引入 Hidden 是为了说明:在所有隐状态中,锚点隐状态封装了大部分演示信息。
We assess all compression methods using the label loyalty and word loyalty introduced in § 2.2, in addition to classification accuracy.
我们用第 2.2 节引入的标签忠诚度与词忠诚度,以及分类准确率,评估所有压缩方法。
3.2.3 Results
| Method | Label Loyalty | Word Loyalty | Acc. |
|---|---|---|---|
| ICL (GPT2-XL) | |||
| Text | |||
| Hidden | |||
| Hidden | |||
| Hidden | |||
| ICL (GPT-J) | |||
| Text | |||
| Hidden | |||
| Hidden | |||
| Hidden |
Table 2: Results of different compression methods on GPT2-XL and GPT-J (averaged over SST-2, TREC, AGNews, and EmoC). Acc. denotes accuracy. The best results are shown in bold. Our method achieves the best compression performance.
3.2.3 结果
| Method | Label Loyalty | Word Loyalty | Acc. |
|---|---|---|---|
| ICL (GPT2-XL) | |||
| Text | |||
| Hidden | |||
| Hidden | |||
| Hidden | |||
| ICL (GPT-J) | |||
| Text | |||
| Hidden | |||
| Hidden | |||
| Hidden |
表 2:GPT2-XL 与 GPT-J 上不同压缩方法的结果(在 SST-2、TREC、AGNews 与 EmoC 上平均)。Acc. 表示准确率。最佳结果加粗。我们的方法取得最佳压缩表现。
We can see from Table 2 that the proposed compression method Hidden achieves the best results among all three compression methods on all metrics and for both models. For example, with the GPT-J model, the compression method with anchor states only leads to a accuracy drop compared to the uncompressed situation, indicating that the compression introduces negligible information loss. Further, we estimate the efficiency improvements over the original ICL. As shown in Table 3, the speed-up ratio ranges from to , as the efficiency gain is influenced by the length of the demonstrations. We refer readers to Appendix I for a more elaborated analysis of the speed-up ratios. Besides, we observe that the acceleration effect is more pronounced in the GPT-J model compared to GPT2-XL, demonstrating its great potential to apply to larger language models.
从表 2 可见,所提压缩方法 Hidden 在全部三个压缩方法、全部指标以及两个模型上都取得最佳结果。例如,对 GPT-J 模型,仅用锚点状态的压缩方法相对未压缩情形仅掉 1.5 个准确率点,表明压缩引入的信息损失可忽略。进一步,我们估计相对原始 ICL 的效率提升。如表 3 所示,加速比从 到 ,因效率增益受演示长度影响。更细致的加速比分析见附录 I。此外,我们观察到加速效果在 GPT-J 上比 GPT2-XL 更明显,显示其应用于更大语言模型的巨大潜力。
| Model | SST-2 | TREC | AGNews | EmoC |
|---|---|---|---|---|
| GPT2-XL | ||||
| GPT-J |
Table 3: Acceleration ratios of the Hidden method.
| Model | SST-2 | TREC | AGNews | EmoC |
|---|---|---|---|---|
| GPT2-XL | ||||
| GPT-J |
表 3:Hidden 方法的加速比。
3.3 Anchor Distances for Error Diagnosis
3.3 用锚点距离做错误诊断
Lastly, we perform an error analysis for ICL by examining the distances between the key vectors in the attention module that correspond to the label words.
最后,我们通过考察注意力模块中对应标签词的键向量之间的距离,对 ICL 做错误分析。
3.3.1 Method
Our previous analysis in § 2.3 shows a strong correlation between the model output and , which is determined by as per Eq. 7. Should the key vectors for label words and be similar, and will also likely be similar, leading to potential label confusion. Furthermore, considering the distribution of query vectors , we employ a PCA-like method to extract the components of the key vectors along the directions with significant variations in , denoted as (see Appendix J for details). We anticipate that the distances between these s can correspond to the category confusion of the model, thus revealing one possible origin of ICL errors. Here, we normalize the distances to a scale of 0-1, with 0 indicating the highest degree of category confusion:
3.3.1 方法
第 2.3 节先前分析表明,模型输出与 强相关,而按式 7, 由 决定。若标签词 与 的键向量 相似,则 与 也很可能相似,从而导致潜在的标签混淆。进一步考虑查询向量 的分布,我们用类似 PCA 的方法提取键向量沿 显著变化方向上的分量,记为 (细节见附录 J)。我们预期这些 之间的距离可对应模型的类别混淆,从而揭示 ICL 错误的一个可能来源。此处把距离归一化到 0–1 尺度,0 表示类别混淆程度最高:
3.3.2 Experiments
We utilize the GPT2-XL model and TREC dataset, as the model displays varying confusion levels between categories on this dataset. We use all 500 samples of the TREC test set and use 1 demonstration per class for convenience of analysis.
3.3.2 实验
我们使用 GPT2-XL 模型与 TREC 数据集,因为该模型在此数据集上各类别间混淆程度不同。我们使用 TREC 测试集全部 500 个样本,并为便于分析每类使用 1 条演示。
We calculate the actual model confusion score, , between category and category using the AUC-ROC metric (detailed in Appendix K). We then compare the predicted confusion score, , and the actual confusion score, , via heatmaps.
我们用 AUC-ROC 指标计算类别 与类别 之间的实际模型混淆分数 (细节见附录 K)。随后通过热力图比较预测混淆分数 与实际混淆分数 。
3.3.3 Results
(a) Confusion matrix of .
3.3.3 结果
(a) 的混淆矩阵。
Figure 6 shows that the proposed approximation metric, , can identify the most confusing case (Description-Entity) and performs reasonably well for highly confusing categories (Entity-Abbreviation, Description-Abbreviation). This high correlation indicates that ICL makes errors in categories with similar label anchors. Overall, this result demonstrates that our anchor-based analysis framework could serve as an interpretation tool for better understanding ICL’s errors.
图 6 显示,所提近似指标 能识别最易混淆的情形(Description-Entity),并对高度混淆类别(Entity-Abbreviation、Description-Abbreviation)表现合理。这种高相关表明 ICL 会在标签锚点相似的类别上犯错。总体而言,该结果表明我们基于锚点的分析框架可作为解释工具,以更好理解 ICL 的错误。
4 Related Work
4 相关工作
The existing literature on in-context learning analysis can be broadly divided into two streams, each focusing on different aspects. The first stream explores the influencing factors of ICL based on input perturbation, such as the order (Min et al., 2022b), the formatting (Yoo et al., 2022; Wei et al., 2022), and the selection of the demonstration (Liu et al., 2022). Designing proper demonstration construction strategies (Ye et al., 2023; Li et al., 2023a) and calibration techniques (Zhao et al., 2021; Min et al., 2022a) could bring clear boosts to the ICL performance. The second stream investigates the inner working mechanism of ICL through different conceptual lenses, such as making an analogy of ICL to gradient descent (von Oswald et al., 2022; Dai et al., 2022) and viewing the process of ICL as a Bayesian inference (Xie et al., 2022).
关于上下文学习分析的既有文献大体可分为两支,各聚焦不同方面。第一支基于输入扰动探索 ICL 的影响因素,例如顺序(Min et al., 2022b)、格式(Yoo et al., 2022; Wei et al., 2022)以及演示选择(Liu et al., 2022)。设计合适的演示构造策略(Ye et al., 2023; Li et al., 2023a)与校准技术(Zhao et al., 2021; Min et al., 2022a)可给 ICL 表现带来明显提升。第二支通过不同概念镜头考察 ICL 的内在工作机制,例如把 ICL 类比为梯度下降(von Oswald et al., 2022; Dai et al., 2022),以及把 ICL 过程视为贝叶斯推断(Xie et al., 2022)。
In this paper, we provide a novel perspective by examining the information flow in language models to gain an understanding of ICL. Our approach offers new insights and demonstrates the potential for leveraging this understanding to improve the effectiveness, efficiency, and interpretability of ICL.
本文通过考察语言模型中的信息流提供新视角,以理解 ICL。我们的方法带来新洞见,并展示利用该理解提升 ICL 有效性、效率与可解释性的潜力。
5 Conclusion
5 结论
In this paper, we propose a hypothesis that label words serve as anchors in in-context learning for aggregating and distributing the task-relevant information flow. Experimental results with attention manipulation and analysis of predictions correlation consolidate the hypothesis holds well in GPT2-XL and GPT-J models. Inspired by the new understanding perspective, we propose three practical applications. First, an anchor re-weighting method is proposed to improve ICL accuracy. Second, we explore a demonstration compression technique to accelerate ICL inference. Lastly, we showcase an analysis framework to diagnose ICL errors on a real-world dataset. These promising applications again verify the hypothesis and open up new directions for future investigations on ICL.
本文提出假设:标签词在上下文学习中充当锚点,以聚合与分发任务相关信息流。注意力操控实验与预测相关性分析巩固了该假设在 GPT2-XL 与 GPT-J 模型上成立。受这一新理解视角启发,我们提出三项实际应用。第一,提出锚点重加权方法以提升 ICL 准确率。第二,探索演示压缩技术以加速 ICL 推理。最后,展示在真实数据集上诊断 ICL 错误的分析框架。这些有前景的应用再次验证假设,并为未来 ICL 研究打开新方向。
Limitations
局限
Our study, while providing valuable insights into in-context learning (ICL), has several limitations. Firstly, our research scope was limited to classification tasks and did not delve into the realm of generative tasks. Additionally, our hypothesis was only examined within conventional ICL paradigms, leaving other ICL paradigms such as the chain of thought prompting (CoT) Wei et al. (2022) unexplored. Secondly, due to hardware constraints, we mainly investigated models up to a scale of 6 billion parameters. Further research that replicates our study using larger-scale models would be beneficial in corroborating our findings and refining the hypotheses set forth in our investigation.
我们的研究虽为上下文学习(ICL)提供有价值洞见,仍有若干局限。首先,研究范围限于分类任务,未深入生成任务。此外,假设仅在常规 ICL 范式下考察,思维链提示(CoT)(Wei et al., 2022)等其他 ICL 范式尚未探索。其次,因硬件限制,我们主要考察规模至多 60 亿参数的模型。用更大规模模型复现本研究,将有助于确证发现并完善我们提出的假设。
Acknowledgement
致谢
We thank all reviewers for their thoughtful and insightful suggestions. This work is supported in part by a Tencent Research Grant and National Natural Science Foundation of China (No. 62176002). Xu Sun is the corresponding author.
我们感谢所有审稿人富有见地的建议。本工作部分得到腾讯研究资助与国家自然科学基金(编号 62176002)支持。孙栩为通讯作者。
References
参考文献
Akyürek et al. (2022) Ekin Akyürek, Dale Schuurmans, Jacob Andreas, Tengyu Ma, and Denny Zhou. 2022. What learning algorithm is in-context learning? investigations with linear models . ArXiv preprint , abs/2211.15661.
Brown et al. (2020) Tom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah, Jared Kaplan, Prafulla Dhariwal, Arvind Neelakantan, Pranav Shyam, Girish Sastry, Amanda Askell, Sandhini Agarwal, Ariel Herbert-Voss, Gretchen Krueger, Tom Henighan, Rewon Child, Aditya Ramesh, Daniel M. Ziegler, Jeffrey Wu, Clemens Winter, Christopher Hesse, Mark Chen, Eric Sigler, Mateusz Litwin, Scott Gray, Benjamin Chess, Jack Clark, Christopher Berner, Sam McCandlish, Alec Radford, Ilya Sutskever, and Dario Amodei. 2020. Language models are few-shot learners . In Advances in Neural Information Processing Systems 33: Annual Conference on Neural Information Processing Systems 2020, NeurIPS 2020, December 6-12, 2020, virtual .
Chatterjee et al. (2019) Ankush Chatterjee, Kedhar Nath Narahari, Meghana Joshi, and Puneet Agrawal. 2019. SemEval-2019 task 3: EmoContext contextual emotion detection in text . In Proceedings of the 13th International Workshop on Semantic Evaluation , pages 39–48, Minneapolis, Minnesota, USA. Association for Computational Linguistics.
Dai et al. (2022) Damai Dai, Yutao Sun, Li Dong, Yaru Hao, Zhifang Sui, and Furu Wei. 2022. Why can gpt learn in-context? language models secretly perform gradient descent as meta optimizers . ArXiv preprint , abs/2212.10559.
Dong et al. (2023) Qingxiu Dong, Lei Li, Damai Dai, Ce Zheng, Zhiyong Wu, Baobao Chang, Xu Sun, Jingjing Xu, and Zhifang Sui. 2023. A survey for in-context learning . ArXiv preprint , abs/2301.00234.
Hovy et al. (2001) Eduard Hovy, Laurie Gerber, Ulf Hermjakob, Chin-Yew Lin, and Deepak Ravichandran. 2001. Toward semantics-based answer pinpointing . In Proceedings of the First International Conference on Human Language Technology Research .
Kingma and Ba (2015) Diederik P. Kingma and Jimmy Ba. 2015. Adam: A method for stochastic optimization . In 3rd International Conference on Learning Representations, ICLR 2015, San Diego, CA, USA, May 7-9, 2015, Conference Track Proceedings .
Kobayashi et al. (2020) Goro Kobayashi, Tatsuki Kuribayashi, Sho Yokoi, and Kentaro Inui. 2020. Attention is not only a weight: Analyzing transformers with vector norms . In Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP) , pages 7057–7075, Online. Association for Computational Linguistics.
Li et al. (2023a) Xiaonan Li, Kai Lv, Hang Yan, Tianyang Lin, Wei Zhu, Yuan Ni, Guotong Xie, Xiaoling Wang, and Xipeng Qiu. 2023a. Unified demonstration retriever for in-context learning . ArXiv preprint , abs/2305.04320.
Li and Roth (2002) Xin Li and Dan Roth. 2002. Learning question classifiers . In COLING 2002: The 19th International Conference on Computational Linguistics .
Li et al. (2023b) Yingcong Li, Muhammed Emrullah Ildiz, Dimitris Papailiopoulos, and Samet Oymak. 2023b. Transformers as algorithms: Generalization and stability in in-context learning.
Liu et al. (2022) Jiachang Liu, Dinghan Shen, Yizhe Zhang, Bill Dolan, Lawrence Carin, and Weizhu Chen. 2022. What makes good in-context examples for GPT-3? In Proceedings of Deep Learning Inside Out (DeeLIO 2022): The 3rd Workshop on Knowledge Extraction and Integration for Deep Learning Architectures , pages 100–114, Dublin, Ireland and Online. Association for Computational Linguistics.
Michel et al. (2019) Paul Michel, Omer Levy, and Graham Neubig. 2019. Are sixteen heads really better than one? In Advances in Neural Information Processing Systems 32: Annual Conference on Neural Information Processing Systems 2019, NeurIPS 2019, December 8-14, 2019, Vancouver, BC, Canada , pages 14014–14024.
Min et al. (2022a) Sewon Min, Mike Lewis, Hannaneh Hajishirzi, and Luke Zettlemoyer. 2022a. Noisy channel language model prompting for few-shot text classification . In Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers) , pages 5316–5330, Dublin, Ireland. Association for Computational Linguistics.
Min et al. (2022b) Sewon Min, Xinxi Lyu, Ari Holtzman, Mikel Artetxe, Mike Lewis, Hannaneh Hajishirzi, and Luke Zettlemoyer. 2022b. Rethinking the role of demonstrations: What makes in-context learning work? In Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing , pages 11048–11064, Abu Dhabi, United Arab Emirates. Association for Computational Linguistics.
Radford et al. (2019) Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever, et al. 2019. Language models are unsupervised multitask learners. OpenAI blog , 1(8):9.
Simonyan et al. (2013) Karen Simonyan, Andrea Vedaldi, and Andrew Zisserman. 2013. Deep inside convolutional networks: Visualising image classification models and saliency maps. CoRR , abs/1312.6034.
Socher et al. (2013) Richard Socher, Alex Perelygin, Jean Wu, Jason Chuang, Christopher D. Manning, Andrew Ng, and Christopher Potts. 2013. Recursive deep models for semantic compositionality over a sentiment treebank . In Proceedings of the 2013 Conference on Empirical Methods in Natural Language Processing , pages 1631–1642, Seattle, Washington, USA. Association for Computational Linguistics.
Touvron et al. (2023) Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, and Guillaume Lample. 2023. Llama: Open and efficient foundation language models . ArXiv , abs/2302.13971.
von Oswald et al. (2022) Johannes von Oswald, Eyvind Niklasson, E. Randazzo, João Sacramento, Alexander Mordvintsev, Andrey Zhmoginov, and Max Vladymyrov. 2022. Transformers learn in-context by gradient descent . ArXiv preprint , abs/2212.07677.
Wang and Komatsuzaki (2021) Ben Wang and Aran Komatsuzaki. 2021. GPT-J-6B: A 6 Billion Parameter Autoregressive Language Model. https://github.com/kingoflolz/mesh-transformer-jax .
Wei et al. (2022) Jason Wei, Xuezhi Wang, Dale Schuurmans, Maarten Bosma, Ed Huai hsin Chi, F. Xia, Quoc Le, and Denny Zhou. 2022. Chain of thought prompting elicits reasoning in large language models . ArXiv preprint , abs/2201.11903.
Wei et al. (2023) Jerry W. Wei, Jason Wei, Yi Tay, Dustin Tran, Albert Webson, Yifeng Lu, Xinyun Chen, Hanxiao Liu, Da Huang, Denny Zhou, and Tengyu Ma. 2023. Larger language models do in-context learning differently . ArXiv , abs/2303.03846.
Xie et al. (2022) Sang Michael Xie, Aditi Raghunathan, Percy Liang, and Tengyu Ma. 2022. An explanation of in-context learning as implicit bayesian inference . In The Tenth International Conference on Learning Representations, ICLR 2022, Virtual Event, April 25-29, 2022 . OpenReview.net.
Ye et al. (2023) Jiacheng Ye, Zhiyong Wu, Jiangtao Feng, Tao Yu, and Lingpeng Kong. 2023. Compositional exemplars for in-context learning . ArXiv preprint , abs/2302.05698.
Yoo et al. (2022) Kang Min Yoo, Junyeob Kim, Hyuhng Joon Kim, Hyunsoo Cho, Hwiyeol Jo, Sang-Woo Lee, Sang-goo Lee, and Taeuk Kim. 2022. Ground-truth labels matter: A deeper look into input-label demonstrations . In Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing , pages 2422–2437, Abu Dhabi, United Arab Emirates. Association for Computational Linguistics.
Zhang et al. (2015) Xiang Zhang, Junbo Jake Zhao, and Yann LeCun. 2015. Character-level convolutional networks for text classification . In Advances in Neural Information Processing Systems 28: Annual Conference on Neural Information Processing Systems 2015, December 7-12, 2015, Montreal, Quebec, Canada , pages 649–657.
Zhao et al. (2021) Zihao Zhao, Eric Wallace, Shi Feng, Dan Klein, and Sameer Singh. 2021. Calibrate before use: Improving few-shot performance of language models . In Proceedings of the 38th International Conference on Machine Learning, ICML 2021, 18-24 July 2021, Virtual Event , volume 139 of Proceedings of Machine Learning Research , pages 12697–12706. PMLR.
Appendix
附录
Appendix A Experimental Settings
附录 A 实验设定
For models, we use GPT2-XL (1.5B) Radford et al. (2019) and GPT-J (6B) Wang and Komatsuzaki (2021) in this paper.
模型方面,本文使用 GPT2-XL(15 亿)(Radford et al., 2019)与 GPT-J(60 亿)(Wang and Komatsuzaki, 2021)。
For datasets, we use a sentiment analysis task, Stanford Sentiment Treebank Binary (SST-2) Socher et al. (2013), a question type classification task, Text REtrieval Conference Question Classification (TREC) Li and Roth (2002); Hovy et al. (2001), a topic classification task, AG’s news topic classification dataset (AGNews) Zhang et al. (2015), and an emotion classification task, EmoContext (EmoC) Chatterjee et al. (2019). The ICL templates of these tasks are shown in Table 4.
数据集方面,我们使用情感分析任务 Stanford Sentiment Treebank Binary(SST-2)(Socher et al., 2013)、问题类型分类任务 Text REtrieval Conference Question Classification(TREC)(Li and Roth, 2002; Hovy et al., 2001)、主题分类任务 AG’s news topic classification dataset(AGNews)(Zhang et al., 2015),以及情绪分类任务 EmoContext(EmoC)(Chatterjee et al., 2019)。这些任务的 ICL 模板见表 4。
| Task | Template | Label Words |
|---|---|---|
| SST-2 | Review: | Positive, Negative |
| Sentiment: | ||
| Review: | ||
| Sentiment: | ||
| TREC | Question: | Abbreviation, Entity |
| Answer Type: | Description, Person | |
| Question: | Location, Number | |
| Answer Type: | ||
| AGNews | Article: | World, Sports |
| Answer: | Business, Technology | |
| Article: | ||
| Answer: | ||
| EmoC | Dialogue: | Others, Happy |
| Emotion: | Sad, Angry | |
| Dialogue: | ||
| Emotion: |
Table 4: Demonstration templates and label words. Here represents the demonstration, represents the input to be predicted, and represents the label word corresponding to the demonstration. To save space, we only show one demonstration for each task.
| Task | Template | Label Words |
|---|---|---|
| SST-2 | Review: | Positive, Negative |
| Sentiment: | ||
| Review: | ||
| Sentiment: | ||
| TREC | Question: | Abbreviation, Entity |
| Answer Type: | Description, Person | |
| Question: | Location, Number | |
| Answer Type: | ||
| AGNews | Article: | World, Sports |
| Answer: | Business, Technology | |
| Article: | ||
| Answer: | ||
| EmoC | Dialogue: | Others, Happy |
| Emotion: | Sad, Angry | |
| Dialogue: | ||
| Emotion: |
表 4:演示模板与标签词。此处 表示演示, 表示待预测输入, 表示对应该演示的标签词。为节省篇幅,每个任务仅展示一条演示。
Appendix B Results of , , and on TREC and EmoC
附录 B TREC 与 EmoC 上 、 与 的结果
Figure 7 illustrates the relative sizes of , , and on TREC and EmoC, mirroring results on SST-2 and AGNews. In shallow layers, (the information flow from the text part to label words) is prominent, while (the information flow from label words to targeted positions) is less significant. However, in deeper layers, dominates. Importantly, and generally exceed , indicating that interactions involving label words are predominant.
图 7 展示 TREC 与 EmoC 上 、 与 的相对大小,与 SST-2 和 AGNews 上的结果一致。在浅层,(从文本部分到标签词的信息流)突出,而 (从标签词到目标位置的信息流)较不显著。但在更深层, 占主导。重要的是, 与 通常超过 ,表明涉及标签词的交互占主导。
(a) Results on the TREC dataset
(a) TREC 数据集上的结果
Appendix C Reason for Using Word Loyalty Besides Label Loyalty
附录 C 除标签忠诚度外使用词忠诚度的理由
| Isolation Layer | Output Label | (sorted by probability) |
|---|---|---|
| First 5 layers | World | “\n”, “ The”, “ Google”,“<|endoftext|>”, “ A” |
| No isolation | World | “ World”, “ Technology”, “ Politics”, “ Israel”, “ Human” |
Table 5: Results on a test sample with the label “World” from AGNews.
| Isolation Layer | Output Label | (sorted by probability) |
|---|---|---|
| First 5 layers | World | “\n”, “ The”, “ Google”,“<|endoftext|>”, “ A” |
| No isolation | World | “ World”, “ Technology”, “ Politics”, “ Israel”, “ Human” |
表 5:AGNews 上一个真实标签为「World」的测试样本结果。
Label loyalty alone may not capture changes in the probability distribution of non-label words or the relative ratio of the probability of the label words within the entire vocabulary. Word loyalty helps address this limitation, which is shown in Table 5.
仅标签忠诚度可能无法捕捉非标签词概率分布的变化,或标签词概率在整个词表中的相对比例。词忠诚度有助于弥补这一局限,如表 5 所示。
Appendix D Isolating Different Numbers of Layers
附录 D 隔离不同数量的层
We study the impact of the numbers of isolated layers, as shown in Figures 8a and 8b. It can be found that isolating shallow layers cause a significant impact, isolating deep layers has a negligible impact on the model, even when the number of isolation layers increases. This further illustrates the important role of information aggregation via label words in the shallow layers.
我们研究隔离层数的影响,如图 8a 与 8b 所示。可见隔离浅层会造成显著影响,而隔离深层对模型影响可忽略,即便隔离层数增加亦然。这进一步说明浅层经标签词进行信息聚合的重要作用。
(a) Effect of different numbers of isolated layers on GPT2-XL
(a) 不同隔离层数对 GPT2-XL 的影响
Appendix E Details for the Calculation of
附录 E 的计算细节
Suppose the positions of the label words in the input are (without loss of generality, we suppose corresponds to the th class), the targeted position is , the sum of the attention matrices of all attention heads at the layer is . We postulate that there’s a strong correlation between the attention distributions on the label words of the target position and the model’s final prediction. We use the AUC-ROC score to quantify this correlation. We regard as a classifier’s prediction for the model output label (that is, is equivalent to the probability of model outputting label ), and compute the AUC-ROC value of this prediction relative to the actual model output. We denote this as . For the case with more demonstrations (Appendix F.1), we simply sum up all of the same class.
设输入 中标签词位置为 (不失一般性,设 对应第 类),目标位置为 ,第 层所有注意力头的注意力矩阵之和为 。我们假定目标位置上对标签词的注意力分布 与模型最终预测之间存在强相关。我们用 AUC-ROC 分数量化该相关:把 视为对模型输出标签的分类器预测(即 等价于模型输出标签 的概率),并计算该预测相对实际模型输出的 AUC-ROC 值,记为 。对更多演示的情形(附录 F.1),我们简单把同类所有 相加。
Appendix F Additional Experimental Results
附录 F 额外实验结果
F.1 Results with More Demonstrations
F.1 更多演示下的结果
We implement our experimental analysis utilizing two demonstrations per class, resulting in a total of 4, 12, 8, and 8 demonstrations respectively for SST-2, TREC, AGNews, and EmoC. Our findings, as depicted in Figure 9, Figure 10, and Figure 11, exhibit a high degree of similarity to the results obtained from experiments that employ one demonstration per class.
我们用每类两条演示实施实验分析,对 SST-2、TREC、AGNews 与 EmoC 分别共有 4、12、8 与 8 条演示。如图 9、图 10 与图 11 所示,我们的发现与每类一条演示的实验结果高度相似。
(a) Results on the SST-2 dataset
(a) SST-2 数据集上的结果
(a) Effect of different numbers of isolated layers on GPT2-XL
(a) 不同隔离层数对 GPT2-XL 的影响
(a) GPT2-XL (total 48 layers).
(a) GPT2-XL(共 48 层)。
F.2 Results for In-Context Learning with semantically-unrelated labels
F.2 语义无关标签上下文学习的结果
The applicability of our analytical conclusions to ICL variants, such as the semantically unrelated label ICL Wei et al. (2023), is an intriguing subject. Given that both GPT2-XL and GPT-J-6B perform at levels akin to random guessing in this ICL setting, we chose LLaMA-33B Touvron et al. (2023) and SST-2 for our experiment. We substituted labels with ’A’/’B’, and adhered to a similar experimental setup as in sections § 2.2 and § 2.3. However, we applied eight shots per class to facilitate the model in achieving an accuracy of 83.0% on SST-2. The outcomes align with those derived in § 2.2 and § 2.3. Figure 12 shows the more pronounced impact of isolating labels in the shallow layers compared to their isolation in the deep layers or the isolation of non-label tokens. Figure 13 confirmed that the model leverages information from anchors in the deeper layers to perform classification.
我们的分析结论是否适用于 ICL 变体——例如语义无关标签 ICL(Wei et al., 2023)——是一个有趣问题。由于 GPT2-XL 与 GPT-J-6B 在该 ICL 设定下表现接近随机猜测,我们选择 LLaMA-33B(Touvron et al., 2023)与 SST-2 做实验。我们用「A」/「B」替换标签,并遵循与第 2.2、2.3 节相似的实验设定。不过我们每类使用八条演示,以使模型在 SST-2 上达到 83.0% 准确率。结果与第 2.2、2.3 节所得一致。图 12 显示:在浅层隔离标签相对在深层隔离或隔离非标签词元影响更明显。图 13 确认模型在更深层利用锚点中的信息做分类。
图 12:在前 5 层或后 5 层中隔离标签词相对随机隔离非标签词的影响。在前 5 层隔离标签词影响更明显,凸显经标签词进行浅层信息聚合的重要性。
图 13:LLaMA-33B 在 SST-2 上各层的 AUCROCl 与 。深层仍与模型预测相关性更高,强化了模型从深层锚点提取信息做分类的观点。
Appendix G Implementation of Anchor Re-weighting
附录 G 锚点重加权的实现
In order to implement anchor re-weighting, specific adjustments are made in the model’s computational process. After calculating the attention matrix of the th head in the th layer, we multiply each by before proceeding with further computations. This means that for each attention head, we introduce the following modifications:
为实现锚点重加权,对模型计算过程做特定调整。在计算第 层第 个头的注意力矩阵 后,我们在继续后续计算前把每个 乘以 。这意味着对每个注意力头引入如下修改:
$$\displaystyle\operatorname{Attention}{l}^{h}(Q,K,V)=\hat{A}{l}^{h}V, \ \displaystyle A_{l}^{h}=\operatorname{softmax}\left(\frac{QK^{T}}{\sqrt{d}}\right), \ \displaystyle\hat{A}{l}^{h}(k,j)=\begin{cases}\exp(\beta^{i}{0,lh})A_{l}^{h}(k,j),&\text{if}\ k=q,j=p_{i}\
A_{l}^{h}(k,j),&\text{otherwise}\end{cases}.$$
Appendix H Training Settings of Anchor Re-weighting
附录 H 锚点重加权的训练设定
For each random seed, we fix the demonstration and sample test samples from the test datasets as described in § 2.2. The optimization of parameter vector is carried out using gradient descent, specifically with the Adam optimizer Kingma and Ba (2015). The learning rate is set at , with and . Due to memory constraints, we use a batch size of 1. This optimization process is repeated for epochs. Owing to limitations in computational resources, we restrict our evaluation to the GPT2-XL model and exclude the GPT-J model from our assessment.
对每个随机种子,我们固定演示,并按第 2.2 节所述从测试数据集采样 1000 个测试样本。参数向量 用梯度下降优化,具体使用 Adam 优化器(Kingma and Ba, 2015)。学习率设为 ,,。因显存限制,批量大小为 1。该优化过程重复 个 epoch。因计算资源限制,我们仅评估 GPT2-XL 模型,不评估 GPT-J。
Appendix I The Factor of and
附录 I 与 的因素
| SST-2 | TREC | AGNews | EmoC | |
|---|---|---|---|---|
| GPT2-XL | ||||
| GPT-J | ||||
Table 6: Acceleration ratios, and .
| SST-2 | TREC | AGNews | EmoC | |
|---|---|---|---|---|
| GPT2-XL | ||||
| GPT-J | ||||
表 6:加速比、 与 。
From Table 6, we observe a correlation between the acceleration ratios and the ratio of the total demonstration length () to the length of the text predicted (). It suggests that a greater ratio of total length to predicted text length may yield a higher acceleration ratio.
从表 6 我们观察到加速比与总演示长度()相对待预测文本长度()之比相关。这表明总长度相对预测文本长度之比越大,可能得到更高加速比。
In addition, the table illustrates that datasets with longer demonstration lengths tend to exhibit higher acceleration ratios. For instance, the AGNews dataset, which has the longest , presents the highest acceleration ratio among the datasets analyzed. These findings could indicate an increased efficiency of the Hidden method in contexts involving longer demonstration lengths.
此外,该表说明演示长度更长的数据集往往呈现更高加速比。例如 最长的 AGNews 数据集在所分析数据集中加速比最高。这些发现可能表明:在涉及更长演示的情境中,Hidden 方法效率更高。
Appendix J Calculation of
附录 J 的计算
For the sampled sequence to be predicted, we denote the query vectors of the target positions as . We then compute the matrix by subtracting the mean vector, , from each query vector. Subsequently, we determine the directions, , that correspond to the M largest variation directions for the centralized query vectors . The direction, , is chosen to maximize the variance of the projection of the centralized query vectors onto it, while also being orthogonal to the previously chosen directions, . This process can be formalized as follows:
对待预测的采样序列 ,记目标位置的查询向量为 。随后通过从每个查询向量减去均值向量 ,计算矩阵 。接着确定对应中心化查询向量 上 个最大变化方向的方向 。第 个方向 被选为:在正交于先前已选方向 的约束下,最大化中心化查询向量在其上投影的方差。该过程可形式化为:
We define as the square root of the variance of the projection of onto the direction, i.e., .
我们定义 为 在第 个方向上投影方差的平方根,即 。
To derive features s, we project the key vector onto the directions and scale the projections by the corresponding standard deviations . Each feature, , is thus calculated as .
为得到特征 ,我们把键向量 投影到方向 上,并用对应标准差 缩放投影。每个特征 因此计算为 。
We further examine the influence of on the prediction confusion matrix, , as depicted in Figure 14. Given the similarity in outcomes for various , we settle on a value of for computation of .
我们进一步考察 对预测混淆矩阵 的影响,如图 14 所示。鉴于不同 结果相似,我们取 计算 。
(a)
Appendix K Calculation of
附录 K 的计算
To gauge the true degree of confusion between categories and for a given model, we suggest utilizing the metric:
为衡量给定模型上类别 与 之间的真实混淆程度,我们建议使用 指标:
First, we procure all test samples bearing true labels or . We then obtain the probabilities and yielded by the model for categories and , respectively, on these samples. These probabilities are normalized to a total of 1. Essentially, we derive a classifier that delivers the probabilities and for the categories and respectively, on the test samples . By calculating the Area Under the Receiver Operating Characteristic Curve (AUC-ROC) value of this classifier , we get the degree of confusion between category and , termed as .
首先,获取所有真实标签为 或 的测试样本 。然后得到模型在这些样本上对类别 与 给出的概率 与 。将这些概率归一化使总和为 1。本质上,我们得到一个在测试样本 上分别对类别 与 给出概率 与 的分类器 。通过计算该分类器 的受试者工作特征曲线下面积(AUC-ROC)值,得到类别 与 之间的混淆程度,称为 。
The computed is a value that never exceeds 1. The closer approximates 1, the less pronounced the confusion, and vice versa.
所计算的 是一个不超过 1 的值。 越接近 1,混淆越不显著,反之亦然。
We use the above metric instead of directly analyzing the output labels of the model because previous work has indicated the issue of insufficient output probability calibration in ICL Zhao et al. (2021), which is greatly affected by factors such as sample ordering and model preferences for specific label words. By leveraging our defined degree of confusion, , we can implicitly alleviate the disturbances arising from insufficient probability calibration on the output labels. This allows for a more accurate representation of the model’s degree of confusion for different categories, mitigating the impact of randomness.
我们使用上述指标而非直接分析模型输出标签,因为先前工作指出 ICL 存在输出概率校准不足的问题(Zhao et al., 2021),它受样本顺序以及模型对特定标签词偏好等因素强烈影响。通过利用我们所定义的混淆程度 ,可以隐式缓解输出标签上因概率校准不足带来的干扰。这允许更准确表征模型对不同类别的混淆程度,减轻随机性影响。
Appendix L Reproducibility
附录 L 可复现性
In the supplementary material, we have provided codes that allow for the faithful replication of our experiments and subsequent result analysis. To ensure consistency and reproducibility across different devices, we have fixed the five random seeds to the values of 42, 43, 44, 45, and 46. We invite readers to delve into the code for additional implementation details that may arouse their interest.
在补充材料中,我们提供了可忠实复现实验及后续结果分析的代码。为确保跨设备一致性与可复现性,我们将五个随机种子固定为 42、43、44、45 与 46。欢迎读者深入代码以了解可能引起兴趣的额外实现细节。





