PyTorch DataLoader
The dataloader class provides a way to specify how many data points to sample from a dataset for each epoch in a model training loop. In addition, you can specify whether to shuffle the datapoints or not.
from torch.utils.data import DataLoader
loader = DataLoader(dataset, batch_size=10, shuffle=True)
Then , when implementing a training loop, you can iterate through the loader and it would provide the specified number of points with the given shuffle criteria.
def train(model, opt, loss, loader, epochs):
model.train()
for epoch in epochs:
for X,y in loader:
#...
pass
pass
pass
Sample Code
class RandomClassificationDataset(Dataset):
def __init__(self, num_samples, input_size, num_classes):
# Generate dummy data (X) and labels (y)
self.X = torch.randn(num_samples, input_size)
self.y = torch.randint(0, num_classes, (num_samples,))
def __len__(self):
# Must return the total number of samples
return len(self.X)
def __getitem__(self, idx):
# Must return a single sample (features, label) at the given index
return self.X[idx], self.y[idx]
# 4. Prepare Dataset and DataLoader
# Instantiate the dataset with 200 total samples
train_dataset = RandomClassificationDataset(num_samples=200, input_size=INPUT_SIZE, num_classes=NUM_CLASSES)
# Wrap the dataset in a DataLoader
train_loader = DataLoader(
dataset=train_dataset,
batch_size=BATCH_SIZE,
shuffle=True, # Shuffle data every epoch to reduce overfitting
drop_last=False # Keep the final batch even if it's smaller than BATCH_SIZE
)