Inference Scaling 实操:Temperature + Top-p + Self-Consistency + Best-of-N 组合提升精度 >2x · 干货攻略
- 链接:https://x.com/rasbt/status/1976554075912094144(X 帖已不可见,内容由 LinkedIn 帖子和 YouTube 视频佐证)
- 分类:x-tips
- 来源:X @rasbt(Sebastian Raschka)
- 作者:Jay
- 更新:2026-09-23
- 仓库:rasbt/reasoning-from-scratch
这是什么
2026 年 9 月 19 日,Sebastian Raschka(@rasbt)在 X 上发布了 "Inference scaling part 1",展示了如何通过组合温度缩放(Temperature Scaling)+ Top-p 过滤 + 多项式采样(Multinomial Sampling) 生成多样化答案,再配合 Self-Consistency 和 Best-of-N 两种推理时缩放方法,使基础模型在 MATH-500 上的准确率从 15.2% 提升到 52.2%,超过 3 倍原生准确率(原帖主张超过 2 倍提升,官方实测达 3.4 倍)。整套方法不需要重新训练模型,完全在推理阶段生效。
该内容是 Raschka 新书 Build a Reasoning Model (From Scratch) 第 4 章的实战部分,配套代码发布在 rasbt/reasoning-from-scratch 仓库 ch05/02_math500-more-inference-scaling-scripts/ 目录。
为什么值得关注
谁在推——@rasbt
Sebastian Raschka 是前威斯康星大学统计学教授、Lightning AI ML 工程师,著有《Build a Large Language Model (From Scratch)》和《Build a Reasoning Model (From Scratch)》,在 ML/AI 领域有超过 50 万关注者。他的分享以代码级实操 + 实验数据著称,不发空泛结论,每条结论都有实验支撑。
解决什么问题
基础 LLM 推理能力弱:Qwen3 base 模型在 MATH-500 上原生准确率仅 15.2%,相当于随机猜测水平。用这套方法,不需要换模型,不需要精调,仅通过改变推理时的采样策略和投票机制,就能把准确率推到 52.2%——比同尺寸 reasoning 模型(48.2%)还高,同时推理时间更短。
核心洞察:传统 temperature=0(greedy decode)只选最高概率 token,丢失了所有"次优路径"中的有效推理。引入采样多样性后,多条推理路径的多数票结果远好于单条贪婪路径。
适用人群
- 想不换模型就提升数学/推理任务准确率的工程师
- 对 LLM 推理过程有深入学习需求的研究者
- 想理解 inference-time scaling 底层机制的读者
核验过程
读过的官方来源
| 来源 | URL | 覆盖内容 |
|---|---|---|
| rasbt/reasoning-from-scratch GitHub README | https://github.com/rasbt/reasoning-from-scratch | 书籍章节结构、代码目录、推理缩放定位 |
| ch05 脚本目录(含性能数据表) | https://github.com/rasbt/reasoning-from-scratch/tree/main/ch05/02_math500-more-inference-scaling-scripts | Self-consistency、Best-of-N 在 MATH-500 上的逐项准确率和耗时数据 |
| rasbt Substack 博客(免费预览) | https://magazine.sebastianraschka.com/p/categories-of-inference-time-scaling | inference scaling 分类框架、书籍章节描述 |
| rasbt LinkedIn 帖子(Sep 2026) | https://www.linkedin.com/posts/sebastianraschka_inference-scaling-lets-us-trade-extra-compute-activity-7395471656275755009 | "35页章节,base 模型 MATH-500 从 15.2% 到 52.2%"(原文主张) |
| YouTube: Inference Scaling 1 (Sep 19, 2026) | https://www.youtube.com/watch?v=t5y-kS9nNxU | 视频逐帧展示脚本运行结果,base model 15.2%,n=10 self-consistency 52% |
交叉验证结论
| 说法 | 核验来源 | 状态 |
|---|---|---|
| Base 模型 MATH-500 准确率 15.2% | GitHub 脚本注释 + YouTube 视频 [1:29:45] | ✅ 两处一致 |
| Self-consistency (n=10) + CoT → 52.2% | LinkedIn 帖子原文 + YouTube [1:33:44] | ✅ 两处一致 |
| 推理模型(reasoning variant)MATH-500 48.2% | GitHub ch05 脚本表 Row 10 | ✅ |
| Self-consistency + avg logprob (n=3) = 44.8% | GitHub ch05 脚本表 Self-consistency 部分 Row 4 | ✅ |
| Best-of-N + avg logprob (n=3) = 43.2% | GitHub ch05 脚本表 Best-of-N 部分 Row 3 | ✅ |
| CoT prompting + base = 33.4% | GitHub ch05 脚本表 Row 1 | ✅ |
| Self-consistency 比推理模型更快(不需要多轮推理 token) | YouTube [1:33:52] | ✅ rasbt 口述确认 |
| temperature=0.9, top_p=0.9 用于自洽性采样 | GitHub 脚本运行参数 Row 2(self-consistency 部分) | ✅ |
| temperature=0.7, top_p=0.9 用于 self-refinement 脚本 | GitHub 脚本运行参数 Row 2(self-refinement 部分) | ✅ |
⚠️ 未独立核验项:原帖主张"精度提升 >2x"——官方实测为 3.4 倍(15.2% → 52.2%),">2x" 口径略保守但符合;">2x" 为 Raschka 本人在 X/LinkedIn 上的描述,并非第三方验证。
上手步骤
环境准备
git clone --depth 1 https://github.com/rasbt/reasoning-from-scratch.git
cd reasoning-from-scratch
# 按 ch02/setup 说明安装 reasoning_from_scratch 包
# 需要 GPU(ch05 推荐,ch04 可在 CPU 运行)
第一步:组合采样函数(Temperature + Top-p + Multinomial)
Raschka 在 YouTube 视频([56:18] 起)中演示了从零实现温度缩放 → softmax 归一化 → 多项式采样全流程。核心逻辑:
import torch
import torch.nn.functional as F
def multinomial_sample(logits, temperature=0.9, top_p=0.9):
# 1. 温度缩放
scaled = logits / temperature
# 2. Top-p 过滤(核心:截断低概率 token)
sorted_probs, sorted_indices = torch.sort(F.softmax(scaled, dim=-1), descending=True)
cumsum = torch.cumsum(sorted_probs, dim=-1)
# 保留概率之和刚好超过 top_p 的 token
mask = cumsum <= top_p
# 至少保留第一个(最高概率)token
mask[..., :-1] = mask[..., 1:].clone()
mask[..., 0] = True
filtered = sorted_probs * mask
filtered = filtered / filtered.sum(dim=-1, keepdim=True)
# 3. 多项式采样
token_idx = torch.multinomial(filtered, num_samples=1)
return sorted_indices.gather(-1, token_idx)
关键参数对比(来自 GitHub 脚本):
- Self-consistency / Best-of-N:temperature=0.9, top_p=0.9
- Self-refinement:temperature=0.7, top_p=0.9
⚠️ 为什么不用
temperature=0? Greedy decode(temperature=0)永远选最高概率 token,没有采样多样性,自洽性需要多条不同推理路径才能发挥效果。
第二步:Self-Consistency + 多数票
生成 N 条不同推理路径 → 提取每条答案 → 多数票输出:
# 运行 self-consistency(GitHub 脚本)
uv run self_consistency_scorer_math500.py \
--which_model "base" \
--temperature 0.9 \
--top_p 0.9 \
--num_samples 10 \
--dataset_size 500 \
--prompt_suffix "\n\nExplain step by step." \
--scoring "none"
GitHub 实测数据(MATH-500,Base 模型):
| 方法 | N | 准确率 | 耗时 |
|---|---|---|---|
| Baseline + CoT | 1 | 33.4% | 129 min |
| Self-consistency + 多数票 | 3 | 43.2% | 328 min |
| Self-consistency + heuristic 打分 | 3 | 43.4% | 327 min |
| Self-consistency + avg logprob | 3 | 44.8% | 328 min |
| Self-consistency + 多数票(n=10) | 10 | 52.2% | 未列单项,n=5 约 3× 基础耗时 |
注:n=10 的单项耗时 GitHub 表未直接列出,YouTube 视频 [1:33:37] 口述"n=5 到 n=10 上升到 52%",总耗时约为基础的 3 倍量级。
第三步:Best-of-N + Scorer 打分
Self-consistency 用多数票,Best-of-N 用打分函数排序答案:
uv run best_of_n_math500.py \
--which_model "base" \
--temperature 0.9 \
--top_p 0.9 \
--num_samples 3 \
--dataset_size 500 \
--prompt_suffix "\n\nExplain step by step." \
--scoring "logprob"
GitHub 实测数据(Base 模型):
| 方法 | N | 准确率 | 耗时 |
|---|---|---|---|
| Baseline + CoT | 1 | 33.4% | 129 min |
| Best-of-N + heuristic | 3 | 40.6% | 328 min |
| Best-of-N + avg logprob | 3 | 43.2% | 330 min |
第四步:Self-Consistency vs Best-of-N 怎么选
| 对比维度 | Self-Consistency(多数票) | Best-of-N(Scorer) |
|---|---|---|
| 核心机制 | 选出现最多的答案 | 选打分最高的答案 |
| 打分函数 | 不需要 | 需要(logprob / heuristic) |
| 适合场景 | 答案可枚举(如选择题、数学题) | 开放式答案需要 judge 模型 |
| n=3 效果 | 43.2% | 43.2%(logprob) |
| n=3 + 打分 | 44.8%(avg logprob) | — |
| 结论 | 优先用 Self-Consistency | 仅在开放式任务或打分为正时考虑 |
坑与适用边界
⚠️ 准确率数字来自 Qwen3 Base 模型,不代表所有模型
GitHub 表头明确标注:数据在 Qwen3 base(DGX Spark GPU)上测得。不同模型架构的采样多样性和推理路径分布差异很大,直接迁移到 GPT-4o / Claude 等模型可能得到不同结果。Raschka 在 YouTube 中也强调"数值有一定随机性"([1:30:00])。
⚠️ 52.2% 是在 n=10 + CoT prompting 条件下达到的
n=10 意味着生成 10 条推理路径并做多数票,计算成本约为原生推理的 10 倍。Raschka 本人也指出:"虽然效果好,但耗时非常长"(YouTube [1:33:52])。实际工程中需要在精度和成本之间做权衡。
⚠️ CoT prompting 的提示工程很重要
--prompt_suffix "\n\nExplain step by step." 这个后缀不是随意加的。GitHub 脚本用 CoT 提示来激发模型的推理步骤,没有 CoT,自洽性的效果会大打折扣(因为采样产生的路径不够多样化)。
⚠️ 52.2% vs 推理模型 48.2% 的结论有前提
Raschka 在 YouTube 中强调([1:33:52]):"base 模型加 self-consistency 比 reasoning 模型更好,但注意耗时很长"。这个比较是在 Qwen3 同尺寸下进行的,不意味着 self-consistency 可以替代专门的推理模型训练。
⚠️ 自洽性在简单问题上可能有害
2025 年 EMNLP 论文 Does Inference Scaling Improve Reasoning Faithfulness? 发现:Self-consistency 对简单问题的保真度反而可能下降(GPT-5.2 在简单题上 break 13%,Claude 在简单题上 break 23%)。对于简单问题,多步采样反而可能引入不一致的答案。
适用边界
适合的场景: - 数学推理(MATH-500、AIME 等 benchmark) - 有明确标准答案的推理任务 - 可以接受多倍推理耗时的离线评估或批量推理
不适合的场景: - 实时性要求高的在线推理(n=10 耗时是原生 10 倍) - 简单/封闭式问题(可能过度推理) - 开放式生成任务(多数票难以应用)
一句话结论
通过 Temperature=0.9 + Top-p=0.9 + 多项式采样生成多样化推理路径,配合 Self-Consistency 多数票,可将 Qwen3 base 在 MATH-500 上从 15.2% 提升至 52.2%(>3 倍),超越同尺寸专用推理模型;Best-of-N 打分方案在 n=3 时与 Self-Consistency 持平;代价是 3~10 倍推理时间,适合离线批量推理场景。