Pytorch: UserWarning: invalid index of a 0-dim tensor

Created on 28 Mar 2018  ·  10Comments  ·  Source: pytorch/pytorch

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.

Most helpful comment

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

All 10 comments

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

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

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

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

/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

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

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

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

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

Follow the instructions in the error message

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bartolsthoorn picture bartolsthoorn  ·  3Comments

a1363901216 picture a1363901216  ·  3Comments

miguelvr picture miguelvr  ·  3Comments

cdluminate picture cdluminate  ·  3Comments

bartvm picture bartvm  ·  3Comments