---
title: "Top-p sampling — Top-p 采样"
source: "https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7"
wiki: "systems-analysis.info/int"
article: "Top-p_sampling_—_Top-p_采样"
language: "zh"
categories:
  - "Category:Chinese"
  - "Category:Large language models"
  - "Category:LLM core concepts"
  - "Category:Machine learning"
  - "Category:Systems analysis terminology"
revision_id: 8229
wiki_created_at: 2026-09-07T01:14:16Z
wiki_modified_at: 2026-09-07T01:14:16Z
downloaded_at: 2026-09-07T23:24:12Z
---

# Top-p sampling — Top-p 采样

**Top-p 采样**，也称为**核采样**（英文：*Nucleus Sampling*），是一种用于大型语言模型（LLM）生成文本的随机解码方法。该方法由 Ari Holtzman 等人于 2019 年提出，作为固定 Top-k 采样的一种改进替代方案。其思想是在生成的每一步，根据累积概率阈值 $p$ 动态选择候选集。<sup>[\[1\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-holtzman2019-1)</sup>

## 概念

Top-p 的核心思想是在每一步选择包含最高概率 token 的**最小**集合，使其总概率不小于给定的阈值 $p$（即“核”，英文：*nucleus*）。数学上，对于词汇表 $V$ 上的条件分布 $P(x \mid x_{1:i - 1})$，其核 $V^{(p)}$ 可以定义如下：

$\sum\limits_{x \in V^{(p)}}P(x \mid x_{1:i - 1}) \geq p\quad\text{且}\quad\forall\, S \subset V^{(p)}:\ \sum\limits_{x \in S}P(x \mid x_{1:i - 1}) < p.$

等价的表述是：将所有 token 按 $P(x \mid x_{1:i - 1})$ 降序排列，并选取累积概率质量 ≥ $p$ 的**最短前缀**。<sup>[\[1\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-holtzman2019-1)</sup>

确定核之后，$V^{(p)}$ 之外的 token 的概率被置为零，而核内的概率则进行归一化（使其总和为 1）。下一个 token 从这个截断后的分布中采样。

### 动态调整

- 当分布“尖锐”（模型非常确定）时，核很小：少数几个 token 的概率质量就已 ≥ $p$，这可以提高文本的连贯性。
- 当分布“平坦”（有许多合理的续写选项）时，核很大：选择范围扩大，从而增加多样性。<sup>[\[1\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-holtzman2019-1)</sup>

## 与其他解码方法的比较

### Top-p vs. Top-k - Top-p 与 Top-k 对比

- **Top-k** 总是从固定数量 $k$ 个最可能的 token 中进行选择。在“尖锐”分布中，这可能会为了凑数而加入不必要的低概率选项；而在“平坦”分布中，则可能切掉未进入前 $k$ 名的合理续写。
- **Top-p** 根据当前步骤的数据调整候选集的大小，使其在不同类型的分布上表现得更灵活、更稳定。<sup>[\[1\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-holtzman2019-1)</sup>

### Top-p vs. Temperature - Top-p 与 Temperature 对比

- **温度**（*temperature*）会重塑整个分布的形状（使其更尖锐或更平滑），但**不会剔除**任何 token：即使是概率极低的选项也保留了非零的被选中机会。<sup>[\[2\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-hf-gen-2)</sup>
- **Top-p** 引入了对分布尾部的**硬截断**——低概率的 token 会被完全排除在采样之外，这有助于防止生成明显不合适的续写。<sup>[\[1\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-holtzman2019-1)</sup>

服务提供商的实践建议是：在调整风格或随机性时，通常只修改 \`temperature\` **或** \`top_p\`，而不是同时修改两者，以避免对分布产生“双重”影响，并简化调试过程。<sup>[\[3\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-azure-rec-3)</sup>

## 实际应用与建议

Top-p 因其灵活性和可控性的结合，在现代 LLM 中得到广泛应用。

- **典型取值范围**。实践中，常使用 $p \approx 0.90\text{–}0.95$ 的值（参见 Transformers 的指南和示例；在许多 SDK 中，**0.95** 作为“默认值”或推荐值出现）。<sup>[\[2\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-hf-gen-2)[\[4\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-vertex-params-4)</sup>
  - 接近 1.0 的值（如 0.98–0.99）会增加多样性，因为更多 token 会被纳入核中。
  - 较小的值（如 0.80–0.90）会提高输出的确定性和“保守性”。
  - 当 $p = 1$ 时，截断消失：在整个词汇表中进行选择（同时考虑温度的影响）。<sup>[\[2\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-hf-gen-2)</sup>

<!-- -->

- **与库和 API 的兼容性**。
  - Transformers 中实现了 *TopPLogitsWarper*，其中额外使用 \`min_tokens_to_keep\` 阈值（通常 ≥1），以防止在 $p$ 值极小和分布“尖锐”时核发生退化。<sup>[\[5\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-hf-warp-5)</sup>
  - 在一些 API 中，\`top_p\` 参数可用，而 \`top_k\` 可能不存在；参数的支持情况及其语义**取决于具体的模型/提供商**（例如，部分推理模型可能会限制对随机性的调整）。请参考 OpenAI/Azure/Google 的官方文档。<sup>[\[6\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-openai-top-p-6)[\[3\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-azure-rec-3)[\[4\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-vertex-params-4)</sup>

<!-- -->

- **长文本与重复性**。一系列实验表明，与贪心搜索/集束搜索（greedy/beam）和固定的 Top-k 相比，核采样能减少文本退化（重复、套话）的倾向，尤其是在长序列上。<sup>[\[1\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-holtzman2019-1)[\[7\]](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_note-tan2024-7)</sup>

## 参见

- 温度
- 大型语言模型

## 参考文献

- Holtzman, A. et al. (2020). *The Curious Case of Neural Text Degeneration*. <a href="https://arxiv.org/abs/1904.09751" class="external text" rel="nofollow">arXiv:1904.09751</a>.
- Fan, A. et al. (2018). *Hierarchical Neural Story Generation*. <a href="https://arxiv.org/abs/1805.04833" class="external text" rel="nofollow">arXiv:1805.04833</a>.
- Meister, C. et al. (2023). *Locally Typical Sampling*. <a href="https://arxiv.org/abs/2202.00666" class="external text" rel="nofollow">arXiv:2202.00666</a>.
- Su, Y.; Collier, N. (2022). *Contrastive Search Is What You Need for Neural Text Generation*. <a href="https://arxiv.org/abs/2210.14140" class="external text" rel="nofollow">arXiv:2210.14140</a>.
- O’Brien, S.; Lewis, M. (2023). *Contrastive Decoding Improves Reasoning in Large Language Models*. <a href="https://arxiv.org/abs/2309.09117" class="external text" rel="nofollow">arXiv:2309.09117</a>.
- Yu, S. et al. (2023). *Conformal Nucleus Sampling*. <a href="https://aclanthology.org/2023.findings-acl.3.pdf" class="external text" rel="nofollow">ACL Findings 2023</a>.
- Tan, Q. et al. (2024). *A Thorough Examination of Decoding Methods in the Era of Large Language Models*. <a href="https://arxiv.org/abs/2402.06925" class="external text" rel="nofollow">arXiv:2402.06925</a>.
- Finlayson, M. et al. (2024). *Basis‑Aware Truncation Sampling for Neural Text Generation*. <a href="https://arxiv.org/abs/2412.14352" class="external text" rel="nofollow">arXiv:2412.14352</a>.
- Chen, S. J. et al. (2025). *Decoding Game: On Minimax Optimality of Heuristic Text Generation Methods*. <a href="https://arxiv.org/abs/2410.03968" class="external text" rel="nofollow">arXiv:2410.03968</a>.
- Sen, J. et al. (2025). *Advancing Decoding Strategies: Enhancements in Locally Typical Sampling for LLMs*. <a href="https://arxiv.org/abs/2506.05387" class="external text" rel="nofollow">arXiv:2506.05387</a>.

## 注释

1.  <span id="cite_note-holtzman2019-1">↑ <sup>[1.0](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-holtzman2019_1-0)</sup> <sup>[1.1](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-holtzman2019_1-1)</sup> <sup>[1.2](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-holtzman2019_1-2)</sup> <sup>[1.3](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-holtzman2019_1-3)</sup> <sup>[1.4](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-holtzman2019_1-4)</sup> <sup>[1.5](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-holtzman2019_1-5)</sup> Holtzman, A., Buys, J., Du, L., Forbes, M., & Choi, Y. (2019). *The Curious Case of Neural Text Degeneration*. arXiv:1904.09751. <a href="https://arxiv.org/abs/1904.09751" class="external autonumber" rel="nofollow">[1]</a></span>
2.  <span id="cite_note-hf-gen-2">↑ <sup>[2.0](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-hf-gen_2-0)</sup> <sup>[2.1](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-hf-gen_2-1)</sup> <sup>[2.2](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-hf-gen_2-2)</sup> Hugging Face Transformers. *Generation strategies (top‑k, top‑p, temperature)*. <a href="https://huggingface.co/docs/transformers/main/en/generation_strategies" class="external autonumber" rel="nofollow">[2]</a></span>
3.  <span id="cite_note-azure-rec-3">↑ <sup>[3.0](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-azure-rec_3-0)</sup> <sup>[3.1](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-azure-rec_3-1)</sup> Microsoft Learn (Azure OpenAI). *Text/Chat Completions — parameters*. Recommendation to "alter temperature **or** top_p but not both". <a href="https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/completions" class="external autonumber" rel="nofollow">[3]</a></span>
4.  <span id="cite_note-vertex-params-4">↑ <sup>[4.0](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-vertex-params_4-0)</sup> <sup>[4.1](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-vertex-params_4-1)</sup> Google AI / Vertex AI. *Generation parameters (topP/topK) for text/Gemini*. Examples with topP≈0.95. <a href="https://ai.google.dev/gemini-api/docs/parameters" class="external autonumber" rel="nofollow">[4]</a> <a href="https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text#parameters" class="external autonumber" rel="nofollow">[5]</a></span>
5.  <span id="cite_note-hf-warp-5">[↑](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-hf-warp_5-0) Transformers API. *TopPLogitsWarper* (parameters and behavior, including \`min_tokens_to_keep\`). <a href="https://huggingface.co/docs/transformers/main/en/main_classes/text_generation#transformers.TopPLogitsWarper" class="external autonumber" rel="nofollow">[6]</a></span>
6.  <span id="cite_note-openai-top-p-6">[↑](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-openai-top-p_6-0) OpenAI API Reference. *top_p*. <a href="https://platform.openai.com/docs/api-reference/chat/create#chat-create-top_p" class="external autonumber" rel="nofollow">[7]</a></span>
7.  <span id="cite_note-tan2024-7">[↑](https://systems-analysis.info/int/Top-p_sampling_%E2%80%94_Top-p_%E9%87%87%E6%A0%B7#cite_ref-tan2024_7-0) Tan, Q. et al. (2024). *A Thorough Examination of Decoding Methods in the Era of Large Language Models*. arXiv:2402.06925. <a href="https://arxiv.org/abs/2402.06925" class="external autonumber" rel="nofollow">[8]</a></span>
