Hi @albanD,
I faced the same problem while trying to do saliency on my model.

def test(self):
self.model.train()
self.model.dropout.eval()

        for indx, data, target, filename in self.test_loader:

            data, target = data.to(self.device), target.to(self.device)
            predictions = self.model(data.float())
            predictions.mean().backward()
            saliency = predictions.grad.data.abs().squeeze() 
            saliency_list = saliency.detach().cpu().numpy() 
            torch.sigmoid(predictions) 

I get an error at saliency = predictions.grad.data.abs().squeeze() `, when I am trying to access the gradient data.
This is the full error code I get
UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the gradient for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations.   """Entry point for launching an IPython kernel. *** AttributeError: 'NoneType' object has no attribute 'data'
Is there a way of sorting this out?