Models do not train on files directly. They train on batches of tensors. PyTorch separates dataset logic from batching logic: a Dataset knows how to return one sample, and a DataLoader knows how to batch, shuffle, and load many samples efficiently.
A clean data pipeline makes experiments reliable. It should handle train/validation splits, transforms, labels, missing data, and batch shapes predictably.
A Dataset should be simple: store references to examples, implement `__len__`, and implement `__getitem__`. Avoid putting training logic inside the dataset.
A DataLoader handles batching. It can shuffle samples, use multiple workers, pin memory for GPU transfer, and call a custom collate function when samples have variable lengths.
This example converts NumPy-style arrays into a dataset suitable for regression or classification.
import torch
from torch.utils.data import Dataset, DataLoader, random_split
class TabularDataset(Dataset):
def __init__(self, features, labels):
self.features = torch.tensor(features, dtype=torch.float32)
self.labels = torch.tensor(labels, dtype=torch.long)
def __len__(self):
return len(self.features)
def __getitem__(self, index):
return self.features[index], self.labels[index]
X = torch.randn(1000, 12).numpy()
y = torch.randint(0, 3, (1000,)).numpy()
dataset = TabularDataset(X, y)
train_ds, val_ds = random_split(dataset, [800, 200])
train_loader = DataLoader(train_ds, batch_size=32, shuffle=True)
val_loader = DataLoader(val_ds, batch_size=64, shuffle=False)
features, labels = next(iter(train_loader))
print(features.shape) # [32, 12]
print(labels.shape) # [32]
import torch
from torch.utils.data import DataLoader, TensorDataset
features = torch.arange(10, dtype=torch.float32).reshape(5, 2)
labels = torch.tensor([0, 1, 0, 1, 0])
loader = DataLoader(TensorDataset(features, labels), batch_size=2, shuffle=False)
for batch_features, batch_labels in loader:
print(tuple(batch_features.shape), batch_labels.tolist())
(2, 2) [0, 1]
(2, 2) [0, 1]
(1, 2) [0]
A Dataset defines how to access individual samples. A DataLoader batches and shuffles samples for training or evaluation.
Input preprocessing transforms usually belong in the dataset or dataloader pipeline. Learned transformations belong in the model.
A dataset may contain non-serializable state, unsafe global code, or worker-specific file access problems.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.