practical-pytorch icon indicating copy to clipboard operation
practical-pytorch copied to clipboard

RuntimeError: dimension specified as 0 but tensor has no dimensions

Open EinAeffchen opened this issue 6 years ago • 5 comments

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

EinAeffchen avatar Jun 03 '18 07:06 EinAeffchen

This is because of pytorch version

joistick11 avatar Jun 13 '18 13:06 joistick11

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

Thanks

BlindElephants avatar Jun 16 '18 16:06 BlindElephants

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

BlindElephants avatar Jun 16 '18 16:06 BlindElephants

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

BlindElephants avatar Jun 16 '18 16:06 BlindElephants

Version 0.2.0 works fine without any changes.

joistick11 avatar Jun 16 '18 17:06 joistick11