Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Fastai Fastbook Learner Class

From Leeroopedia


Knowledge Sources
Domains Deep Learning, Software Architecture, Training Infrastructure
Last Updated 2026-02-09 17:00 GMT

Overview

Concrete implementation of the Learner abstraction with a callback system, as built from scratch in fastbook Chapter 19.

Description

The Learner class defined in Chapter 19 assembles a model, DataLoaders, loss function, optimizer factory, learning rate, and a list of callbacks into a single trainable object. The fit method runs the standard epoch/batch training loop, firing callback events at each stage. The Callback base class uses GetAttr to provide transparent access to all Learner state.

Usage

Use this Learner class when you want to:

  • Train any PyTorch model with a standardized interface.
  • Compose training behaviors (metrics, scheduling, logging) via callbacks.
  • Understand the architecture behind fastai's production Learner by studying the from-scratch implementation.

Code Reference

Source Location

  • Repository: fastbook
  • File: 19_learner.ipynb (Chapter 19), "Learner" section

Signature

class Learner:
    def __init__(self, model, dls, loss_func, lr, cbs, opt_func=SGD):
        store_attr(self, 'model,dls,loss_func,lr,cbs,opt_func')
        for cb in cbs: cb.learner = self

    def one_batch(self):
        self('before_batch')
        xb, yb = self.batch
        self.preds = self.model(xb)
        self.loss = self.loss_func(self.preds, yb)
        if self.model.training:
            self.loss.backward()
            self.opt.step()
        self('after_batch')

    def one_epoch(self, train):
        self.model.training = train
        self('before_epoch')
        dl = self.dls.train if train else self.dls.valid
        for self.num, self.batch in enumerate(progress_bar(dl, leave=False)):
            self.one_batch()
        self('after_epoch')

    def fit(self, n_epochs):
        self('before_fit')
        self.opt = self.opt_func(self.model.parameters(), self.lr)
        self.n_epochs = n_epochs
        try:
            for self.epoch in range(n_epochs):
                self.one_epoch(True)
                self.one_epoch(False)
        except CancelFitException: pass
        self('after_fit')

    def __call__(self, name):
        for cb in self.cbs: getattr(cb, name, noop)()
class Callback(GetAttr):
    _default = 'learner'

Import

import torch
import torch.nn as nn
from fastai.callback.core import Callback, GetAttr, store_attr
from fastai.optimizer import SGD

I/O Contract

Inputs

Name Type Required Description
model nn.Module Yes The PyTorch model to train
dls DataLoaders (with .train and .valid) Yes Training and validation DataLoaders
loss_func callable Yes Loss function: (predictions, targets) -> scalar loss
lr float Yes Learning rate passed to the optimizer
cbs list[Callback] Yes List of callback instances (can be empty list [])
opt_func callable No Optimizer constructor, defaults to SGD. Signature: (params, lr) -> optimizer

Outputs

Name Type Description
self.model nn.Module Trained model with optimized parameters
self.loss Tensor Loss value from the most recent batch
self.preds Tensor Predictions from the most recent batch
self.epoch int Current/final epoch number

Callback Events

Event Name Fired When Common Uses
before_fit Start of fit(), before any epochs Move model to GPU, initialize metrics
before_epoch Start of each epoch Reset metric accumulators
before_batch Start of each batch, before forward pass Move batch to GPU, apply input transforms
after_batch End of each batch, after optimizer step Accumulate metrics, log loss
after_epoch End of each epoch Print metrics, check early stopping
after_fit End of fit(), after all epochs Final cleanup, save model

Usage Examples

Basic Usage

import torch.nn as nn

# Define model
model = nn.Sequential(
    nn.Linear(28*28, 30),
    nn.ReLU(),
    nn.Linear(30, 1)
)

# Define a simple metrics callback
class TrackLoss(Callback):
    def before_fit(self):
        self.losses = []

    def after_batch(self):
        self.losses.append(self.loss.item())

    def after_fit(self):
        print(f"Final loss: {self.losses[-1]:.4f}")

# Create Learner and train
learn = Learner(
    model=model,
    dls=dls,
    loss_func=mnist_loss,
    lr=0.1,
    cbs=[TrackLoss()]
)
learn.fit(10)

Device Management Callback

class SetupLearnerCB(Callback):
    def before_fit(self):
        self.model.cuda()

    def before_batch(self):
        self.learner.batch = self.batch[0].cuda(), self.batch[1].cuda()

Metrics Tracking Callback

class TrackMetrics(Callback):
    def before_epoch(self):
        self.accs = []
        self.losses = []

    def after_batch(self):
        if not self.model.training:
            with torch.no_grad():
                acc = (self.preds.sigmoid() > 0.5) == (self.batch[1] == 1)
                self.accs.append(acc.float().mean())
            self.losses.append(self.loss.item())

    def after_epoch(self):
        if not self.model.training:
            avg_acc = torch.stack(self.accs).mean()
            avg_loss = sum(self.losses) / len(self.losses)
            print(f"Epoch {self.epoch}: loss={avg_loss:.4f}, acc={avg_acc:.4f}")

Assembling All Components

# This brings together every piece from the Neural Network From Scratch workflow:
# 1. Tensor data pipeline -> produces dls (DataLoaders)
# 2. Loss function -> mnist_loss or F.cross_entropy
# 3. SGD optimizer -> opt_func=SGD
# 4. Neural network with activation -> model = nn.Sequential(...)
# 5. Backpropagation -> handled by loss.backward() inside one_batch
# 6. Training loop -> handled by fit/one_epoch/one_batch
# 7. Learner + callbacks -> this class

learn = Learner(
    model=nn.Sequential(nn.Linear(28*28, 30), nn.ReLU(), nn.Linear(30, 1)),
    dls=dls,
    loss_func=mnist_loss,
    lr=0.1,
    cbs=[SetupLearnerCB(), TrackMetrics()],
    opt_func=SGD
)
learn.fit(20)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment