Note
Click here to download the full example code
Learn the Basics || Quickstart || Tensors || Datasets & DataLoaders || Transforms || Build Model || Autograd || Optimization || Save & Load Model
Transforms¶
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
.
import torch
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda
ds = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor(),
target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/raw/train-images-idx3-ubyte.gz
0%| | 0/26421880 [00:00<?, ?it/s]
0%| | 65536/26421880 [00:00<01:12, 362879.44it/s]
1%| | 163840/26421880 [00:00<00:55, 469084.86it/s]
1%| | 262144/26421880 [00:00<00:52, 502540.82it/s]
1%|1 | 360448/26421880 [00:00<00:50, 518067.04it/s]
2%|1 | 491520/26421880 [00:00<00:43, 589791.29it/s]
2%|2 | 622592/26421880 [00:01<00:40, 635952.28it/s]
3%|2 | 753664/26421880 [00:01<00:38, 665165.70it/s]
3%|3 | 917504/26421880 [00:01<00:34, 740857.44it/s]
4%|4 | 1081344/26421880 [00:01<00:31, 801028.90it/s]
5%|4 | 1277952/26421880 [00:01<00:28, 889913.90it/s]
6%|5 | 1507328/26421880 [00:01<00:24, 997294.74it/s]
7%|6 | 1736704/26421880 [00:02<00:22, 1089220.79it/s]
8%|7 | 1998848/26421880 [00:02<00:20, 1199076.45it/s]
9%|8 | 2293760/26421880 [00:02<00:18, 1319128.11it/s]
10%|9 | 2621440/26421880 [00:02<00:16, 1466096.16it/s]
11%|#1 | 2981888/26421880 [00:02<00:14, 1622352.30it/s]
13%|#2 | 3342336/26421880 [00:03<00:12, 1888465.91it/s]
14%|#3 | 3571712/26421880 [00:03<00:12, 1826812.28it/s]
15%|#5 | 4030464/26421880 [00:03<00:10, 2056985.31it/s]
17%|#7 | 4521984/26421880 [00:03<00:09, 2271647.57it/s]
19%|#9 | 5046272/26421880 [00:03<00:07, 2696807.24it/s]
20%|## | 5341184/26421880 [00:03<00:08, 2565496.00it/s]
22%|##2 | 5865472/26421880 [00:03<00:06, 3154954.04it/s]
24%|##3 | 6324224/26421880 [00:04<00:06, 2984769.34it/s]
26%|##6 | 6979584/26421880 [00:04<00:05, 3790677.44it/s]
28%|##8 | 7438336/26421880 [00:04<00:05, 3403200.21it/s]
31%|### | 8060928/26421880 [00:04<00:04, 4049407.94it/s]
33%|###3 | 8749056/26421880 [00:04<00:04, 4032161.38it/s]
36%|###5 | 9502720/26421880 [00:04<00:03, 4836960.49it/s]
39%|###8 | 10256384/26421880 [00:04<00:03, 4682743.61it/s]
42%|####2 | 11108352/26421880 [00:04<00:02, 5565798.45it/s]
45%|####5 | 11993088/26421880 [00:05<00:02, 5432905.60it/s]
49%|####8 | 12877824/26421880 [00:05<00:02, 6223459.53it/s]
53%|#####2 | 13991936/26421880 [00:05<00:01, 6972085.50it/s]
56%|#####5 | 14745600/26421880 [00:05<00:01, 6541751.40it/s]
60%|#####9 | 15794176/26421880 [00:05<00:01, 7510300.64it/s]
65%|######5 | 17203200/26421880 [00:05<00:01, 7761789.39it/s]
70%|######9 | 18481152/26421880 [00:05<00:00, 8953279.48it/s]
76%|#######5 | 20021248/26421880 [00:06<00:00, 9023237.20it/s]
80%|######## | 21266432/26421880 [00:06<00:00, 9827409.72it/s]
88%|########8 | 23298048/26421880 [00:06<00:00, 10567581.37it/s]
94%|#########3| 24739840/26421880 [00:06<00:00, 11443984.60it/s]
100%|##########| 26421880/26421880 [00:06<00:00, 4051942.49it/s]
Extracting data/FashionMNIST/raw/train-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw/train-labels-idx1-ubyte.gz
0%| | 0/29515 [00:00<?, ?it/s]
100%|##########| 29515/29515 [00:00<00:00, 324389.67it/s]
Extracting data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz
0%| | 0/4422102 [00:00<?, ?it/s]
1%|1 | 65536/4422102 [00:00<00:12, 361114.60it/s]
5%|5 | 229376/4422102 [00:00<00:06, 677682.80it/s]
19%|#8 | 819200/4422102 [00:00<00:01, 1849386.23it/s]
37%|###7 | 1638400/4422102 [00:00<00:00, 2895273.71it/s]
58%|#####7 | 2555904/4422102 [00:00<00:00, 3663469.87it/s]
81%|######## | 3571712/4422102 [00:01<00:00, 4307151.17it/s]
100%|##########| 4422102/4422102 [00:01<00:00, 3728637.53it/s]
Extracting data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz
0%| | 0/5148 [00:00<?, ?it/s]
100%|##########| 5148/5148 [00:00<00:00, 31894057.60it/s]
Extracting data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw
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¶
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
.
target_transform = Lambda(lambda y: 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 9.939 seconds)