MNIST is a collection of black-and-white images of digits 0–9, each 28 × 28 pixels. Its predefined training and testing splits make it a quick-running dataset for image examples and toy attention problems. section 3.1.1 section 10.0
When loading MNIST with PyTorch:
- Use torchvision.datasets.MNIST with train=True or False to select a split; download=True downloads data if needed. section 3.1.1
- Without a transform, examples are PIL images. transforms.ToTensor() converts them to tensors with values from 0.0 to 1.0. section 3.1.1
- A transformed image has shape (1, 28, 28): one grayscale channel followed by its height and width. The dataset returns an image and its label. section 3.1.1
mnist_data_train = torchvision.datasets.MNIST("./data", train=True,
download=True, transform=transforms.ToTensor())
mnist_data_test = torchvision.datasets.MNIST("./data", train=False,
download=True, transform=transforms.ToTensor())
x_example, y_example = mnist_data_train[0]
print(x_example.shape)
torch.Size([1, 28, 28])
See the torchvision loading code and the printed shape of a transformed example. section 3.1.1
rand_order = torch.randperm(x_example.shape[1] * x_example.shape[2])
x_shuffled = x_example.view(-1)[rand_order].view(x_example.shape)
imshow(x_shuffled[0,:], cmap=’gray’)
[11]: <matplotlib.image.AxesImage at 0x7f6a1fb72cd0>
See a pixel-shuffling example that changes an image while retaining its tensor shape. section 3.1.1




