baselines README

The project's README from github.com/openai/baselines. As one markdown file: README.md.

Status: Maintenance (expect bug fixes and minor updates)

Build status

Baselines

OpenAI Baselines is a set of high-quality implementations of reinforcement learning algorithms.

These algorithms will make it easier for the research community to replicate, refine, and identify new ideas, and will create good baselines to build research on top of. Our DQN implementation and its variants are roughly on par with the scores in published papers. We expect they will be used as a base around which new ideas can be added, and as a tool for comparing a new approach against existing ones.

Prerequisites

Baselines requires python3 (>=3.5) with the development headers. You'll also need system packages CMake, OpenMPI and zlib. Those can be installed as follows

Ubuntu

sudo apt-get update && sudo apt-get install cmake libopenmpi-dev python3-dev zlib1g-dev

Mac OS X

Installation of system packages on Mac requires Homebrew. With Homebrew installed, run the following:

brew install cmake openmpi

Virtual environment

From the general python package sanity perspective, it is a good idea to use virtual environments (virtualenvs) to make sure packages from different projects do not interfere with each other. You can install virtualenv (which is itself a pip package) via

pip install virtualenv

Virtualenvs are essentially folders that have copies of python executable and all python packages. To create a virtualenv called venv with python3, one runs

virtualenv /path/to/venv --python=python3

To activate a virtualenv:

. /path/to/venv/bin/activate

More thorough tutorial on virtualenvs and options can be found here

Tensorflow versions

The master branch supports Tensorflow from version 1.4 to 1.14. For Tensorflow 2.0 support, please use tf2 branch.

Installation

MuJoCo

Some of the baselines examples use MuJoCo (multi-joint dynamics in contact) physics simulator, which is proprietary and requires binaries and a license (temporary 30-day license can be obtained from www.mujoco.org). Instructions on setting up MuJoCo can be found here

Testing the installation

All unit tests in baselines can be run using pytest runner:

pip install pytest
pytest

Training models

Most of the algorithms in baselines repo are used as follows:

python -m baselines.run --alg=<name of the algorithm> --env=<environment_id> [additional arguments]

Example 1. PPO with MuJoCo Humanoid

For instance, to train a fully-connected network controlling MuJoCo humanoid using PPO2 for 20M timesteps

python -m baselines.run --alg=ppo2 --env=Humanoid-v2 --network=mlp --num_timesteps=2e7

Note that for mujoco environments fully-connected network is default, so we can omit --network=mlp The hyperparameters for both network and the learning algorithm can be controlled via the command line, for instance:

python -m baselines.run --alg=ppo2 --env=Humanoid-v2 --network=mlp --num_timesteps=2e7 --ent_coef=0.1 --num_hidden=32 --num_layers=3 --value_network=copy

will set entropy coefficient to 0.1, and construct fully connected network with 3 layers with 32 hidden units in each, and create a separate network for value function estimation (so that its parameters are not shared with the policy network, but the structure is the same)

See docstrings in common/models.py for description of network parameters for each type of model, and docstring for baselines/ppo2/ppo2.py/learn() for the description of the ppo2 hyperparameters.

Example 2. DQN on Atari

DQN with Atari is at this point a classics of benchmarks. To run the baselines implementation of DQN on Atari Pong:

python -m baselines.run --alg=deepq --env=PongNoFrameskip-v4 --num_timesteps=1e6

Saving, loading and visualizing models

Saving and loading the model

The algorithms serialization API is not properly unified yet; however, there is a simple method to save / restore trained models. --save_path and --load_path command-line option loads the tensorflow state from a given path before training, and saves it after the training, respectively. Let's imagine you'd like to train ppo2 on Atari Pong, save the model and then later visualize what has it learnt.

python -m baselines.run --alg=ppo2 --env=PongNoFrameskip-v4 --num_timesteps=2e7 --save_path=~/models/pong_20M_ppo2

This should get to the mean reward per episode about 20. To load and visualize the model, we'll do the following - load the model, train it for 0 steps, and then visualize:

python -m baselines.run --alg=ppo2 --env=PongNoFrameskip-v4 --num_timesteps=0 --load_path=~/models/pong_20M_ppo2 --play

NOTE: Mujoco environments require normalization to work properly, so we wrap them with VecNormalize wrapper. Currently, to ensure the models are saved with normalization (so that trained models can be restored and run without further training) the normalization coefficients are saved as tensorflow variables. This can decrease the performance somewhat, so if you require high-throughput steps with Mujoco and do not need saving/restoring the models, it may make sense to use numpy normalization instead. To do that, set 'use_tf=False` in baselines/run.py.

Logging and vizualizing learning curves and other training metrics

By default, all summary data, including progress, standard output, is saved to a unique directory in a temp folder, specified by a call to Python's tempfile.gettempdir(). The directory can be changed with the --log_path command-line option.

python -m baselines.run --alg=ppo2 --env=PongNoFrameskip-v4 --num_timesteps=2e7 --save_path=~/models/pong_20M_ppo2 --log_path=~/logs/Pong/

NOTE: Please be aware that the logger will overwrite files of the same name in an existing directory, thus it's recommended that folder names be given a unique timestamp to prevent overwritten logs.

Another way the temp directory can be changed is through the use of the $OPENAI_LOGDIR environment variable.

For examples on how to load and display the training data, see here.

Subpackages

Benchmarks

Results of benchmarks on Mujoco (1M timesteps) and Atari (10M timesteps) are available here for Mujoco and here for Atari respectively. Note that these results may be not on the latest version of the code, particular commit hash with which results were obtained is specified on the benchmarks page.

To cite this repository in publications:

@misc{baselines,
  author = {Dhariwal, Prafulla and Hesse, Christopher and Klimov, Oleg and Nichol, Alex and Plappert, Matthias and Radford, Alec and Schulman, John and Sidor, Szymon and Wu, Yuhuai and Zhokhov, Peter},
  title = {OpenAI Baselines},
  year = {2017},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/openai/baselines}},
}

baselines (openai/baselines)

Searching Database…

16.8K ·GitHub
这是什么?OpenAI Baselines 是一套高质量的强化学习算法实现,目的是让研究人员能够复现已发表的结果、改进想法,并把新方法与可靠的基准进行比较。
它是我导入的库,还是我运行的工具?两者都是:它以 Python 包的形式安装,里面包含各种算法实现,你可以通过 baselines.run 入口加命令行参数来启动训练。
我怎么把它安装好?先克隆仓库,安装你喜欢的 TensorFlow 版本,然后在仓库根目录安装 baselines 包——最好在虚拟环境(virtualenv)里操作,README 会一步步教你。
安装之前我需要准备什么?需要 Python 3.5 或更新版本(带开发头文件),以及 CMake、OpenMPI 和 zlib(macOS 上可通过 Homebrew 安装)。master 分支支持 TensorFlow 1.4-1.14;要用 TensorFlow 2.0 请切换到 tf2 分支。
所有示例环境都能开箱即用吗?不能——MuJoCo 示例需要 MuJoCo 物理仿真器,它是收费软件,需要安装它的二进制文件并获得许可证;可以在 mujoco.org 申请一个 30 天的临时许可证。
这个项目还在积极开发吗?它处于维护模式:主要是修复 bug 和小幅更新,不会有大的新功能。
实现了哪些算法?A2C、ACER、ACKTR、DDPG、DQN、GAIL、HER、PPO2 和 TRPO,每种算法各占一个子包(另外还临时保留了一个已过时的 PPO1)。
我具体怎么训练一个模型,比如在 Atari Pong 上跑 DQN?运行 baselines.run,用命令行参数指定环境、算法和训练步数——比如在 Pong 上跑 DQN,或在 MuJoCo Humanoid 上用 PPO2 训练 20M 步。网络结构和算法超参数也可以通过命令行选项设置。
训练好的模型能保存起来以后回看效果吗?可以——--savepath 会在训练结束后保存 TensorFlow 的状态,--loadpath 用于恢复;以 0 步训练加载就是纯演示运行,在 Pong 上平均每局奖励大约能到 20。
训练日志存在哪里?默认存在 Python 临时文件夹下一个唯一的目录里;可以用 --logpath 或环境变量 $OPENAILOGDIR 更改位置。注意:如果目录里已有同名文件,日志会覆盖它们。
有没有基准分数可以用来对比我的结果?有——官方发布了 MuJoCo(1M 步)和 Atari(10M 步)的基准测试结果,基准页面上注明了对应的 commit hash。
Creator
openai
Size
6.5 MB
Stars
16.8K
Updated
CommitJan 31
Age
9y
Commits · 90 Days