I never solved that particular issue, but I would recommend using a custom autograd function if you want to change things in the backward pass: https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html 21