The d2l package collects shared utility classes and functions reused across the book — Trainer, Module, DataModule, Classifier, plotting helpers, etc.
This page lists those helpers and their roles. 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.plot_curves draws seed medians with min-to-max bands: the one entry point for every learning curve in the RL chapters.show_grid renders a gridworld’s values and greedy policy, reading the map shape from the environment description.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 chapter-specific concepts.Trainer.fit(model, data) shape; legacy chapters retain framework-specific helpers where older examples depend on them.