0
0
PyTorchml~10 mins

Train/val/test split in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to split the dataset into training and testing sets using PyTorch.

PyTorch
from torch.utils.data import random_split

dataset_size = len(dataset)
train_size = int(0.8 * dataset_size)
test_size = dataset_size - train_size
train_dataset, test_dataset = random_split(dataset, [[1], test_size])
Drag options to blanks, or click blank then click option'
Atrain_size
Btest_size
Cdataset_size
D0.8
Attempts:
3 left
💡 Hint
Common Mistakes
Using the test size instead of train size for the first split.
Passing a float instead of an integer for the split size.
2fill in blank
medium

Complete the code to create a validation set from the training dataset using PyTorch.

PyTorch
val_size = int(0.1 * len(train_dataset))
train_size = len(train_dataset) - [1]
train_dataset, val_dataset = random_split(train_dataset, [train_size, val_size])
Drag options to blanks, or click blank then click option'
Alen(train_dataset)
Btrain_size
C0.1
Dval_size
Attempts:
3 left
💡 Hint
Common Mistakes
Using the new training size instead of validation size in the subtraction.
Not converting the validation size to an integer.
3fill in blank
hard

Fix the error in the code to correctly split the dataset into train, validation, and test sets.

PyTorch
train_size = int(0.7 * len(dataset))
val_size = int(0.2 * len(dataset))
test_size = int(0.1 * len(dataset))
train_dataset, val_dataset, test_dataset = random_split(dataset, [train_size, val_size, [1]])
Drag options to blanks, or click blank then click option'
Atest_size
Bval_size
Ctrain_size
Dlen(dataset) - (train_size + val_size)
Attempts:
3 left
💡 Hint
Common Mistakes
Using validation size twice in the split sizes.
Calculating test size incorrectly.
4fill in blank
hard

Fill both blanks to create DataLoader objects for training and validation datasets with batch size 32.

PyTorch
from torch.utils.data import DataLoader

train_loader = DataLoader(train_dataset, batch_size=[1], shuffle=[2])
val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)
Drag options to blanks, or click blank then click option'
A32
B16
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Not shuffling training data.
Using wrong batch size.
5fill in blank
hard

Fill all three blanks to create a dictionary with dataset sizes for train, validation, and test sets.

PyTorch
dataset_sizes = {
    'train': len([1]),
    'val': len([2]),
    'test': len([3])
}
Drag options to blanks, or click blank then click option'
Atrain_dataset
Bval_dataset
Ctest_dataset
Ddataset
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original dataset length instead of split datasets.
Mixing up dataset variables.