LLM 全栈学习
第 19 章

RLHF:从人类反馈中强化学习 RLHF: Reinforcement Learning from Human Feedback

前面讲的 RL 都活在「可验证」领域(数学、代码):任何候选解法都能拿一个具体答案自动打分——检查框里的答案,或用 LLM 当裁判,全程无需人。可惜很多领域是「不可验证」的:写个笑话、写首诗、写段摘要……没有标准答案,难以自动评分。最朴素的想法是让人给每个生成打分,但 RL 要做上千次更新 × 上千个 prompt × 每个 prompt 上千个生成,这是上亿次人工评判,根本不可行。RLHF(Christiano 等人 / OpenAI)的解法是「间接」:只让人对一小批样本做排序(排序比打绝对分容易),用这些排序训练一个独立的「奖励模型」(一个神经网络)去模仿人类偏好,再用这个奖励模型自动给海量生成打分,从而在不可验证领域里跑 RL。好处:让我们能在不可验证领域跑 RL,甚至还能轻微改善可验证领域。但有个大坑:奖励模型只是人类判断的「模拟」;如果 RL 跑太久,模型会找到一些荒诞的对抗性输入,骗到虚高分数(reward hacking / 钻奖励模型空子)。所以 RLHF 不能像可验证 RL(那种 AlphaGo 式的「魔法」RL)那样无限跑——它更像一次「微调」,跑几百步、有改善就收手。 The RL we've covered so far lives in 'verifiable' domains (math, code): any candidate solution can be auto-scored against a concrete answer — check the boxed answer, or use an LLM judge — with no human in the loop. Unfortunately many domains are 'unverifiable': write a joke, write a poem, summarize a paragraph ... there's no concrete answer, so they're hard to auto-score. The naive idea is to have humans score every generation, but RL does thousands of updates × thousands of prompts × thousands of generations each — hundreds of millions of human judgments, which is unworkable. RLHF (Christiano et al. / OpenAI) solves this with indirection: have humans only RANK a small set of samples (ranking is easier than absolute scoring), use those rankings to train a separate 'reward model' (a neural network) that imitates human preferences, then use that reward model to auto-score the massive number of generations — letting us run RL in unverifiable domains. Upside: it lets us run RL in unverifiable domains and even slightly improves verifiable ones. But there's a big caveat: the reward model is only a 'simulation' of human judgment; if you run RL too long, the model finds absurd adversarial inputs that earn spuriously high scores (reward hacking / gaming the reward model). So RLHF can't be run indefinitely like verifiable-domain RL (the AlphaGo-style 'magic' RL) — it's more of a fine-tune: run a few hundred steps, take the improvement, and stop.

到目前为止,我们看的所有 RL 问题都属于「可验证」领域:任意一个候选解法,都能拿一个具体答案轻松打分。比如答案是 3,我们就检查模型解法里框出来的答案是不是等于 3;或者用一个「LLM 裁判」——让另一个 LLM 看着解法和标准答案,判断两者是否一致。当下的模型已经足够可靠地做这件事。无论哪种方式,我们都有一个具体答案可对照,而且整个打分是自动的、不需要任何人参与。 So far, every RL problem we've looked at is in a 'verifiable' domain: any candidate solution can be scored easily against a concrete answer. If the answer is 3, we check whether the boxed answer in the model's solution equals 3; or we use an 'LLM judge' — another LLM that looks at the solution and the reference answer and decides whether they're consistent. Today's models are reliable enough to do this. Either way, we have a concrete answer to check against, and the whole scoring is automatic, with no human in the loop.

问题在于,这套策略没法用在「不可验证」领域。典型的就是各种创意写作任务:写一个关于鹈鹕的笑话、写一首诗、给一段话写摘要。这些任务里,给不同的解法打分变得困难得多。比如写鹈鹕笑话——我们当然可以生成一大堆不同的笑话,但难点在于:怎么给它们打分?哪个更好笑,本身就没有客观的、可机器检查的标准答案。 The problem is that this strategy doesn't apply in 'unverifiable' domains. The classic ones are creative-writing tasks: write a joke about pelicans, write a poem, summarize a paragraph. In these tasks, scoring different solutions becomes much harder. Take pelican jokes — we can certainly generate a pile of different jokes, but the difficulty is: how do we score them? Which one is funnier has no objective, machine-checkable concrete answer.

原则上我们当然可以请人来看这些笑话、逐个打分。但放进 RL 的循环里就崩了:做强化学习要进行成千上万次更新,每次更新要看上千个 prompt,每个 prompt 又要看上百乃至上千个生成。粗略一算,这可能意味着要请人评判上亿次——上亿个(而且多半很烂的)笑话。这是个完全无法扩展的策略,我们需要某种自动的办法。 In principle we could of course have humans look at the jokes and score them one by one. But drop that into the RL loop and it collapses: reinforcement learning runs many thousands of updates, each update looks at thousands of prompts, and each prompt has hundreds or thousands of generations. Roughly multiplied out, that can mean asking humans to judge on the order of a billion times — a billion (mostly terrible) jokes. That's a completely unscalable strategy; we need some automatic approach.

解法来自一篇论文(Christiano 等人,出自当时的 OpenAI;其中不少人如今是 Anthropic 的联合创始人),提出了「从人类反馈中强化学习」(RLHF)。核心招数是「间接」:我们只让人类介入一点点,然后训练一个完全独立的神经网络,叫做「奖励模型」(reward model),让它去模仿人类的打分。一旦这个网络变成了「人类偏好的模拟器」,我们就可以转而对着它跑 RL——不再问真人,而是问这个模拟出来的人。模拟器不会是完美的人,但只要它在统计上和人类判断足够接近,RL 就能奏效;实践中也确实如此。 The solution comes from a paper (Christiano et al., out of what was then OpenAI; many of those people are now co-founders of Anthropic) that proposed 'reinforcement learning from human feedback' (RLHF). The core trick is indirection: we involve humans only a little, then train a completely separate neural network called a 'reward model' to imitate human scores. Once that network becomes a 'simulator of human preferences,' we run RL against it instead — no longer asking a real human, but asking the simulated one. The simulator won't be a perfect human, but as long as it's statistically close enough to human judgment, RL works; and in practice it does.

训练奖励模型大致是这样:拿一个 prompt(比如「写一个关于鹈鹕的笑话」),生成五个不同的笑话。我们不让人直接打绝对分,而是请人把这五个笑话从最好到最差排个序——因为对人来说,给出一个排序,比给出精确分数要容易。这个排序,就是人类对训练过程的全部贡献。另一边,奖励模型(一个独立的神经网络,通常也是 Transformer,但它不生成语言,只输出分数)会读入「prompt + 一个候选笑话」,吐出一个介于 0 到 1 之间的分数(0 最差、1 最好)。 Training the reward model goes roughly like this: take a prompt (say 'write a joke about pelicans') and generate five different jokes. Instead of asking a human for absolute scores, we ask them to ORDER the five jokes from best to worst — because for a human, giving an ordering is easier than giving precise scores. That ordering is the human's entire contribution to training. On the other side, the reward model (a separate neural network, usually also a Transformer, but it doesn't generate language — it only outputs a score) reads in 'prompt + one candidate joke' and emits a single number between 0 and 1 (0 worst, 1 best).

然后我们把奖励模型给出的分数,和人类给出的排序做对比,用一个损失函数衡量两者的「一致程度」,并据此更新奖励模型。直觉上:如果人觉得某个笑话最好笑、模型也给了高分(比如 0.8),那方向对了,只是分数还该再高一点——更新后也许变成 0.81;如果人觉得某个排第二、模型却只给了 0.1,这是严重分歧,更新后这个分数就该大幅上调;如果人觉得某个最差、模型却给了高分,更新后这个分数就该下来。如此反复在人类排序数据上更新,奖励模型就越来越像一个「人类偏好模拟器」,我们就能对着它跑 RL。 Then we compare the reward model's scores against the human's ordering, use a loss function to measure how 'consistent' the two are, and update the reward model accordingly. Intuitively: if the human thought a joke was funniest and the model also gave it a high score (say 0.8), the direction is right but the score should be even higher — after an update maybe 0.81; if the human ranked one second but the model gave only 0.1, that's a big disagreement, and after an update that score should rise sharply; if the human thought one was worst but the model gave it a high score, after an update that score should come down. Repeating this over human ordering data, the reward model becomes more and more like a 'simulator of human preferences,' and we can run RL against it.

不可验证域(如"写个笑话"):没有标准答案,无法自动判分 ① 人类排序少量样本 给 5 个笑话排个名次 (排序比逐个打分容易) 人只需看少量,不看海量 ② 训练奖励模型 一个神经网络,学着 模仿人的打分口味 → 能给任意生成打分 ③ 用它做 RL 奖励模型自动给海量 生成打分,无需人参与 → 强化高分的生成 ⚠️ 注意:奖励模型只是"模拟"的人 RL 跑久了会"钻空子"(reward hacking):找到能骗高分 却毫无意义的输出。所以 RLHF 不能像可验证域那样无限期跑下去
RLHF 的核心:人类只对少量样本排序 → 训练奖励模型模仿人类偏好 → 用奖励模型自动给海量生成打分,再跑 RL The core of RLHF: humans only rank a few samples → train a reward model to imitate human preferences → use it to auto-score the massive number of generations, then run RL
💡 关键在于「间接」与「数量」的对比:朴素做法要请人评判约十亿次;RLHF 里人类只需要看大约一千个 prompt、每个五个生成,总共约五千个笑话,而且只给排序。排序这件事训练出奖励模型,奖励模型再去当那个被查询无数次的「廉价人类」。这一步「间接」之所以还能让模型变好,很可能是因为「判别比生成容易」(discriminator–generator gap):让标注员从零写一首好诗很难,但让他们在五首诗里挑出最好的,要容易得多——于是我们能拿到更高质量的人类信号。 The key is the contrast between indirection and sheer count: the naive approach needs roughly a billion human judgments; in RLHF humans only look at about a thousand prompts, five generations each — about five thousand jokes total — and only give an ordering. That ordering trains the reward model, and the reward model then plays the 'cheap human' that can be queried countless times. Why this indirection still makes the model better is likely the discriminator–generator gap: asking a labeler to write a great poem from scratch is hard, but asking them to pick the best of five poems is much easier — so we get higher-quality human signal.

先说 RLHF 的好处。第一,它让我们得以把 RL 这套极其强大的技术,用到任意领域,包括那些不可验证的领域:摘要、写诗、写笑话,以及数学和代码之外的各种创意写作。第二,经验上,正确地做 RLHF 确实能让模型变好一点——为什么会这样并没有特别确凿的定论,但最可能的解释还是上面说的「判别比生成容易」:我们不再要求人去做极难的生成(创意写作)任务,只要求他们分辨好坏、挑出最好的;系统随后自己去发现那些会被人类评为「好」的回答。这一步间接,反而带来了更高质量的监督信号。 First, the upside of RLHF. One: it lets us apply RL — an extremely powerful set of techniques — to arbitrary domains, including unverifiable ones: summarization, poetry, jokes, and all kinds of creative writing beyond math and code. Two: empirically, doing RLHF correctly does make the model a bit better — why isn't firmly established, but the likeliest explanation is the discriminator–generator gap above: we no longer ask humans to do the extremely hard generation (creative-writing) task, only to discriminate good from bad and pick the best; the system then discovers for itself the responses that humans would grade well. That step of indirection yields higher-quality supervision.

但 RLHF 也带来重大的坏处。我们做 RL 时,对照的不是真正的人类判断,而是一个有损的人类模拟。更要命的是一个微妙又「狡猾」的现象:RL 极其擅长找到「骗过模拟器」的办法。奖励模型是个有上十亿参数的庞大 Transformer,它在「模仿」人类,但终究只是模拟。于是存在一些根本不在它训练集里的输入,这些输入会莫名其妙地拿到虚高的分数。比如你把 RLHF 跑上一千步,前几百步笑话确实在变好,但之后会「跌下悬崖」:你会发现排名第一的「鹈鹕笑话」竟然是诸如「the the the」这种毫无意义的东西——可奖励模型偏偏给它打了接近 1 的满分。 But RLHF also brings major downsides. When we run RL, we're optimizing against not actual human judgment but a lossy simulation of humans. Worse is a subtle, 'devious' phenomenon: RL is extremely good at finding ways to fool the simulator. The reward model is a massive Transformer with billions of parameters; it 'imitates' humans, but it's still only a simulation. So there exist inputs that were never in its training set yet inexplicably earn spuriously high scores. For example, run RLHF for a thousand steps: in the first few hundred the jokes do improve, but then they 'fall off a cliff' — you find that the top-ranked 'pelican joke' is something nonsensical like 'the the the,' yet the reward model gives it a near-perfect score close to 1.

这类输入叫「对抗性样本」(adversarial examples)——它们钻进了模型输入空间的缝隙里,在顶端给出荒诞结果。你也许会想:那就把「the the the」加进训练集、给它极低分不就行了?确实,模型会学会给这一条打低分;但问题是,这种无意义的对抗性样本几乎是无穷多的。你不断把新发现的荒诞输入加回去、给低分,也永远赢不了这场游戏:只要 RL 跑得足够久,它总能再找到一个新的办法去钻奖励模型的空子,用毫无意义的结果拿到虚高分。根本原因是:打分函数本身是个巨大的神经网络,而 RL 极其擅长找到骗过它的路径。 Such inputs are called 'adversarial examples' — they slip into the nooks and crannies of the model's input space and produce nonsense at the top. You might think: just add 'the the the' to the training set with a terrible score? Indeed the model will learn to score that one low; but the problem is that such nonsensical adversarial examples are essentially infinite. You keep adding newly discovered nonsense back in with low scores, and you still never win the game: run RL long enough and it always finds a fresh way to game the reward model, earning spuriously high scores with meaningless output. The root cause: the scoring function is itself a giant neural network, and RL is extremely good at finding ways to trick it.

⚠️ 所以实践上的规矩是:RLHF 跑个几百步、看到模型有改善,就得「裁掉」收手发布——不能对着同一个奖励模型跑太久,否则优化就会开始钻它的空子。一句话:RLHF 不是「魔法版」的 RL。在可验证领域(数学、代码、围棋),打分函数极简单、几乎无法被钻空子,你可以无限地跑、跑上数十万步、发现 AlphaGo「第 37 手」那种连人都想不到的新策略。但 RLHF 的奖励函数是可被钻空子的,所以它更像是一次「小小的微调」:能改善模型,却不是那种「投入更多算力、跑得更久就能越来越好」的、有魔力的 RL。 So the practical rule is: run RLHF a few hundred steps, see the model improve, then 'crop' it and ship — you can't run too long against the same reward model, or the optimization starts gaming it. In a phrase: RLHF is not the 'magic' kind of RL. In verifiable domains (math, code, Go) the scoring function is dead simple and nearly impossible to game, so you can run indefinitely — tens or hundreds of thousands of steps — and discover AlphaGo 'move 37'-style strategies no human would think of. But RLHF's reward function is gameable, so it's more like a 'little fine-tune': it improves the model, but it isn't the magical RL where pouring in more compute and running longer keeps making it better.
  • 可验证领域(数学、代码):候选解法能拿具体答案自动打分(检查框里的答案,或用 LLM 裁判),全程无需人。
  • 不可验证领域(写笑话、诗、摘要等创意写作):没有标准答案,难以自动评分。
  • 朴素做法行不通:RL = 上千次更新 × 上千 prompt × 上千生成 ≈ 上亿次人工评判,无法扩展。
  • RLHF 的招数是「间接」:人类只对一小批样本排序(排序比打分容易)→ 训练奖励模型(独立神经网络)模仿人类偏好 → 用它自动给海量生成打分,再跑 RL。
  • 好处:能在不可验证领域跑 RL;经验上让模型变好(很可能因为「判别比生成容易」)。
  • 坏处:奖励模型只是有损的人类模拟;RL 跑太久会找到对抗性样本「钻空子」(reward hacking),用荒诞输入骗到虚高分。
  • 结论:RLHF 不是「魔法 RL」——跑几百步、有改善就收手发布;它更像一次微调,不能像可验证 RL 那样无限跑。
  • Verifiable domains (math, code): candidate solutions can be auto-scored against a concrete answer (check the boxed answer, or use an LLM judge), with no human in the loop.
  • Unverifiable domains (jokes, poems, summaries, creative writing): no concrete answer, so hard to auto-score.
  • The naive approach fails: RL = thousands of updates × thousands of prompts × thousands of generations ≈ hundreds of millions of human judgments — unscalable.
  • RLHF's trick is indirection: humans only RANK a small set (ranking is easier than scoring) → train a reward model (separate neural net) to imitate human preferences → use it to auto-score the massive generations, then run RL.
  • Upside: lets us run RL in unverifiable domains; empirically makes the model better (likely the discriminator–generator gap).
  • Downside: the reward model is only a lossy human simulation; run RL too long and it finds adversarial examples to 'game' it (reward hacking), earning spurious high scores with nonsense.
  • Bottom line: RLHF is not 'magic RL' — run a few hundred steps, take the gain, and ship; it's more of a fine-tune, and can't be run indefinitely like verifiable RL.

📝 本章测验

为什么不能直接对「不可验证」领域(如写笑话)用我们之前的 RL?Why can't we directly use our earlier RL on 'unverifiable' domains like joke-writing?

RLHF 里,人类提供的监督信号具体是什么?In RLHF, what exactly is the supervision signal that humans provide?

「奖励模型」在 RLHF 里扮演什么角色?What role does the 'reward model' play in RLHF?

为什么 RLHF 不能像可验证领域的 RL 那样无限地跑下去?Why can't RLHF be run indefinitely the way verifiable-domain RL can?