How to save the labels and output of CNN?

I have a problem in implementing the CNN.

“for (images,labels) in test_loader” can output the images and labels of each batch-size, but
how can save all the labels and CNN’s output of images from test_loader on whole not just one batch-size.
Thanks a lot!

Could you clarify a bit?

If you can iterate over batches of test data, then you can save them in any way you like: python list, system files, database…?

results = []
for images, labels in test_loader:  # for each batch in test data...
   predictions = my_CNN(images)  # get predictions for this batch
   results.append([images, labels, predictions]) # keep them for further processing

but I assume this is not what you’re after?

According to the way you load the data, there are two Situation

#Situation 1:
trainset = torchvision.datasets.CIFAR10(root=’./data’, train=True, download=True, transform=transform_train)
trainloader =, batch_size=128, shuffle=False, num_workers=2)

img = trainloader.dataset.train_data
labels = trainloader.dataset.train_labels

#Situation 2:
trainset = torchvision.datasets.ImageFolder(root=’./train/train’, transform=transform_train)
trainloader =, batch_size=64, shuffle=True, num_workers=2)

data = trainset.imgs
img =[item[0] for item in data]
labels =[item[1] for item in data]

Thanks so much! I use the python list, and it works.