@Zen_with_AI: Sebastian Raschka's latest episode of "Reasoning from Scratch," Episode 5, is here! This time, it focuses on two super …
Summary
This article describes Sebastian Raschka's YouTube video episode on reasoning techniques in AI, focusing on log-probability scoring and self-refinement to improve model performance without retraining.
View Cached Full Text
Cached at: 09/26/26, 09:08 PM
Sebastian Raschka’s latest episode of “Reasoning from Scratch,” Episode 5, is here! This time, it focuses on two super practical reasoning techniques:
1️⃣ Log-probability scoring
From token probabilities and sequence likelihoods to numerically stable calculations, and then using average log-prob to score answers. This approach isn’t just for selecting the optimal response—it’s also the core foundation for pre-training cross-entropy and distillation.
2️⃣ Self-refinement
Have the model first generate an answer → write its own critique → then revise the answer based on the critique, iterating to improve quality. The video also includes a full PyTorch implementation and MATH-500 evaluation results.
Perfect for those who want to understand “reasoning-time scaling” and “how to get models to improve themselves” from the ground up. Highly recommend watching it all the way through!
YouTube: https://youtube.com/watch?v=TVMyOJ_3Gxo…
Sebestian Raschka的“从零推理”系列第5集:详解自我优化技术
TL;DR:本集视频介绍了推理时的“自我优化”技术,包括如何通过简单的基于规则的评分和对数概率评分来提升模型生成答案的质量,并为后续的强化学习训练打下基础。
引言:推理技术概述与自我优化简介
欢迎回到“从零推理”系列第五部分。在之前的视频中,我们探讨了推理技术,即无需重新训练模型就能提升其性能的方法。特别是,我们介绍了一种称为自一致性的技术,它是多数投票的一种变体。
本集将重点讨论第二种推理技术——自我优化。目标是通过该技术提高模型在数学500基准测试上的准确率,并生成更高质量的答案。未来视频将讨论训练技术,即用于开发推理模型的核心强化学习技巧。在此之前,自我优化本身也包含了一些我们稍后在强化学习部分将重用的基础构件。
三种推理时扩展方法回顾
视频提供了一个概览,总结了该系列涵盖的三种推理时扩展方法:
- 思维链提示:通过添加“请逐步解释”来修改提示。这是一种简单技术,但在数学500基准测试上为某些模型(尤其是基础模型)带来了显著的准确率提升。
- 自一致性采样:即多数投票。这需要修改文本生成函数以输出多样化的答案。为此,我们引入了温度缩放(增加采样多样性)和多项式采样,并结合Top-P过滤来剔除低概率的噪声词元。
- 自我优化:本集的焦点。它将引入一项基础技术——对数概率评分。值得注意的是,温度缩放和Top-P过滤在此依然相关。这个过程是层层递进的,我们将在自我优化中复用之前的概念,并在后续的强化学习训练视频中再次复用对数概率评分。
实践:加载模型与简单评分方法
让我们直接进入代码实践。首先,需要加载预训练的大语言模型。
环境设置与基础生成
我们使用与之前相同的代码。为了可复现性,建议从CPU开始调试(例如,使用device = "cpu"),待结果稳定后可切换。我们重用之前的文本生成函数,并继续使用“请逐步解释”的提示,因为这对基础模型很有帮助。
通过一个简单查询(例如计算 127 * 13 - 1568 + 33 * 7),模型生成答案。由于使用了较高的温度(0.9)和Top-P设置,多次运行可能生成不同但正确的答案(例如83)。这里引出一个问题:当多个答案都正确时,如何选择最佳的一个展示给用户?
引入基于规则的评分(启发式评分)
一个简单的方法是基于答案的长度进行评分。虽然字符数更容易计算,但词元数是更准确的“黄金标准”。通常,字符越多,词元也越多。如果两个答案都正确,我们可能更倾向于较短的答案,因为它生成成本更低。
视频展示了一个基于规则的评分器实现。该评分器为每个生成的答案分配一个分数。例如,对于两个正确答案,较长的答案得分为2.087,较短的答案得分为2.408(分数越高越好)。因此,评分器会推荐较短的答案。
评分函数解析
该评分器结合了几个组成部分:
- 格式奖励:如果答案生成了带方框的最终结果(使用
extract_final_candidate函数提取),则获得奖励。 - 数字奖励:如果答案中包含可提取的数字(即使不在方框内),则获得额外奖励。
- 简洁奖励:这是一个关键部分,使用了一个基于字符数的指数衰减函数。更短的答案会获得更高的奖励分数。视频中展示了一个图表,说明一个2000词元的答案几乎得不到奖励,而一个非常短的答案可以获得高达1.5的奖励。不过,这种过于简洁的奖励可能并不理想,因为它会抑制模型的解释过程。这些值(如奖励分数和衰减参数)本质上是超参数,可以根据需要调整。
该评分函数的实现代码如下(概念性描述):
# 从答案中提取最终候选答案
final_candidate = extract_final_candidate(answer, fallback=None) # 使用回退选项之一
score = 0
if final_candidate is not None:
score += 2 # 方框奖励
if final_candidate.isdigit():
score += 1 # 数字奖励
# 添加简洁奖励(基于字符数的指数衰减)
conciseness_reward = ... # 计算简洁奖励
score += conciseness_reward
进阶:对数概率评分
除了基于规则的启发式评分,另一种更复杂的方法是基于概率的评分,即对数概率评分。这涉及到模型对其生成文本的置信度。
概念解释
当模型使用贪婪解码时,它总是选择最高概率的词元。然而,在生成完整回答时,我们可以收集每个生成词元的概率,并计算整个序列的对数概率。一个高的对数概率(负值较小)通常表示模型对其生成的内容更“自信”,这可能与答案的质量相关。对数概率评分是我们将在自我优化中使用的评分器,其基础构件也将在后续的强化学习部分被复用。
计算对数概率
要计算一个生成序列的对数概率,需要获取模型在每个步骤输出的原始logits,并将其转换为概率。这通常涉及softmax计算。然后,对于序列中的每个词元,取其对数概率,最后将这些值相加(或取平均)。
视频指出,这个对数概率评分的概念将在自我优化中发挥作用,并且是一个可以插入到自我优化函数中的模块化组件。
总结与展望
本集介绍了自我优化的实践步骤:
- 加载预训练模型。
- 开发一个简单的基于规则的评分方法作为代理,用于评估答案质量。
- 引入更强大的对数概率评分方法,它基于模型生成的置信度。
这些评分方法(基于规则和基于概率)都是可以在自我优化技术中使用的不同策略。它们都为后续更复杂的流程(如强化学习训练)提供了基础工具和概念铺垫。
Source: YouTube - @Zen_with_AI: Sebastian Raschka’s latest episode of “Reasoning from Scratch,” Episode 5
Sebastian Raschka (@rasbt): Reasoning from scratch, round number 5! This time, talking about log-probability scoring (also a great fundamental concept for loss functions like cross-entropy in pre-training and distillation) and self-refinement.
00:00 Introduction and inference-time scaling recap 05:02
Similar Articles
Sebastian Raschka's from-scratch video course on building reasoning models
Sebastian Raschka released an 11-hour, eight-video course on building reasoning models from scratch in PyTorch, covering evaluation, inference scaling, reinforcement learning with verifiable rewards, and distillation, serving as a sequel to his earlier Coding LLMs from the Ground Up course.
@rasbt: After 18 months of writing, coding, and experimenting, Build a Reasoning Model (From Scratch) is finally out! My first …
Sebastian Raschka announces the release of his book 'Build a Reasoning Model (From Scratch)' after 18 months of work, covering inference scaling, reinforcement learning, and distillation from scratch.
@rasbt: Reasoning from scratch, round number 6! An introduction (and implementation) of Reinforcement Learning with Verifiable …
Sebastian Raschka's sixth 'Reasoning from Scratch' video introduces and implements Reinforcement Learning with Verifiable Rewards (RLVR) and Group Relative Policy Optimization (GRPO), covering reward design, advantage computation, GRPO loss, and MATH-500 training results.
What Is Reasoning
This article explains how reasoning traces work in AI models, discussing their implementation, concealment, and extraction techniques, with examples from models like GPT-OSS and DeepSeek.
@paul_cal: >The "reasoning" systems produce lots of token sequences & pick the best one Sorry Yann this isn't how Fable or Astra w…
@paul_cal contests Yann LeCun's view on AI reasoning systems, asserting that Fable and Astra utilize autoregressive methods more effectively than LeCun suggests.