Callbacks

APIs to setup global hooks for functions and solvers.

Function callback

nnabla.callback.set_function_pre_hook(string key, function_pre_hook)

Set function_pre_hook globally with a key as an callback identifier. All callbacks registered through this API will be called just before performing forward / backward of all functions.

Parameters:
  • key (string) – A name of callback which identifies registered callback. This can be used when deleting callback.

  • function_pre_hook (obj) – Callable object to be registered which takes a function as a argument.

Example

from nnabla import set_function_pre_hook

def callback(f):
    print(f)

# callback() is executed right before every function during fwd/bwd
set_function_pre_hook("print_function_name", callback)

loss = graph(...)

# Names of all functions will be printed out in stdout during fwd/bwd
loss.forward()
loss.backward()
nnabla.callback.set_function_post_hook(string key, function_post_hook)

Set function_post_hook globally with a key as an callback identifier. All callbacks registered through this API will be called just after performing forward / backward of all functions.

Parameters:
  • key (string) – A name of callback which identifies registered callback. This can be used when deleting callback.

  • function_pre_hook (obj) – Callable object to be registered which takes a function as a argument.

Example

from nnabla import set_function_post_hook

def callback(f):
    print(f)

# callback() is executed right after every function during fwd/bwd
set_function_post_hook("print_function_name", callback)

loss = graph(...)

# Names of all functions will be printed out in stdout during fwd/bwd
loss.forward()
loss.backward()
nnabla.callback.unset_function_pre_hook(string key)

Unset function_pre_hook which was previously set as global callback through set_function_pre_hook.

Parameters:

key (string) – A name of callback which identifies registered callback. The callback whose name equals to key is deleted.

Example

from nnabla import set_function_pre_hook, unset_function_pre_hook

def callback(f):
    print(f)

# callback() is executed right after every function during fwd/bwd
set_function_pre_hook("print_function_name", callback)

loss = graph(...)

# Names of all functions will be printed out in stdout during fwd/bwd
loss.forward()
loss.backward()

# Unset callback()
unset_function_pre_hook("print_function_name")

# Nothing will be shown.
loss.forward()
loss.backward()
nnabla.callback.unset_function_post_hook(string key)

Unset function_post_hook which was previously set as global callback through set_function_post_hook.

Parameters:

key (string) – A name of callback which identifies registered callback. The callback whose name equals to key is deleted.

Example

from nnabla import set_function_post_hook, unset_function_post_hook

def callback(f):
    print(f)

# callback() is executed right after every function during fwd/bwd
set_function_post_hook("print_function_name", callback)

loss = graph(...)

# Names of all functions will be printed out in stdout during fwd/bwd
loss.forward()
loss.backward()

# Unset callback()
unset_function_post_hook("print_function_name")

# Nothing will be shown.
loss.forward()
loss.backward()

Solver callback

nnabla.callback.set_solver_pre_hook(string key, solver_pre_hook)

Set solver_pre_hook globally with key as a callback identifier. All callbacks registered through this API will be called right before performing solver functions,

e.g. update, weight_decay, clip_grad_by_norm, and so on.

The registerd callbacks are performed sequentially in order of registration before processing each parameter.

Parameters:
  • key (string) – A callback identifier. This can be used when deleting callback.

  • solver_pre_hook (obj) – Callable object which takes no arguments.

nnabla.callback.set_solver_post_hook(string key, solver_post_hook)

Set solver_post_hook globally with key as a callback identifier. All callbacks registered through this API will be called right before performing solver functions,

e.g. update, weight_decay, clip_grad_by_norm, and so on.

The registerd callbacks are performed sequentially in order of registration before processing each parameter.

Parameters:
  • key (string) – A callback identifier. This can be used when deleting callback.

  • solver_pre_hook (obj) – Callable object which takes no arguments.

nnabla.callback.unset_solver_pre_hook(string key)

Unset solver_pre_hook which was previously set as global callback through set_solver_pre_hook.

Parameters:

key (string) – A callback identifier. Delete a callback whose key equals to this.

nnabla.callback.unset_solver_post_hook(string key)

Unset solver_post_hook which was previously set as global callback through set_solver_post_hook.

Parameters:

key (string) – A callback identifier. Delete a callback whose key equals to this.