- PyTorch or Caffe2: PyTorch
- OS:Ubuntu 16.04
- PyTorch version: master
- How you installed PyTorch (conda, pip, source): source
- Python version: 3.6.5
- CUDA/cuDNN version: 9.0/7.0.5
- GPU models and configuration:GTX1080ti
- GCC version (if compiling from source): 5.4.0
- CMake version: 3.5.1
Code
import torch
import torch.nn as nn
from torch.autograd import Variable
a = torch.LongTensor(3).random_(5)
b = torch.randn(1, 5)
loss = nn.CrossEntropyLoss()
for i in a:
# i.unsqueeze_(dim=0) # Generally, I think this line should be unnecessary.
print(loss(b,i))
I see the following error message:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-76-ad21bbc3c1e6> in <module>()
9 for i in a:
10 # i.unsqueeze_(dim=0) # Generally, I don't want to add this line.
---> 11 print(loss(b,i))
~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
369 result = self._slow_forward(*input, **kwargs)
370 else:
--> 371 result = self.forward(*input, **kwargs)
372 for hook in self._forward_hooks.values():
373 hook_result = hook(self, input, result)
~/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
746 _assert_no_grad(target)
747 return F.cross_entropy(input, target, self.weight, self.size_average,
--> 748 self.ignore_index, self.reduce)
749
750
~/.local/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce)
1451 >>> loss.backward()
1452 """
-> 1453 return nll_loss(log_softmax(input, 1), target, weight, size_average, ignore_index, reduce)
1454
1455
~/.local/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce)
1340 raise ValueError('Expected 2 or more dimensions (got {})'.format(dim))
1341
-> 1342 if input.size(0) != target.size(0):
1343 raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
1344 .format(input.size(0), target.size(0)))
RuntimeError: dimension specified as 0 but tensor has no dimensions
Question
I have to uncomment the following line to make this code work
# i.unsqueeze_(dim=0) # Generally, I think this line should be unnecessary.
but I think it should be unnecessary. Is it a bug?