---
title: "In-Context Learning — 上下文学习"
source: "https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0"
wiki: "systems-analysis.info/int"
article: "In-Context_Learning_—_上下文学习"
language: "zh"
categories:
  - "Category:Chinese"
  - "Category:Large language models"
  - "Category:Machine learning"
revision_id: 3220
wiki_created_at: 2026-09-06T23:17:29Z
wiki_modified_at: 2026-09-06T23:17:29Z
downloaded_at: 2026-09-07T22:55:49Z
---

# In-Context Learning — 上下文学习

**上下文学习**（英语：**In-Context Learning**，简称**ICL**）是大型语言模型 (LLM)的一项基本能力，指模型仅通过在请求的上下文（提示）中提供的示例（演示）来“即时”学习新任务。其关键特点是，这种适应过程不会更新模型的权重（参数），也就是说，无需进行传统的微调（fine-tuning）<sup>[\[1\]](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_note-lakera-1)[\[2\]](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_note-hopsworks-2)</sup>。

该机制使模型能够表现出惊人的灵活性，解决那些并非为其专门训练的任务。ICL已成为使大型语言模型如此强大和通用的关键突破之一<sup>[\[3\]](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_note-gradient_pub-3)</sup>。

## 工作机制

关于ICL工作原理的精确理解仍是一个活跃的研究领域，但有几种主流理论解释了这一现象。

### Transformer作为元优化器

一种流行的理论认为，Transformer架构在预训练期间学会了在其前向传播（forward passes）中实现学习算法。当模型收到带有示例的提示时，它会隐式地执行一种优化来解决所呈现的任务，调整其内部状态（激活），而不是权重<sup>[\[4\]](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_note-arxiv_grad-4)</sup>。

### 贝叶斯推断

另一种理论将ICL视为一种贝叶斯推断。模型在海量数据上进行预训练后，对众多概念有了先验知识。上下文中的示例则作为证据，使模型能够根据隐藏概念更新其后验概率分布。换言之，示例帮助模型“理解”当前需要解决的是其已知的数千个任务中的哪一个<sup>[\[5\]](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_note-stanford-5)</sup>。

## Types of In-Context Learning - 上下文学习的类型

根据提供的示例数量，ICL可分为三种主要类型。

- **Few-shot Learning（少样本学习）**：这是最常见且最均衡的方法。向模型提供几个（通常为2到10个）演示示例。

*示例（情感分类）：*

    文本：“今天天气真好！”
    情感：积极

    文本：“我讨厌堵车。”
    情感：消极

    文本：“这部电影相当一般。”
    情感：

**预期回答：**

    中性

- **One-shot Learning（单样本学习）**：只给模型提供一个示例。这通常足以设定回答的格式，并比零样本方法显著提高性能。

<!-- -->

- **Zero-shot Learning（零样本学习）**：不向模型提供任何示例，只提供任务的指令或描述。在这种情况下，模型完全依赖于预训练期间获得的知识。

## 实际应用

正确应用ICL可以在无需昂贵的开发和微调的情况下解决广泛的任务。

- **对于创意和风格类任务**（以特定风格生成代码，模仿特定作者的风格写作）：
  - 建议使用**Few-shot Learning**。
  - 示例帮助模型捕捉所需的风格、格式和输出结构。

<!-- -->

- **对于指令清晰的简单任务**（翻译、摘要、回答简单问题）：
  - 通常**Zero-shot Learning**就足够了。
  - 如果这些任务是预训练的一部分，现代模型能够很好地处理它们。

<!-- -->

- **对于输出格式很重要的任务**（生成JSON、实体提取）：
  - 建议使用**One-shot**或**Few-shot Learning**。
  - 即使是一个示例也能清晰地设定所需的回答结构，防止格式错误。

## 优点与缺点

<table class="wikitable">
<caption>ICL的优缺点对比</caption>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="header">
<th>优点</th>
<th>缺点</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><ul>
<li><strong>灵活性与速度：</strong> 无需重新训练即可即时适应新任务。</li>
<li><strong>节约资源：</strong> 无需为微调收集数据、标注和计算能力。</li>
<li><strong>易用性：</strong> 允许没有机器学习专业知识的用户通过简单的文本示例来配置模型。</li>
</ul></td>
<td><ul>
<li><strong>上下文窗口的限制：</strong> 示例的数量受模型最大上下文长度的限制。</li>
<li><strong>对示例的敏感性：</strong> 结果在很大程度上取决于所提供演示的质量、顺序和格式。</li>
<li><strong>推理阶段成本高：</strong> 包含大量示例的长提示会增加生成成本和时间。</li>
<li><strong>安全风险：</strong> 将机密信息作为示例传递可能不安全。</li>
</ul></td>
</tr>
</tbody>
</table>

ICL的优缺点对比

## 与其他范式的比较

### ICL vs. Fine-tuning - ICL与微调的对比

微调（Fine-tuning）通过改变模型的权重，将新知识“印刻”到模型中。这使模型成为特定领域的专家，但降低了其通用灵活性。相比之下，ICL不改变权重，更为灵活，但在需要深厚领域知识的专门任务上，其性能可能不及微调。

### ICL vs. RAG (Retrieval-Augmented Generation) - ICL与RAG的对比

这两种方法都扩展了模型的上下文，但目的不同：

- **ICL** 使用示例来**教**模型*如何*执行任务（演示技能）。
- **RAG** 使用检索到的信息来**告知**模型回答问题所需的事实（提供知识）。

在实践中，ICL和RAG经常结合使用以达到最佳效果。

## 参考文献

- Brown, T. B. et al. (2020). *Language Models are Few-Shot Learners*. <a href="https://arxiv.org/abs/2005.14165" class="external text" rel="nofollow">arXiv:2005.14165</a>.
- Dai, D. et al. (2022). *Why Can GPT Learn In-Context? Language Models Implicitly Perform Gradient Descent as Meta-Optimizers*. <a href="https://arxiv.org/abs/2212.10559" class="external text" rel="nofollow">arXiv:2212.10559</a>.
- Panwar, M.; Ahuja, K.; Goyal, N. (2024). *In-Context Learning through the Bayesian Prism*. <a href="https://arxiv.org/abs/2306.04891" class="external text" rel="nofollow">arXiv:2306.04891</a>.
- Müller, S. et al. (2021). *Transformers Can Do Bayesian Inference*. <a href="https://arxiv.org/abs/2112.10510" class="external text" rel="nofollow">arXiv:2112.10510</a>.
- Garg, S. et al. (2022). *What Can Transformers Learn In-Context? A Case Study of Simple Function Classes*. <a href="https://arxiv.org/abs/2208.01066" class="external text" rel="nofollow">arXiv:2208.01066</a>.
- Min, S. et al. (2022). *Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?*. <a href="https://arxiv.org/abs/2202.12837" class="external text" rel="nofollow">arXiv:2202.12837</a>.
- Wang, X. et al. (2023). *Explaining and Finding Good Demonstrations for In-Context Learning*. <a href="https://arxiv.org/abs/2302.13971" class="external text" rel="nofollow">arXiv:2302.13971</a>.
- Xie, S. et al. (2024). *A Survey on In-Context Learning*. <a href="https://arxiv.org/abs/2301.00234" class="external text" rel="nofollow">arXiv:2301.00234</a>.
- Yu, Z.; Ananiadou, S. (2024). *How Do Large Language Models Learn In-Context? Query and Key Matrices of In-Context Heads Are Two Towers for Metric Learning*. <a href="https://arxiv.org/abs/2402.02872" class="external text" rel="nofollow">arXiv:2402.02872</a>.
- Wibisono, K. C.; Wang, Y. (2024). *From Unstructured Data to In-Context Learning: Exploring What Tasks Can Be Learned and When*. <a href="https://arxiv.org/abs/2406.00131" class="external text" rel="nofollow">arXiv:2406.00131</a>.
- Chan, J. K. et al. (2022). *Data Distributional Properties Drive Emergent In-Context Learning in Transformers*. <a href="https://arxiv.org/abs/2205.05055" class="external text" rel="nofollow">arXiv:2205.05055</a>.
- Hahn, M.; Goyal, N. (2023). *A Theory of Emergent In-Context Learning as Implicit Structure Induction*. <a href="https://arxiv.org/abs/2303.07971" class="external text" rel="nofollow">arXiv:2303.07971</a>.

## 注释

1.  <span id="cite_note-lakera-1">[↑](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_ref-lakera_1-0) <a href="https://www.lakera.ai/blog/what-is-in-context-learning" class="external text" rel="nofollow">What is In-Context Learning (ICL)?</a> // Lakera.ai</span>
2.  <span id="cite_note-hopsworks-2">[↑](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_ref-hopsworks_2-0) <a href="https://www.hopsworks.ai/dictionary/in-context-learning-icl" class="external text" rel="nofollow">In-Context Learning (ICL)</a> // Hopsworks.ai</span>
3.  <span id="cite_note-gradient_pub-3">[↑](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_ref-gradient_pub_3-0) <a href="https://thegradient.pub/in-context-learning-in-context/" class="external text" rel="nofollow">In-Context Learning, In Context</a> // The Gradient</span>
4.  <span id="cite_note-arxiv_grad-4">[↑](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_ref-arxiv_grad_4-0) <a href="https://arxiv.org/abs/2212.10559" class="external text" rel="nofollow">Why Can GPT Learn In-Context? Language Models Secretly Perform Gradient Descent as Meta-Optimizers</a> // arXiv, 2022.</span>
5.  <span id="cite_note-stanford-5">[↑](https://systems-analysis.info/int/In-Context_Learning_%E2%80%94_%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AD%A6%E4%B9%A0#cite_ref-stanford_5-0) <a href="https://ai.stanford.edu/blog/understanding-incontext/" class="external text" rel="nofollow">Understanding In-Context Learning</a> // Stanford Human-Centered AI, 2023.</span>
