I trained a model on 0 and 1 labels now I’m testing it on unlabeled data
labels=[0,1]
for i, images in enumerate(imgset_loader):
images = images.to(device)
net = net.double()
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
print(labels[predicted])
The last line returns an error : TypeError: only integer tensors of a single element can be converted to an index
I want the prediction to show a percentage of how many 0 labels it found on the data, how many 1s, and how many others.
Transform labels
to a tensor via labels = torch.tensor(labels)
and it should work.
mularo
(Mularo)
3
def validation(model,device,data_loader_val):
correct = 0
total = 0
accuracy = 0.0
val_loss = 0.0
with torch.no_grad():
for data in data_loader_val:
images, true_labels = data
images,true_labels = images.to(device), true_labels(device)
#测试数据
output = model(images)
val_loss += neural_funcs.cross_entropy(output,true_labels).item()
pred = output.max(1,keepdim=True)[1] #值,索引
correct += pred.eq(true_labels.view_as(pred)).sum().item()
val_loss /= len(data_loader_val.dataset)
total = len(data_loader_val.dataset)
accuracy = correct/total
我在运行代码是,显示这里出现错误,报错信息是
请问是为什么?怎么可以解决呢?
true_labels
seems to be a tensor, so your code seems to be missing the .to()
operation.