import torch
import torchvision
import torchvision.transforms as transforms
Loading data
= transforms.Compose(
transform
[transforms.ToTensor(),0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
transforms.Normalize((
= 4
batch_size
= torchvision.datasets.CIFAR10(root='./data', train=True,
trainset =True, transform=transform)
download= torch.utils.data.DataLoader(trainset, batch_size=batch_size,
trainloader =True, num_workers=2)
shuffle
= torchvision.datasets.CIFAR10(root='./data', train=False,
testset =True, transform=transform)
download= torch.utils.data.DataLoader(testset, batch_size=batch_size,
testloader =False, num_workers=2)
shuffle
= ('plane', 'car', 'bird', 'cat',
classes 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
Files already downloaded and verified
Files already downloaded and verified
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
= img / 2 + 0.5 # unnormalize
img = img.numpy()
npimg 1, 2, 0)))
plt.imshow(np.transpose(npimg, (
plt.show()
# get some random training images
= iter(trainloader)
dataiter = next(dataiter)
images, labels
# show images
imshow(torchvision.utils.make_grid(images))# print labels
print(' '.join(f'{classes[labels[j]]:5s}' for j in range(batch_size)))
cat truck cat deer
Creating a DataLoader
A DataLoader
is an iterable feeding data to a model. When we train a model, we run it for each element of the DataLoader in a for loop:
for i in data_loader:
<some model>