深度强化学习课程文档
实战
并获得增强的文档体验
开始
实战
我们学习了 ML-Agents 是什么以及它是如何工作的。我们还研究了我们将要使用的两个环境。现在我们准备好训练我们的智能体了!

为了验证此实战以获得认证,你只需将你训练好的模型推送到 Hub。为了验证此实战,没有最低结果要求。但是,如果你想获得好的结果,你可以尝试达到以下目标
- 对于 Pyramids:平均奖励 = 1.75
- 对于 SnowballTarget:平均奖励 = 15 或每回合射击 30 个目标。
有关认证过程的更多信息,请查看此部分 👉 https://huggingface.co/deep-rl-course/en/unit0/introduction#certification-process
要开始实战,请点击 “在 Colab 中打开” 按钮 👇
我们强烈建议学生使用 Google Colab 进行实战练习,而不是在他们的个人电脑上运行。
通过使用 Google Colab,你可以专注于学习和实验,而无需担心设置环境的技术方面。
单元 5:ML-Agents 导论

在本 notebook 中,你将学习 ML-Agents 并训练两个智能体。
- 第一个将学习向生成的靶子射击雪球。
- 第二个需要按下按钮来生成金字塔,然后导航到金字塔,将其击倒,并移动到顶部的金砖。为此,它需要探索其环境,我们将使用一种称为好奇心的技术。
之后,你将能够在你的浏览器上直接观看你的智能体玩耍。
有关认证过程的更多信息,请查看此部分 👉 https://huggingface.co/deep-rl-course/en/unit0/introduction#certification-process
⬇️ 这是你将在本单元结束时达成的示例。 ⬇️


🎮 环境:
- Pyramids
- SnowballTarget
📚 RL-库:
我们一直在努力改进我们的教程,因此,如果你在本 notebook 中发现任何问题,请在 GitHub Repo 上提交 issue。
本 notebook 的目标 🏆
在本 notebook 结束时,你将
- 了解 ML-Agents 的工作原理和环境库。
- 能够在 Unity 环境中训练智能体。
先决条件 🏗️
在深入学习本 notebook 之前,你需要
🔲 📚 通过阅读单元 5 🤗,学习 ML-Agents 是什么以及它是如何工作的
让我们训练我们的智能体 🚀
设置 GPU 💪
- 为了加速智能体的训练,我们将使用 GPU。为此,请转到 “运行时” > “更改运行时类型”

硬件加速器 > GPU

克隆仓库 🔽
- 我们需要克隆包含 ML-Agents 的仓库。
# Clone the repository (can take 3min)
git clone --depth 1 https://github.com/Unity-Technologies/ml-agents
设置虚拟环境 🔽
为了使 ML-Agents 在 Colab 中成功运行,Colab 的 Python 版本必须满足库的 Python 要求。
我们可以在 setup.py 文件中的 python_requires 参数下检查支持的 Python 版本。这些文件是设置 ML-Agents 库以供使用所必需的,并且可以在以下位置找到
/content/ml-agents/ml-agents/setup.py
/content/ml-agents/ml-agents-envs/setup.py
Colab 当前的 Python 版本(可以使用
!python --version
检查)与库的python_requires
参数不匹配,因此,安装可能会静默失败,并在稍后执行相同命令时导致如下错误/bin/bash: line 1: mlagents-learn: command not found
/bin/bash: line 1: mlagents-push-to-hf: command not found
为了解决这个问题,我们将创建一个 Python 版本与 ML-Agents 库兼容的虚拟环境。
注意:
为了未来的兼容性,如果 Colab 的 Python 版本不兼容,请始终检查安装文件中的 python_requires
参数,并将你的虚拟环境设置为下面给出的脚本中支持的最大 Python 版本
# Colab's Current Python Version (Incompatible with ML-Agents)
!python --version
# Install virtualenv and create a virtual environment
!pip install virtualenv
!virtualenv myenv
# Download and install Miniconda
!wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
!chmod +x Miniconda3-latest-Linux-x86_64.sh
!./Miniconda3-latest-Linux-x86_64.sh -b -f -p /usr/local
# Activate Miniconda and install Python ver 3.10.12
!source /usr/local/bin/activate
!conda install -q -y --prefix /usr/local python=3.10.12 ujson # Specify the version here
# Set environment variables for Python and conda paths
!export PYTHONPATH=/usr/local/lib/python3.10/site-packages/
!export CONDA_PREFIX=/usr/local/envs/myenv
# Python Version in New Virtual Environment (Compatible with ML-Agents)
!python --version
安装依赖项 🔽
# Go inside the repository and install the package (can take 3min)
%cd ml-agents
pip3 install -e ./ml-agents-envs
pip3 install -e ./ml-agents
SnowballTarget ⛄
如果你需要复习此环境的工作原理,请查看此部分 👉 https://huggingface.co/deep-rl-course/unit5/snowball-target
下载并将环境 zip 文件移动到 ./training-envs-executables/linux/ 中
- 我们的环境可执行文件在一个 zip 文件中。
- 我们需要下载它并将其放置到
./training-envs-executables/linux/
中 - 我们使用 linux 可执行文件,因为我们使用 colab,并且 colab 机器的操作系统是 Ubuntu (linux)
# Here, we create training-envs-executables and linux
mkdir ./training-envs-executables
mkdir ./training-envs-executables/linux
我们使用 wget 从 https://github.com/huggingface/Snowball-Target 下载了文件 SnowballTarget.zip
wget "https://github.com/huggingface/Snowball-Target/raw/main/SnowballTarget.zip" -O ./training-envs-executables/linux/SnowballTarget.zip
我们解压缩 executable.zip 文件
unzip -d ./training-envs-executables/linux/ ./training-envs-executables/linux/SnowballTarget.zip
确保你的文件是可访问的
chmod -R 755 ./training-envs-executables/linux/SnowballTarget
定义 SnowballTarget 配置文件
- 在 ML-Agents 中,你在 config.yaml 文件中定义训练超参数。
有多个超参数。为了更好地理解它们,你应该阅读 文档 中对每个超参数的解释
你需要在 ./content/ml-agents/config/ppo/ 中创建一个 SnowballTarget.yaml
配置文件
我们将为你提供此配置的初步版本(复制并粘贴到你的 SnowballTarget.yaml
文件中),但你应该对其进行修改。
behaviors:
SnowballTarget:
trainer_type: ppo
summary_freq: 10000
keep_checkpoints: 10
checkpoint_interval: 50000
max_steps: 200000
time_horizon: 64
threaded: true
hyperparameters:
learning_rate: 0.0003
learning_rate_schedule: linear
batch_size: 128
buffer_size: 2048
beta: 0.005
epsilon: 0.2
lambd: 0.95
num_epoch: 3
network_settings:
normalize: false
hidden_units: 256
num_layers: 2
vis_encode_type: simple
reward_signals:
extrinsic:
gamma: 0.99
strength: 1.0


作为实验,尝试修改其他一些超参数。Unity 在这里提供了非常 好的文档 来解释每个超参数。
现在你已经创建了配置文件并了解了大多数超参数的作用,我们准备好训练我们的智能体了 🔥。
训练智能体
为了训练我们的智能体,我们需要启动 mlagents-learn
并选择包含环境的可执行文件。
我们定义四个参数
mlagents-learn <config>
:超参数配置文件的路径。--env
:环境可执行文件的位置。--run_id
:你想给你的训练运行 id 起的名称。--no-graphics
:在训练期间不启动可视化。

训练模型并使用 --resume
标志在中断的情况下继续训练。
如果你使用
--resume
,第一次可能会失败。尝试重新运行代码块以绕过错误。
训练将花费 10 到 35 分钟,具体取决于你的配置。去喝杯 ☕️ 休息一下吧 🤗。
mlagents-learn ./config/ppo/SnowballTarget.yaml --env=./training-envs-executables/linux/SnowballTarget/SnowballTarget --run-id="SnowballTarget1" --no-graphics
将智能体推送到 Hugging Face Hub
- 现在我们已经训练了我们的智能体,我们准备好将其推送到 Hub 并在你的浏览器上可视化它玩耍了🔥。
为了能够与社区分享你的模型,还需要遵循三个步骤
1️⃣ (如果尚未完成)创建 HF 帐户 ➡ https://huggingface.co/join
2️⃣ 登录并存储来自 Hugging Face 网站的身份验证令牌。
- 创建一个新的令牌 (https://huggingface.co/settings/tokens),具有写入权限

- 复制令牌
- 运行下面的单元格并粘贴令牌
from huggingface_hub import notebook_login
notebook_login()
如果你不想使用 Google Colab 或 Jupyter Notebook,你需要改用此命令:huggingface-cli login
然后我们需要运行 mlagents-push-to-hf
。
我们定义四个参数
--run-id
:训练运行 id 的名称。--local-dir
:智能体的保存位置,它是 results/<run_id name>,所以在我的例子中是 results/First Training。--repo-id
:你想创建或更新的 Hugging Face repo 的名称。它始终是 <你的 huggingface 用户名>/<repo 名称>。如果 repo 不存在,它将自动创建--commit-message
:由于 HF repo 是 git 仓库,你需要提供提交消息。

例如
mlagents-push-to-hf --run-id="SnowballTarget1" --local-dir="./results/SnowballTarget1" --repo-id="ThomasSimonini/ppo-SnowballTarget" --commit-message="First Push"
mlagents-push-to-hf --run-id= # Add your run id --local-dir= # Your local dir --repo-id= # Your repo id --commit-message= # Your commit message
如果一切正常,你应该在过程结束时看到这个(但 url 会有所不同 😆)
Your model is pushed to the hub. You can view your model here: https://huggingface.co/ThomasSimonini/ppo-SnowballTarget
这是你模型的链接。它包含一个模型卡,其中解释了如何使用它,你的 Tensorboard 和你的配置文件。最棒的是它是一个 git 仓库,这意味着你可以有不同的提交,通过新的推送更新你的仓库等等。
但现在最棒的部分来了:能够在网上可视化你的智能体 👀。
观看你的智能体玩耍 👀
这个步骤很简单
前往此处:https://huggingface.co/spaces/ThomasSimonini/ML-Agents-SnowballTarget
启动游戏,然后点击右下角的按钮将其全屏显示

在步骤 1 中,输入你的用户名(你的用户名区分大小写:例如,我的用户名是 ThomasSimonini,而不是 thomassimonini 或 ThOmasImoNInI),然后点击搜索按钮。
在步骤 2 中,选择你的模型仓库。
在步骤 3 中,选择你想重放的模型
- 我有多个模型,因为我们每 500000 个时间步保存一个模型。
- 但因为我想要最新的模型,所以我选择
SnowballTarget.onnx
👉 最好尝试使用不同的模型步骤,以查看智能体的改进。
并且不要犹豫在 discord 的 #rl-i-made-this 频道中分享你的智能体获得的最佳分数 🔥
现在让我们尝试一个更具挑战性的环境,名为 Pyramids。
Pyramids 🏆
下载并将环境 zip 文件移动到 ./training-envs-executables/linux/ 中
- 我们的环境可执行文件在一个 zip 文件中。
- 我们需要下载它并将其放置到
./training-envs-executables/linux/
中 - 我们使用 linux 可执行文件,因为我们正在使用 colab,并且 colab 机器的操作系统是 Ubuntu (linux)
我们使用 wget 从 https://huggingface.co/spaces/unity/ML-Agents-Pyramids/resolve/main/Pyramids.zip 下载了文件 Pyramids.zip
wget "https://huggingface.co/spaces/unity/ML-Agents-Pyramids/resolve/main/Pyramids.zip" -O ./training-envs-executables/linux/Pyramids.zip
解压缩它
unzip -d ./training-envs-executables/linux/ ./training-envs-executables/linux/Pyramids.zip
确保你的文件是可访问的
chmod -R 755 ./training-envs-executables/linux/Pyramids/Pyramids
修改 PyramidsRND 配置文件
- 与第一个环境(自定义环境)相反,Pyramids 是由 Unity 团队制作的。
- 因此 PyramidsRND 配置文件已经存在,并且位于 ./content/ml-agents/config/ppo/PyramidsRND.yaml
- 你可能会问为什么 “RND” 在 PyramidsRND 中。RND 代表 random network distillation(随机网络蒸馏),这是一种生成好奇心奖励的方法。如果你想了解更多相关信息,我们写了一篇文章解释这项技术:https://medium.com/data-from-the-trenches/curiosity-driven-learning-through-random-network-distillation-488ffd8e5938
对于此训练,我们将修改一件事
- 总训练步数超参数过高,因为我们只需 100 万次训练步数即可达到基准(平均奖励 = 1.75)。👉 为此,我们转到 config/ppo/PyramidsRND.yaml,并将 max_steps 更改为 1000000。

作为实验,你也应该尝试修改其他一些超参数。Unity 在这里提供了非常 好的文档 来解释每个超参数。
我们现在准备好训练我们的智能体了 🔥。
训练智能体
训练将花费 30 到 45 分钟,具体取决于你的机器,去喝杯 ☕️ 休息一下吧 🤗。
mlagents-learn ./config/ppo/PyramidsRND.yaml --env=./training-envs-executables/linux/Pyramids/Pyramids --run-id="Pyramids Training" --no-graphics
将智能体推送到 Hugging Face Hub
- 现在我们已经训练了我们的智能体,我们准备好将其推送到 Hub,以便能够在你的浏览器上可视化它玩耍🔥。
mlagents-push-to-hf --run-id= # Add your run id --local-dir= # Your local dir --repo-id= # Your repo id --commit-message= # Your commit message
观看你的智能体玩耍 👀
👉 https://huggingface.co/spaces/unity/ML-Agents-Pyramids
🎁 奖励:为什么不在另一个环境上训练?
现在你知道如何使用 MLAgents 训练智能体了,为什么不尝试另一个环境呢?
MLAgents 提供了 17 个不同的环境,我们还在构建一些自定义环境。最好的学习方法是自己尝试,玩得开心。
你可以在 Hugging Face 上找到当前可用环境的完整列表 👉 https://github.com/huggingface/ml-agents#the-environments
对于可视化你的智能体的演示 👉 https://huggingface.co/unity
目前我们已经集成
今天的内容就到这里。恭喜你完成本教程!
最好的学习方法是实践和尝试。为什么不尝试另一个环境呢?ML-Agents 有 18 个不同的环境,但你也可以创建自己的环境。查看文档并玩得开心!
单元 6 见 🔥,