docs(chapter4): 修正奖励模型公式中的数学符号和格式错误

修复公式中的数学符号显示问题,包括 KL 散度计算和奖励函数中的点乘符号
This commit is contained in:
KMnO4-zx
2025-06-17 12:16:06 +08:00
parent 08ee8ef753
commit d49819cd9d
2 changed files with 164 additions and 2 deletions

162
.gitignore vendored Normal file
View File

@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.idea/
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
.history
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.DS_Store
.obsidian

View File

@@ -339,9 +339,9 @@ RMReward Model即奖励模型。RM 是用于拟合人类偏好,来给 LL
1. 从 SFT 之后的 LLM 初始化两个模型分别作为 Actor Model 和 Ref Model从训练的 RM 初始化两个模型分别作为 Reward Model 和 Critic Model 1. 从 SFT 之后的 LLM 初始化两个模型分别作为 Actor Model 和 Ref Model从训练的 RM 初始化两个模型分别作为 Reward Model 和 Critic Model
2. 输入一个 PromptActor Model 和 Ref Model 分别就 Prompt 生成回复; 2. 输入一个 PromptActor Model 和 Ref Model 分别就 Prompt 生成回复;
3. Actor Response 和 Ref Response 计算 KL 散度:$r_{KL} = -\theta_{KL}D_{KL}(\pi_{PPO}(y|x)||\pi_{base}(y|x))$其中,$\pi_{PPO}(y|x)$即为 Actor Model 的输出,而 $\pi_{base}(y|x)$即为 Ref Model 的输出,$theta_{KL}D_{KL}$即是计算 KL 散度的方法; 3. Actor Response 和 Ref Response 计算 KL 散度:$r_{KL} = -\theta_{KL}D_{KL}(\pi_{PPO}(y|x)||\pi_{base}(y|x))$其中,$\pi_{PPO}(y|x)$即为 Actor Model 的输出,而 $\pi_{base}(y|x)$即为 Ref Model 的输出,$\theta_{KL}D_{KL}$即是计算 KL 散度的方法;
4. Actor Response 分别输入到 Reward Model 和 Critic Model 进行打分其中Reward Model 输出的是回复对应的标量奖励Critic Model 还会输出累加奖励即从i位置到最后的累积奖励 4. Actor Response 分别输入到 Reward Model 和 Critic Model 进行打分其中Reward Model 输出的是回复对应的标量奖励Critic Model 还会输出累加奖励即从i位置到最后的累积奖励
5. 计算的 KL 散度、两个模型的打分均输入到奖励函数中,计算奖励:$loss = -(kl_{ctl}*r_{KL} + \gamma * V_{t+1} - V_{t})logP(A_t|V_t)$,这里的 $kl_{ctl}是控制 KL 散度对结果影响的权重参数,$\gamma$ 是控制下一个时间(也就是样本)打分对结果影响的权重参数,$V_t$ 是 Critic Model 的打分输出,$A_t$ 则是 Reward Model 的打分输出; 5. 计算的 KL 散度、两个模型的打分均输入到奖励函数中,计算奖励:$loss = -(kl_{ctl} \cdot r_{KL} + \gamma \cdot V_{t+1} - V_{t}) \log P(A_t|V_t)$,这里的 $kl_{ctl}$ 是控制 KL 散度对结果影响的权重参数,$\gamma$ 是控制下一个时间(也就是样本)打分对结果影响的权重参数,$V_t$ 是 Critic Model 的打分输出,$A_t$ 则是 Reward Model 的打分输出;
6. 根据奖励函数分别计算出的 actor loss 和 critic loss更新 Actor Model 的参数和 Critic Model 的参数注意Actor Model 和 Critic Model 的参数更新方法是不同的,此处就不再一一赘述了,感兴趣的读者可以深入研究强化学习的相关理论。 6. 根据奖励函数分别计算出的 actor loss 和 critic loss更新 Actor Model 的参数和 Critic Model 的参数注意Actor Model 和 Critic Model 的参数更新方法是不同的,此处就不再一一赘述了,感兴趣的读者可以深入研究强化学习的相关理论。
在上述过程中,因为要使用到四个模型,显存占用会数倍于 SFT。例如如果我们 RM 和 LLM 都是用 7B 的体量PPO 过程中大概需要 240G4张 80G A100每张卡占用 60G显存来进行模型加载。那么为什么我们需要足足四个模型呢Actor Model 和 Critic Model 较为容易理解,而之所以我们还需要保持原参数不更新的 Ref Model 和 Reward Model是为了限制模型的更新不要过于偏离原模型以至于丢失了 Pretrain 和 SFT 赋予的能力。 在上述过程中,因为要使用到四个模型,显存占用会数倍于 SFT。例如如果我们 RM 和 LLM 都是用 7B 的体量PPO 过程中大概需要 240G4张 80G A100每张卡占用 60G显存来进行模型加载。那么为什么我们需要足足四个模型呢Actor Model 和 Critic Model 较为容易理解,而之所以我们还需要保持原参数不更新的 Ref Model 和 Reward Model是为了限制模型的更新不要过于偏离原模型以至于丢失了 Pretrain 和 SFT 赋予的能力。