Boss lesson · Unit 03
Train a classifier
Batches, a validation split and a model that learns from 200 points
What you practise
DatasetandDataLoader, batches and shuffling, train/validation split, a small classifier trained on synthetic data, reading the loss and accuracy as it learns
Try a question from this lesson
Pick one
A DataLoader cuts a dataset into batches. With 200 samples and batches of 32, what does this print?
from torch.utils.data import (
DataLoader, TensorDataset,
)
ds = TensorDataset(torch.zeros(200, 2))
loader = DataLoader(ds, batch_size=32)
print(len(loader))Hint
Fill full batches until fewer than a batch remains. What happens to the leftovers?
Correct answer
7
Why
Six full batches use 192 samples, and the last 8 form a smaller seventh batch. The length of a loader is its number of batches, not samples. Pass drop_last=True to discard the short one.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Dataset and DataLoader
A Dataset answers two questions: how many samples are there, and what is sample number i?
A DataLoader wraps it: it shuffles the order, fetches samples, and stacks them into batch tensors. TensorDataset is the ready-made Dataset for data that already sits in tensors.
class Points(Dataset):
def __init__(self, x, y):
self.x, self.y = x, y
def __len__(self):
return len(self.x)
def __getitem__(self, i):
return self.x[i], self.y[i]11 more exercises in the app
- 3× fill the blanks
- 2× type the answer
- 4× multiple choice
- 1× order the lines
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.