Python Engineer

Free Python and Machine Learning Tutorials

Become A Patron and get exclusive content! Get access to ML From Scratch notebooks, join a private Slack channel, get priority response, and more! I really appreciate the support!

back to course overview

Dataset Transforms - PyTorch Beginner 10

07 Jan 2020

Learn all the basics you need to get started with this deep learning framework! In this part we learn how we can use dataset transforms together with the built-in Dataset class. Apply built-in transforms to images, arrays, and tensors. Or write your own custom Transform classes.

All code from this course can be found on GitHub.

Dataset Transforms in PyTorch

import torch import torchvision from torch.utils.data import Dataset import numpy as np class WineDataset(Dataset): def __init__(self, transform=None): xy = np.loadtxt('./data/wine/wine.csv', delimiter=',', dtype=np.float32, skiprows=1) self.n_samples = xy.shape[0] # note that we do not convert to tensor here self.x_data = xy[:, 1:] self.y_data = xy[:, [0]] self.transform = transform def __getitem__(self, index): sample = self.x_data[index], self.y_data[index] if self.transform: sample = self.transform(sample) return sample def __len__(self): return self.n_samples # Custom Transforms # implement __call__(self, sample) class ToTensor: # Convert ndarrays to Tensors def __call__(self, sample): inputs, targets = sample return torch.from_numpy(inputs), torch.from_numpy(targets) class MulTransform: # multiply inputs with a given factor def __init__(self, factor): self.factor = factor def __call__(self, sample): inputs, targets = sample inputs *= self.factor return inputs, targets print('Without Transform') dataset = WineDataset() first_data = dataset[0] features, labels = first_data print(type(features), type(labels)) print(features, labels) print('\nWith Tensor Transform') dataset = WineDataset(transform=ToTensor()) first_data = dataset[0] features, labels = first_data print(type(features), type(labels)) print(features, labels) print('\nWith Tensor and Multiplication Transform') composed = torchvision.transforms.Compose([ToTensor(), MulTransform(4)]) dataset = WineDataset(transform=composed) first_data = dataset[0] features, labels = first_data print(type(features), type(labels)) print(features, labels)