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 for training machine learning algorithms. We use transforms to perform some manipulation of the data and make it suitable for training.
All TorchVision datasets have two parameters -transform
to modify the features and target_transform
to modify the labels - that accept callables containing the transformation logic. The torchvision.transforms module offers several 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 use ToTensor
and Lambda
.
0%| | 0.00/26.4M [00:00<?, ?B/s] 0%| | 65.5k/26.4M [00:00<01:12, 364kB/s] 1%| | 164k/26.4M [00:00<00:55, 470kB/s] 3%|▎ | 688k/26.4M [00:00<00:16, 1.58MB/s] 11%|█ | 2.82M/26.4M [00:00<00:04, 5.60MB/s] 32%|███▏ | 8.55M/26.4M [00:00<00:01, 15.0MB/s] 55%|█████▌ | 14.5M/26.4M [00:01<00:00, 21.2MB/s] 78%|███████▊ | 20.6M/26.4M [00:01<00:00, 25.1MB/s] 100%|██████████| 26.4M/26.4M [00:01<00:00, 19.4MB/s] 0%| | 0.00/29.5k [00:00<?, ?B/s] 100%|██████████| 29.5k/29.5k [00:00<00:00, 329kB/s] 0%| | 0.00/4.42M [00:00<?, ?B/s] 1%|▏ | 65.5k/4.42M [00:00<00:12, 362kB/s] 5%|▌ | 229k/4.42M [00:00<00:06, 683kB/s] 21%|██▏ | 950k/4.42M [00:00<00:01, 2.19MB/s] 87%|████████▋ | 3.83M/4.42M [00:00<00:00, 7.63MB/s] 100%|██████████| 4.42M/4.42M [00:00<00:00, 6.10MB/s] 0%| | 0.00/5.15k [00:00<?, ?B/s] 100%|██████████| 5.15k/5.15k [00:00<00:00, 47.7MB/s]ToTensor()#
ToTensor converts a PIL image or NumPy ndarray
into a FloatTensor
. and scales the image’s pixel intensity values in the range [0., 1.]
Lambda transforms apply any user-defined lambda function. Here, we define a function to 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 calls scatter_ which assigns a value=1
on the index as given by the label y
.
Total running time of the script: (0 minutes 4.348 seconds)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4