chainer.function_hooks.PrintHook¶
-
class
chainer.function_hooks.PrintHook(sep=None, end='n', file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, flush=True)[source]¶ Function hook that prints debug information.
This function hook outputs the debug information of input arguments of
forwardandbackwardmethods involved in the hooked functions at preprocessing time (that is, just before each method is called).Unlike simple “debug print” technique, where users insert print functions at every function to be inspected, we can show the information of all functions involved with single
withstatement.Further, this hook enables us to show the information of
backwardmethods without inserting print functions into Chainer’s library code.- Parameters
sep – (deprecated since v4.0.0) Ignored.
end – Character to be added at the end of print function.
file – Output file_like object that that redirect to.
flush – If
True, this hook forcibly flushes the text stream at the end of preprocessing.
Example
The basic usage is to use it with
withstatement.>>> from chainer import function_hooks >>> l = L.Linear(10, 10) >>> x = chainer.Variable(np.zeros((1, 10), np.float32)) >>> with chainer.function_hooks.PrintHook(): ... y = l(x) ... z = F.sum(y) ... z.backward() # doctest:+SKIP
In this example,
PrintHookshows the debug information of forward propagation ofLinearFunction(which is implicitly called byl) andSum(called byF.sum) and backward propagation ofzandy.Methods
-
added(function)[source]¶ Callback function invoked when the function hook is registered
- Parameters
function (FunctionNode) – Function object to which the function hook is added.
Noneif the function hook is registered globally.
-
backward_postprocess(function, in_data, out_grad)[source]¶ Callback function invoked after backward propagation.
- Parameters
function (FunctionNode) – Function object to which the function hook is registered.
in_data (tuple of numpy.ndarray or tuple of cupy.ndarray) – Input of forward propagation.
out_grad (tuple of numpy.ndarray or tuple of cupy.ndarray) – Gradient data of backward propagation.
-
backward_preprocess(function, in_data, out_grad)[source]¶ Callback function invoked before backward propagation.
- Parameters
function (FunctionNode) – Function object to which the function hook is registered.
in_data (tuple of numpy.ndarray or tuple of cupy.ndarray) – Input data of forward propagation.
out_grad (tuple of numpy.ndarray or tuple of cupy.ndarray) – Gradient data of backward propagation.
-
deleted(function)[source]¶ Callback function invoked when the function hook is unregistered
- Parameters
function (FunctionNode) – Function object from which the function hook is deleted.
Noneif the function hook was registered globally.
-
forward_postprocess(function, in_data)[source]¶ Callback function invoked after forward propagation.
- Parameters
function (FunctionNode) – Function object to which the function hook is registered.
in_data (tuple of numpy.ndarray or tuple of cupy.ndarray) – Input data of forward propagation.
-
forward_preprocess(function, in_data)[source]¶ Callback function invoked before forward propagation.
- Parameters
function (FunctionNode) – Function object to which the function hook is registered.
in_data (tuple of numpy.ndarray or tuple of cupy.ndarray) – Input data of forward propagation.
Attributes
-
name= 'PrintHook'¶