Skip to content
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

Merged
merged 18 commits into from
Dec 1, 2017

Conversation

muupan
Copy link
Member

@muupan muupan commented Nov 9, 2017

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

@muupan muupan changed the title [WIP] Add a utility function to draw a computational graph Add a utility function to draw a computational graph Nov 15, 2017
@toslunar
Copy link
Member

I checked examples/gym/train_reinforce_gym.py runs with and without graphviz.

Copy link
Member

@toslunar toslunar left a 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:
Copy link
Member

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')
Copy link
Member

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:

Copy link
Member Author

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?

Copy link
Member

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
Copy link
Member

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]
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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')
  • and got
    tmpgraph
  • But it should have (5, 5, 5) shaped variable like
    tmpgraph

Copy link
Member Author

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.

@muupan
Copy link
Member Author

muupan commented Nov 30, 2017

I added ActionValue.params and used them instead of .max and .greedy_actions.

Copy link
Member

@toslunar toslunar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@muupan muupan merged commit 386a9c8 into chainer:master Dec 1, 2017
@muupan muupan deleted the draw-graph branch December 1, 2017 05:50
@muupan muupan added this to the v0.3 milestone Dec 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants