chainer.LinkHook

class chainer.LinkHook[source]

Base class of hooks for links.

LinkHook is a callback object that is registered to a Link. Registered link hooks are invoked before and after calling Link.forward() method of each link.

Link hooks that derive from LinkHook may override the following method:

By default, these methods do nothing.

Specifically, when the __call__() method of some link is invoked, forward_preprocess() (resp. forward_postprocess()) of all link hooks registered to this link are called before (resp. after) Link.forward() method of the link.

There are two ways to register LinkHook objects to Link objects.

The first one is to use with statement. Link hooks hooked in this way are registered to all links within with statement and are unregistered at the end of with statement.

Note

Chainer stores the dictionary of registered link hooks as a thread local object. So, link hooks registered are different depending on threads.

The other one is to register directly to a Link object by calling its add_hook() method. Link hooks registered in this way can be removed by delete_hook() method. Contrary to former registration method, link hooks are registered only to the link which add_hook() is called.

Parameters:name (str) – Name of this link hook.

Methods

__enter__()[source]
__exit__(*_)[source]
added(link)[source]

Callback function invoked when the link hook is registered

Parameters:link (Link) – Link object to which the link hook is registered. None if the link hook is registered globally.
deleted(link)[source]

Callback function invoked when the link hook is unregistered

Parameters:link (Link) – Link object to which the link hook is unregistered. None if the link hook had been registered globally.
forward_postprocess(args)[source]

Callback function invoked after a forward call of a link.

Parameters:args

Callback data. It has the following attributes:

  • link (Link)
    Link object.
  • forward_name (str)
    Name of the forward method.
  • args (tuple)
    Non-keyword arguments given to the forward method.
  • kwargs (dict)
    Keyword arguments given to the forward method.
  • out
    Return value of the forward method.
forward_preprocess(args)[source]

Callback function invoked before a forward call of a link.

Parameters:args

Callback data. It has the following attributes:

  • link (Link)
    Link object.
  • forward_name (str)
    Name of the forward method.
  • args (tuple)
    Non-keyword arguments given to the forward method.
  • kwargs (dict)
    Keyword arguments given to the forward method.

Attributes

name = 'LinkHook'