CIFAR-10 is a labeled natural-image dataset used to make computer-vision classification concrete. In PyTorch, torchvision.datasets.CIFAR10 downloads the data and returns a Dataset containing small RGB images; separate training and validation sets can be loaded by changing the train argument. section 7.1.1 section 7.5
Listing 7.1 Loading the CIFAR-10 data
# In[2]: from torchvision import datasets import os data_path = '../data-unversioned/p1ch7/' os.makedirs(data_path, exist_ok=True) cifar10 = datasets.CIFAR10(data_path, train=True, download=True) #1 cifar10_val = datasets.CIFAR10(data_path, train=False, download=True) #2
Load the training and validation datasets into a local directory, allowing torchvision to download missing files. section 7.1.1
A practical CIFAR-10 preparation workflow includes:
- Convert images to tensors with torchvision transforms. section 7.1.4 section 7.5
- Compute per-channel statistics from the training images, then normalize each RGB channel to zero mean and unit standard deviation. section 7.1.4
- Use Dataset and DataLoader abstractions to sample batches, change batch size, and shuffle training data. section 7.5
Using CIFAR-10 with a convolution
A CIFAR-10 image has shape 3 × 32 × 32. An nn.Conv2d(3, 16, kernel_size=3) layer applies sixteen 3 × 3 filters to its three RGB channels, producing an output shaped 16 × 30 × 30 for one image. section 8.2

