mirror of
https://github.com/wassname/ray.git
synced 2026-07-25 13:30:52 +08:00
[rllib] Support parallel, parameterized evaluation (#6981)
* eval api * update * sync eval filters * sync fix * docs * update * docs * update * link * nit * doc updates * format
This commit is contained in:
@@ -212,6 +212,10 @@ You can mix supervised losses into any RLlib algorithm through custom models. Fo
|
||||
|
||||
**PyTorch**: There is no explicit API for adding losses to custom torch models. However, you can modify the loss in the policy definition directly. Like for TF models, offline datasets can be incorporated by creating an input reader and calling ``reader.next()`` in the loss forward pass.
|
||||
|
||||
Self-Supervised Model Losses
|
||||
----------------------------
|
||||
|
||||
You can also use the ``custom_loss()`` API to add in self-supervised losses such as VAE reconstruction loss and L2-regularization.
|
||||
|
||||
Variable-length / Parametric Action Spaces
|
||||
------------------------------------------
|
||||
|
||||
@@ -13,7 +13,7 @@ Example: Training on previously saved experiences
|
||||
|
||||
.. note::
|
||||
|
||||
For custom models and enviroments, you'll need to use the `Python API <rllib-training.html#python-api>`__.
|
||||
For custom models and enviroments, you'll need to use the `Python API <rllib-training.html#basic-python-api>`__.
|
||||
|
||||
In this example, we will save batches of experiences generated during online training to disk, and then leverage this saved data to train a policy offline using DQN. First, we run a simple policy gradient algorithm for 100k steps with ``"output": "/tmp/cartpole-out"`` to tell RLlib to write simulation outputs to the ``/tmp/cartpole-out`` directory.
|
||||
|
||||
|
||||
@@ -4,6 +4,9 @@ RLlib Table of Contents
|
||||
Training APIs
|
||||
-------------
|
||||
* `Command-line <rllib-training.html>`__
|
||||
|
||||
- `Evaluating Trained Policies <rllib-training.html#evaluating-trained-policies>`__
|
||||
|
||||
* `Configuration <rllib-training.html#configuration>`__
|
||||
|
||||
- `Specifying Parameters <rllib-training.html#specifying-parameters>`__
|
||||
@@ -14,9 +17,7 @@ Training APIs
|
||||
|
||||
- `Tuned Examples <rllib-training.html#tuned-examples>`__
|
||||
|
||||
* `Python API <rllib-training.html#python-api>`__
|
||||
|
||||
- `Custom Training Workflows <rllib-training.html#custom-training-workflows>`__
|
||||
* `Basic Python API <rllib-training.html#basic-python-api>`__
|
||||
|
||||
- `Computing Actions <rllib-training.html#computing-actions>`__
|
||||
|
||||
@@ -24,10 +25,16 @@ Training APIs
|
||||
|
||||
- `Accessing Model State <rllib-training.html#accessing-model-state>`__
|
||||
|
||||
* `Advanced Python APIs <rllib-training.html#advanced-python-apis>`__
|
||||
|
||||
- `Custom Training Workflows <rllib-training.html#custom-training-workflows>`__
|
||||
|
||||
- `Global Coordination <rllib-training.html#global-coordination>`__
|
||||
|
||||
- `Callbacks and Custom Metrics <rllib-training.html#callbacks-and-custom-metrics>`__
|
||||
|
||||
- `Customized Evaluation During Training <rllib-training.html#customized-evaluation-during-training>`__
|
||||
|
||||
- `Rewriting Trajectories <rllib-training.html#rewriting-trajectories>`__
|
||||
|
||||
- `Curriculum Learning <rllib-training.html#curriculum-learning>`__
|
||||
@@ -64,6 +71,7 @@ Models, Preprocessors, and Action Distributions
|
||||
* `Custom Preprocessors <rllib-models.html#custom-preprocessors>`__
|
||||
* `Custom Action Distributions <rllib-models.html#custom-action-distributions>`__
|
||||
* `Supervised Model Losses <rllib-models.html#supervised-model-losses>`__
|
||||
* `Self-Supervised Model Losses <rllib-models.html#self-supervised-model-losses>`__
|
||||
* `Variable-length / Parametric Action Spaces <rllib-models.html#variable-length-parametric-action-spaces>`__
|
||||
* `Autoregressive Action Distributions <rllib-models.html#autoregressive-action-distributions>`__
|
||||
|
||||
|
||||
@@ -61,6 +61,8 @@ and renders its behavior in the environment specified by ``--env``.
|
||||
|
||||
(Type ``rllib rollout --help`` to see the available evaluation options.)
|
||||
|
||||
For more advanced evaluation functionality, refer to `Customized Evaluation During Training <#customized-evaluation-during-training>`__.
|
||||
|
||||
Configuration
|
||||
-------------
|
||||
|
||||
@@ -107,8 +109,8 @@ You can run these with the ``rllib train`` command as follows:
|
||||
|
||||
rllib train -f /path/to/tuned/example.yaml
|
||||
|
||||
Python API
|
||||
----------
|
||||
Basic Python API
|
||||
----------------
|
||||
|
||||
The Python API provides the needed flexibility for applying RLlib to new problems. You will need to use this API if you wish to use `custom environments, preprocessors, or models <rllib-models.html>`__ with RLlib.
|
||||
|
||||
@@ -177,13 +179,6 @@ Tune will schedule the trials to run in parallel on your Ray cluster:
|
||||
- PPO_CartPole-v0_0_lr=0.01: RUNNING [pid=21940], 16 s, 4013 ts, 22 rew
|
||||
- PPO_CartPole-v0_1_lr=0.001: RUNNING [pid=21942], 27 s, 8111 ts, 54.7 rew
|
||||
|
||||
Custom Training Workflows
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
In the `basic training example <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_env.py>`__, Tune will call ``train()`` on your trainer once per training iteration and report the new training results. Sometimes, it is desirable to have full control over training, but still run inside Tune. Tune supports `custom trainable functions <tune-usage.html#trainable-api>`__ that can be used to implement `custom training workflows (example) <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_train_fn.py>`__.
|
||||
|
||||
For even finer-grained control over training, you can use RLlib's lower-level `building blocks <rllib-concepts.html>`__ directly to implement `fully customized training workflows <https://github.com/ray-project/ray/blob/master/rllib/examples/rollout_worker_custom_workflow.py>`__.
|
||||
|
||||
Computing Actions
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -431,6 +426,16 @@ Similar to accessing policy state, you may want to get a reference to the underl
|
||||
|
||||
This is especially useful when used with `custom model classes <rllib-models.html>`__.
|
||||
|
||||
Advanced Python APIs
|
||||
--------------------
|
||||
|
||||
Custom Training Workflows
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
In the `basic training example <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_env.py>`__, Tune will call ``train()`` on your trainer once per training iteration and report the new training results. Sometimes, it is desirable to have full control over training, but still run inside Tune. Tune supports `custom trainable functions <tune-usage.html#trainable-api>`__ that can be used to implement `custom training workflows (example) <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_train_fn.py>`__.
|
||||
|
||||
For even finer-grained control over training, you can use RLlib's lower-level `building blocks <rllib-concepts.html>`__ directly to implement `fully customized training workflows <https://github.com/ray-project/ray/blob/master/rllib/examples/rollout_worker_custom_workflow.py>`__.
|
||||
|
||||
Global Coordination
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
Sometimes, it is necessary to coordinate between pieces of code that live in different processes managed by RLlib. For example, it can be useful to maintain a global average of a certain variable, or centrally control a hyperparameter used by policies. Ray provides a general way to achieve this through *named actors* (learn more about Ray actors `here <actors.html>`__). As an example, consider maintaining a shared global counter that is incremented by environments and read periodically from your driver program:
|
||||
@@ -515,6 +520,68 @@ Custom metrics can be accessed and visualized like any other training result:
|
||||
|
||||
.. image:: custom_metric.png
|
||||
|
||||
Customized Evaluation During Training
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
RLlib will report online training rewards, however in some cases you may want to compute
|
||||
rewards with different settings (e.g., with exploration turned off, or on a specific set
|
||||
of environment configurations). You can evaluate policies during training by setting one
|
||||
or more of the ``evaluation_interval``, ``evaluation_num_episodes``, ``evaluation_config``,
|
||||
``evaluation_num_workers``, and ``custom_eval_function`` configs
|
||||
(see `trainer.py <https://github.com/ray-project/ray/blob/master/rllib/agents/trainer.py>`__ for further documentation).
|
||||
|
||||
There is an end to end example of how to set up custom online evaluation in `custom_eval.py <https://github.com/ray-project/ray/blob/master/rllib/examples/custom_eval.py>`__. Note that if you only want to eval your policy at the end of training, you can set ``evaluation_interval: N``, where ``N`` is the number of training iterations before stopping.
|
||||
|
||||
Below are some examples of how the custom evaluation metrics are reported nested under the ``evaluation`` key of normal training results:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
------------------------------------------------------------------------
|
||||
Sample output for `python custom_eval.py`
|
||||
------------------------------------------------------------------------
|
||||
|
||||
INFO trainer.py:623 -- Evaluating current policy for 10 episodes.
|
||||
INFO trainer.py:650 -- Running round 0 of parallel evaluation (2/10 episodes)
|
||||
INFO trainer.py:650 -- Running round 1 of parallel evaluation (4/10 episodes)
|
||||
INFO trainer.py:650 -- Running round 2 of parallel evaluation (6/10 episodes)
|
||||
INFO trainer.py:650 -- Running round 3 of parallel evaluation (8/10 episodes)
|
||||
INFO trainer.py:650 -- Running round 4 of parallel evaluation (10/10 episodes)
|
||||
|
||||
Result for PG_SimpleCorridor_2c6b27dc:
|
||||
...
|
||||
evaluation:
|
||||
custom_metrics: {}
|
||||
episode_len_mean: 15.864661654135338
|
||||
episode_reward_max: 1.0
|
||||
episode_reward_mean: 0.49624060150375937
|
||||
episode_reward_min: 0.0
|
||||
episodes_this_iter: 133
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
------------------------------------------------------------------------
|
||||
Sample output for `python custom_eval.py --custom-eval`
|
||||
------------------------------------------------------------------------
|
||||
|
||||
INFO trainer.py:631 -- Running custom eval function <function ...>
|
||||
Update corridor length to 4
|
||||
Update corridor length to 7
|
||||
Custom evaluation round 1
|
||||
Custom evaluation round 2
|
||||
Custom evaluation round 3
|
||||
Custom evaluation round 4
|
||||
|
||||
Result for PG_SimpleCorridor_0de4e686:
|
||||
...
|
||||
evaluation:
|
||||
custom_metrics: {}
|
||||
episode_len_mean: 9.15695067264574
|
||||
episode_reward_max: 1.0
|
||||
episode_reward_mean: 0.9596412556053812
|
||||
episode_reward_min: 0.0
|
||||
episodes_this_iter: 223
|
||||
foo: 1
|
||||
|
||||
Rewriting Trajectories
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
Reference in New Issue
Block a user