Bitpath

Boss lesson · Unit 03

Train a classifier

Batches, a validation split and a model that learns from 200 points

13 exercisesabout 3 min

What you practise

  • Dataset and DataLoader, 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?

Python
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?

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.

Python
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.

One email when Bitpath is ready to try. Nothing else. Privacy