mirror of
https://github.com/wassname/ray.git
synced 2026-07-13 08:10:21 +08:00
c60ccbad46
* wip * Sat Dec 30 15:07:28 PST 2017 * log video * video doesn't work well * scenario integration * Sat Dec 30 17:30:22 PST 2017 * Sat Dec 30 17:31:05 PST 2017 * Sat Dec 30 17:31:32 PST 2017 * Sat Dec 30 17:32:16 PST 2017 * Sat Dec 30 17:34:11 PST 2017 * Sat Dec 30 17:34:50 PST 2017 * Sat Dec 30 17:35:34 PST 2017 * Sat Dec 30 17:38:49 PST 2017 * Sat Dec 30 17:40:39 PST 2017 * Sat Dec 30 17:43:00 PST 2017 * Sat Dec 30 17:43:04 PST 2017 * Sat Dec 30 17:45:56 PST 2017 * Sat Dec 30 17:46:26 PST 2017 * Sat Dec 30 17:47:02 PST 2017 * Sat Dec 30 17:51:53 PST 2017 * Sat Dec 30 17:52:54 PST 2017 * Sat Dec 30 17:56:43 PST 2017 * Sat Dec 30 18:27:07 PST 2017 * Sat Dec 30 18:27:52 PST 2017 * fix train * Sat Dec 30 18:41:51 PST 2017 * Sat Dec 30 18:54:11 PST 2017 * Sat Dec 30 18:56:22 PST 2017 * Sat Dec 30 19:05:04 PST 2017 * Sat Dec 30 19:05:23 PST 2017 * Sat Dec 30 19:11:53 PST 2017 * Sat Dec 30 19:14:31 PST 2017 * Sat Dec 30 19:16:20 PST 2017 * Sat Dec 30 19:18:05 PST 2017 * Sat Dec 30 19:18:45 PST 2017 * Sat Dec 30 19:22:44 PST 2017 * Sat Dec 30 19:24:41 PST 2017 * Sat Dec 30 19:26:57 PST 2017 * Sat Dec 30 19:40:37 PST 2017 * wip models * reward bonus * test prep * Sun Dec 31 18:45:25 PST 2017 * Sun Dec 31 18:58:28 PST 2017 * Sun Dec 31 18:59:34 PST 2017 * Sun Dec 31 19:03:33 PST 2017 * Sun Dec 31 19:05:05 PST 2017 * Sun Dec 31 19:09:25 PST 2017 * fix train * kill * add tuple preprocessor * Sun Dec 31 20:38:33 PST 2017 * Sun Dec 31 22:51:24 PST 2017 * Sun Dec 31 23:14:13 PST 2017 * Sun Dec 31 23:16:04 PST 2017 * Mon Jan 1 00:08:35 PST 2018 * Mon Jan 1 00:10:48 PST 2018 * Mon Jan 1 01:08:31 PST 2018 * Mon Jan 1 14:45:44 PST 2018 * Mon Jan 1 14:54:56 PST 2018 * Mon Jan 1 17:29:29 PST 2018 * switch to euclidean dists * Mon Jan 1 17:39:27 PST 2018 * Mon Jan 1 17:41:47 PST 2018 * Mon Jan 1 17:44:18 PST 2018 * Mon Jan 1 17:47:09 PST 2018 * Mon Jan 1 20:31:02 PST 2018 * Mon Jan 1 20:39:33 PST 2018 * Mon Jan 1 20:40:55 PST 2018 * Mon Jan 1 20:55:06 PST 2018 * Mon Jan 1 21:05:52 PST 2018 * fix env path * merge richards fix * fix hash * Mon Jan 1 22:04:00 PST 2018 * Mon Jan 1 22:25:29 PST 2018 * Mon Jan 1 22:30:42 PST 2018 * simplified reward function * add framestack * add env configs * simplify speed reward * Tue Jan 2 17:36:15 PST 2018 * Tue Jan 2 17:49:16 PST 2018 * Tue Jan 2 18:10:38 PST 2018 * add lane keeping simple mode * Tue Jan 2 20:25:26 PST 2018 * Tue Jan 2 20:30:30 PST 2018 * Tue Jan 2 20:33:26 PST 2018 * Tue Jan 2 20:41:42 PST 2018 * ppo lane keep * simplify discrete actions * Tue Jan 2 21:41:05 PST 2018 * Tue Jan 2 21:49:03 PST 2018 * Tue Jan 2 22:12:23 PST 2018 * Tue Jan 2 22:14:42 PST 2018 * Tue Jan 2 22:20:59 PST 2018 * Tue Jan 2 22:23:43 PST 2018 * Tue Jan 2 22:26:27 PST 2018 * Tue Jan 2 22:27:20 PST 2018 * Tue Jan 2 22:44:00 PST 2018 * Tue Jan 2 22:57:58 PST 2018 * Tue Jan 2 23:08:51 PST 2018 * Tue Jan 2 23:11:32 PST 2018 * update dqn reward * Thu Jan 4 12:29:40 PST 2018 * Thu Jan 4 12:30:26 PST 2018 * Update train_dqn.py * fix
240 lines
7.6 KiB
ReStructuredText
240 lines
7.6 KiB
ReStructuredText
Ray RLlib: A Scalable Reinforcement Learning Library
|
|
====================================================
|
|
|
|
Ray RLlib is a reinforcement learning library that aims to provide both performance and composability:
|
|
|
|
- Performance
|
|
- High performance algorithm implementions
|
|
- Pluggable distributed RL execution strategies
|
|
|
|
- Composability
|
|
- Integration with the `Ray.tune <tune.html>`__ hyperparam tuning tool
|
|
- Support for multiple frameworks (TensorFlow, PyTorch)
|
|
- Scalable primitives for developing new algorithms
|
|
- Shared models between algorithms
|
|
|
|
You can find the code for RLlib `here on GitHub <https://github.com/ray-project/ray/tree/master/python/ray/rllib>`__, and the NIPS symposium paper `here <https://arxiv.org/abs/1712.09381>`__.
|
|
|
|
RLlib currently provides the following algorithms:
|
|
|
|
- `Proximal Policy Optimization (PPO) <https://arxiv.org/abs/1707.06347>`__ which
|
|
is a proximal variant of `TRPO <https://arxiv.org/abs/1502.05477>`__.
|
|
|
|
- `The Asynchronous Advantage Actor-Critic (A3C) <https://arxiv.org/abs/1602.01783>`__.
|
|
|
|
- `Deep Q Networks (DQN) <https://arxiv.org/abs/1312.5602>`__.
|
|
|
|
- Evolution Strategies, as described in `this
|
|
paper <https://arxiv.org/abs/1703.03864>`__. Our implementation
|
|
is adapted from
|
|
`here <https://github.com/openai/evolution-strategies-starter>`__.
|
|
|
|
These algorithms can be run on any `OpenAI Gym MDP <https://github.com/openai/gym>`__,
|
|
including custom ones written and registered by the user.
|
|
|
|
Installation
|
|
------------
|
|
|
|
RLlib has extra dependencies on top of **ray**:
|
|
|
|
.. code-block:: bash
|
|
|
|
pip install 'ray[rllib]'
|
|
|
|
For usage of PyTorch models, visit the `PyTorch website <http://pytorch.org/>`__
|
|
for instructions on installing PyTorch.
|
|
|
|
Getting Started
|
|
---------------
|
|
|
|
You can train a simple DQN agent with the following command
|
|
|
|
::
|
|
|
|
python ray/python/ray/rllib/train.py --run DQN --env CartPole-v0
|
|
|
|
By default, the results will be logged to a subdirectory of ``~/ray_results``.
|
|
This subdirectory will contain a file ``params.json`` which contains the
|
|
hyperparameters, a file ``result.json`` which contains a training summary
|
|
for each episode and a TensorBoard file that can be used to visualize
|
|
training process with TensorBoard by running
|
|
|
|
::
|
|
|
|
tensorboard --logdir=~/ray_results
|
|
|
|
|
|
The ``train.py`` script has a number of options you can show by running
|
|
|
|
::
|
|
|
|
python ray/python/ray/rllib/train.py --help
|
|
|
|
The most important options are for choosing the environment
|
|
with ``--env`` (any OpenAI gym environment including ones registered by the user
|
|
can be used) and for choosing the algorithm with ``--run``
|
|
(available options are ``PPO``, ``A3C``, ``ES`` and ``DQN``).
|
|
|
|
Specifying Parameters
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Each algorithm has specific hyperparameters that can be set with ``--config`` - see the
|
|
``DEFAULT_CONFIG`` variable in
|
|
`PPO <https://github.com/ray-project/ray/blob/master/python/ray/rllib/ppo/ppo.py>`__,
|
|
`A3C <https://github.com/ray-project/ray/blob/master/python/ray/rllib/a3c/a3c.py>`__,
|
|
`ES <https://github.com/ray-project/ray/blob/master/python/ray/rllib/es/es.py>`__ and
|
|
`DQN <https://github.com/ray-project/ray/blob/master/python/ray/rllib/dqn/dqn.py>`__.
|
|
|
|
In an example below, we train A3C by specifying 8 workers through the config flag.
|
|
::
|
|
|
|
python ray/python/ray/rllib/train.py --env=PongDeterministic-v4 --run=A3C --config '{"num_workers": 8}'
|
|
|
|
Evaluating Trained Agents
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
In order to save checkpoints from which to evaluate agents,
|
|
set ``--checkpoint-freq`` (number of training iterations between checkpoints)
|
|
when running ``train.py``.
|
|
|
|
|
|
You can evaluate a simple DQN agent with the following command
|
|
|
|
::
|
|
|
|
python ray/python/ray/rllib/eval.py \
|
|
/tmp/ray/default/DQN_CartPole-v0_0upjmdgr0/checkpoint-1 \
|
|
--run DQN --env CartPole-v0
|
|
|
|
|
|
By default, the script reconstructs a DQN agent from the checkpoint
|
|
located at ``/tmp/ray/default/DQN_CartPole-v0_0upjmdgr0/checkpoint-1``
|
|
and renders its behavior in the environment specified by ``--env``.
|
|
Checkpoints are be found within the experiment directory,
|
|
specified by ``--local-dir`` and ``--experiment-name`` when running ``train.py``.
|
|
|
|
Tuned Examples
|
|
--------------
|
|
|
|
Some good hyperparameters and settings are available in
|
|
`the repository <https://github.com/ray-project/ray/blob/master/python/ray/rllib/test/tuned_examples.sh>`__
|
|
(some of them are tuned to run on GPUs). If you find better settings or tune
|
|
an algorithm on a different domain, consider submitting a Pull Request!
|
|
|
|
Python User API
|
|
---------------
|
|
|
|
You will be using this part of the API if you run the existing algorithms
|
|
on a new problem. Here is an example how to use it:
|
|
|
|
::
|
|
|
|
import ray
|
|
import ray.rllib.ppo as ppo
|
|
|
|
ray.init()
|
|
|
|
config = ppo.DEFAULT_CONFIG.copy()
|
|
alg = ppo.PPOAgent(config=config, env="CartPole-v1")
|
|
|
|
# Can optionally call alg.restore(path) to load a checkpoint.
|
|
|
|
for i in range(10):
|
|
# Perform one iteration of the algorithm.
|
|
result = alg.train()
|
|
print("result: {}".format(result))
|
|
print("checkpoint saved at path: {}".format(alg.save()))
|
|
|
|
Custom Environments
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
To train against a custom environment, i.e. one not in the gym catalog, you
|
|
can register a function that creates the env to refer to it by name. For example:
|
|
|
|
::
|
|
|
|
import ray
|
|
from ray.tune.registry import register_env
|
|
from ray.rllib import ppo
|
|
|
|
env_creator = lambda env_config: create_my_env()
|
|
env_creator_name = "custom_env"
|
|
register_env(env_creator_name, env_creator)
|
|
|
|
ray.init()
|
|
alg = ppo.PPOAgent(env=env_creator_name)
|
|
|
|
|
|
Custom Models and Preprocessors
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
RLlib includes default neural network models and preprocessors for common gym
|
|
environments, but you can also specify your own as follows. The interfaces for
|
|
custom model and preprocessor classes are documented in the
|
|
`RLlib Developer Guide <rllib-dev.html>`__.
|
|
|
|
::
|
|
|
|
import ray
|
|
from ray.rllib.models import ModelCatalog
|
|
|
|
ModelCatalog.register_custom_preprocessor("my_prep", MyPreprocessorClass)
|
|
ModelCatalog.register_custom_model("my_model", MyModelClass)
|
|
|
|
ray.init()
|
|
alg = ppo.PPOAgent(env="CartPole-v0", config={
|
|
"custom_preprocessor": "my_prep",
|
|
"custom_model": "my_model",
|
|
"custom_options": {}, # extra options to pass to your classes
|
|
})
|
|
|
|
Using RLlib with Ray.tune
|
|
-------------------------
|
|
|
|
All Agents implemented in RLlib support the
|
|
`tune Trainable <tune.html#ray.tune.trainable.Trainable>`__ interface.
|
|
|
|
Here is an example of using the command-line interface with RLlib:
|
|
|
|
::
|
|
|
|
python ray/python/ray/rllib/train.py -f tuned_examples/cartpole-grid-search-example.yaml
|
|
|
|
Here is an example using the Python API. The same config passed to ``Agents`` may be placed
|
|
in the ``config`` section of the experiments.
|
|
|
|
::
|
|
|
|
from ray.tune.tune import run_experiments
|
|
from ray.tune.variant_generator import grid_search
|
|
|
|
|
|
experiment = {
|
|
'cartpole-ppo': {
|
|
'run': 'PPO',
|
|
'env': 'CartPole-v0',
|
|
'resources': {
|
|
'cpu': 2,
|
|
'driver_cpu_limit': 1},
|
|
'stop': {
|
|
'episode_reward_mean': 200,
|
|
'time_total_s': 180
|
|
},
|
|
'config': {
|
|
'num_sgd_iter': grid_search([1, 4]),
|
|
'num_workers': 2,
|
|
'sgd_batchsize': grid_search([128, 256, 512])
|
|
}
|
|
},
|
|
# put additional experiments to run concurrently here
|
|
}
|
|
|
|
run_experiments(experiment)
|
|
|
|
.. _`managing a cluster with parallel ssh`: using-ray-on-a-large-cluster.html
|
|
|
|
Contributing to RLlib
|
|
---------------------
|
|
|
|
See the `RLlib Developer Guide <rllib-dev.html>`__.
|