Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: iteration over a 0-d tensor #4

Closed
TreyLawrence opened this issue May 1, 2018 · 13 comments
Closed

TypeError: iteration over a 0-d tensor #4

TreyLawrence opened this issue May 1, 2018 · 13 comments

Comments

@TreyLawrence
Copy link

Hello! I'm running into an issue with training the Penn Treeback model:
python main.py --data data/penn --dropouti 0.4 --dropoutl 0.29 --dropouth 0.225 --seed 28 --batch_size 12 --lr 20.0 --epoch 1000 --nhid 960 --nhidlast 620 --emsize 280 --n_experts 15 --save PTB --single_gpu

Traceback (most recent call last):
  File "main.py", line 260, in <module>
    train()
  File "main.py", line 202, in train
    hidden[s_id] = repackage_hidden(hidden[s_id])
  File "/mos/utils.py", line 12, in repackage_hidden
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in <genexpr>
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in repackage_hidden
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in <genexpr>
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in repackage_hidden
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in <genexpr>
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in repackage_hidden
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in <genexpr>
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in repackage_hidden
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in <genexpr>
    return tuple(repackage_hidden(v) for v in h)
  File "/mos/utils.py", line 12, in repackage_hidden
    return tuple(repackage_hidden(v) for v in h)
  File "/usr/local/lib/python3.5/dist-packages/torch/tensor.py", line 360, in __iter__
    raise TypeError('iteration over a 0-d tensor')
TypeError: iteration over a 0-d tensor
@TreyLawrence
Copy link
Author

This is with python3. If I try with python2 I get:

Traceback (most recent call last):
  File "main.py", line 116, in <module>
    corpus = data.Corpus(args.data)
  File "/spell/data.py", line 30, in __init__
    self.train = self.tokenize(os.path.join(path, 'train.txt'))
  File "/spell/data.py", line 38, in tokenize
    with open(path, 'r', encoding='utf-8') as f:
TypeError: 'encoding' is an invalid keyword argument for this function

@zihangdai
Copy link
Owner

What Pytorch version you are using?
print(torch.__version__)

@TreyLawrence
Copy link
Author

Thanks for the quick reply!
I'm using 0.4.0

@kimiyoung
Copy link
Collaborator

You will need pytorch 0.2.0 to run our code and replicate the results.

@guillaume-chevalier
Copy link

I'd like an update is possible

@octavian-ganea
Copy link

I'm also interested. Is it possible to easily fix the code to work with the latest pytorch version ? Thanks!

@badripatro
Copy link

badripatro commented Oct 5, 2018

Hi ,
I am getting same error

File "train/train_GD.py", line 272, in train
ques_hidden = repackage_hidden(ques_hidden, batch_size)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in repackage_hidden
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in repackage_hidden
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in repackage_hidden
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in repackage_hidden
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/gpuserver150/data150/workspace_project/visual_dialog/visDial_pytorch_vae_ale/misc/utils.py", line 22, in repackage_hidden
return tuple(repackage_hidden(v, batch_size) for v in h)
File "/home/badri/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 381, in iter
raise TypeError('iteration over a 0-d tensor')
TypeError: iteration over a 0-d tensor
Exception ignored in: <bound method _DataLoaderIter.del of <torch.utils.data.dataloader._DataLoaderIter object at 0x7f16c0be6ac8>>


I need in pytorch version 0.4 to run my code.

@huu4ontocord
Copy link

Has this been fixed? I think the problem is the function never finds a Variable and it recurses all the way to the single element in a tensor.

@huu4ontocord
Copy link

I think the answer is to use isinstanceof instead of type in repackage_hidden

if isinstance(h, Variable):

@kimiyoung
Copy link
Collaborator

I think this should be fairly easy to fix, but sorry that we don't have time for this at this point. You are welcome to initiate a pull request.

@kimiyoung
Copy link
Collaborator

resolved by #9

@Harrite77
Copy link

Execuse me, I got the same error today, could you tell me any method to solve it? thank you!

@c04s84
Copy link

c04s84 commented Apr 1, 2019

if isinstance(h, Variable):

this solved my problem. thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants