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

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

Open EinAeffchen opened 6 years ago

EinAeffchen commented 6 years ago
Training for 2000 epochs...
Traceback (most recent call last):
  File ".\train.py", line 64, in <module>
    loss = train(*random_training_set(args.chunk_len))
  File ".\train.py", line 49, in train
    loss += criterion(output, target[c])
  File "C:\Users\leond\AppData\Local\Continuum\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\leond\AppData\Local\Continuum\Anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 759, in forward
    self.ignore_index, self.reduce)
  File "C:\Users\leond\AppData\Local\Continuum\Anaconda3\lib\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 "C:\Users\leond\AppData\Local\Continuum\Anaconda3\lib\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

Just tried to run train.py with the shakespeare.txt without changing anything

joistick11 commented 6 years ago

This is because of pytorch version

BlindElephants commented 6 years ago

I'm also experiencing this. Any help or work around?

Thanks

BlindElephants commented 6 years ago

@joistick11 What is the proper version of PyTorch that should be run to avoid this problem?

BlindElephants commented 6 years ago

Nvm, corrected it in line based on answers found elsewhere on Github and Stack:

For example in the file: practical-pytorch/char-rnn-generation/train.py, line 49:

loss += criterion(output, target[c])

should be changed to:

loss += criterion(output, target[c].unsqueeze(0))

Although when I run this, I still am getting the following warning: "UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number"

Change line 54 from this:

return loss.data[0] / args.chunk_len

to:

return loss.data.item() / args.chunk_len
joistick11 commented 6 years ago

Version 0.2.0 works fine without any changes.