2019-09-26 09:46:40 +00:00
.. _ppo2:
2020-05-05 13:02:35 +00:00
.. automodule :: stable_baselines3.ppo
2019-09-26 09:46:40 +00:00
PPO
===
The `Proximal Policy Optimization <https://arxiv.org/abs/1707.06347> `_ algorithm combines ideas from A2C (having multiple workers)
and TRPO (it uses a trust region to improve the actor).
2022-07-30 10:52:35 +00:00
The main idea is that after an update, the new policy should be not too far from the old policy.
2019-09-26 09:46:40 +00:00
For that, ppo uses clipping to avoid too large update.
.. note ::
PPO contains several modifications from the original algorithm not documented
2022-05-31 16:11:16 +00:00
by OpenAI: advantages are normalized and value function can be also clipped.
2019-09-26 09:46:40 +00:00
Notes
-----
- Original paper: https://arxiv.org/abs/1707.06347
- Clear explanation of PPO on Arxiv Insights channel: https://www.youtube.com/watch?v=5P7I-xPq8u8
- OpenAI blog post: https://blog.openai.com/openai-baselines-ppo/
2020-01-22 16:17:12 +00:00
- Spinning Up guide: https://spinningup.openai.com/en/latest/algorithms/ppo.html
2022-07-12 21:47:53 +00:00
- 37 implementation details blog: https://iclr-blog-track.github.io/2022/03/25/ppo-implementation-details/
2019-09-26 09:46:40 +00:00
Can I use?
----------
2022-05-31 16:11:16 +00:00
.. note ::
A recurrent version of PPO is available in our contrib repo: https://sb3-contrib.readthedocs.io/en/master/modules/ppo_recurrent.html
However we advise users to start with simple frame-stacking as a simpler, faster
and usually competitive alternative, more info in our report: https://wandb.ai/sb3/no-vel-envs/reports/PPO-vs-RecurrentPPO-aka-PPO-LSTM-on-environments-with-masked-velocity--VmlldzoxOTI4NjE4
See also `Procgen paper appendix Fig 11. <https://arxiv.org/abs/1912.01588> `_ .
In practice, you can stack multiple observations using `` VecFrameStack `` .
2019-09-26 09:46:40 +00:00
- Recurrent policies: ❌
- Multi processing: ✔️
- Gym spaces:
============= ====== ===========
Space Action Observation
============= ====== ===========
2020-05-18 12:42:13 +00:00
Discrete ✔️ ✔️
2019-09-26 09:46:40 +00:00
Box ✔️ ✔️
2020-05-18 12:42:13 +00:00
MultiDiscrete ✔️ ✔️
MultiBinary ✔️ ✔️
2021-05-11 10:29:30 +00:00
Dict ❌ ✔️
2019-09-26 09:46:40 +00:00
============= ====== ===========
Example
-------
2021-08-09 13:23:25 +00:00
This example is only to demonstrate the use of the library and its functions, and the trained agents may not solve the environments. Optimized hyperparameters can be found in RL Zoo `repository <https://github.com/DLR-RM/rl-baselines3-zoo> `_ .
2021-09-29 08:16:59 +00:00
Train a PPO agent on `` CartPole-v1 `` using 4 environments.
2019-09-26 09:46:40 +00:00
.. code-block :: python
2020-05-07 08:10:51 +00:00
import gym
2019-09-26 09:46:40 +00:00
2020-07-10 08:38:35 +00:00
from stable_baselines3 import PPO
2020-10-22 09:05:52 +00:00
from stable_baselines3.common.env_util import make_vec_env
2019-09-26 09:46:40 +00:00
2020-05-07 08:10:51 +00:00
# Parallel environments
2021-03-06 13:17:43 +00:00
env = make_vec_env("CartPole-v1", n_envs=4)
2019-09-26 09:46:40 +00:00
2021-03-06 13:17:43 +00:00
model = PPO("MlpPolicy", env, verbose=1)
2020-05-07 08:10:51 +00:00
model.learn(total_timesteps=25000)
model.save("ppo_cartpole")
2019-09-26 09:46:40 +00:00
2020-05-07 08:10:51 +00:00
del model # remove to demonstrate saving and loading
2019-09-26 09:46:40 +00:00
2020-05-07 08:10:51 +00:00
model = PPO.load("ppo_cartpole")
2019-09-26 09:46:40 +00:00
2020-05-07 08:10:51 +00:00
obs = env.reset()
while True:
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
2019-09-26 09:46:40 +00:00
2020-10-28 08:55:16 +00:00
Results
-------
Atari Games
^^^^^^^^^^^
The complete learning curves are available in the `associated PR #110 <https://github.com/DLR-RM/stable-baselines3/pull/110> `_ .
PyBullet Environments
^^^^^^^^^^^^^^^^^^^^^
Results on the PyBullet benchmark (2M steps) using 6 seeds.
The complete learning curves are available in the `associated issue #48 <https://github.com/DLR-RM/stable-baselines3/issues/48> `_ .
.. note ::
Hyperparameters from the `gSDE paper <https://arxiv.org/abs/2005.05719> `_ were used (as they are tuned for PyBullet envs).
*Gaussian* means that the unstructured Gaussian noise is used for exploration,
*gSDE* (generalized State-Dependent Exploration) is used otherwise.
+--------------+--------------+--------------+--------------+-------------+
| Environments | A2C | A2C | PPO | PPO |
+==============+==============+==============+==============+=============+
| | Gaussian | gSDE | Gaussian | gSDE |
+--------------+--------------+--------------+--------------+-------------+
| HalfCheetah | 2003 +/- 54 | 2032 +/- 122 | 1976 +/- 479 | 2826 +/- 45 |
+--------------+--------------+--------------+--------------+-------------+
| Ant | 2286 +/- 72 | 2443 +/- 89 | 2364 +/- 120 | 2782 +/- 76 |
+--------------+--------------+--------------+--------------+-------------+
| Hopper | 1627 +/- 158 | 1561 +/- 220 | 1567 +/- 339 | 2512 +/- 21 |
+--------------+--------------+--------------+--------------+-------------+
| Walker2D | 577 +/- 65 | 839 +/- 56 | 1230 +/- 147 | 2019 +/- 64 |
+--------------+--------------+--------------+--------------+-------------+
How to replicate the results?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Clone the `rl-zoo repo <https://github.com/DLR-RM/rl-baselines3-zoo> `_ :
.. code-block :: bash
git clone https://github.com/DLR-RM/rl-baselines3-zoo
cd rl-baselines3-zoo/
Run the benchmark (replace `` $ENV_ID `` by the envs mentioned above):
.. code-block :: bash
python train.py --algo ppo --env $ENV_ID --eval-episodes 10 --eval-freq 10000
Plot the results (here for PyBullet envs only):
.. code-block :: bash
python scripts/all_plots.py -a ppo -e HalfCheetah Ant Hopper Walker2D -f logs/ -o logs/ppo_results
python scripts/plot_from_file.py -i logs/ppo_results.pkl -latex -l PPO
2019-09-26 09:46:40 +00:00
Parameters
----------
.. autoclass :: PPO
:members:
:inherited-members:
2020-10-22 09:56:43 +00:00
PPO Policies
-------------
.. autoclass :: MlpPolicy
:members:
:inherited-members:
.. autoclass :: stable_baselines3.common.policies.ActorCriticPolicy
:members:
:noindex:
.. autoclass :: CnnPolicy
:members:
.. autoclass :: stable_baselines3.common.policies.ActorCriticCnnPolicy
:members:
:noindex:
2021-05-11 10:29:30 +00:00
.. autoclass :: MultiInputPolicy
:members:
.. autoclass :: stable_baselines3.common.policies.MultiInputActorCriticPolicy
:members:
:noindex: