Inplace-operation in F.binary_cross_entropy() or squeeze() during gradient computation

I am running into the error:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4608, 1]], which is output 0 of AsStridedBackward0, is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

And I’ve narrow it down to one single line of the code:

g_adversarial_loss = F.binary_cross_entropy(d_fake_output.squeeze(), valid)

it seems that there is some inplace operation in the code above, but I can’t find it. How can I solve the Error?

Thank you in advance!

I guess a previous operation might have manipulated a tensor inplace and raises the error in the backward pass. Since you are working with a GAN, have a look at this post and check if you could be hitting the same issue.