Nonetype of var.grad

I‘m trying to rewrite the deepfool code in PyTorch0.4, but there are some problems on var.grad. I don’t know why I can’t get the gradients of a Tensor, I’d appreciate if someone could help me.

It is because x is not the leaf anymore. I can only get the grad of pert_image.

Hi,

You can do x.retain_grad() for the gradients to be kept for non-leaf tensors. Doc here.