I have some problem with getting the output gradient w.r.t input. It is simple mnist model.
I already add .reuires_grad to input.
but it doesn’t work.
sample_img.grad  is None
    sample_img, sample_label = mnist_test[0]

    sample_img = sample_img.to(device)
    sample_img.requires_grad = True
    prediction = model(sample_img.unsqueeze(dim=0))
    cost = criterion(prediction, torch.tensor([sample_label]).to(device))
    optimizer.zero_grad()
    cost.backward()
    print(sample_label)
    print(sample_img.shape)

    plt.imshow(sample_img.detach().cpu().squeeze(),cmap='gray')
    plt.show()

    print(sample_img.grad)

Thank you!