torch.autograd.graph.Node.register_prehook
Tensor
])
->
Tuple
[
Tensor
]
or
None
The hook should not modify its argument, but it can optionally return
a new gradient which will be used in place of grad_outputs.
This function returns a handle with a method handle.remove()
that removes the hook from the module.
a = torch.tensor([0., 0., 0.], requires_grad=True)
b = a.clone()
assert isinstance(b.grad_fn, torch.autograd.graph.Node)
handle = b.grad_fn.register_prehook(lambda gI: (gI[0] * 2,))
b.sum().backward(retain_graph=True)
print(a.grad)
tensor([2., 2., 2.])
handle.remove()
a.grad = None
b.sum().backward(retain_graph=True)
print(a.grad)
tensor([1., 1., 1.])
- Return type:
RemovableHandle