element 0 of tensors does not have a grad_fn 
❓ Questions and Help
What is your question?
Hi,
I got RuntimeError
element 0 of tensors does not have a grad_fn in TEST phase
Code
def test_step(self, batch, batch_nb):
    imgs, labels = batch
    self.last_imgs = imgs

    # get gradients
    self.classifier.eval()
    copied_input = imgs.clone()
    copied_input.requires_grad_(True)
    output = self.classifier(copied_input)
    loss = self.criterion_ce(output, labels)
    gradients = torch.autograd.grad(loss, copied_input)[0]
    self.last_grad = gradients

def criterion_ce(self, y_hat, y):
    return F.cross_entropy(y_hat, y)

What have you tried?
Of course, the above code snippet works well in the training phase.
However, grad_fn of output and loss is None in the test/val phase (In the training phase, there are AddmmBackward and NllLossBackward, respectively).
How can I get the input tensor gradients in the Test/Val phase?
What's your environment?

OS: Linux
conda