Question in python 60min tutorial - Training a Classifier

Hi I’m an newbie in pytorch
I’m studying torch on this tutoral

import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose(
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root=’./data’, train=True,
download=True, transform=transform)
trainloader =, batch_size=4,
shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root=’./data’, train=False,
download=True, transform=transform)
testloader =, batch_size=4,
shuffle=False, num_workers=2)

classes = (‘plane’, ‘car’, ‘bird’, ‘cat’,
‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’)

import matplotlib.pyplot as plt
import numpy as np

functions to show an image

def imshow(img):
img = img * 0.5 + 0.5 # unnormalize std = 0.5, mean = 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))

get some random training images

dataiter = iter(trainloader)
images, labels =

show images


print labels

print(’ ‘.join(’%5s’ % classes[labels[j]] for j in range(4)))

and here’s my question.
I don’t understand this line.

images, labels =

What I thought was goes to images and labels each which is an same data,
but when I printed out the images and labels right after that line just like below.

print(images, labels)

The output wasn’t same.
images got img file in array and labels got label numbers
and I don’t know why.
How did the image get the image data in arrays and labels get label numbers respectively??

Thank you for your answers

The DataLoader calls into the passed Dataset's __getitem__ method to load and process the next sample.
Have a look at these lines of code to see, how the CIFAR10 datasets loads each sample.

Thank you.
I got it!