import torch.nn as nn
m = nn.LogSoftmax(dim=1)
loss = nn.NLLLoss()
input = torch.tensor(a, requires_grad=True)
target = torch.tensor([1, 1])
output = loss(m(input), target)
tensor([[ 0.4404, -0.4404],
[ 0.2500, -0.2500]])
How to get input.grad?What’s the formula？
I am not sure if you are asking what input.grad represents or the full formula (which would require to write the full derivation of the network w/ the chain rule).
input.grad gives you the gradients of
input (since you ran
Maybe this tutorial could help you gain some intuition about what is happening behind.
Hope this answers your question
Thanks. Now I understand it.