关于torch.size()和torch.size(0)

分类模型测试代码中有以下代码段:

correct = 0
total = 0
# since we're not training, we don't need to calculate the gradients for our outputs
with torch.no_grad():
    for data in testloader:
        images, labels = data
        # calculate outputs by running images through the network
        outputs = net(images)
        # the class with the highest energy is what we choose as prediction
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (
    100 * correct / total))

其中:

dataiter = iter(test_dataloader)
images, labels = dataiter.next()
print(labels)
print(f"label.size()返回: {labels.size()} \n type: {type(labels.size())}")
print(f"label.size(0)返回: {labels.size(0)} \n type: {type(labels.size(0))}")
tensor([3, 8, 8, 0])

>>>label.size()返回: torch.Size([4]) 
>>> type: 
>>>label.size(0)返回: 4 
>>> type: 

你可能感兴趣的:(关于torch.size()和torch.size(0))