[learn-the-basics/quickstart#4] Datasets & DataLoaders: Iterating and Visualizing the Dataset: newbie's question about labels_map

Hello,

TL;DR

Instead of tutorials/beginner_source/basics /data_tutorial.py#L77

labels_map = {
    0: "T-Shirt",
    1: "Trouser",
    [...]
    9: "Ankle Boot",
}

can I use vision/torchvision/datasets /mnist.py#L229

    classes = ["T-shirt/top", "Trouser", ... , "Ankle boot"]

?

Context

Thanks to my colleagues, I started to learn from the introductory tutorial:

Intro > Learn the Basics > Quickstart

And at the end of

Quickstart > Working with data

there is a link to

Learn the Basics > Datasets & DataLoaders

Question

In

Datasets & DataLoaders > Loading a Dataset

the snippet defines a labels map: tutorials/beginner_source/basics/data_tutorial.py at 326edb0931085ae3823e7a7c8a8f69d9d32084b3 · pytorch/tutorials · GitHub

[...]
labels_map = {
    0: "T-Shirt",
    1: "Trouser",
    2: "Pullover",
    3: "Dress",
    4: "Coat",
    5: "Sandal",
    6: "Shirt",
    7: "Sneaker",
    8: "Bag",
    9: "Ankle Boot",
}
[...]

However, this map seems to be already defined in class FashionMNIST(MNIST): vision/torchvision/datasets/mnist.py at 85a1dccc8b7d7bf1893fd10e1be14d017480c064 · pytorch/vision · GitHub

    [...]
    classes = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
    [...]

Since I am lazy, I mean, since I may introduce typos in my script, can I use FashionMNIST.classes ?

MWE

Here is a demo (how can I upload a script?):

import torch
from torchvision import datasets
from torchvision.transforms import v2
import matplotlib.pyplot as plt


training_data = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])
)

test_data = datasets.FashionMNIST(
    root="data",
    train=False,
    download=True,
    transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])
)

figure = plt.figure(figsize=(8, 8))
cols, rows = 3, 3
for i in range(1, cols * rows + 1):
    sample_idx = torch.randint(len(training_data), size=(1,)).item()
    img, label = training_data[sample_idx]
    figure.add_subplot(rows, cols, i)
    plt.title(training_data.classes[label])
    plt.axis("off")
    plt.imshow(img.squeeze(), cmap="gray")
plt.show()
$ date
lun. 08 juin 2026 15:27:09 CEST
$ python --version
Python 3.12.3
$ pip show torch
Name: torch
Version: 2.12.0+cpu
[...]
$ python data_tutorial.py

Best regards,

Salomon