githubEdit

PyTorch Foundation

From

Pytorch Basic

Dataset and Dataloader

torch.utils.data.Datasetstores the sample and labels. torch.utils.data.Datasetwraps an iterable around the dataset.

Example datasets include Image Datasetsarrow-up-right, Text Datasetsarrow-up-right, and Audio Datasetsarrow-up-right

Custom Dataset class must have:

  • __init__: run once when instantiating the object. The example below specifies the label, directory, and transformation of feature data and label data.

  • __len__: returns the number of samples in the dataset

  • __getitem__: loads and returns a sample from the dataset at the given index idx with necessary transformation

# Source: PyTorch Official Tutorial 
# https://pytorch.org/tutorials/beginner/basics/data_tutorial.html#iterating-and-visualizing-the-dataset
import os
import pandas as pd
from torchvision.io import read_image

# Custom Dataset Construction
class CustomImageDataset(Dataset):
    def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
        self.img_labels = pd.read_csv(annotations_file)
        self.img_dir = img_dir
        self.transform = transform
        self.target_transform = target_transform

    def __len__(self):
        return len(self.img_labels)

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)i
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image, label
        
   
# Iterate Dataset through dataloader      
from torch.utils.data import DataLoader
train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)
test_dataloader = DataLoader(test_data, batch_size=64, shuffle=True)

train_features, train_labels = next(iter(train_dataloader))
# train_features first dimension is batch size 
img = train_features[0].squeeze()
label = train_labels[0]
plt.imshow(img, cmap="gray")
plt.show()


# Lambda function example of create one-hot encoding label 
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))
)

Building Neural Network

  • inherit parent class

  • forward specify how data is passed through all the layers.

Differentiation through autograd

  • Gradient is only available if requires_grad property is set to true.

  • Gradient calculation is only done once using backward

  • If several backward call is needed, we need to pass retain_graph = True to the backward call

  • torch.no_grad() disable gradient tracking or detach, we can use them to mark parameters in neural network as frozen parameters

Training Loop and Saving Model

  • Optimizer input includes the parameters needing optimized

  • At each bach, predict, calculate loss, clean optimizer, calculate gradient, and then step.

  • Iterate through each set of data to calculate loss without tracking gradient.

Last updated