Practical-pytorch: RuntimeError: dimension specified as 0 but tensor has no dimensions

Created on 3 Jun 2018  路  5Comments  路  Source: spro/practical-pytorch

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

Most helpful comment

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

All 5 comments

This is because of pytorch version

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

Thanks

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

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

Version 0.2.0 works fine without any changes.

Was this page helpful?
0 / 5 - 0 ratings