Created by: JepsonWong
This PR support backward hook for dygraph.
The backward hook will be called every time a gradient will respect to the Variable is computed. The hook shold have the following signature:
- hook(grad) -> Variable or None The hook should not modify its param, but it can optionally return a new gradient which will change the Variable's value.
register_hook
function will return a RemovablePyCallableObject object with method remove
that can remove the hook from the VarBase.
execute_hooks
function will exectute the hook functions from the Variable.
import paddle.fluid as fluid
import numpy as np
def hook1(g):
g = 2 * g
print g
return g
def hook2(g):
g = 4 * g
print g
with fluid.dygraph.guard():
# var's type is VarBase
remove_hook1 = var.register_hook(hook1)
remove_hook2 = var.register_hook(hook2)
remove_hook1.remove()
remove_hook2.remove()