how to get NLLLoss grad?

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?

Hi @laizewei

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 output w.r.t. input (since you ran output.backward()).

Maybe this tutorial could help you gain some intuition about what is happening behind.

Hope this answers your question

Thanks. Now I understand it.