我对暹罗神经网络很陌生,最近发现了这个例子和Colab 笔记本。
运行代码时出现以下错误:
IndexError:0-dim 张量的无效索引。使用 tensor.item() 将 0-dim 张量转换为 Python 数字
在线上:
result=torch.max(res,1)[1][0][0][0].data[0].tolist()
我发现了一些关于的东西,tensor.item()但我真的不知道如何在这里使用它。
编辑:
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))
那就是我得到错误的代码。