Search code examples
pythonpytorchgoogle-colaboratorytensorindex-error

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


Im pretty new to Siamese Neural Networks and recently found this example and Colab notebook.

When running the code i get the following error:

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

on the line:

result=torch.max(res,1)[1][0][0][0].data[0].tolist()

I found something about the tensor.item() but i really just don't know how to use it here.

EDIT:

test_dataloader = DataLoader(test_dataset,num_workers=6,batch_size=1,shuffle=True)
accuracy=0
counter=0
correct=0
for i, data in enumerate(test_dataloader,0): 
x0, x1 , label = data
# onehsot applies in the output of 128 dense vectors which is then  converted to 2 dense vectors
output1,output2 = model(x0.to(device),x1.to(device))
res=torch.abs(output1.cuda() - output2.cuda())
label=label[0].tolist()
label=int(label[0])
result=torch.max(res,1)[1][0][0][0].data.item().tolist()
if label == result:
correct=correct+1
counter=counter+1
#   if counter ==20:
#      break

accuracy=(correct/len(test_dataloader))*100
print("Accuracy:{}%".format(accuracy))

Thats the code i get the error on.


Solution

  • What this error message says is that you're trying to index into an array which has just one item in it. For example,

    In [10]: aten = torch.tensor(2)   
    
    In [11]: aten  
    Out[11]: tensor(2)
    
    In [12]: aten[0]
    ---------------------------------------------------------------------------
    IndexError Traceback (most recent call last)
    <ipython-input-12-5c40f6ab046a> in <module>
    ----> 1 aten[0]
    
    IndexError: invalid index of a 0-dim tensor.  Use tensor.item() to convert a 0-dim 
    tensor to a Python number
    

    In the above case, aten is a tensor with just a single number in it. So, using an index (or more) to retrieve that number throws the IndexError.

    The correct way to extract the number(item) out of the tensor is to use tensor.item(), here aten.item() as in:

    In [14]: aten.item()
    Out[14]: 2