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

UserWarning: invalid index of a 0-dim tensor #6061

Closed
ll490187880 opened this issue Mar 28, 2018 · 10 comments
Closed

UserWarning: invalid index of a 0-dim tensor #6061

ll490187880 opened this issue Mar 28, 2018 · 10 comments

Comments

@ll490187880
Copy link

ll490187880 commented Mar 28, 2018

When I installed the newest version of pytorch with source code ,I got the error :

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
train_loss += loss.data[0]
Traceback (most recent call last):......................... 10/2984 Step: 61ms | Tot: 2s170ms | Loss: File "testgn.py", line 267, in
train(epoch)
File "testgn.py", line 199, in train
% (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
RuntimeError: value cannot be converted to type uint8_t without overflow: 264

By the way,the code runs well on the pytorch installed by conda.

@qinduanyinghua
Copy link

I think you need solve the problem of the type of correct, I believe that the type of correct you get is 'byteTensor'.

@qinduanyinghua
Copy link

I'm sorry to hear that, I've had this problem before, I hope my idea can help you.

@vishwakftw
Copy link
Contributor

You might have to change loss.data[0] to loss.item() as indicated in the error message.

@ezyang ezyang closed this as completed Mar 28, 2018
@ezyang
Copy link
Contributor

ezyang commented Mar 28, 2018

Yes, this is a side effect of Variable/Tensor merge. Follow the error message (or cast it explicitly to a float with float(loss)

@biswajitcsecu
Copy link

/home/kerax/usys/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:31: 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
/home/kerax/usys/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:32: 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

Iter: 0, D: 1.351, G:0.6499

@AbdelnasserMostafa
Copy link

I got the same error but fixed it with loss.item() as [vishwakftw] mentioned above. Thank you

@hierarchyJK
Copy link

Thank for [vishwakftw], this is the pytorch'version problem

@Niraj-Lunavat
Copy link

I am getting this error, does anyone knows how to solve it, Thanks in advance!


IndexError Traceback (most recent call last)

in ()
21 save_file_name = 'jigsaw_cifar100_e{}_s{}.pk'.format(n_epochs, sinkhorn_iter)
22 history = train_model(model, criterion, optimizer, train_loader, validation_loader,
---> 23 n_epochs=n_epochs, save_file_name=save_file_name)
24

in train_model(model, criterion, optimizer, train_loader, validation_loader, n_epochs, save_file_name)
38 optimizer.zero_grad()
39 outputs = model(x_in)
---> 40 n_correct_pred += compute_acc(vecmat2perm2x2(outputs), perms, False).data[0]
41 loss = criterion(outputs, y_in)
42 loss.backward()

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

@Niraj-Lunavat
Copy link

I am getting this error, does anyone knows how to solve it, Thanks in advance!


IndexError Traceback (most recent call last)

in ()
21 save_file_name = 'jigsaw_cifar100_e{}_s{}.pk'.format(n_epochs, sinkhorn_iter)
22 history = train_model(model, criterion, optimizer, train_loader, validation_loader,
---> 23 n_epochs=n_epochs, save_file_name=save_file_name)
24

in train_model(model, criterion, optimizer, train_loader, validation_loader, n_epochs, save_file_name)
38 optimizer.zero_grad()
39 outputs = model(x_in)
---> 40 n_correct_pred += compute_acc(vecmat2perm2x2(outputs), perms, False).data[0]
41 loss = criterion(outputs, y_in)
42 loss.backward()

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

@ezyang
Copy link
Contributor

ezyang commented Oct 2, 2019

Follow the instructions in the error message

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

8 participants