Hi,
here you ahve a tutorial Debugging and Visualisation in PyTorch using Hooks 2 which looks nice.
Here you have a tutorial about how to modify the backward function:
Extending PyTorch — PyTorch master documentation