D
D
dBegginer2020-01-20 04:49:59
Neural networks
dBegginer, 2020-01-20 04:49:59

How does nn.NLLLoss work in pyTorch?

loss_object = torch.nn.NLLLoss()
lsoftmax = torch.nn.LogSoftmax(dim=-1)

loss = loss_object(lsoftmax(outputs), targets)

Traceback (most recent call last):
  File "source.py", line 60, in <module>
    loss = loss_object(lsoftmax(outputs), targets)    
  File "/home/m/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/m/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 204, in forward
    return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/m/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1848, in nll_loss
    out_size, target.size()))
ValueError: Expected target size (64, 768), got torch.Size([64, 20])

inputs.shape
torch.Size([64, 20, 768])
targets.shape
torch.Size([64, 20])
Why is shape = (64, 728) expected and not at least the same size as inputs?
How to implement tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True, reduction='none')?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dBegginer, 2020-01-20
@dBegginer

Solution of the problem:

loss_object = torch.nn.CrossEntropyLoss()
loss = loss_object(outputs.view(-1, outputs.size(-1)), targets.contiguous().view(-1))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question