Failed to use transforms.ToTensor and transforms.Normalize to normalize the MNIST dataset
I used the following code to normalize the MNIST dataset, when I print the first sample, it fails to normalize as the max element is 255, not 1.
train_transform = transforms.Compose([
   transforms.ToTensor(), 
   transforms.Normalize((0.1307,), (0.3081,))])

train_set = torchvision.datasets.MNIST(
   root=data_dir, train=True, download=True, transform=train_transform)

When I check the range of the dataset input images:
print(&quot;min:%f max:%f&quot; %(train_set.data.min(), train_set.data.max()))
output result:min:0.000000 max:255.000000

I was expecting [0, 1] instead, I don't know why that is. Is there something wrong?

The reason why you have a range of [0,255] is that you are accessing the underlying data of the dataset via the data attribute. This means the transforms have not been applied yet to the data.
&gt;&gt;&gt; train_transform = T.Compose([T.ToTensor()])
&gt;&gt;&gt; train_set = torchvision.datasets.MNIST(
       root='.', train=True, download=True, transform=train_transform)

Your access of the data:
&gt;&gt;&gt; f'min:{train_set.data.min()} max:{train_set.data.max()}'
min:0.000000 max:255.000000

You have to access the dataset by its proper interface in order for the transform pipeline to take effect. To make sure you could unroll the entire dataset's inputs into a tensor and look at its range:
&gt;&gt;&gt; x, y = zip(*train_set)
&gt;&gt;&gt; x_ = torch.stack(x)
&gt;&gt;&gt; f'min:{x_.min()} max:{x_.max()}'
min:tensor(0.) max:tensor(1.)

