Utility Functions and Classes

The d2l Utility Layer

The d2l package collects shared utility classes and functions reused across the book — Trainer, Module, DataModule, Classifier, plotting helpers, etc.

This page is the canonical listing of those helpers. Most chapters subclass and extend them rather than reimplement from scratch.

Core base classes

  • HyperParameters records constructor arguments so models, data modules, and trainers can expose reproducible settings.
  • Module owns model logic: forward, loss, metrics, and optimizer configuration.
  • DataModule owns data logic: download, preprocessing, batching, and train/validation iterators.
  • Trainer coordinates the loop without hiding the math used in chapter-specific subclasses.

Plotting and progress

  • ProgressBoard and Animator collect streaming metrics and redraw one compact figure during training.
  • The display helpers are intentionally stateful: they remember previous points so chapter code can call plot or add from inside minibatch loops.
  • For lecture slides, the important invariant is not the plotting implementation; it is the API shape: log scalar metrics against an x-axis such as epoch, update count, or wall-clock time.

Training loop primitives

  • Accumulator keeps running sums for losses, accuracies, and counts, avoiding per-minibatch global state.
  • accuracy, evaluate_accuracy, and evaluate_loss separate evaluation from parameter updates.
  • grad_clipping rescales gradients when their global norm is too large, a recurring requirement for recurrent and sequence models.

Data loading helpers

  • load_array turns aligned tensors into minibatches.
  • download, extract, and download_extract provide cached, checksum-aware dataset access for examples throughout the book.
  • tokenize, truncate_pad, and the Fashion-MNIST label helpers standardize common preprocessing steps used before the full NLP and vision abstractions are introduced.

Reinforcement learning helpers

  • make_env('FrozenLake-v1', seed) returns a small tabular MDP with transition tuples (p, s', r, done) for value iteration.
  • show_value_function_progress and show_Q_function_progress visualize how values and greedy policies evolve over dynamic-programming or TD iterations.
  • These utilities are deliberately tiny: they make the Bellman updates visible before moving to function approximation.

Sequence-model helpers

  • read_data_nmt, preprocess_nmt, and tokenize_nmt prepare the English-French translation corpus.
  • build_array_nmt converts variable-length token sequences into padded arrays plus valid lengths.
  • MaskedSoftmaxCELoss, sequence_mask, train_seq2seq, and predict_seq2seq implement the core sequence-to-sequence training and decoding machinery reused in the attention chapter.

Recap

  • The d2l library provides reusable training, plotting, and data primitives so chapter code can focus on the ideas, not boilerplate.
  • Newer chapters use the Trainer.fit(model, data) shape; legacy chapters retain framework-specific helpers where older examples depend on them.