-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[rllib] Refactor rllib to have a common sample collection pathway #2149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 40 commits
682ae7e
846a3a6
a5e1416
cfb77be
7966e63
3c07c29
332683c
3cea2c9
d7472e5
b4a782b
8738fa3
a88957c
370abf0
6c2bcbb
56429fb
2380c8f
f16f8f0
71d78b5
5ab8723
c6d68ff
fa015ff
e2a41a9
84624fe
3c4a9fd
c56dcef
04220bf
6f5ef1b
95a69df
c62a236
a9090a4
a63efae
19db8bd
c2b4243
0e56fd4
ed0b359
70ea79d
496946f
53b4e55
da02fa9
3657108
1f435f7
6dbd0e8
f910464
5685e32
f2af5dc
ad9a205
1b9b192
21cecdd
b4ef184
80577c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function | ||
|
|
||
| import tensorflow as tf | ||
| import gym | ||
| from ray.rllib.utils.error import UnsupportedSpaceException | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: space between ray imports and non-ray
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| from ray.rllib.utils.process_rollout import process_rollout | ||
| from ray.rllib.utils.tf_policy_graph import TFPolicyGraph | ||
|
|
||
|
|
||
| class A3CTFPolicyGraph(TFPolicyGraph): | ||
| """The TF policy base class.""" | ||
|
|
||
| def __init__(self, ob_space, action_space, registry, config): | ||
| self.registry = registry | ||
| self.local_steps = 0 | ||
| self.config = config | ||
| self.summarize = config.get("summarize") | ||
|
|
||
| self._setup_graph(ob_space, action_space) | ||
| assert all(hasattr(self, attr) | ||
| for attr in ["vf", "logits", "x", "var_list"]) | ||
| print("Setting up loss") | ||
| self.setup_loss(action_space) | ||
| self.is_training = tf.placeholder_with_default(True, ()) | ||
| self.sess = tf.get_default_session() | ||
|
|
||
| TFPolicyGraph.__init__( | ||
| self, self.sess, obs_input=self.x, | ||
| action_sampler=self.action_dist.sample(), loss=self.loss, | ||
| loss_inputs=self.loss_in, is_training=self.is_training, | ||
| state_inputs=self.state_in, state_outputs=self.state_out) | ||
|
|
||
| # TODO(ekl) move session creation and init to CommonPolicyEvaluator | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't session creation already in CommonPolicyEvaluator?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| self.sess.run(tf.global_variables_initializer()) | ||
|
|
||
| if self.summarize: | ||
| bs = tf.to_float(tf.shape(self.x)[0]) | ||
| tf.summary.scalar("model/policy_graph", self.pi_loss / bs) | ||
| tf.summary.scalar("model/value_loss", self.vf_loss / bs) | ||
| tf.summary.scalar("model/entropy", self.entropy / bs) | ||
| tf.summary.scalar("model/grad_gnorm", tf.global_norm(self._grads)) | ||
| tf.summary.scalar("model/var_gnorm", tf.global_norm(self.var_list)) | ||
| self.summary_op = tf.summary.merge_all() | ||
|
|
||
| def _setup_graph(self, ob_space, ac_space): | ||
| raise NotImplementedError | ||
|
|
||
| def setup_loss(self, action_space): | ||
| if isinstance(action_space, gym.spaces.Box): | ||
| ac_size = action_space.shape[0] | ||
| self.ac = tf.placeholder(tf.float32, [None, ac_size], name="ac") | ||
| elif isinstance(action_space, gym.spaces.Discrete): | ||
| self.ac = tf.placeholder(tf.int64, [None], name="ac") | ||
| else: | ||
| raise UnsupportedSpaceException( | ||
| "Action space {} is not supported for A3C.".format( | ||
| action_space)) | ||
| self.adv = tf.placeholder(tf.float32, [None], name="adv") | ||
| self.r = tf.placeholder(tf.float32, [None], name="r") | ||
|
|
||
| log_prob = self.action_dist.logp(self.ac) | ||
|
|
||
| # The "policy gradients" loss: its derivative is precisely the policy | ||
| # gradient. Notice that self.ac is a placeholder that is provided | ||
| # externally. adv will contain the advantages, as calculated in | ||
| # process_rollout. | ||
| self.pi_loss = - tf.reduce_sum(log_prob * self.adv) | ||
|
|
||
| delta = self.vf - self.r | ||
| self.vf_loss = 0.5 * tf.reduce_sum(tf.square(delta)) | ||
| self.entropy = tf.reduce_sum(self.action_dist.entropy()) | ||
| self.loss = (self.pi_loss + | ||
| self.vf_loss * self.config["vf_loss_coeff"] + | ||
| self.entropy * self.config["entropy_coeff"]) | ||
|
|
||
| def optimizer(self): | ||
| return tf.train.AdamOptimizer(self.config["lr"]) | ||
|
|
||
| def gradients(self, optimizer): | ||
| grads = tf.gradients(self.loss, self.var_list) | ||
| self.grads, _ = tf.clip_by_global_norm(grads, self.config["grad_clip"]) | ||
| clipped_grads = list(zip(self.grads, self.var_list)) | ||
| return clipped_grads | ||
|
|
||
| def extra_compute_grad_fetches(self): | ||
| if self.summarize: | ||
| return {"summary": self.summary_op} | ||
| else: | ||
| return {} | ||
|
|
||
| def postprocess_trajectory(self, sample_batch, other_agent_batches=None): | ||
| completed = sample_batch["dones"][-1] | ||
| if completed: | ||
| last_r = 0.0 | ||
| else: | ||
| next_state = [] | ||
| for i in range(len(self.state_in)): | ||
| next_state.append([sample_batch["state_out_{}".format(i)][-1]]) | ||
| last_r = self.value(sample_batch["new_obs"][-1], *next_state) | ||
| return process_rollout( | ||
| sample_batch, last_r, self.config["gamma"], self.config["lambda"]) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to avoid using mutable objects as default values, perhaps
state=Noneandstate = [] if state is None else stateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done