PyTorch

1 minute read

Published:

This lesson covers PyTorch Tutorial, https://pytorch.org/tutorials/beginner/basics/intro.html

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
    • target_transform
      • to modify the labels
    • both accept callables containing the transformation logic.
    • torchvision.transforms
      • offers several commonly-used transforms out of the box
  • FashionMNIST
    • features are in PIL Image format
    • labels are integers
  • For training, we need the
    • features as normalized tensors
    • labels as one-hot encoded tensors
  • To make these transformations, we use ToTensor and Lambda.
topic = "pytorch"
lesson = 4

from n import *
home, models_path = get_project_dir("FashionMNIST")
print(home)
/home/naneja/datasets/n/FashionMNIST
import torch
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda

# zero tensor of size 10
tt = torch.zeros(10, dtype=torch.float)

# assigns value 1 on the index given by input y
tt = lambda y: tt.scatter_(dim=0, 
                           index=torch.tensor(y), 
                           value=1)

# Lambda transforms user defined lambda function
tt = Lambda(tt)

ds = datasets.FashionMNIST(
    root=home,
    train=True,
    download=True,
    transform=ToTensor(),
    target_transform=tt
)

ToTensor()

  • converts a PIL image or NumPy ndarray into a FloatTensor
  • 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))