ToTensor converts a PIL image into a PyTorch tensor with values in the possible range 0.0–1.0, making image data ready for model input. For MNIST, the result uses channel-first shape (C, H, W), such as (1, 28, 28) for grayscale images. section 3.1.1
Apply ToTensor while loading MNIST
mnist_train = torchvision.datasets.MNIST("./data", train=True, download=True, transform=transforms.ToTensor()) x_example, y_example = mnist_train[0] print(x_example.shape) # torch.Size([1, 28, 28]) Place ToTensor inside transforms.Compose when combining it with augmentation. Keep the test transform as transforms.ToTensor() so evaluation remains deterministic. section 3.1.1 section 3.6
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])
Shows the complete MNIST dataset configuration and the resulting tensor shape. section 3.1.1
