WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PyTorch · Basic · question 11 of 100

How do you split a dataset into training and validation sets using PyTorch?

📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

In PyTorch, you can split a dataset into training and validation set using the ‘random_split‘ function from the ‘torch.utils.data‘ module.

Here is an example code snippet:

import torch
from torch.utils.data import Dataset, DataLoader, random_split

# create your custom dataset
class MyDataset(Dataset):
    def __init__(self):
        # initialize the dataset

    def __len__(self):
        # return size of your dataset

    def __getitem__(self, index):
        # get a sample from the dataset

# create an instance of the dataset
dataset = MyDataset()

# define the size of the validation set
val_size = 0.2

# calculate the size of the training set
train_size = len(dataset) - int(len(dataset) * val_size)

# split the dataset into training and validation sets
train_dataset, val_dataset = random_split(dataset, [train_size, len(dataset) - train_size])

# create data loaders for the training and validation sets
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32, shuffle=True)

In this code snippet, we create a custom dataset class ‘MyDataset‘. We then initialize an instance of this dataset and define the percentage of data that we want to use for validation (‘val_size‘). We then calculate the size of the training set and use the ‘random_split‘ function to split the dataset into training and validation sets based on the calculated sizes. Finally, we create data loaders for the training and validation sets using the ‘DataLoader‘ class.

Note that the ‘random_split‘ function takes the dataset and a list of sizes for each split. The sum of the sizes should equal the length of the dataset. In this example, we pass ‘[train_size, len(dataset) - train_size]‘ to the function. The first element of the list specifies the size of the training set, and the second element is the size of the validation set.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview — then scores it.
📞 Practice PyTorch — free 15 min
📕 Buy this interview preparation book: 100 PyTorch questions & answers — PDF + EPUB for $5

All 100 PyTorch questions · All topics