我正在尝试获得我的准确性,我有这样的代码:
num_correct = 0.0
for inputs, labels in dataloader(
valid_features, valid_labels, batch_size=batch_size, sequence_length=20):
top_val, top_class = torch.exp(output).topk(1)
num_correct += torch.sum(top_class.squeeze() == labels)
#...
print(#...,
"Accuracy: {:.3f}".format(num_correct*1.0 / len(valid_labels) *1.0)它总是打印0.000,所以我决定将原始值打印到num_correct:print(top_class.squeeze(), labels)中
tensor([ 1, 3, 3, ..., 3, 4, 3], device='cuda:0') tensor([ 1, 1, 3, ..., 3, 3, 3], device='cuda:0')
tensor([ 4, 3, 1, ..., 4, 4, 3], device='cuda:0') tensor([ 4, 3, 1, ..., 4, 4, 3], device='cuda:0')
tensor([ 2, 4, 2, ..., 4, 4, 4], device='cuda:0') tensor([ 3, 4, 1, ..., 4, 4, 4], device='cuda:0')
tensor([ 0, 1, 3, ..., 2, 3, 0], device='cuda:0') tensor([ 0, 1, 3, ..., 2, 2, 0], device='cuda:0')这些数据看起来非常准确。所以..。我可以将其提取到numpy并完成,但有一种pytorch方法。
发布于 2021-05-01 02:10:58
这是可行的:
num_correct += torch.sum(torch.eq(top_class.squeeze(), labels)).item()https://stackoverflow.com/questions/67337015
复制相似问题