How to find Shape and columns for dataloader

In the below code , I see that we are loading the data into the variable “trainloader” and iterating through the same. In the below example, the code assumes that there are two columns of data , images & labels respectively. How do I check the shape and column headers in the data “trainloader” . Any help is much appreciated.

Download and load the training data

trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

dataiter = iter(trainloader)
images, labels = dataiter.next()
print(type(images))
print(images.shape)
print(labels.shape)

Column headers are not necessary information, as you can name them whatever you wish (in this case, images, labels).

I have more often seen the train loader being iterated as such:

for i, (images, labels) in enumerate(trainloader):
    print(type(images))

In such a case, you could get the number of items returned by the train loader with n_columns = len(trainloader[0]).

1 Like