import torch
import torch.nn as nn
m = nn.LogSoftmax(dim=1)
loss = nn.NLLLoss()
a=[[2., 0.],
[1., 1.]]
input = torch.tensor(a, requires_grad=True)
target = torch.tensor([1, 1])
output = loss(m(input), target)
output.backward()
print(input.grad)
tensor([[ 0.4404, -0.4404],
[ 0.2500, -0.2500]])
How to get input.grad?What’s the formula？