spro / practical-pytorch

Go to https://github.com/pytorch/tutorials - this repo is deprecated and no longer maintained
MIT License
4.51k stars 1.1k forks source link

X entropy loss input dimensions #123

Open rubencart opened 5 years ago

rubencart commented 5 years ago

output gives tensor([[-1.5800e-01, ... 1.1632e-01]], grad_fn=<AddmmBackward>) target gives tensor([10, 29, 14, 73, 96, 55, ..., 94]) target[c] gives tensor(10)

So criterion(output, target[c]) gives RuntimeError: dimension specified as 0 but tensor has no dimensions. See PyTorch doc's about CrossEntropyLoss, it expects inputs of shape: "Input: (N,C) where C = number of classes" and "Target: (N)".

target[c].view([1]) gives tensor([10]), which fixes the error.

return loss.data[0] / args.chunk_len gives IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number.

rubencart commented 5 years ago

Never mind, I see that this PR is equivalent to this PR: https://github.com/spro/practical-pytorch/pull/120