mirror of
https://github.com/wassname/ray.git
synced 2026-07-01 14:53:47 +08:00
[rllib] Fix documentation on custom policies (#4910)
* wip * add docs * lint * todo sections * fix doc
This commit is contained in:
@@ -100,6 +100,8 @@ COMMON_CONFIG = {
|
||||
"clip_actions": True,
|
||||
# Whether to use rllib or deepmind preprocessors by default
|
||||
"preprocessor_pref": "deepmind",
|
||||
# The default learning rate
|
||||
"lr": 0.0001,
|
||||
|
||||
# === Evaluation ===
|
||||
# Evaluate with every `evaluation_interval` training iterations.
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
|
||||
import ray
|
||||
from ray import tune
|
||||
from ray.rllib.agents.trainer_template import build_trainer
|
||||
from ray.rllib.policy.sample_batch import SampleBatch
|
||||
from ray.rllib.policy.tf_policy_template import build_tf_policy
|
||||
from ray.rllib.utils import try_import_tf
|
||||
|
||||
tf = try_import_tf()
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--iters", type=int, default=200)
|
||||
|
||||
|
||||
def policy_gradient_loss(policy, batch_tensors):
|
||||
actions = batch_tensors[SampleBatch.ACTIONS]
|
||||
rewards = batch_tensors[SampleBatch.REWARDS]
|
||||
return -tf.reduce_mean(policy.action_dist.logp(actions) * rewards)
|
||||
|
||||
|
||||
# <class 'ray.rllib.policy.tf_policy_template.MyTFPolicy'>
|
||||
MyTFPolicy = build_tf_policy(
|
||||
name="MyTFPolicy",
|
||||
loss_fn=policy_gradient_loss,
|
||||
)
|
||||
|
||||
# <class 'ray.rllib.agents.trainer_template.MyCustomTrainer'>
|
||||
MyTrainer = build_trainer(
|
||||
name="MyCustomTrainer",
|
||||
default_policy=MyTFPolicy,
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
ray.init()
|
||||
args = parser.parse_args()
|
||||
tune.run(
|
||||
MyTrainer,
|
||||
stop={"training_iteration": args.iters},
|
||||
config={
|
||||
"env": "CartPole-v0",
|
||||
"num_workers": 2,
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
|
||||
import ray
|
||||
from ray import tune
|
||||
from ray.rllib.agents.trainer_template import build_trainer
|
||||
from ray.rllib.policy.sample_batch import SampleBatch
|
||||
from ray.rllib.policy.torch_policy_template import build_torch_policy
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--iters", type=int, default=200)
|
||||
|
||||
|
||||
def policy_gradient_loss(policy, batch_tensors):
|
||||
logits, _, values, _ = policy.model({
|
||||
SampleBatch.CUR_OBS: batch_tensors[SampleBatch.CUR_OBS]
|
||||
}, [])
|
||||
action_dist = policy.dist_class(logits)
|
||||
log_probs = action_dist.logp(batch_tensors[SampleBatch.ACTIONS])
|
||||
return -batch_tensors[SampleBatch.REWARDS].dot(log_probs)
|
||||
|
||||
|
||||
# <class 'ray.rllib.policy.torch_policy_template.MyTorchPolicy'>
|
||||
MyTorchPolicy = build_torch_policy(
|
||||
name="MyTorchPolicy", loss_fn=policy_gradient_loss)
|
||||
|
||||
# <class 'ray.rllib.agents.trainer_template.MyCustomTrainer'>
|
||||
MyTrainer = build_trainer(
|
||||
name="MyCustomTrainer",
|
||||
default_policy=MyTorchPolicy,
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
ray.init()
|
||||
args = parser.parse_args()
|
||||
tune.run(
|
||||
MyTrainer,
|
||||
stop={"training_iteration": args.iters},
|
||||
config={
|
||||
"env": "CartPole-v0",
|
||||
"num_workers": 2,
|
||||
})
|
||||
Reference in New Issue
Block a user