pytorch
mini-batches
machine learning
data processing
tutorial

How to get mini-batches in pytorch in a clean and efficient way?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In deep learning, processing data in mini-batches is a crucial step for training models effectively and efficiently. Mini-batches help stabilize gradient descent by smoothing the gradient during optimization and reduce memory requirements by not loading the entire dataset at once. PyTorch offers various utilities and techniques to handle mini-batches cleanly and efficiently. Here, we'll explore these methods, focusing on the DataLoader class, and provide detailed examples.

Introduction to Mini-Batches in PyTorch

PyTorch provides flexible data handling functionality through torch.utils.data, which includes Dataset and DataLoader classes. The Dataset class is designed for accessing individual data points, while the DataLoader class is built for efficient data iteration and batching, with support for multi-threaded data loading.

Creating a PyTorch Dataset

To leverage mini-batches efficiently, the first step is to create a custom Dataset. PyTorch's Dataset is an abstract class, requiring you to define two methods: __len__ and __getitem__.

python
1import torch
2from torch.utils.data import Dataset
3
4class CustomDataset(Dataset):
5    def __init__(self, data, labels):
6        self.data = data
7        self.labels = labels
8
9    def __len__(self):
10        return len(self.data)
11
12    def __getitem__(self, idx):
13        sample = self.data[idx]
14        label = self.labels[idx]
15        return sample, label
16
17# Sample data
18data = torch.randn(1000, 28, 28)  # 1000 samples of 28x28 images
19labels = torch.randint(0, 10, (1000,))  # 1000 labels for 10 classes
20
21dataset = CustomDataset(data, labels)

Utilizing DataLoader for Mini-Batching

With the Dataset defined, the next step is to wrap it with a DataLoader to enable mini-batch processing. DataLoader takes care of shuffling, batching, and parallel data loading by spawning multiple worker threads.

python
1from torch.utils.data import DataLoader
2
3# Create DataLoader
4data_loader = DataLoader(
5    dataset,
6    batch_size=32,
7    shuffle=True,
8    num_workers=4
9)
10
11# Iterate over DataLoader
12for batch_idx, (data, labels) in enumerate(data_loader):
13    # Your training code here
14    print(f"Batch {batch_idx + 1}:")
15    print(f"Data Shape: {data.shape}")
16    print(f"Label Shape: {labels.shape}")

Key parameters of the DataLoader:

  • batch_size: Number of samples in each mini-batch.
  • shuffle: Whether to shuffle the dataset at the beginning of each epoch.
  • num_workers: How many subprocesses to use for data loading. Setting it higher can improve data loading speed.

Collate Function for Custom Batching

The default collate function works well for most datasets, but you might need custom collation for variable-size data (e.g., sequences). By defining a custom collate function, you can handle data concatenation as per specific requirements.

python
1def custom_collate_fn(batch):
2    data, labels = zip(*batch)
3    data = torch.stack(data)
4    labels = torch.tensor(labels)
5    return data, labels
6
7# Passing custom collate function
8data_loader_with_custom_collate = DataLoader(
9    dataset,
10    batch_size=32,
11    shuffle=True,
12    num_workers=4,
13    collate_fn=custom_collate_fn
14)

Data Augmentation and Transformations

Sometimes, preprocessing steps, such as normalization or data augmentation, are required to enhance model generalization. PyTorch supports transformation pipelines using torchvision.transforms.

python
1from torchvision import transforms
2
3transform = transforms.Compose([
4    transforms.RandomHorizontalFlip(),
5    transforms.RandomRotation(10),
6    transforms.ToTensor(),
7    transforms.Normalize((0.5,), (0.5,))
8])
9
10class AugmentedDataset(Dataset):
11    def __init__(self, data, labels, transform=None):
12        self.data = data
13        self.labels = labels
14        self.transform = transform
15
16    def __len__(self):
17        return len(self.data)
18
19    def __getitem__(self, idx):
20        sample = self.data[idx]
21        label = self.labels[idx]
22        if self.transform:
23            sample = self.transform(sample)
24        return sample, label
25
26augmented_dataset = AugmentedDataset(data, labels, transform=transform)
27augmented_loader = DataLoader(
28    augmented_dataset,
29    batch_size=32,
30    shuffle=True,
31    num_workers=4
32)

Efficient Training with Mini-Batches

Training efficiently with mini-batches involves leveraging gradient accumulation, learning rate scheduling, and potentially using mixed precision training to speed up computation and reduce memory footprint. PyTorch's torch.optim and torch.cuda.amp modules are handy for these purposes.

Summary Table

Below is a summary of key points related to handling mini-batches in PyTorch:

AspectDescription
Dataset DefinitionUse Dataset class Define __len__ and __getitem__.
DataLoader Key Paramsbatch_size, shuffle, num_workers.
Custom Collate FunctionDefine when default collation fails or for variable-size data.
TransformationsUse transforms.Compose for data preprocessing and augmentation.
Efficient TrainingUse gradient accumulation, learning rate scheduling, and mixed precision training.

Conclusion

PyTorch provides extensive tools for creating and handling mini-batches efficiently. Understanding these techniques helps in speeding up deep learning model development and evaluation, enabling scalable and efficient processing. These capabilities, combined with custom transformations and data collations, allow for handling complex datasets in a streamlined manner. With these tips, you're ready to implement mini-batching in any PyTorch project, enhancing both performance and computational efficiency.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design