title: 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
body: <p>Im pretty new to Siamese Neural Networks and recently found <a href="https://innovationincubator.com/siamese-neural-network-with-pytorch-code-example/" rel="nofollow noreferrer">this example</a> and <a href="https://colab.research.google.com/drive/1FsixLon5Zz3_rFA0xIHzc8Tvnnw8FLr8#scrollTo=EDMURKIHPI7E" rel="nofollow noreferrer">Colab notebook</a>.</p>
  
  <p>When running the code i get the following error:    </p>
  
  <blockquote>
    <p>IndexError: invalid index of a 0-dim tensor. Use tensor.item() to
    convert a 0-dim tensor to a Python number</p>
  </blockquote>
  
  <p>on the line:</p>
  
  <pre><code>result=torch.max(res,1)[1][0][0][0].data[0].tolist()
  </code></pre>
  
  <p>I found something about the <code>tensor.item()</code> but i really just don't know how to use it here.</p>
  
  <p>EDIT:</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>Thats the code i get the error on. </p>