I am getting the following error

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

in a fairly complex neural network.
Is there any way to debug this kind of errors, to see where is it being thrown?
Using the function register_backward_hook via nn.apply(fn) where fn registers a hook that prints the name of the class during the backward pass, did not take me any further. It looks like the prints happen before the exception is raised, so it looks like the gradients can be computed, but after that the backward pass breaks.