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.
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.ProgressBoard and Animator collect streaming metrics and redraw one compact figure during training.plot or add from inside minibatch loops.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.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.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.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.d2l library provides reusable training, plotting, and data primitives so chapter code can focus on the ideas, not boilerplate.Trainer.fit(model, data) shape; legacy chapters retain framework-specific helpers where older examples depend on them.