-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix FunctionHook
documentation
#5188
Conversation
ff5d09a
to
22cd2a5
Compare
22cd2a5
to
6e0a7e2
Compare
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.
Added some comments.
I also added suggestions on fixing document around this change, but if you feel it's too much for this PR, it's ok to do it in another one.
@@ -59,9 +66,10 @@ class FunctionHook(object): | |||
>>> with chainer.function_hooks.TimerHook() as m: | |||
... _ = model1(x) | |||
... y = model2(x) |
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.
It's not related to the current change, but could you make the above example code more readable by inserting blank lines appropriately?
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.
Hmm, can we insert blank lines in doctest-style code samples?
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.
Yes, if you prefix the blank lines with >>>
or ...
like follows.
"""
>>> class Model(chainer.Chain):
... def __init__(self):
... super(Model, self).__init__()
... with self.init_scope():
... self.l = L.Linear(10, 10)
...
... def __call__(self, x1):
... return F.exp(self.l(x1))
...
>>> model1 = Model()
>>> model2 = Model()
>>>
>>> x = chainer.Variable(np.zeros((1, 10), np.float32))
>>>
>>> with chainer.function_hooks.TimerHook() as m:
... _ = model1(x)
... y = model2(x)
...
>>> model3 = Model()
>>> z = model3(y)
>>>
>>> print('Total time : {}'.format(m.total_time()))
... # doctest:+ELLIPSIS
Total time : ...
"""
By the way, is it necessary to write this example in doctest style?
chainer/function_hook.py
Outdated
@@ -76,15 +84,24 @@ class FunctionHook(object): | |||
as a thread local object. So, function hooks registered | |||
are different depending on threads. | |||
|
|||
If the hook is registered in this way, :meth:`~chainer.FunctionHook.added` |
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.
It takes time for me to notice that this sentence is a continuation of the last paragraph above the long example/note sections. How about moving it to the end of the last paragraph?
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.
Fixed.
chainer/function_hook.py
Outdated
Function hooks registered in this way can be removed by | ||
:meth:`~chainer.Function.delete_hook` method. | ||
:meth:`~chainer.FunctionNode.delete_hook` method. | ||
Contrary to former registration method, function hooks are registered | ||
only to the function which :meth:`~chainer.FunctionNode.add_hook` | ||
is called. |
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.
It's also not related to this change, but I think it's a good chance to fix some expressions:
- The other one is to
register directly
to... ->register it directly
- Contrary to
former registration method
, ... ->the former registration method
- only to the function which add_hook is called. -> only to the function whose add_hook method is called.
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.
Fixed.
chainer/function_hook.py
Outdated
@@ -76,15 +84,24 @@ class FunctionHook(object): | |||
as a thread local object. So, function hooks registered | |||
are different depending on threads. | |||
|
|||
If the hook is registered in this way, :meth:`~chainer.FunctionHook.added` | |||
and :meth:`~chainer.FunctionHook.deleted` are given ``None`` as the | |||
``function`` argument. |
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.
How about rephrasing (matching with the below one):
None is passed to the function argument of added and deleted.
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.
Is "pass to an argument" a correct expression? Maybe "as" is better?
-> "None is passed as the function argument of added and deleted."
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.
Fixed using "passed as".
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.
Ah, yes you are right. I was confused with "pass to function as an argument".
chainer/function_hook.py
Outdated
Contrary to former registration method, function hooks are registered | ||
only to the function which :meth:`~chainer.FunctionNode.add_hook` | ||
is called. | ||
|
||
If the hook is registered in this way, the :class:`~chainer.FunctionNode` | ||
instance is given as the ``function`` argument 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.
Suggestion (similar to the above one): given as -> passed to
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.
Fixed using "passed as".
chainer/function_hook.py
Outdated
@@ -118,7 +136,8 @@ def deleted(self, function=None): | |||
|
|||
Args: | |||
function(~chainer.FunctionNode): Function object to which | |||
the function hook is deleted. | |||
the function hook is deleted. ``None`` if the function hook | |||
had been registered globally. |
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.
has been?
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'm not confident about my expression either, but the hook is unregistered (the state of "being registered" is negative) at the moment of this callback.
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.
According to @hvy, "had been" is okay but "was" is more natural expression. I fixed as such.
@@ -118,7 +136,8 @@ def deleted(self, function=None): | |||
|
|||
Args: | |||
function(~chainer.FunctionNode): Function object to which |
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.
(again, suggestion outside of this change)
to which -> from which
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.
Fixed.
ff43b55
to
095e04b
Compare
095e04b
to
d0a1248
Compare
chainer/function_hook.py
Outdated
@@ -76,14 +84,24 @@ class FunctionHook(object): | |||
as a thread local object. So, function hooks registered | |||
are different depending on threads. | |||
|
|||
The other one is to register directly to | |||
The other one is to register it directly to | |||
:class:`~chainer.FunctionNode` object with |
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.
Nitpick but
with -> with the
chainer/function_hook.py
Outdated
:class:`~chainer.FunctionNode` object with | ||
:meth:`~chainer.Function.add_hook` method. | ||
:meth:`~chainer.FunctionNode.add_hook` method. | ||
Function hooks registered in this way can be removed by |
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.
Nitpick, actually not directly related to this PR...
registered in this way ... by -> registered this way ... by the
only to the function whose :meth:`~chainer.FunctionNode.add_hook` | ||
method is called. | ||
|
||
If the hook is registered globally using ``with`` statement, ``None`` is |
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'd place an "a" before the "with
". What do you think?
Jenkins, test this please. |
Jenkins CI test (for commit dc0ee72, target branch master) succeeded! |
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. More fixes could be done in a new PR.
Fix `FunctionHook` documentation Conflicts: chainer/function_hook.py
Fixed some incorrect/missing information.