Batched Data Loading in PyTorch with DataLoader and TensorDataset

PyTorch provides efficient utilities for handling batched data during model trianing through DataLoader and TensorDataset. These tools enable memory-efficient, shuffled, and parallelized data loading—critical for scalable neural network training.

Begin by importing the necessary components:

from torch.utils.data import DataLoader, TensorDataset
import torch

First, create input and target tensors:

batch_size = 8
features = torch.linspace(1, 10, 10)
targets = torch.linspace(10, 1, 10)

Wrap these tensors into a dataset object:

training_data = TensorDataset(features, targets)

Then configure a data loader that batches and optionally shuffles the data:

loader = DataLoader(
    training_data,
    batch_size=batch_size,
    shuffle=True,
    num_workers=2
)

During training, iterate over epochs and batches:

num_epochs = 3
for epoch_idx in range(num_epochs):
    for batch_idx, (x_batch, y_batch) in enumerate(loader):
        # Simulate training step
        print(f'Epoch: {epoch_idx} | Batch: {batch_idx} | x: {x_batch.numpy()} | y: {y_batch.numpy()}')

When shuffle=True, each epoch processes samples in a randomized order, improving generalization. If shuffle=False, the order remains fixed across epochs.

If the dataset size isn’t evenly divisible by the batch size, the final batch contains the remaining samples. For example, with 10 samples and batch_size=8, the first batch has 8 samples and the second has 2.

Tags: pytorch DataLoader TensorDataset batch training Machine Learning

Posted on Sat, 12 Sep 2026 16:50:32 +0000 by Bookmark