统计MNIST(手写数字识别)数字分别有多少个

import torchvision as tv

train_data = tv.datasets.MNIST(root="datasets/",train=True,download=False,transform=None)

targets = train_data.targets
result = {}#字典的键是不重复的
print(targets[0].item())

for e in targets:
    if e.item() in result:
        result[e.item()] += 1
    else:
        result[e.item()] = 1

print(result)

#{5: 5421, 0: 5923, 4: 5842, 1: 6742, 9: 5949, 2: 5958, 3: 6131, 6: 5918, 7: 6265, 8: 5851}

你可能感兴趣的:(python,深度学习,pytorch)