Transforms — PyTorch Tutorials 2.7.0+cu126 documentation (original) (raw)

beginner/basics/transforms_tutorial

Run in Google Colab

Colab

Download Notebook

Notebook

View on GitHub

GitHub

Note

Click hereto download the full example code

Learn the Basics ||Quickstart ||Tensors ||Datasets & DataLoaders ||Transforms ||Build Model ||Autograd ||Optimization ||Save & Load Model

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 andtarget_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:13, 361kB/s] 1%| | 229k/26.4M [00:00<00:38, 681kB/s] 4%|3 | 950k/26.4M [00:00<00:11, 2.18MB/s] 14%|#4 | 3.77M/26.4M [00:00<00:03, 7.44MB/s] 38%|###7 | 10.0M/26.4M [00:00<00:00, 17.2MB/s] 53%|#####2 | 14.0M/26.4M [00:01<00:00, 22.1MB/s] 73%|#######3 | 19.4M/26.4M [00:01<00:00, 27.8MB/s] 85%|########4 | 22.4M/26.4M [00:01<00:00, 25.1MB/s] 95%|#########5| 25.1M/26.4M [00:01<00:00, 24.4MB/s] 100%|##########| 26.4M/26.4M [00:01<00:00, 17.9MB/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%|1 | 65.5k/4.42M [00:00<00:12, 360kB/s] 4%|3 | 164k/4.42M [00:00<00:06, 620kB/s] 9%|8 | 393k/4.42M [00:00<00:04, 999kB/s] 31%|###1 | 1.38M/4.42M [00:00<00:00, 3.61MB/s] 69%|######8 | 3.05M/4.42M [00:00<00:00, 6.25MB/s] 100%|##########| 4.42M/4.42M [00:00<00:00, 6.05MB/s]

0%| | 0.00/5.15k [00:00<?, ?B/s] 100%|##########| 5.15k/5.15k [00:00<00:00, 40.1MB/s]

ToTensor()

ToTensorconverts a PIL image or NumPy ndarray into a FloatTensor. and scales the image’s pixel intensity values in the range [0., 1.]

Lambda Transforms

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 callsscatter_ which assigns avalue=1 on the index as given by the label y.