liveBook · Manning

MNIST dataset

As explained in Inside Deep Learning: Math, Algorithms, Models

A classic handwritten-digit image dataset of 28×28 grayscale examples used here to illustrate image loading and tensor structure.

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

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains loading, splits, image-to-tensor conversion, dimensions, and visual manipulations without detailing dataset internals.
Examples
Shows grayscale digits, repeated or combined channels, and shuffled pixels as image examples.
What you can do after
Apply it via torchvision to load a split and convert PIL images into 0–1 tensors.
How it connects
Connects MNIST to PyTorch, torchvision, image tensors, convolutional neural networks, and attention mechanisms.

Also explained in

Compare how 5 books explain MNIST dataset →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.