forked from rll/rllab
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Added asynchronous plotting to TensorFlow #126
Open
jonashen
wants to merge
7
commits into
integration
Choose a base branch
from
tf_plotting
base: integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c07843e
Added async plotting to TensorFlow
jonashen edfc757
Changed Plotter to be a class
jonashen c7bc9d6
Fixed relative imports
jonashen 5483eef
Removed busy waiting of plotter thread, credits to Angel
jonashen 3c26089
Passed sess to plotter
jonashen e0ca0b8
Migrated one-liner function call
jonashen 47a9604
Added Enum, Message namedtuple for better readability
jonashen 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 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 |
---|---|---|
|
@@ -36,4 +36,3 @@ blackbox.zip | |
blackbox | ||
rllab/config_personal.py | ||
*.swp | ||
sandbox |
This file contains 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 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 @@ | ||
from sandbox.rocky.tf.plotter.plotter import Plotter |
This file contains 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,108 @@ | ||
import atexit | ||
from collections import namedtuple | ||
import enum | ||
from enum import Enum | ||
from multiprocessing import Process | ||
import platform | ||
from queue import Empty, Queue | ||
from threading import Thread | ||
|
||
import numpy as np | ||
import tensorflow as tf | ||
|
||
from rllab.sampler.utils import rollout | ||
|
||
__all__ = ["Plotter"] | ||
|
||
|
||
class Op(Enum): | ||
STOP = 0 | ||
UPDATE = 1 | ||
DEMO = 2 | ||
|
||
|
||
Message = namedtuple("Message", ["op", "args", "info"]) | ||
|
||
|
||
class Plotter(object): | ||
def __init__(self, env, policy, sess=None, graph=None): | ||
self.env = env | ||
self.policy = policy | ||
self.sess = tf.get_default_session() if sess is None else sess | ||
self.graph = tf.get_default_graph() if graph is None else graph | ||
self.worker_thread = Thread(target=self._start_worker, daemon=True) | ||
self.queue = Queue() | ||
|
||
def _start_worker(self): | ||
env = None | ||
policy = None | ||
max_length = None | ||
initial_rollout = True | ||
try: | ||
with self.sess.as_default(), self.sess.graph.as_default(): | ||
# Each iteration will process ALL messages currently in the queue | ||
while True: | ||
msgs = {} | ||
# If true, block and yield processor | ||
if initial_rollout: | ||
msg = self.queue.get() | ||
msgs[msg.op] = msg | ||
# Only fetch the last message of each type | ||
while not self.queue.empty(): | ||
msg = self.queue.get() | ||
msgs[msg.op] = msg | ||
else: | ||
# Only fetch the last message of each type | ||
while not self.queue.empty(): | ||
msg = self.queue.get_nowait() | ||
msgs[msg.op] = msg | ||
|
||
if Op.STOP in msgs: | ||
break | ||
if Op.UPDATE in msgs: | ||
env, policy = msgs[Op.UPDATE].args | ||
if Op.DEMO in msgs: | ||
param_values, max_length = msgs[Op.DEMO].args | ||
policy.set_param_values(param_values) | ||
initial_rollout = False | ||
rollout( | ||
env, | ||
policy, | ||
max_path_length=max_length, | ||
animated=True, | ||
speedup=5) | ||
else: | ||
if max_length: | ||
rollout( | ||
env, | ||
policy, | ||
max_path_length=max_length, | ||
animated=True, | ||
speedup=5) | ||
except KeyboardInterrupt: | ||
pass | ||
|
||
def shutdown(self): | ||
if self.worker_thread.is_alive(): | ||
self.queue.put(Message(op=Op.STOP, args=None, info=None)) | ||
self.queue.task_done() | ||
self.queue.join() | ||
self.worker_thread.join() | ||
|
||
def start(self): | ||
if not self.worker_thread.is_alive(): | ||
tf.get_variable_scope().reuse_variables() | ||
self.worker_thread.start() | ||
self.queue.put( | ||
Message(op=Op.UPDATE, args=(self.env, self.policy), info=None)) | ||
self.queue.task_done() | ||
atexit.register(self.shutdown) | ||
|
||
def update_plot(self, policy, max_length=np.inf): | ||
if self.worker_thread.is_alive(): | ||
self.queue.put( | ||
Message( | ||
op=Op.DEMO, | ||
args=(policy.get_param_values(), max_length), | ||
info=None)) | ||
self.queue.task_done() |
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.
Can you add a comment here explaining that the worker processes all messages in the queue per loop, not one message per loop?