I’m not familiar with your code, but some minor issues you should check:


p.detach() won’t do anything, as it’s not the inplace version (p.detach_() would be the inplace method)

Variables are deprecated since PyTorch 0.4 so you can use tensors in newer versions
Are you sure you need retain_graph=True? Sometimes this line is added as a workaround for valid errors, which will not fix the actual error.