mirror of
https://github.com/wassname/ray.git
synced 2026-08-14 12:40:23 +08:00
[rllib] Refactor pytorch custom model support (#3634)
This commit is contained in:
@@ -50,7 +50,7 @@ Importance Weighted Actor-Learner Architecture (IMPALA)
|
||||
|
||||
`[paper] <https://arxiv.org/abs/1802.01561>`__
|
||||
`[implementation] <https://github.com/ray-project/ray/blob/master/python/ray/rllib/agents/impala/impala.py>`__
|
||||
In IMPALA, a central learner runs SGD in a tight loop while asynchronously pulling sample batches from many actor processes. RLlib's IMPALA implementation uses DeepMind's reference `V-trace code <https://github.com/deepmind/scalable_agent/blob/master/vtrace.py>`__. Note that we do not provide a deep residual network out of the box, but one can be plugged in as a `custom model <rllib-models.html#custom-models>`__. Multiple learner GPUs and experience replay are also supported.
|
||||
In IMPALA, a central learner runs SGD in a tight loop while asynchronously pulling sample batches from many actor processes. RLlib's IMPALA implementation uses DeepMind's reference `V-trace code <https://github.com/deepmind/scalable_agent/blob/master/vtrace.py>`__. Note that we do not provide a deep residual network out of the box, but one can be plugged in as a `custom model <rllib-models.html#custom-models-tensorflow>`__. Multiple learner GPUs and experience replay are also supported.
|
||||
|
||||
Tuned examples: `PongNoFrameskip-v4 <https://github.com/ray-project/ray/blob/master/python/ray/rllib/tuned_examples/pong-impala.yaml>`__, `vectorized configuration <https://github.com/ray-project/ray/blob/master/python/ray/rllib/tuned_examples/pong-impala-vectorized.yaml>`__, `multi-gpu configuration <https://github.com/ray-project/ray/blob/master/python/ray/rllib/tuned_examples/pong-impala-fast.yaml>`__, `{BeamRider,Breakout,Qbert,SpaceInvaders}NoFrameskip-v4 <https://github.com/ray-project/ray/blob/master/python/ray/rllib/tuned_examples/atari-impala.yaml>`__
|
||||
|
||||
|
||||
@@ -27,10 +27,10 @@ The following is a list of the built-in model hyperparameters:
|
||||
:start-after: __sphinx_doc_begin__
|
||||
:end-before: __sphinx_doc_end__
|
||||
|
||||
Custom Models
|
||||
-------------
|
||||
Custom Models (TensorFlow)
|
||||
--------------------------
|
||||
|
||||
Custom models should subclass the common RLlib `model class <https://github.com/ray-project/ray/blob/master/python/ray/rllib/models/model.py>`__ and override the ``_build_layers_v2`` method. This method takes in a dict of tensor inputs (the observation ``obs``, ``prev_action``, and ``prev_reward``, ``is_training``), and returns a feature layer and float vector of the specified output size. You can also override the ``value_function`` method to implement a custom value branch. A self-supervised loss can be defined via the ``loss`` method. The model can then be registered and used in place of a built-in model:
|
||||
Custom TF models should subclass the common RLlib `model class <https://github.com/ray-project/ray/blob/master/python/ray/rllib/models/model.py>`__ and override the ``_build_layers_v2`` method. This method takes in a dict of tensor inputs (the observation ``obs``, ``prev_action``, and ``prev_reward``, ``is_training``), and returns a feature layer and float vector of the specified output size. You can also override the ``value_function`` method to implement a custom value branch. A self-supervised loss can be defined via the ``loss`` method. The model can then be registered and used in place of a built-in model:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -152,10 +152,61 @@ Batch Normalization
|
||||
|
||||
You can use ``tf.layers.batch_normalization(x, training=input_dict["is_training"])`` to add batch norm layers to your custom model: `code example <https://github.com/ray-project/ray/blob/master/python/ray/rllib/examples/batch_norm_model.py>`__. RLlib will automatically run the update ops for the batch norm layers during optimization (see `tf_policy_graph.py <https://github.com/ray-project/ray/blob/master/python/ray/rllib/evaluation/tf_policy_graph.py>`__ and `multi_gpu_impl.py <https://github.com/ray-project/ray/blob/master/python/ray/rllib/optimizers/multi_gpu_impl.py>`__ for the exact handling of these updates).
|
||||
|
||||
Custom Models (PyTorch)
|
||||
-----------------------
|
||||
|
||||
Similarly, you can create and register custom PyTorch models for use with PyTorch-based algorithms (e.g., A2C, QMIX). See these examples of `fully connected <https://github.com/ray-project/ray/blob/master/python/ray/rllib/models/pytorch/fcnet.py>`__, `convolutional <https://github.com/ray-project/ray/blob/master/python/ray/rllib/models/pytorch/visionnet.py>`__, and `recurrent <https://github.com/ray-project/ray/blob/master/python/ray/rllib/agents/qmix/model.py>`__ torch models.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import ray
|
||||
from ray.rllib.agents import a3c
|
||||
from ray.rllib.models import ModelCatalog
|
||||
from ray.rllib.models.pytorch.model import TorchModel
|
||||
|
||||
class CustomTorchModel(TorchModel):
|
||||
|
||||
def __init__(self, obs_space, num_outputs, options):
|
||||
TorchModel.__init__(self, obs_space, num_outputs, options)
|
||||
... # setup hidden layers
|
||||
|
||||
def _forward(self, input_dict, hidden_state):
|
||||
"""Forward pass for the model.
|
||||
|
||||
Prefer implementing this instead of forward() directly for proper
|
||||
handling of Dict and Tuple observations.
|
||||
|
||||
Arguments:
|
||||
input_dict (dict): Dictionary of tensor inputs, commonly
|
||||
including "obs", "prev_action", "prev_reward", each of shape
|
||||
[BATCH_SIZE, ...].
|
||||
hidden_state (list): List of hidden state tensors, each of shape
|
||||
[BATCH_SIZE, h_size].
|
||||
|
||||
Returns:
|
||||
(outputs, feature_layer, values, state): Tensors of size
|
||||
[BATCH_SIZE, num_outputs], [BATCH_SIZE, desired_feature_size],
|
||||
[BATCH_SIZE], and [len(hidden_state), BATCH_SIZE, h_size].
|
||||
"""
|
||||
obs = input_dict["obs"]
|
||||
...
|
||||
return logits, features, value, hidden_state
|
||||
|
||||
ModelCatalog.register_custom_model("my_model", CustomTorchModel)
|
||||
|
||||
ray.init()
|
||||
agent = a3c.A2CAgent(env="CartPole-v0", config={
|
||||
"use_pytorch": True,
|
||||
"model": {
|
||||
"custom_model": "my_model",
|
||||
"custom_options": {}, # extra options to pass to your model
|
||||
},
|
||||
})
|
||||
|
||||
Custom Preprocessors
|
||||
--------------------
|
||||
|
||||
Similarly, custom preprocessors should subclass the RLlib `preprocessor class <https://github.com/ray-project/ray/blob/master/python/ray/rllib/models/preprocessors.py>`__ and be registered in the model catalog. Note that you can alternatively use `gym wrapper classes <https://github.com/openai/gym/tree/master/gym/wrappers>`__ around your environment instead of preprocessors.
|
||||
Custom preprocessors should subclass the RLlib `preprocessor class <https://github.com/ray-project/ray/blob/master/python/ray/rllib/models/preprocessors.py>`__ and be registered in the model catalog. Note that you can alternatively use `gym wrapper classes <https://github.com/openai/gym/tree/master/gym/wrappers>`__ around your environment instead of preprocessors.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
@@ -76,7 +76,8 @@ Models and Preprocessors
|
||||
------------------------
|
||||
* `RLlib Models and Preprocessors Overview <rllib-models.html>`__
|
||||
* `Built-in Models and Preprocessors <rllib-models.html#built-in-models-and-preprocessors>`__
|
||||
* `Custom Models <rllib-models.html#custom-models>`__
|
||||
* `Custom Models (TensorFlow) <rllib-models.html#custom-models-tensorflow>`__
|
||||
* `Custom Models (PyTorch) <rllib-models.html#custom-models-pytorch>`__
|
||||
* `Custom Preprocessors <rllib-models.html#custom-preprocessors>`__
|
||||
* `Customizing Policy Graphs <rllib-models.html#customizing-policy-graphs>`__
|
||||
* `Variable-length / Parametric Action Spaces <rllib-models.html#variable-length-parametric-action-spaces>`__
|
||||
|
||||
Reference in New Issue
Block a user