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

Fix FunctionHook documentation #5188

Merged
merged 6 commits into from
Sep 13, 2018
Merged

Conversation

niboshi
Copy link
Member

@niboshi niboshi commented Aug 6, 2018

Fixed some incorrect/missing information.

@niboshi niboshi changed the title Fix FunctionHook documentation Fix FunctionHook documentation Aug 6, 2018
Copy link
Member

@beam2d beam2d left a 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)
Copy link
Member

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?

Copy link
Member Author

@niboshi niboshi Aug 7, 2018

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?

Copy link
Member

@beam2d beam2d Aug 8, 2018

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?

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

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

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

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.

Copy link
Member Author

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."

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed using "passed as".

Copy link
Member

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".

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed using "passed as".

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

Choose a reason for hiding this comment

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

has been?

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'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.

Copy link
Member Author

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

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

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

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

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

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?

@toslunar toslunar added the cat:document Documentation such as function documentations, comments and tutorials. label Aug 10, 2018
@toslunar toslunar self-assigned this Sep 12, 2018
@toslunar
Copy link
Member

Jenkins, test this please.

@chainer-ci
Copy link
Member

Jenkins CI test (for commit dc0ee72, target branch master) succeeded!

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. More fixes could be done in a new PR.

@toslunar toslunar merged commit d4f99d2 into chainer:master Sep 13, 2018
@toslunar toslunar added this to the v5.0.0rc1 milestone Sep 13, 2018
@toslunar toslunar added the to-be-backported Pull request that should be backported. label Sep 13, 2018
toslunar added a commit to toslunar/chainer that referenced this pull request Sep 13, 2018
Fix `FunctionHook` documentation

Conflicts:
	chainer/function_hook.py
@niboshi niboshi deleted the function-hook-doc branch September 13, 2018 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat:document Documentation such as function documentations, comments and tutorials. to-be-backported Pull request that should be backported.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants