# RL 奖励黑客

强化学习的最终目标是最大化某种回报（比如速度、收益、某个指标）。但强化学习可能会 **作弊。** 当强化学习算法学会某个技巧或利用某些手段来增加回报，而实际上并没有完成最终任务时，这被称为“**回报操纵**".

这就是模型学会修改单元测试以通过编程挑战的原因，这些问题是现实部署的关键障碍。另一些很好的例子来自于 [维基百科](https://en.wikipedia.org/wiki/Reward_hacking).

<div align="center"><figure><img src="https://i.pinimg.com/originals/55/e0/1b/55e01b94a9c5546b61b59ae300811c83.gif" alt="" width="188"><figcaption></figcaption></figure></div>

**可以对抗回报操纵吗？可以！** 在我们的 [免费 gpt-oss 强化学习笔记本](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-\(20B\)-GRPO.ipynb) 中，我们探讨了如何在代码生成环境中对抗回报操纵，并展示了针对常见错误模式的切实可行的解决方案。我们看到模型会编辑计时函数、外包给其他库、缓存结果，甚至直接作弊。经过对抗之后，结果是我们的模型生成真正优化过的矩阵乘法内核，而不是巧妙的作弊手段。

## :trophy: 回报操纵概述

强化学习中一些常见的回报操纵示例包括：

#### 懒惰

强化学习学会使用 Numpy、Torch 等库，这些库调用了优化过的 CUDA 内核。我们可以通过检查生成的代码是否导入了其他非标准的 Python 库来阻止强化学习算法调用优化代码。

#### 缓存与作弊

强化学习学会缓存输出结果，并且通过检查 Python 全局变量来找到实际输出。

我们可以通过用一个大的伪矩阵清空缓存来阻止强化学习算法使用缓存数据。我们还必须通过多个循环和轮次来仔细基准测试。

#### 作弊

强化学习学会编辑计时函数，使其输出为 0 秒。我们可以通过限制其 `局部变量` 和 `全局变量`来阻止强化学习算法使用全局或缓存变量。我们还将使用 `exec` 来创建函数，因此我们必须将输出保存到一个空字典中。我们还通过以下方式禁止全局变量访问 `types.FunctionType(f.__code__, {})`\\


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/zh/kai-shi-shi-yong/reinforcement-learning-rl-guide/advanced-rl-documentation/rl-reward-hacking.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
