GitRead 中文社区
首页趋势排行榜原创文章严选周刊
更多资源
新手训练营
精选专题热门话题GitHub 文档资源精选实战指南

deep-reinforcement-learning

Repo for the Deep Reinforcement Learning Nanodegree program

  • 语言Jupyter Notebook
  • 默认分支master
  • 大小3.4 MB
  • 更新7月9日
  • Issues2
在 GitHub 打开
avatarudacity

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

项目背景与痛点

2017 到 2018 年,强化学习的资料出现了一个明显的断层:理论侧有 Sutton & Barto 的书、David Silver 的课程、OpenAI 的 Spinning Up 文档,但真正能让学习者跑通一个完整训练闭环的代码屈指可数。Gym 里的 CartPole 太玩具化,既没有连续动作空间,也没有稀疏奖励和多智能体博弈;而现实中真正让人头疼的问题,如高维状态、策略崩塌、探索与利用的平衡,几乎都无法在 CartPole 上暴露出来。

Udacity 在 Deep Reinforcement Learning Nanodegree 中选择了一条非常务实的路线:用 Unity ML-Agents 编译好的二进制环境(Banana、Reacher、Tennis)把环境复杂度整体封装掉,学习者只需要安装 unityagents、gym、torch 三个包,就能拿到一个状态维度、动作空间都与工业场景接近的仿真器。仓库本身不是算法库,而是一套渐进式脚手架(scaffolding):从“随机智能体”起步,一步步补上 Q 网络、目标网络、经验回放,最终得到能达标的智能体。它真正解决的问题是“从纸面公式到可运行代码”之间那段落差,并非“如何写一个更好的 DQN”。

核心技术揭秘

这个仓库最值得称道的设计是统一接口约定,而不是任何单篇算法的实现。翻遍 p1_navigation、p2_continuous-control、p3_collab-compet,所有智能体都被限制在五个方法里:act(state)、step(state, action, reward, next_state, done)、save()、load(),外加环境的 reset()/step() 标准 Gym 签名。这条约定让 DQN、DDPG、MADDPG 可以平铺对比,同一段训练循环代码只需替换 Agent 实例就能切换算法。这是教学仓库里少见的架构自觉。

网络结构上,DQN 用两层 64 单元的 MLP 处理 37 维状态、4 个离散动作;DDPG 的 Actor/Critic 则是 (256, 128) 的全连接堆叠,Actor 末端接 tanh 把动作压进 [-1, 1],目标网络用 tau=1e-3 做软更新,探索噪声采用 Ornstein-Uhlenbeck 过程。真正有工程味道的细节在 ReplayBuffer:它没有用 deque 存元组,而是预分配 num_states × buffer_size 的 numpy 数组,按索引循环写入。这一步避免了高频训练中的内存碎片与对象拷贝,是整份教学代码里最“不像作业”的地方。

数据流可以概括为一条闭环:

graph TD A[Notebook 训练循环] --> B[Agent.act<br/>策略前向] B --> C[Unity 环境<br/>Banana / Reacher / Tennis] C --> D[next_state, reward, done] D --> E[ReplayBuffer<br/>预分配 numpy 数组] E --> F[随机采样 mini-batch] F --> G[Critic / Q 网络<br/>计算 TD 目标] G --> H[反向传播<br/>更新在线网络] H --> I[软更新<br/>目标网络] I --> J{"连续 100 局<br/>均分达标?"} J -->|"未达标"| A J -->|"达标"| K[保存 checkpoint.pth]

值得一提的是它把“达标”写成了硬性阈值:Banana 环境要求连续 100 局平均分 ≥ 13,Reacher 要求 ≥ 30,Tennis 要求 ≥ 0.5。这种把评测标准写成固定指标的做法,比“训练到看起来收敛”严谨得多,也让不同实现之间有了可比的基准。

理论密度最高的是 p3_collab-compet 的 MADDPG:每个智能体拥有独立的 Actor,但 Critic 接收所有智能体的联合观测与联合动作,本质上实现了 CTDE(集中式训练、分布式执行)。这是从单智能体到多智能体的关键转折点,也是仓库里唯一需要认真阅读原文才能看懂的部分。

功能亮点与差异

和 OpenAI Spinning Up 相比,本仓库的 tutorial notebooks 大量改编自后者,但补上了 Spinning Up 缺失的部分:交互式 notebook、逐步填空式的引导、matplotlib 实时分数曲线。Spinning Up 适合“读懂”,这里适合“跑通”。

和 Stable-Baselines、RLlib 相比,差别就大了:那些是工程库,抽象、多进程采样、日志、checkpoint 管理一应俱全,代价是源码可读性被层层封装稀释。本仓库恰恰相反,它的全部价值在于可读性:一个文件、三百行、网络结构一眼可见。你想知道 DDPG 的软更新到底改了什么参数,打开就是答案。

真正能拉开差距的还有三点:环境是免编译的预构建二进制,跨平台即插即用;同一接口让 DQN 与 DDPG 的差异集中在 act() 与损失函数上,对比成本极低;所有项目的验证标准固定,可以直接横向比较。

应用场景与落地建议

它适合三类人:刚学完理论需要动手的人、想快速验证某个算法变体的人、准备面试需要复现基线的人。

但它不适合直接进生产,原因很具体:依赖 unityagents 0.4.0(早已停更)、Python 3.6 与旧版 PyTorch,新环境安装极易失败;没有日志系统、没有 checkpoint 版本管理、没有多进程采样,超参数全是硬编码的魔法数字;Unity 环境是闭源二进制,你无法修改奖励函数或状态定义;随机种子处理也不严谨,实验几乎不可复现。

如果要点出可迁移的部分:ReplayBuffer 的预分配写法、Agent 五方法抽象、CTDE 的联合 Critic 输入构造,这三样可以直接借鉴到自有代码里。其余部分,生产中更建议参考 CleanRL(单文件可读)或 TorchRL / RLlib(工程完备)。部署时务必用 conda 或 Docker 锁死版本,否则光是 unityagents 的编译就足以让人放弃。

综合评价

优点:训练闭环完整;算法递进路径清晰(DQN → DDPG → MADDPG);接口统一,横向对比成本低;代码可读性优于任何工程库;评测门槛明确,结果可验证。

缺点:技术栈陈旧且安装脆弱;没有任何单元测试;超参数缺乏解释;环境闭源不可改;教学代码的工程债务明显,不应被误当作可复用组件。

一句话概括:这是一座强化学习的驾驶练习场,路面平整、教练在旁边、每个弯道都标好了线;但真要上路,你得换一辆车。

内容已同步仓库最新状态(最后提交:7月9日)。 编辑团队负责选题与抽样审核,详见 编辑准则。

相关仓库

jackfrued/Python-100-Days avatar
Python-100-Daysjackfrued
7月29日
187.8k
55.7k
724

Python——100天从新手到大师

Jupyter Notebook
396.1 MB
8
详情
microsoft/generative-ai-for-beginners avatar
generative-ai-for-beginnersmicrosoft
4 天前
123.2k
66.2k
18

21 节课,开始使用生成式 AI 构建

aiazurechatgpt+11
Jupyter Notebook
6.3 GB
0
详情
rasbt/LLMs-from-scratch avatar
LLMs-from-scratchrasbt
3 小时前
108.1k
16.7k
1

从零开始,逐步用 PyTorch 实现一个类似 ChatGPT 的大语言模型

aiartificial-intelligenceattention-mechanism+16
Jupyter Notebook
16.7 MB
0
详情
microsoft/ML-For-Beginners avatar
ML-For-Beginnersmicrosoft
2 天前
92.1k
22.9k
27

12周,26节课,52个小测验,面向所有人的经典机器学习

data-scienceeducationmachine-learning+9
Jupyter Notebook
2.2 GB
0
详情
microsoft/ai-agents-for-beginners avatar
ai-agents-for-beginnersmicrosoft
2 天前
79.3k
25.8k
2

开始构建 AI 智能体的 18 节课

agentic-aiagentic-frameworkagentic-rag+8
Jupyter Notebook
3.6 GB
0
详情
openai/openai-cookbook avatar
openai-cookbookopenai
2 小时前
77.0k
13.1k
355

使用 OpenAI API 的示例和指南

chatgptgpt-4openai+1
Jupyter Notebook
976.3 MB
0
详情
CompVis/stable-diffusion avatar
stable-diffusionCompVis
2024年6月18日
73.6k
10.7k
619

一个潜空间文本到图像扩散模型

Jupyter Notebook
42.7 MB
0
详情
microsoft/AI-For-Beginners avatar
AI-For-Beginnersmicrosoft
4 天前
70.7k
13.8k
8

12 周,24 节课,人人可用的 AI!

aiartificial-intelligencecnn+7
Jupyter Notebook
1.3 GB
0
详情

关于

cross-entropyddpgdeep-reinforcement-learningdqndynamic-programminghill-climbingml-agentsneural-networksopenai-gymopenai-gym-solutionsppopytorchpytorch-rlreinforcement-learningreinforcement-learning-algorithmsrl-algorithms
  • MIT License
  • 5.2k Stars
  • 2.3k Forks
  • 173 Watching

简要分析

热度分
9885
价值分
3806
活跃状态
活跃
GitHub

语言占比

Jupyter Notebook
Python
TeX

贡献者

juanudacityjuanudacityalexisbcookalexisbcookSudKulSudKulronny-udacityronny-udacityAdrianTrachteAdrianTrachtedaniel-fudgedaniel-fudge

Deep Reinforcement Learning Nanodegree

Trained Agents

This repository contains material related to Udacity’s Deep Reinforcement Learning Nanodegree program.

Table of Contents

Tutorials

The tutorials lead you through implementing various algorithms in reinforcement learning. All of the code is in PyTorch (v0.4) and Python 3.

  • Dynamic Programming: Implement Dynamic Programming algorithms such as Policy Evaluation, Policy Improvement, Policy Iteration, and Value Iteration.
  • Monte Carlo: Implement Monte Carlo methods for prediction and control.
  • Temporal-Difference: Implement Temporal-Difference methods such as Sarsa, Q-Learning, and Expected Sarsa.
  • Discretization: Learn how to discretize continuous state spaces, and solve the Mountain Car environment.
  • Tile Coding: Implement a method for discretizing continuous state spaces that enables better generalization.
  • Deep Q-Network: Explore how to use a Deep Q-Network (DQN) to navigate a space vehicle without crashing.
  • Robotics: Use a C++ API to train reinforcement learning agents from virtual robotic simulation in 3D. (External link)
  • Hill Climbing: Use hill climbing with adaptive noise scaling to balance a pole on a moving cart.
  • Cross-Entropy Method: Use the cross-entropy method to train a car to navigate a steep hill.
  • REINFORCE: Learn how to use Monte Carlo Policy Gradients to solve a classic control task.
  • Proximal Policy Optimization: Explore how to use Proximal Policy Optimization (PPO) to solve a classic reinforcement learning task. (Coming soon!)
  • Deep Deterministic Policy Gradients: Explore how to use Deep Deterministic Policy Gradients (DDPG) with OpenAI Gym environments.
    • Pendulum: Use OpenAI Gym’s Pendulum environment.
    • BipedalWalker: Use OpenAI Gym’s BipedalWalker environment.
  • Finance: Train an agent to discover optimal trading strategies.

Labs / Projects

The labs and projects can be found below. All of the projects use rich simulation environments from Unity ML-Agents. In the Deep Reinforcement Learning Nanodegree program, you will receive a review of your project. These reviews are meant to give you personalized feedback and to tell you what can be improved in your code.

  • The Taxi Problem: In this lab, you will train a taxi to pick up and drop off passengers.
  • Navigation: In the first project, you will train an agent to collect yellow bananas while avoiding blue bananas.
  • Continuous Control: In the second project, you will train an robotic arm to reach target locations.
  • Collaboration and Competition: In the third project, you will train a pair of agents to play tennis!

Resources

  • Cheatsheet: You are encouraged to use this PDF file to guide your study of reinforcement learning.

OpenAI Gym Benchmarks

Classic Control

  • Acrobot-v1 with Tile Coding and Q-Learning
  • Cartpole-v0 with Hill Climbing | solved in 13 episodes
  • Cartpole-v0 with REINFORCE | solved in 691 episodes
  • MountainCarContinuous-v0 with Cross-Entropy Method | solved in 47 iterations
  • MountainCar-v0 with Uniform-Grid Discretization and Q-Learning | solved in <50000 episodes
  • Pendulum-v0 with Deep Deterministic Policy Gradients (DDPG)

Box2d

  • BipedalWalker-v2 with Deep Deterministic Policy Gradients (DDPG)
  • CarRacing-v0 with Deep Q-Networks (DQN) | Coming soon!
  • LunarLander-v2 with Deep Q-Networks (DQN) | solved in 1504 episodes

Toy Text

  • FrozenLake-v0 with Dynamic Programming
  • Blackjack-v0 with Monte Carlo Methods
  • CliffWalking-v0 with Temporal-Difference Methods

Dependencies

To set up your python environment to run the code in this repository, follow the instructions below.

  1. Create (and activate) a new environment with Python 3.6.

    • Linux or Mac:
    conda create --name drlnd python=3.6
    source activate drlnd
    
    • Windows:
    conda create --name drlnd python=3.6 
    activate drlnd
    
  2. If running in Windows, ensure you have the “Build Tools for Visual Studio 2019” installed from this site. This article may also be very helpful. This was confirmed to work in Windows 10 Home.

  3. Follow the instructions in this repository to perform a minimal install of OpenAI gym.

    • Next, install the classic control environment group by following the instructions here.
    • Then, install the box2d environment group by following the instructions here.
  4. Clone the repository (if you haven’t already!), and navigate to the python/ folder. Then, install several dependencies.

    git clone https://github.com/udacity/deep-reinforcement-learning.git
    cd deep-reinforcement-learning/python
    pip install .
    
  5. Create an IPython kernel for the drlnd environment.

    python -m ipykernel install --user --name drlnd --display-name "drlnd"
    
  6. Before running code in a notebook, change the kernel to match the drlnd environment by using the drop-down Kernel menu.

Kernel

Want to learn more?

Come learn with us in the Deep Reinforcement Learning Nanodegree program at Udacity!

评论

登录 后发表评论
暂无评论
GitRead 中文社区
深度解读 · 趋势洞察 · 价值精选

关于

关于我们联系我们编辑准则收录标准赞赏人员

发现

技术趋势新手训练营资源精选实战指南精选专题热门话题GitHub 文档

条款

隐私政策服务条款网站地图
26.9万收录项目
5.2万中文话题
155精选专题
发现技术价值,连接开源智慧
© 2026 GitRead · GitHub 中文社区 本站与 GitHub, Inc. 无隶属关系;GitHub 是 GitHub, Inc. 的商标。