How to apply transformation to tensor from compose in pyTorch

Viewed 519

I want to Normalize the MNIST dataset with pyTorch. The code I was given to load the dataset is:

mnist_train = datasets.MNIST(data_dir, download=True, train=True, transform=transforms.ToTensor())

Then I have to calculate the mean and standard deviation:

meanarray = np.zeros(60000)
stdarray = np.zeros(60000)

for i in range(len(mnist_train)):
    meanarray[i] = torch.mean(mnist_train[i][0])
    stdarray[i] = torch.std(mnist_train[i][0])

mean = meanarray
std = stdarray

Then I have given code for the compose:

mnist_transforms = transforms.Compose([transforms.ToTensor(), transforms.Normalize((mean,), (std,))])

But now is my question, How can I apply this transformation to my dataset? I know there is a "forward" function in the Normalize class that should do it. But I dont understand how to call it.

0 Answers
Related