Hi everyone,
I tried to use torch.distributions.Categorical and do not understand, why these two methods of calculating loss and gradient do not deliver identical results, only losses are equal:
import torch
inp = torch.tensor( [[ 2 / 7, 4. / 7, 1 / 7 ]], requires_grad = True )
for a in range( 3 ):
    action = torch.tensor( [a] )
    m = torch.distributions.Categorical( probs=inp )
    loss = -m.log_prob( action )
    loss.backward()
    print( loss, inp.grad )
    inp.grad.zero_()

for a in range( 3 ):
    action = torch.tensor( [a] )
    loss = torch.nn.NLLLoss()( inp.log(), action )
    loss.backward()
    print( loss, inp.grad )
    inp.grad.zero_()

Output:

tensor([1.2528], grad_fn=) tensor([[-2.5000,  1.0000,  1.0000]])
tensor([0.5596], grad_fn=) tensor([[ 1.0000, -0.7500,  1.0000]])
tensor([1.9459], grad_fn=) tensor([[ 1.0000,  1.0000, -6.0000]])
tensor(1.2528, grad_fn=) tensor([[-3.5000,  0.0000,  0.0000]])
tensor(0.5596, grad_fn=) tensor([[ 0.0000, -1.7500,  0.0000]])
tensor(1.9459, grad_fn=) tensor([[ 0.0000,  0.0000, -7.0000]])

I would prefer to be a fool, or is this a bug? 
Best regards,
Roman