LLM探索:GPT类模型的几个常用参数 Top-k, Top-p, Temperature
A recent breakthrough in artificial intelligence (AI) is the introduction of language processing technologies that enable us to build more intelligent systems with a richer understanding of language than ever before. Large pre-trained Transformer language models, or simply large language models, vastly extend the capabilities of what systems are able to do with text.
目录8 个章节
前言
上一篇文章介绍了几个开源LLM的环境搭建和本地部署,在使用ChatGPT接口或者自己本地部署的LLM大模型的时候,经常会遇到这几个参数,本文简单介绍一下~
- temperature
- top_p
- top_k
关于LLM
上一篇也有介绍过,这次看到一个不错的图
A recent breakthrough in artificial intelligence (AI) is the introduction of language processing technologies that enable us to build more intelligent systems with a richer understanding of language than ever before. Large pre-trained Transformer language models, or simply large language models, vastly extend the capabilities of what systems are able to do with text.

LLM看似很神奇,但本质还是一个概率问题,神经网络根据输入的文本,从预训练的模型里面生成一堆候选词,选择概率高的作为输出,上面这三个参数,都是跟采样有关(也就是要如何从候选词里选择输出)。
temperature
用于控制模型输出的结果的随机性,这个值越大随机性越大。一般我们多次输入相同的prompt之后,模型的每次输出都不一样。
- 设置为 0,对每个prompt都生成固定的输出
- 较低的值,输出更集中,更有确定性
- 较高的值,输出更随机(更有创意😃)

一般来说,prompt 越长,描述得越清楚,模型生成的输出质量就越好,置信度越高,这时可以适当调高 temperature 的值;反过来,如果 prompt 很短,很含糊,这时再设置一个比较高的 temperature 值,模型的输出就很不稳定了。
遇事不决就调参,调一下,万一就生成了不错的回答呢?
PS:ChatGLM提供的例子把范围限定在0-1之间。
top_k & top_p
这俩也是采样参数,跟 temperature 不一样的采样方式。
前面有介绍到,模型在输出之前,会生成一堆 token,这些 token 根据质量高低排名。
比如下面这个图片,输入 The name of that country is the 这句话,模型生成了一堆 token,然后根据不同的 decoding strategy 从 tokens 中选择输出。

这里的 decoding strategy 可以选择
- greedy decoding: 总是选择最高分的 token,有用但是有些弊端,详见下文
- top-k: 从 tokens 里选择 k 个作为候选,然后根据它们的
likelihood scores来采样 - top-p: 候选词列表是动态的,从 tokens 里按百分比选择候选词
top-k 与 top-p 为选择 token 引入了随机性,让其他高分的 token 有被选择的机会,不像 greedy decoding 一样总是选最高分的。
greedy decoding
好处是简单,坏处是容易生成循环、重复的内容
Greedy decoding is a reasonable strategy but has some drawbacks such as outputs with repetitive loops of text. Think of the suggestions in your smartphone's auto-suggest. When you continually pick the highest suggested word, it may devolve into repeated sentences.
top-k
设置越大,生成的内容可能性越大;
设置越小,生成的内容越固定;
设置为1时,和 greedy decoding 效果一样。

Changing the top-k parameter sets the size of the shortlist the model samples from as it outputs each token. Setting top-k to 1 gives us greedy decoding.
top-p
top-p 又名 Nucleus Sampling(核采样)
与 top-k 固定选取前 k 个 tokens 不同,top-p 选取的 tokens 数量不是固定的,这个方法是设定一个概率阈值。
继续上面的例子,将 top-p 设定为 0.15,即选择前 15% 概率的 tokens 作为候选。如下图所示,United 和 Netherlands 的概率加起来为 15% ,所以候选词就是这俩,最后再从这些候选词里,根据概率分数,选择 united 这个词。

Top-p is usually set to a high value (like 0.75) with the purpose of limiting the long tail of low-probability tokens that may be sampled. We can use both top-k and top-p together. If both
kandpare enabled,pacts afterk.
经常遇到的默认 top-p 值就是 0.7/0.8 这样,还是那个说法,设置太低模型的输出太固定,设置太高,模型彻底放飞自我也不好。
参考资料
_EOF_
LLM探索:GPT类模型的几个常用参数 Top-k, Top-p, Temperature
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
ChatGLM-6B本地部署
下载项目代码 git clone https://github.com/THUDM/ChatGLM-6B.git 创建虚拟环境,建议使用 conda 管理 conda create -n chatglm python==3.8 安装依赖 conda activate chatglm pip install -r requirements.txt conda install cudatoolkit=
2026-05-09 · LLM · 约 1 分钟
LLM探索:环境搭建与模型本地部署
又来折腾LLM本地部署?ChatGLM-6B在6GB显存下就能跑,MOSS却要显存翻倍,这差距。硬件要求高得离谱,但作者硬是在Ubuntu上搞定,佩服?开源LLM本地部署指南,就这?
2026-05-09 · LLM · 约 1 分钟
LLM探索:离线部署Ollama和one-api服务
离线部署Ollama?手动下载上传,导入模型文件,啧,麻烦。one-api用Docker,tiktoken需下载重命名,更烦。添加渠道?简单。
2026-05-09 · LLM · 约 1 分钟
2GB 显存还能跑大模型吗?我在 MX250 老笔记本上部署 Qwen3.5 2BB
七年前的Matebook14,MX250显存2GB,硬塞Qwen3.5 2B——这届玩家是真的敢。核心思路倒是比模型选择更值得学:先看清楚硬件预算,再决定能跑什么,而不是硬上7B然后等CPU掉帧到怀疑人生。1.81GiB直接顶满2GB显存,KV Cache挤出来的空间里,4K Context刚好够用。至于Agent场景?省省吧,2B让MX250当推理节点做轻量任务已经仁至义尽。
2026-09-15 · LLM · 约 1 分钟
讨论
共 0 条加载评论中…