hunkim / PyTorchZeroToAll

Simple PyTorch Tutorials Zero to ALL!
http://bit.ly/PyTorchZeroAll
3.89k stars 1.2k forks source link

RuntimeError: dimension specified as 0 but tensor has no dimensions #24

Open jiapei100 opened 6 years ago

jiapei100 commented 6 years ago

line 78 of 12_2_hello_rnn.py What's wrong with it? loss += criterion(output, label)

$ python 12_2_hello_rnn.py Model( (rnn): RNN(5, 5, batch_first=True) ) predicted string: lTraceback (most recent call last): File "12_2_hello_rnn.py", line 78, in loss += criterion(output, label) File "/home/jiapei/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in call result = self.forward(*input, **kwargs) File "/home/jiapei/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 759, in forward self.ignore_index, self.reduce) File "/home/jiapei/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1442, in cross_entropy return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce) File "/home/jiapei/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1328, in nll_loss if input.size(0) != target.size(0): RuntimeError: dimension specified as 0 but tensor has no dimensions

wbaik commented 6 years ago

I ran into the same issue, guessing this happened from a recent update in pytorch . @jiapei100 A solution, though I am sure this is not the best way, was adding this before line 78 label = label.unsqueeze(0)

Is this a better solution? Adding this after line 19 @hunkim y_data = [[y] for y in y_data]

lauraliu61b commented 6 years ago

I guess another possible solution is to replace the original label at line 78 with torch.LongTensor([label]).