Based on your comments it seems you would like to apply something like a second order gradients, since you want to create grad_fns inside the backward. If that’s the case, enable the gradient calculation via with torch.enable_grad():.