Note
Go to the endto download the full example code.
Learn the Basics ||Quickstart ||Tensors ||Datasets & DataLoaders ||Transforms ||Build Model ||Autograd ||Optimization ||Save & Load Model
Transforms#
Created On: Feb 09, 2021 | Last Updated: Aug 11, 2021 | Last Verified: Not Verified
Data does not always come in its final processed form that is required fortraining machine learning algorithms. We usetransforms to perform somemanipulation of the data and make it suitable for training.
All TorchVision datasets have two parameters -transform to modify the features andtarget_transform to modify the labels - that accept callables containing the transformation logic.Thetorchvision.transforms module offersseveral commonly-used transforms out of the box.
The FashionMNIST features are in PIL Image format, and the labels are integers.For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors.To make these transformations, we useToTensor andLambda.
importtorchfromtorchvisionimportdatasetsfromtorchvision.transformsimportToTensor,Lambdads=datasets.FashionMNIST(root="data",train=True,download=True,transform=ToTensor(),target_transform=Lambda(lambday:torch.zeros(10,dtype=torch.float).scatter_(0,torch.tensor(y),value=1)))
0%| | 0.00/26.4M [00:00<?, ?B/s] 0%| | 65.5k/26.4M [00:00<01:12, 364kB/s] 1%| | 229k/26.4M [00:00<00:38, 684kB/s] 3%|▎ | 885k/26.4M [00:00<00:12, 2.03MB/s] 14%|█▎ | 3.57M/26.4M [00:00<00:03, 7.09MB/s] 36%|███▌ | 9.40M/26.4M [00:00<00:01, 16.2MB/s] 57%|█████▋ | 15.1M/26.4M [00:01<00:00, 25.2MB/s] 69%|██████▉ | 18.3M/26.4M [00:01<00:00, 26.2MB/s] 81%|████████▏ | 21.5M/26.4M [00:01<00:00, 24.4MB/s]100%|██████████| 26.4M/26.4M [00:01<00:00, 19.3MB/s] 0%| | 0.00/29.5k [00:00<?, ?B/s]100%|██████████| 29.5k/29.5k [00:00<00:00, 326kB/s] 0%| | 0.00/4.42M [00:00<?, ?B/s] 1%|▏ | 65.5k/4.42M [00:00<00:11, 363kB/s] 5%|▌ | 229k/4.42M [00:00<00:06, 683kB/s] 20%|██ | 885k/4.42M [00:00<00:01, 2.03MB/s] 81%|████████ | 3.57M/4.42M [00:00<00:00, 7.10MB/s]100%|██████████| 4.42M/4.42M [00:00<00:00, 6.11MB/s] 0%| | 0.00/5.15k [00:00<?, ?B/s]100%|██████████| 5.15k/5.15k [00:00<00:00, 61.5MB/s]
ToTensor()#
ToTensorconverts a PIL image or NumPyndarray into aFloatTensor. and scalesthe image’s pixel intensity values in the range [0., 1.]
Lambda Transforms#
Lambda transforms apply any user-defined lambda function. Here, we define a functionto turn the integer into a one-hot encoded tensor.It first creates a zero tensor of size 10 (the number of labels in our dataset) and callsscatter_ which assigns avalue=1 on the index as given by the labely.
target_transform=Lambda(lambday:torch.zeros(10,dtype=torch.float).scatter_(dim=0,index=torch.tensor(y),value=1))
Further Reading#
Total running time of the script: (0 minutes 4.392 seconds)