Displaying MNIST images

Hi
I have the below code but i don’t know where is wrong that i get error

trainset = torchvision.datasets.MNIST(root ='./data',
                                        download=True,
                                        transform=transforms.Compose(
        [transforms.ToTensor(), transforms.Lambda(lambda x: x * 64)]
    ))
x= trainset[5] 
plt.imshow(x, cmap='gray')

here is the error:

ValueError: only one element tensors can be converted to Python scalars

What you are loading is the train_loader. You can get a batch of images from it using

images, labels = next(iter(train_loader))

The shapes of these are as follows

images.shape
> (torch.Size([128, 1, 28, 28]))

For plotting you need to use the last 2 dimensions (width and height)

plt.imshow(images[0].reshape(28,28), cmap="gray")
5 Likes

thank you …it worked for me
just one question:
in here:

(torch.Size([128, 1, 28, 28]))

what is 128 …is that batch_size?

Yes, it is the batch size.