Search code examples
neural-networkpytorch

Validation dataset in PyTorch using DataLoaders


I want to load the MNIST dataset in PyTorch and Torchvision, dividing it into train, validation, and test parts. So far I have:

def load_dataset():
    train_loader = torch.utils.data.DataLoader(
        torchvision.datasets.MNIST(
            '/data/', train=True, download=True,
            transform=torchvision.transforms.Compose([
                torchvision.transforms.ToTensor()])),
        batch_size=batch_size_train, shuffle=True)

    test_loader = torch.utils.data.DataLoader(
        torchvision.datasets.MNIST(
            '/data/', train=False, download=True,
            transform=torchvision.transforms.Compose([
                torchvision.transforms.ToTensor()])),
        batch_size=batch_size_test, shuffle=True)

How can I divide the training dataset into training and validation if it's in the DataLoader? I want to use the last 10000 examples from the training dataset as a validation dataset (I know that I should do a CV for more accurate results, I just want a quick validation here).


Solution

  • Splitting the training dataset into training and validation in PyTorch turns out to be much harder than it should be.

    First, split the training set into training and validation subsets (class Subset), which are not datasets (class Dataset):

    train_subset, val_subset = torch.utils.data.random_split(
            train, [50000, 10000], generator=torch.Generator().manual_seed(1))
    

    Then get actual data from those datasets:

    X_train = train_subset.dataset.data[train_subset.indices]
    y_train = train_subset.dataset.targets[train_subset.indices]
    
    X_val = val_subset.dataset.data[val_subset.indices]
    y_val = val_subset.dataset.targets[val_subset.indices]
    

    Note that this way we don't have Dataset objects, so we can't use DataLoader objects for batch training. If you want to use DataLoaders, they work directly with Subsets:

    train_loader = DataLoader(dataset=train_subset, shuffle=True, batch_size=BATCH_SIZE)
    val_loader = DataLoader(dataset=val_subset, shuffle=False, batch_size=BATCH_SIZE)