-
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
Merged
Merged
Changes from 48 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
682ae7e
wip
ericl 846a3a6
cls
ericl a5e1416
re
ericl cfb77be
wip
ericl 7966e63
Merge branch 'fix-classmethod' into v2-refactor
ericl 3c07c29
wip
ericl 332683c
a3c working
ericl 3cea2c9
torch support
ericl d7472e5
pg works
ericl b4a782b
lint
ericl 8738fa3
rm v2
ericl a88957c
consumer id
ericl 370abf0
clean up pg
ericl 6c2bcbb
clean up more
ericl 56429fb
fix python 2.7
ericl 2380c8f
Merge branch 'fix-classmethod' into v2-refactor
ericl f16f8f0
tf session management
ericl 71d78b5
docs
ericl 5ab8723
dqn wip
ericl c6d68ff
fix compile
ericl fa015ff
dqn
ericl e2a41a9
apex runs
ericl 84624fe
up
ericl 3c4a9fd
impotrs
ericl c56dcef
ddpg
ericl 04220bf
quotes
ericl 6f5ef1b
Merge remote-tracking branch 'upstream/master' into v2-refactor
ericl 95a69df
fix tests
ericl c62a236
fix last r
ericl a9090a4
fix tests
ericl a63efae
lint
ericl 19db8bd
pass checkpoint restore
ericl c2b4243
kwar
ericl 0e56fd4
nits
ericl ed0b359
policy graph
ericl 70ea79d
fix yapf
ericl 496946f
com
ericl 53b4e55
class
ericl da02fa9
Merge remote-tracking branch 'upstream/master' into v2-refactor
ericl 3657108
pyt
ericl 1f435f7
update
ericl 6dbd0e8
Merge remote-tracking branch 'upstream/master' into v2-refactor
ericl f910464
test cpe
ericl 5685e32
unit test
ericl f2af5dc
fix ddpg2
ericl ad9a205
args
ericl 1b9b192
faster test
ericl 21cecdd
common
ericl b4ef184
updates
ericl 80577c8
updates
ericl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| 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 | ||
| from ray.rllib.utils.process_rollout import compute_advantages | ||
| 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) | ||
|
|
||
| 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 | ||
| # compute_advantages. | ||
| 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 compute_advantages( | ||
| sample_batch, last_r, self.config["gamma"], self.config["lambda"]) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: space between ray imports and non-ray
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.
Done