-
Notifications
You must be signed in to change notification settings - Fork 224
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
Add a utility function to draw a computational graph #166
Conversation
since it's not available in Python 2.x.
I checked |
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.
Thanks. I left some comments.
def test_draw_computational_graph(self): | ||
x = chainer.Variable(np.zeros(5)) | ||
y = x ** 2 + chainer.Variable(np.ones(5)) | ||
with tempdir() as d: |
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.
I don't mind just doing d = tempfile.mkdtemp()
that leaves a temp directory.
|
||
All the messages to stdout or stderr are suppressed. | ||
""" | ||
FNULL = open(os.devnull, 'w') |
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.
To avoid ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='w' encoding='UTF-8'>
, use with and 'wb':
with open(os.devnull, 'wb') as FNULL:
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.
On which environment did you see that warning?
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.
I saw python -m unittest tests/misc_tests/test_is_return_code_zero.py
produces
/Users/tos/Documents/chainerrl/tests/misc_tests/test_is_return_code_zero.py:17: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='w' encoding='UTF-8'>
self.assertTrue(chainerrl.misc.is_return_code_zero(['ls']))
/Users/tos/Documents/chainerrl/tests/misc_tests/test_is_return_code_zero.py:19: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='w' encoding='UTF-8'>
['ls --nonexistentoption']))
/Users/tos/Documents/chainerrl/tests/misc_tests/test_is_return_code_zero.py:21: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='w' encoding='UTF-8'>
['nonexistentcommand']))
.
----------------------------------------------------------------------
Ran 1 test in 0.025s
OK
in a venv of python 3.6.2 from brew for macOS.
I've just found neither nosetests
nor pytest
prints the warning.
vs = chainerrl.misc.collect_variables([[v]]) | ||
self.assertEqual(vs, [v]) | ||
|
||
# ActionValue |
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.
Could you test QuadraticActionValue
?
if isinstance(obj, chainer.Variable): | ||
return [obj] | ||
elif isinstance(obj, chainerrl.action_value.ActionValue): | ||
return [obj.greedy_actions, obj.max] |
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.
The choice of variables [obj.greedy_actions, obj.max]
for ActionValue might not be natural to everyone.
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.
I agree. I chose them because 1) they are required variables of action values and 2) they are sufficient to draw a computational graph. Do you have any suggestions?
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.
I would like to add params
property to ActionValue
and to postpone drawing ActionValue
until then.
For example, greedy_actions
and max
of QuadraticActionValue
on an unbounded space are independent of the mat
argument.
I run
qav = chainerrl.action_value.QuadraticActionValue(
chainer.Variable(np.zeros((5, 5), dtype=np.float32)),
chainer.Variable(np.ones((5, 5, 5), dtype=np.float32)),
chainer.Variable(np.zeros((5, 1), dtype=np.float32)),
)
var = chainerrl.misc.collect_variables(qav)
chainerrl.misc.draw_computational_graph(var, 'tmpgraph')
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.
Fair enough. I'll implement ActionValue.params.
I added ActionValue.params and used them instead of |
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.
LGTM
This PR adds
chainer.misc.draw_computational_graph
to help draw computational graphs of RL models as PNG images.I'm not sure if I should draw a graph in every example, but it would be a good habit. For this PR I just add such code only in
examples/gym/train_reinforce_gym.py