Bird
Raised Fist0
PyTorchml~20 mins

Train/val/test split in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Train/Val/Test Split Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of PyTorch dataset split code
What will be the output of the following code snippet that splits a dataset into train, validation, and test sets using PyTorch's random_split?
PyTorch
from torch.utils.data import random_split
from torch.utils.data import Dataset

class DummyDataset(Dataset):
    def __init__(self, length):
        self.data = list(range(length))
    def __len__(self):
        return len(self.data)
    def __getitem__(self, idx):
        return self.data[idx]

full_dataset = DummyDataset(100)
train_size = 70
val_size = 20
test_size = 10
train_set, val_set, test_set = random_split(full_dataset, [train_size, val_size, test_size])

print(len(train_set), len(val_set), len(test_set))
A33 33 34
B100 0 0
C70 20 10
D50 25 25
Attempts:
2 left
💡 Hint
Check how the sizes are passed to random_split and what lengths are printed.
🧠 Conceptual
intermediate
1:30remaining
Purpose of validation set in train/val/test split
What is the main purpose of the validation set in a train/val/test split during machine learning model training?
ATo tune hyperparameters and prevent overfitting
BTo train the model with more data
CTo evaluate the model's performance on unseen data after training
DTo test the model's final accuracy before deployment
Attempts:
2 left
💡 Hint
Think about which set helps decide model settings without biasing final evaluation.
❓ Hyperparameter
advanced
2:00remaining
Choosing split ratios for train/val/test
Which of the following split ratios is most appropriate for a dataset with 10,000 samples to ensure reliable training, validation, and testing?
A60% train, 20% validation, 20% test
B50% train, 25% validation, 25% test
C90% train, 5% validation, 5% test
D80% train, 10% validation, 10% test
Attempts:
2 left
💡 Hint
Consider enough data for training and meaningful validation/testing.
🔧 Debug
advanced
2:00remaining
Identify error in PyTorch dataset splitting code
What error will the following code raise when trying to split a dataset into train, validation, and test sets?
PyTorch
from torch.utils.data import random_split
full_dataset = list(range(50))
train_set, val_set, test_set = random_split(full_dataset, [30, 15, 10])
ATypeError: 'list' object has no attribute '__len__'
BValueError: Sum of input lengths does not equal the length of the input dataset
CTypeError: random_split expects a Dataset object, not a list
DNo error, splits successfully
Attempts:
2 left
💡 Hint
Check the sum of split sizes compared to dataset length.
❓ Model Choice
expert
2:30remaining
Best approach to split highly imbalanced dataset
You have a highly imbalanced classification dataset with 1% positive and 99% negative samples. Which approach is best to split the dataset into train, validation, and test sets to maintain class distribution?
AUse stratified splitting to keep class proportions in each subset
BManually shuffle and split the dataset randomly
CUse random_split from PyTorch directly without stratification
DSplit only into train and test, skip validation
Attempts:
2 left
💡 Hint
Think about preserving class balance in each subset for fair evaluation.

Practice

(1/5)
1. What is the main purpose of splitting data into train, validation, and test sets in PyTorch?
easy
A. To train the model, tune hyperparameters, and evaluate performance separately
B. To increase the size of the dataset artificially
C. To reduce the number of features in the dataset
D. To convert data into tensors automatically

Solution

  1. Step 1: Understand the role of each data split

    The training set is used to teach the model, the validation set helps tune settings, and the test set checks how well the model works on new data.
  2. Step 2: Match purpose to options

    Only To train the model, tune hyperparameters, and evaluate performance separately correctly describes these roles. Other options describe unrelated tasks.
  3. Final Answer:

    To train the model, tune hyperparameters, and evaluate performance separately -> Option A
  4. Quick Check:

    Train/val/test split = separate training, tuning, testing [OK]
Hint: Remember: train teaches, val tunes, test checks [OK]
Common Mistakes:
  • Thinking splitting increases dataset size
  • Confusing splitting with feature selection
  • Assuming splitting converts data types
2. Which of the following is the correct way to split a PyTorch dataset into train and test sets using random_split?
easy
A. train_set, test_set = random_split(dataset, [80%, 20%])
B. train_set, test_set = random_split(dataset, [80, 20])
C. train_set, test_set = random_split(dataset, [0.8, 0.2])
D. train_set, test_set = random_split(dataset, [80])

Solution

  1. Step 1: Check the expected input for random_split

    random_split expects the dataset and a list of lengths (integers) for each split, not fractions or percentages.
  2. Step 2: Evaluate each option

    train_set, test_set = random_split(dataset, [80, 20]) uses integer lengths [80, 20], which is correct. train_set, test_set = random_split(dataset, [0.8, 0.2]) uses floats, train_set, test_set = random_split(dataset, [80%, 20%]) uses invalid syntax with %, and train_set, test_set = random_split(dataset, [80]) provides only one length.
  3. Final Answer:

    train_set, test_set = random_split(dataset, [80, 20]) -> Option B
  4. Quick Check:

    random_split needs integer lengths [OK]
Hint: Use integer lengths, not fractions, in random_split [OK]
Common Mistakes:
  • Passing fractions or percentages instead of integers
  • Providing only one length instead of two
  • Using invalid syntax like % in list
3. Given the code below, what will be the length of val_set?
from torch.utils.data import random_split

full_dataset = list(range(100))
train_size = 70
val_size = 20
test_size = 10
train_set, val_set, test_set = random_split(full_dataset, [train_size, val_size, test_size])
print(len(val_set))
medium
A. 10
B. 70
C. 20
D. 100

Solution

  1. Step 1: Understand random_split lengths

    The list [70, 20, 10] tells random_split to split the dataset into parts of those exact lengths.
  2. Step 2: Identify val_set length

    val_set corresponds to the second split with length 20, so len(val_set) will be 20.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    val_size = 20 means val_set length = 20 [OK]
Hint: Lengths in random_split match output sizes exactly [OK]
Common Mistakes:
  • Confusing sizes with percentages
  • Assuming val_set length is sum of others
  • Forgetting order of splits in output
4. You wrote this code to split your dataset but get a ValueError: Sum of input lengths does not equal the length of the input dataset!. What is the error?
from torch.utils.data import random_split

dataset = list(range(50))
train_set, test_set = random_split(dataset, [30, 25])
medium
A. The sum of lengths 30 + 25 is greater than dataset length 50
B. random_split requires fractions, not lengths
C. dataset must be a PyTorch tensor, not a list
D. random_split cannot split into two parts

Solution

  1. Step 1: Check dataset length and split lengths

    Dataset length is 50. The sum of split lengths is 30 + 25 = 55, which is more than 50.
  2. Step 2: Understand random_split requirements

    random_split requires the sum of split lengths to exactly match dataset length.
  3. Final Answer:

    The sum of lengths 30 + 25 is greater than dataset length 50 -> Option A
  4. Quick Check:

    Sum of split lengths must equal dataset length [OK]
Hint: Sum of split lengths must equal dataset size exactly [OK]
Common Mistakes:
  • Passing split lengths that don't sum to dataset size
  • Thinking random_split accepts fractions
  • Assuming dataset must be tensor, not list
5. You want to split a PyTorch dataset of 1000 samples into 70% train, 15% validation, and 15% test sets. Which code correctly performs this split using random_split?
hard
A. train_set, val_set, test_set = random_split(dataset, [0.7, 0.15, 0.15])
B. train_set, val_set, test_set = random_split(dataset, [700, 150, 100])
C. train_set, val_set, test_set = random_split(dataset, [700, 150])
D. train_set, val_set, test_set = random_split(dataset, [700, 150, 150])

Solution

  1. Step 1: Calculate integer split sizes

    70% of 1000 is 700, 15% is 150, so splits are [700, 150, 150].
  2. Step 2: Check options for correctness

    train_set, val_set, test_set = random_split(dataset, [700, 150, 150]) uses correct integer lengths summing to 1000. train_set, val_set, test_set = random_split(dataset, [0.7, 0.15, 0.15]) uses floats (invalid), train_set, val_set, test_set = random_split(dataset, [700, 150]) misses one split, train_set, val_set, test_set = random_split(dataset, [700, 150, 100]) sums to 950 (incorrect).
  3. Final Answer:

    train_set, val_set, test_set = random_split(dataset, [700, 150, 150]) -> Option D
  4. Quick Check:

    Use integer lengths summing to dataset size [OK]
Hint: Convert percentages to integers summing to dataset size [OK]
Common Mistakes:
  • Passing fractions instead of integers
  • Not summing split sizes to dataset length
  • Missing one of the splits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard