23.8  The d2l API Document

This section displays classes and functions (sorted alphabetically) in the d2l package, showing where they are defined in the book so you can find more detailed implementations and explanations. See also the source code on the GitHub repository.

23.8.1 Classes

class Accumulator

Accumulator(n)

For accumulating sums over n variables. Section 23.7

  • add(*args)
  • reset()

class AddNorm(nn.Module)

AddNorm(norm_shape, dropout)

The residual connection followed by layer normalization. Section 11.7

  • forward(X, Y)

class AdditiveAttention(nn.Module)

AdditiveAttention(num_hiddens, dropout, **kwargs)

Additive attention. Section 11.3

  • forward(queries, keys, values, valid_lens)

class Animator

Animator(xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1, figsize=(3.5, 2.5))

For plotting data in animation. Section 23.7

  • add(x, y)

class AttentionDecoder(d2l.Decoder)

The base attention-based decoder interface. Section 11.4

  • attention_weights()

class BERTEncoder(nn.Module)

BERTEncoder(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_blks, dropout, max_len=1000, **kwargs)

BERT encoder. Section 15.8

  • forward(tokens, segments, valid_lens)

class BERTModel(nn.Module)

BERTModel(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_blks, dropout, max_len=1000)

The BERT model. Section 15.8

  • forward(tokens, segments, valid_lens=None, pred_positions=None)

class BananasDataset(torch.utils.data.Dataset)

BananasDataset(is_train)

A customized dataset to load the banana detection dataset. Section 14.6

class Benchmark

Benchmark(description='Done')

For measuring running time. Section 13.1

class Classifier(d2l.Module)

The base class of classification models. Section 4.3

  • validation_step(batch)
  • accuracy(Y_hat, Y, averaged=True) — Compute the number of correct predictions. Section 4.3
  • loss(Y_hat, Y, averaged=True)
  • layer_summary(X_shape)

class DataModule(d2l.HyperParameters)

DataModule(root='../data', num_workers=4)

The base class of data. Section 3.2

  • get_dataloader(train)
  • train_dataloader()
  • val_dataloader()
  • get_tensorloader(tensors, train, indices=slice(0, None))

class Decoder(nn.Module)

The base decoder interface for the encoder–decoder architecture. Section 10.6

  • init_state(enc_all_outputs, *args)
  • forward(X, state)

class DotProductAttention(nn.Module)

DotProductAttention(dropout)

Scaled dot product attention. Section 11.3

  • forward(queries, keys, values, valid_lens=None)

class Encoder(nn.Module)

The base encoder interface for the encoder–decoder architecture. Section 10.6

  • forward(X, *args)

class EncoderDecoder(d2l.Classifier)

EncoderDecoder(encoder, decoder)

The base class for the encoder–decoder architecture. Section 10.6

  • forward(enc_X, dec_X, *args)
  • predict_step(batch, device, num_steps, save_attention_weights=False)

class FashionMNIST(d2l.DataModule)

FashionMNIST(batch_size=64, resize=(28, 28))

The Fashion-MNIST dataset. Section 4.2

  • text_labels(indices) — Return text labels. Section 4.2
  • get_dataloader(train)
  • visualize(batch, nrows=1, ncols=8, labels=[])

class GRU(d2l.RNN)

GRU(num_inputs, num_hiddens, num_layers, dropout=0)

The multilayer GRU model. Section 10.3

class HyperParameters

The base class of hyperparameters. Section 3.2

  • save_hyperparameters(ignore=[]) — Save function arguments into class attributes. Section 23.7

class LeNet(d2l.Classifier)

LeNet(lr=0.1, num_classes=10)

The LeNet-5 model. Section 7.6

class LinearRegression(d2l.Module)

LinearRegression(lr)

The linear regression model implemented with high-level APIs. Section 3.5

  • forward(X)
  • loss(y_hat, y)
  • configure_optimizers()
  • get_w_b()

class LinearRegressionScratch(d2l.Module)

LinearRegressionScratch(num_inputs, lr, sigma=0.01)

The linear regression model implemented from scratch. Section 3.4

  • forward(X)
  • loss(y_hat, y)
  • configure_optimizers()

class MTFraEng(d2l.DataModule)

MTFraEng(batch_size, num_steps=9, num_train=512, num_val=128)

The English-French dataset. Section 10.5

  • get_dataloader(train)
  • build(src_sentences, tgt_sentences)

class MaskLM(nn.Module)

MaskLM(vocab_size, num_hiddens, **kwargs)

The masked language model task of BERT. Section 15.8

  • forward(X, pred_positions)

class MaskedSoftmaxCELoss(nn.CrossEntropyLoss)

The softmax cross-entropy loss with masks. Section 23.7

  • forward(pred, label, valid_len)

class Module(d2l.nn_Module, d2l.HyperParameters)

Module(plot_train_per_epoch=2, plot_valid_per_epoch=1)

The base class of models. Section 3.2

  • loss(y_hat, y)
  • forward(X)
  • plot(key, value, train) — Plot a point in animation.
  • training_step(batch)
  • validation_step(batch)
  • configure_optimizers()
  • apply_init(inputs, init=None)

class MultiHeadAttention(d2l.Module)

MultiHeadAttention(num_hiddens, num_heads, dropout, bias=False, **kwargs)

Multi-head attention. Section 11.5

  • forward(queries, keys, values, valid_lens)
  • transpose_qkv(X) — Transposition for parallel computation of multiple attention heads. Section 11.5
  • transpose_output(X) — Reverse the operation of transpose_qkv. Section 11.5

class NextSentencePred(nn.Module)

NextSentencePred(**kwargs)

The next sentence prediction task of BERT. Section 15.8

  • forward(X)

class PositionWiseFFN(nn.Module)

PositionWiseFFN(ffn_num_hiddens, ffn_num_outputs)

The positionwise feed-forward network. Section 11.7

  • forward(X)

class PositionalEncoding(nn.Module)

PositionalEncoding(num_hiddens, dropout, max_len=1000)

Positional encoding. Section 11.6

  • forward(X)

class ProgressBoard(d2l.HyperParameters)

ProgressBoard(xlabel=None, ylabel=None, xlim=None, ylim=None, xscale='linear', yscale='linear', ls=['-', '--', '-.', ':'], colors=['C0', 'C1', 'C2', 'C3'], fig=None, axes=None, figsize=(3.5, 2.5), display=True)

The board that plots data points in animation. Section 3.2

  • draw(x, y, label, every_n=1)

class RNN(d2l.Module)

RNN(num_inputs, num_hiddens)

The RNN model implemented with high-level APIs. Section 9.6

  • forward(inputs, H=None)

class RNNLM(d2l.RNNLMScratch)

The RNN-based language model implemented with high-level APIs. Section 9.6

  • init_params()
  • output_layer(hiddens)

class RNNLMScratch(d2l.Classifier)

RNNLMScratch(rnn, vocab_size, lr=0.01)

The RNN-based language model implemented from scratch. Section 9.5

  • init_params()
  • training_step(batch)
  • validation_step(batch)
  • one_hot(X)
  • output_layer(rnn_outputs)
  • forward(X, state=None)
  • predict(prefix, num_preds, vocab, device=None)

class RNNScratch(d2l.Module)

RNNScratch(num_inputs, num_hiddens, sigma=0.01)

The RNN model implemented from scratch. Section 9.5

  • forward(inputs, state=None)

class RandomGenerator

RandomGenerator(sampling_weights)

Randomly draw among {1, …, n} according to n sampling weights. Section 15.3

  • draw()

class ResNeXtBlock(nn.Module)

ResNeXtBlock(num_channels, groups, bot_mul, use_1x1conv=False, strides=1)

The ResNeXt block. Section 8.6

  • forward(X)

class Residual(nn.Module)

Residual(num_channels, use_1x1conv=False, strides=1)

The Residual block of ResNet models. Section 8.6

  • forward(X)

class SGD(d2l.HyperParameters)

SGD(params, lr)

Minibatch stochastic gradient descent. Section 3.4

  • step()
  • zero_grad()

class SNLIDataset(torch.utils.data.Dataset)

SNLIDataset(dataset, num_steps, vocab=None)

A customized dataset to load the SNLI dataset. Section 16.4

class Seq2Seq(d2l.EncoderDecoder)

Seq2Seq(encoder, decoder, tgt_pad, lr)

The RNN encoder–decoder for sequence to sequence learning. Section 10.7.3

  • validation_step(batch)
  • configure_optimizers()

class Seq2SeqEncoder(d2l.Encoder)

Seq2SeqEncoder(vocab_size, embed_size, num_hiddens, num_layers, dropout=0)

The RNN encoder for sequence-to-sequence learning. Section 10.7

  • forward(X, *args)

class SoftmaxRegression(d2l.Classifier)

SoftmaxRegression(num_outputs, lr)

The softmax regression model. Section 4.5

  • forward(X)

class SyntheticRegressionData(d2l.DataModule)

SyntheticRegressionData(w, b, noise=0.01, num_train=1000, num_val=1000, batch_size=32)

Synthetic data for linear regression. Section 3.3

  • get_dataloader(train)

class TimeMachine(d2l.DataModule)

TimeMachine(batch_size, num_steps, num_train=10000, num_val=5000)

The Time Machine dataset. Section 9.2

  • build(raw_text, vocab=None)
  • get_dataloader(train)

class Timer

Record multiple running times. Section 12.5

  • start() — Start the timer.
  • stop() — Stop the timer and record the time in a list.
  • avg() — Return the average time.
  • sum() — Return the sum of time.
  • cumsum() — Return the accumulated time.

class TokenEmbedding

TokenEmbedding(embedding_name)

Token Embedding. Section 15.7

class Trainer(d2l.HyperParameters)

Trainer(max_epochs, num_gpus=0, gradient_clip_val=0)

The base class for training models with data. Section 3.2

  • prepare_data(data)
  • fit(model, data)
  • fit_epoch()
  • prepare_batch(batch)
  • prepare_model(model)
  • clip_gradients(grad_clip_val, model)

class TransformerEncoder(d2l.Encoder)

TransformerEncoder(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_blks, dropout, use_bias=False)

The Transformer encoder. Section 11.7

  • forward(X, valid_lens)

class TransformerEncoderBlock(nn.Module)

TransformerEncoderBlock(num_hiddens, ffn_num_hiddens, num_heads, dropout, use_bias=False)

The Transformer encoder block. Section 11.7

  • forward(X, valid_lens)

class VOCSegDataset(torch.utils.data.Dataset)

VOCSegDataset(is_train, crop_size, voc_dir)

A customized dataset to load the VOC dataset. Section 14.9

  • normalize_image(img)
  • filter(imgs)

class Vocab

Vocab(tokens=[], min_freq=0, reserved_tokens=[])

Vocabulary for text. Section 9.2

  • to_tokens(indices)
  • unk()

class Accumulator

Accumulator(n)

For accumulating sums over n variables. Section 23.7

  • add(*args)
  • reset()

class AddNorm(tf.keras.layers.Layer)

AddNorm(norm_shape, dropout)

The residual connection followed by layer normalization. Section 11.7

  • call(X, Y, training=False, **kwargs)

class AdditiveAttention(tf.keras.layers.Layer)

AdditiveAttention(key_size, query_size, num_hiddens, dropout, **kwargs)

Additive attention. Section 11.3

  • call(queries, keys, values, valid_lens, training=False, **kwargs)

class Animator

Animator(xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1, figsize=(3.5, 2.5))

For plotting data in animation. Section 23.7

  • add(x, y)

class AttentionDecoder(d2l.Decoder)

The base attention-based decoder interface. Section 11.4

  • attention_weights()

class Benchmark

Benchmark(description='Done')

For measuring running time. Section 13.1

class Classifier(d2l.Module)

The base class of classification models. Section 4.3

  • validation_step(batch)
  • accuracy(Y_hat, Y, averaged=True) — Compute the number of correct predictions. Section 4.3
  • loss(Y_hat, Y, averaged=True)
  • layer_summary(X_shape)

class DataModule(d2l.HyperParameters)

DataModule(root='../data')

The base class of data. Section 3.2

  • get_dataloader(train)
  • train_dataloader()
  • val_dataloader()
  • get_tensorloader(tensors, train, indices=slice(0, None))

class Decoder(tf.keras.layers.Layer)

The base decoder interface for the encoder–decoder architecture. Section 10.6

  • init_state(enc_all_outputs, *args)
  • call(X, state)

class DotProductAttention(tf.keras.layers.Layer)

DotProductAttention(dropout)

Scaled dot product attention. Section 11.3

  • call(queries, keys, values, valid_lens=None, training=False, **kwargs)

class Encoder(tf.keras.layers.Layer)

The base encoder interface for the encoder–decoder architecture. Section 10.6

  • call(X, *args)

class EncoderDecoder(d2l.Classifier)

EncoderDecoder(encoder, decoder)

The base class for the encoder–decoder architecture. Section 10.6

  • call(enc_X, dec_X, *args)
  • predict_step(batch, device, num_steps, save_attention_weights=False)

class FashionMNIST(d2l.DataModule)

FashionMNIST(batch_size=64, resize=(28, 28))

The Fashion-MNIST dataset. Section 4.2

  • text_labels(indices) — Return text labels. Section 4.2
  • get_dataloader(train)
  • visualize(batch, nrows=1, ncols=8, labels=[])

class GRU(d2l.RNN)

GRU(num_hiddens, num_layers, dropout=0)

The multilayer GRU model. Section 10.3

  • forward(X, state=None)

class HyperParameters

The base class of hyperparameters. Section 3.2

  • save_hyperparameters(ignore=[]) — Save function arguments into class attributes. Section 23.7

class LeNet(d2l.Classifier)

LeNet(lr=0.1, num_classes=10)

The LeNet-5 model. Section 7.6

class LinearRegression(d2l.Module)

LinearRegression(lr)

The linear regression model implemented with high-level APIs. Section 3.5

  • forward(X)
  • loss(y_hat, y)
  • configure_optimizers()
  • get_w_b()

class LinearRegressionScratch(d2l.Module)

LinearRegressionScratch(num_inputs, lr, sigma=0.01)

The linear regression model implemented from scratch. Section 3.4

  • forward(X)
  • loss(y_hat, y)
  • configure_optimizers()

class MTFraEng(d2l.DataModule)

MTFraEng(batch_size, num_steps=9, num_train=512, num_val=128)

The English-French dataset. Section 10.5

  • get_dataloader(train)
  • build(src_sentences, tgt_sentences)

class MaskedSoftmaxCELoss(tf.keras.losses.Loss)

MaskedSoftmaxCELoss(valid_len)

The softmax cross-entropy loss with masks. Section 23.7

  • call(label, pred)

class Module(d2l.nn_Module, d2l.HyperParameters)

Module(plot_train_per_epoch=2, plot_valid_per_epoch=1)

The base class of models. Section 3.2

  • loss(y_hat, y)
  • forward(X)
  • call(X, *args, training=None, **kwargs)
  • plot(key, value, train) — Plot a point in animation.
  • training_step(batch)
  • validation_step(batch)
  • configure_optimizers()

class MultiHeadAttention(d2l.Module)

MultiHeadAttention(key_size, query_size, value_size, num_hiddens, num_heads, dropout, bias=False, **kwargs)

Multi-head attention. Section 11.5

  • call(queries, keys, values, valid_lens, training=False, **kwargs)
  • transpose_qkv(X) — Transposition for parallel computation of multiple attention heads. Section 11.5
  • transpose_output(X) — Reverse the operation of transpose_qkv. Section 11.5

class PositionWiseFFN(tf.keras.layers.Layer)

PositionWiseFFN(ffn_num_hiddens, ffn_num_outputs)

The positionwise feed-forward network. Section 11.7

  • call(X)

class PositionalEncoding(tf.keras.layers.Layer)

PositionalEncoding(num_hiddens, dropout, max_len=1000)

Positional encoding. Section 11.6

  • call(X, training=False, **kwargs)

class ProgressBoard(d2l.HyperParameters)

ProgressBoard(xlabel=None, ylabel=None, xlim=None, ylim=None, xscale='linear', yscale='linear', ls=['-', '--', '-.', ':'], colors=['C0', 'C1', 'C2', 'C3'], fig=None, axes=None, figsize=(3.5, 2.5), display=True)

The board that plots data points in animation. Section 3.2

  • draw(x, y, label, every_n=1)

class RNN(d2l.Module)

RNN(num_hiddens)

The RNN model implemented with high-level APIs. Section 9.6

  • forward(inputs, H=None)

class RNNLM(d2l.RNNLMScratch)

The RNN-based language model implemented with high-level APIs. Section 9.6

  • init_params()
  • output_layer(hiddens)

class RNNLMScratch(d2l.Classifier)

RNNLMScratch(rnn, vocab_size, lr=0.01)

The RNN-based language model implemented from scratch. Section 9.5

  • init_params()
  • training_step(batch)
  • validation_step(batch)
  • one_hot(X)
  • output_layer(rnn_outputs)
  • forward(X, state=None)
  • predict(prefix, num_preds, vocab, device=None)

class RNNScratch(d2l.Module)

RNNScratch(num_inputs, num_hiddens, sigma=0.01)

The RNN model implemented from scratch. Section 9.5

  • forward(inputs, state=None)

class RandomGenerator

RandomGenerator(sampling_weights)

Randomly draw among {1, …, n} according to n sampling weights. Section 15.3

  • draw()

class ResNeXtBlock(tf.keras.Model)

ResNeXtBlock(num_channels, groups, bot_mul, use_1x1conv=False, strides=1)

The ResNeXt block. Section 8.6

  • call(X)

class Residual(tf.keras.Model)

Residual(num_channels, use_1x1conv=False, strides=1)

The Residual block of ResNet models. Section 8.6

  • call(X)

class SGD(d2l.HyperParameters)

SGD(lr)

Minibatch stochastic gradient descent. Section 3.4

  • apply_gradients(grads_and_vars)

class Seq2Seq(d2l.EncoderDecoder)

Seq2Seq(encoder, decoder, tgt_pad, lr)

The RNN encoder–decoder for sequence to sequence learning. Section 10.7.3

  • validation_step(batch)
  • configure_optimizers()

class Seq2SeqEncoder(d2l.Encoder)

Seq2SeqEncoder(vocab_size, embed_size, num_hiddens, num_layers, dropout=0)

The RNN encoder for sequence-to-sequence learning. Section 10.7

  • call(X, *args)

class SoftmaxRegression(d2l.Classifier)

SoftmaxRegression(num_outputs, lr)

The softmax regression model. Section 4.5

  • forward(X)

class SyntheticRegressionData(d2l.DataModule)

SyntheticRegressionData(w, b, noise=0.01, num_train=1000, num_val=1000, batch_size=32)

Synthetic data for linear regression. Section 3.3

  • get_dataloader(train)

class TimeMachine(d2l.DataModule)

TimeMachine(batch_size, num_steps, num_train=10000, num_val=5000)

The Time Machine dataset. Section 9.2

  • build(raw_text, vocab=None)
  • get_dataloader(train)

class Timer

Record multiple running times. Section 12.5

  • start() — Start the timer.
  • stop() — Stop the timer and record the time in a list.
  • avg() — Return the average time.
  • sum() — Return the sum of time.
  • cumsum() — Return the accumulated time.

class TokenEmbedding

TokenEmbedding(embedding_name)

Token Embedding. Section 15.7

class TrainCallback(tf.keras.callbacks.Callback)

TrainCallback(net, train_iter, test_iter, num_epochs, device_name)

A callback to visiualize the training progress. Section 23.7

  • on_epoch_begin(epoch, logs=None)
  • on_epoch_end(epoch, logs)

class Trainer(d2l.HyperParameters)

Trainer(max_epochs, num_gpus=0, gradient_clip_val=0)

The base class for training models with data. Section 3.2

  • prepare_data(data)
  • prepare_model(model)
  • fit(model, data)
  • prepare_batch(batch)
  • fit_epoch()
  • clip_gradients(grad_clip_val, grads)

class TransformerEncoder(d2l.Encoder)

TransformerEncoder(vocab_size, key_size, query_size, value_size, num_hiddens, norm_shape, ffn_num_hiddens, num_heads, num_blks, dropout, bias=False)

The Transformer encoder. Section 11.7

  • call(X, valid_lens, training=False, **kwargs)

class TransformerEncoderBlock(tf.keras.layers.Layer)

TransformerEncoderBlock(key_size, query_size, value_size, num_hiddens, norm_shape, ffn_num_hiddens, num_heads, dropout, bias=False)

The Transformer encoder block. Section 11.7

  • call(X, valid_lens, training=False, **kwargs)

class Vocab

Vocab(tokens=[], min_freq=0, reserved_tokens=[])

Vocabulary for text. Section 9.2

  • to_tokens(indices)
  • unk()

class Accumulator

Accumulator(n)

For accumulating sums over n variables. Section 23.7

  • add(*args)
  • reset()

class AddNorm(nn.Module)

The residual connection followed by layer normalization. Section 11.7

class Animator

Animator(xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1, figsize=(3.5, 2.5))

For plotting data in animation. Section 23.7

  • add(x, y)

class ArrayDataLoader

ArrayDataLoader(*args, batch_size=32, shuffle=False, drop_last=False, **kwargs)

A simple data loader for JAX that batches arrays or dataset objects. Section 23.7

class BERTEncoder(nn.Module)

BERT encoder. Section 15.8

  • setup()

class BERTModel(nn.Module)

The BERT model. Section 15.8

  • setup()

class BananasDataset

BananasDataset(is_train)

A customized dataset to load the banana detection dataset. Section 14.6

class Benchmark

Benchmark(description='Done')

For measuring running time. Section 13.1

class Classifier(d2l.Module)

The base class of classification models. Section 4.3

  • training_step(params, batch, state)
  • validation_step(params, batch, state)
  • accuracy(params, X, Y, state, averaged=True) — Compute the number of correct predictions. Section 4.3
  • layer_summary(X_shape, key=d2l.get_key())
  • loss(params, X, Y, state, averaged=True)

class DataModule(d2l.HyperParameters)

DataModule(root='../data')

The base class of data. Section 3.2

  • get_dataloader(train)
  • train_dataloader()
  • val_dataloader()
  • get_tensorloader(tensors, train, indices=slice(0, None))

class Decoder(nn.Module)

The base decoder interface for the encoder–decoder architecture. Section 10.6

  • setup()
  • init_state(enc_all_outputs, *args)

class DotProductAttention(nn.Module)

Scaled dot product attention. Section 11.3

class Encoder(nn.Module)

The base encoder interface for the encoder–decoder architecture. Section 10.6

  • setup()

class EncoderDecoder(d2l.Classifier)

The base class for the encoder–decoder architecture. Section 10.6

  • predict_step(params, batch, num_steps, save_attention_weights=False)

class FashionMNIST(d2l.DataModule)

FashionMNIST(batch_size=64, resize=(28, 28))

The Fashion-MNIST dataset. Section 4.2

  • text_labels(indices) — Return text labels. Section 4.2
  • get_dataloader(train)
  • visualize(batch, nrows=1, ncols=8, labels=[])

class GRU(d2l.RNN)

The multilayer GRU model. Section 10.3

class HyperParameters

The base class of hyperparameters. Section 3.2

  • save_hyperparameters(ignore=[]) — Save function arguments into class attributes. Section 23.7

class LeNet(d2l.Classifier)

The LeNet-5 model. Section 7.6

  • setup()

class LinearRegression(d2l.Module)

The linear regression model implemented with high-level APIs. Section 3.5

  • setup()
  • forward(X)
  • loss(params, X, y, state)
  • configure_optimizers()
  • get_w_b(state)

class LinearRegressionScratch(d2l.Module)

The linear regression model implemented from scratch. Section 3.4

  • setup()
  • forward(X)
  • loss(params, X, y, state)
  • configure_optimizers()

class MTFraEng(d2l.DataModule)

MTFraEng(batch_size, num_steps=9, num_train=512, num_val=128)

The English-French dataset. Section 10.5

  • get_dataloader(train)
  • build(src_sentences, tgt_sentences)

class MaskLM(nn.Module)

The masked language model task of BERT. Section 15.8

class Module(d2l.nn_Module, d2l.HyperParameters)

The base class of models. Section 3.2

  • loss(y_hat, y)
  • forward(X, *args, **kwargs)
  • plot(key, value, train) — Plot a point in animation.
  • training_step(params, batch, state)
  • validation_step(params, batch, state)
  • configure_optimizers()
  • apply_init(dummy_input, key)

class NextSentencePred(nn.Module)

The next sentence prediction task of BERT. Section 15.8

class PositionWiseFFN(nn.Module)

The positionwise feed-forward network. Section 11.7

  • setup()

class PositionalEncoding(nn.Module)

Positional encoding. Section 11.6

  • setup()

class ProgressBoard(d2l.HyperParameters)

ProgressBoard(xlabel=None, ylabel=None, xlim=None, ylim=None, xscale='linear', yscale='linear', ls=['-', '--', '-.', ':'], colors=['C0', 'C1', 'C2', 'C3'], fig=None, axes=None, figsize=(3.5, 2.5), display=True)

The board that plots data points in animation. Section 3.2

  • draw(x, y, label, every_n=1)

class RNN(nn.Module)

The RNN model implemented with high-level APIs. Section 9.6

class RNNLM(d2l.RNNLMScratch)

The RNN-based language model implemented with high-level APIs. Section 9.6

  • setup()
  • output_layer(hiddens)
  • forward(X, state=None)

class RNNLMScratch(d2l.Classifier)

The RNN-based language model implemented from scratch. Section 9.5

  • setup()
  • training_step(params, batch, state)
  • validation_step(params, batch, state)
  • one_hot(X)
  • output_layer(rnn_outputs)
  • forward(X, state=None)
  • predict(prefix, num_preds, vocab, params)

class RNNScratch(nn.Module)

The RNN model implemented from scratch. Section 9.5

  • setup()

class RandomGenerator

RandomGenerator(sampling_weights)

Randomly draw among {1, …, n} according to n sampling weights. Section 15.3

  • draw()

class ResNeXtBlock(nn.Module)

The ResNeXt block. Section 8.6

  • setup()

class ResNet18(nn.Module)

A slightly modified ResNet-18 model. Section 13.6

  • setup()

class Residual(nn.Module)

The Residual block of ResNet models. Section 8.6

  • setup()

class SGD(d2l.HyperParameters)

SGD(lr)

Minibatch stochastic gradient descent. Section 3.4

  • init(params)
  • update(updates, state, params=None)

class SNLIDataset

SNLIDataset(dataset, num_steps, vocab=None)

A customized dataset to load the SNLI dataset. Section 16.4

class Seq2Seq(d2l.EncoderDecoder)

The RNN encoder–decoder for sequence to sequence learning. Section 10.7.3

  • validation_step(params, batch, state)
  • configure_optimizers()

class Seq2SeqEncoder(d2l.Encoder)

The RNN encoder for sequence-to-sequence learning. Section 10.7

  • setup()

class SyntheticRegressionData(d2l.DataModule)

SyntheticRegressionData(w, b, noise=0.01, num_train=1000, num_val=1000, batch_size=32)

Synthetic data for linear regression. Section 3.3

  • get_dataloader(train)

class TimeMachine(d2l.DataModule)

TimeMachine(batch_size, num_steps, num_train=10000, num_val=5000)

The Time Machine dataset. Section 9.2

  • build(raw_text, vocab=None)
  • get_dataloader(train)

class Timer

Record multiple running times. Section 12.5

  • start() — Start the timer.
  • stop() — Stop the timer and record the time in a list.
  • avg() — Return the average time.
  • sum() — Return the sum of time.
  • cumsum() — Return the accumulated time.

class TokenEmbedding

TokenEmbedding(embedding_name)

Token Embedding. Section 15.7

class Trainer(d2l.HyperParameters)

Trainer(max_epochs, num_gpus=0, gradient_clip_val=0)

The base class for training models with data. Section 3.2

  • prepare_data(data)
  • prepare_model(model)
  • fit(model, data, key=None)
  • fit_epoch()
  • prepare_batch(batch)
  • clip_gradients(grad_clip_val, grads)

class TransformerEncoder(d2l.Encoder)

The Transformer encoder. Section 11.7

  • setup()

class TransformerEncoderBlock(nn.Module)

The Transformer encoder block. Section 11.7

  • setup()

class VOCSegDataset

VOCSegDataset(is_train, crop_size, voc_dir)

A customized dataset to load the VOC dataset. Section 14.9

  • normalize_image(img)
  • filter(imgs)

class Vocab

Vocab(tokens=[], min_freq=0, reserved_tokens=[])

Vocabulary for text. Section 9.2

  • to_tokens(indices)
  • unk()

class Accumulator

Accumulator(n)

For accumulating sums over n variables. Section 23.7

  • add(*args)
  • reset()

class AddNorm(nn.Block)

AddNorm(dropout)

The residual connection followed by layer normalization. Section 11.7

  • forward(X, Y)

class AdditiveAttention(nn.Block)

AdditiveAttention(num_hiddens, dropout, **kwargs)

Additive attention. Section 11.3

  • forward(queries, keys, values, valid_lens)

class Animator

Animator(xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1, figsize=(3.5, 2.5))

For plotting data in animation. Section 23.7

  • add(x, y)

class AttentionDecoder(d2l.Decoder)

The base attention-based decoder interface. Section 11.4

  • attention_weights()

class BERTEncoder(nn.Block)

BERTEncoder(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_blks, dropout, max_len=1000, **kwargs)

BERT encoder. Section 15.8

  • forward(tokens, segments, valid_lens)

class BERTModel(nn.Block)

BERTModel(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_blks, dropout, max_len=1000)

The BERT model. Section 15.8

  • forward(tokens, segments, valid_lens=None, pred_positions=None)

class BananasDataset(gluon.data.Dataset)

BananasDataset(is_train)

A customized dataset to load the banana detection dataset. Section 14.6

class Benchmark

Benchmark(description='Done')

For measuring running time. Section 13.1

class Classifier(d2l.Module)

The base class of classification models. Section 4.3

  • validation_step(batch)
  • accuracy(Y_hat, Y, averaged=True) — Compute the number of correct predictions. Section 4.3
  • loss(Y_hat, Y, averaged=True)
  • layer_summary(X_shape)

class DataModule(d2l.HyperParameters)

DataModule(root='../data', num_workers=4)

The base class of data. Section 3.2

  • get_dataloader(train)
  • train_dataloader()
  • val_dataloader()
  • get_tensorloader(tensors, train, indices=slice(0, None))

class Decoder(nn.Block)

The base decoder interface for the encoder–decoder architecture. Section 10.6

  • init_state(enc_all_outputs, *args)
  • forward(X, state)

class DotProductAttention(nn.Block)

DotProductAttention(dropout)

Scaled dot product attention. Section 11.3

  • forward(queries, keys, values, valid_lens=None)

class Encoder(nn.Block)

The base encoder interface for the encoder–decoder architecture. Section 10.6

  • forward(X, *args)

class EncoderDecoder(d2l.Classifier)

EncoderDecoder(encoder, decoder)

The base class for the encoder–decoder architecture. Section 10.6

  • forward(enc_X, dec_X, *args)
  • predict_step(batch, device, num_steps, save_attention_weights=False)

class FashionMNIST(d2l.DataModule)

FashionMNIST(batch_size=64, resize=(28, 28))

The Fashion-MNIST dataset. Section 4.2

  • text_labels(indices) — Return text labels. Section 4.2
  • get_dataloader(train)
  • visualize(batch, nrows=1, ncols=8, labels=[])

class GRU(d2l.RNN)

GRU(num_hiddens, num_layers, dropout=0)

The multilayer GRU model. Section 10.3

class HyperParameters

The base class of hyperparameters. Section 3.2

  • save_hyperparameters(ignore=[]) — Save function arguments into class attributes. Section 23.7

class LeNet(d2l.Classifier)

LeNet(lr=0.1, num_classes=10)

The LeNet-5 model. Section 7.6

class LinearRegression(d2l.Module)

LinearRegression(lr)

The linear regression model implemented with high-level APIs. Section 3.5

  • forward(X)
  • loss(y_hat, y)
  • configure_optimizers()
  • get_w_b()

class LinearRegressionScratch(d2l.Module)

LinearRegressionScratch(num_inputs, lr, sigma=0.01)

The linear regression model implemented from scratch. Section 3.4

  • forward(X)
  • loss(y_hat, y)
  • configure_optimizers()

class MTFraEng(d2l.DataModule)

MTFraEng(batch_size, num_steps=9, num_train=512, num_val=128)

The English-French dataset. Section 10.5

  • get_dataloader(train)
  • build(src_sentences, tgt_sentences)

class MaskLM(nn.Block)

MaskLM(vocab_size, num_hiddens, **kwargs)

The masked language model task of BERT. Section 15.8

  • forward(X, pred_positions)

class MaskedSoftmaxCELoss(gluon.loss.SoftmaxCELoss)

The softmax cross-entropy loss with masks. Section 23.7

  • forward(pred, label, valid_len)

class Module(d2l.nn_Module, d2l.HyperParameters)

Module(plot_train_per_epoch=2, plot_valid_per_epoch=1)

The base class of models. Section 3.2

  • loss(y_hat, y)
  • forward(X)
  • plot(key, value, train) — Plot a point in animation.
  • training_step(batch)
  • validation_step(batch)
  • configure_optimizers()
  • get_scratch_params()
  • parameters()
  • set_scratch_params_device(device)

class MultiHeadAttention(d2l.Module)

MultiHeadAttention(num_hiddens, num_heads, dropout, use_bias=False, **kwargs)

Multi-head attention. Section 11.5

  • forward(queries, keys, values, valid_lens)
  • transpose_qkv(X) — Transposition for parallel computation of multiple attention heads. Section 11.5
  • transpose_output(X) — Reverse the operation of transpose_qkv. Section 11.5

class NextSentencePred(nn.Block)

NextSentencePred(**kwargs)

The next sentence prediction task of BERT. Section 15.8

  • forward(X)

class PositionWiseFFN(nn.Block)

PositionWiseFFN(ffn_num_hiddens, ffn_num_outputs)

The positionwise feed-forward network. Section 11.7

  • forward(X)

class PositionalEncoding(nn.Block)

PositionalEncoding(num_hiddens, dropout, max_len=1000)

Positional encoding. Section 11.6

  • forward(X)

class ProgressBoard(d2l.HyperParameters)

ProgressBoard(xlabel=None, ylabel=None, xlim=None, ylim=None, xscale='linear', yscale='linear', ls=['-', '--', '-.', ':'], colors=['C0', 'C1', 'C2', 'C3'], fig=None, axes=None, figsize=(3.5, 2.5), display=True)

The board that plots data points in animation. Section 3.2

  • draw(x, y, label, every_n=1)

class RNN(d2l.Module)

RNN(num_hiddens)

The RNN model implemented with high-level APIs. Section 9.6

  • forward(inputs, H=None)

class RNNLM(d2l.RNNLMScratch)

The RNN-based language model implemented with high-level APIs. Section 9.6

  • init_params()
  • output_layer(hiddens)

class RNNLMScratch(d2l.Classifier)

RNNLMScratch(rnn, vocab_size, lr=0.01)

The RNN-based language model implemented from scratch. Section 9.5

  • init_params()
  • training_step(batch)
  • validation_step(batch)
  • one_hot(X)
  • output_layer(rnn_outputs)
  • forward(X, state=None)
  • predict(prefix, num_preds, vocab, device=None)

class RNNScratch(d2l.Module)

RNNScratch(num_inputs, num_hiddens, sigma=0.01)

The RNN model implemented from scratch. Section 9.5

  • forward(inputs, state=None)

class RandomGenerator

RandomGenerator(sampling_weights)

Randomly draw among {1, …, n} according to n sampling weights. Section 15.3

  • draw()

class ResNeXtBlock(nn.Block)

ResNeXtBlock(num_channels, groups, bot_mul, use_1x1conv=False, strides=1, **kwargs)

The ResNeXt block. Section 8.6

  • forward(X)

class Residual(nn.Block)

Residual(num_channels, use_1x1conv=False, strides=1, **kwargs)

The Residual block of ResNet models. Section 8.6

  • forward(X)

class SGD(d2l.HyperParameters)

SGD(params, lr)

Minibatch stochastic gradient descent. Section 3.4

  • step(_)

class SNLIDataset(gluon.data.Dataset)

SNLIDataset(dataset, num_steps, vocab=None)

A customized dataset to load the SNLI dataset. Section 16.4

class Seq2Seq(d2l.EncoderDecoder)

Seq2Seq(encoder, decoder, tgt_pad, lr)

The RNN encoder–decoder for sequence to sequence learning. Section 10.7.3

  • validation_step(batch)
  • configure_optimizers()

class Seq2SeqEncoder(d2l.Encoder)

Seq2SeqEncoder(vocab_size, embed_size, num_hiddens, num_layers, dropout=0)

The RNN encoder for sequence-to-sequence learning. Section 10.7

  • forward(X, *args)

class SoftmaxRegression(d2l.Classifier)

SoftmaxRegression(num_outputs, lr)

The softmax regression model. Section 4.5

  • forward(X)

class SyntheticRegressionData(d2l.DataModule)

SyntheticRegressionData(w, b, noise=0.01, num_train=1000, num_val=1000, batch_size=32)

Synthetic data for linear regression. Section 3.3

  • get_dataloader(train)

class TimeMachine(d2l.DataModule)

TimeMachine(batch_size, num_steps, num_train=10000, num_val=5000)

The Time Machine dataset. Section 9.2

  • build(raw_text, vocab=None)
  • get_dataloader(train)

class Timer

Record multiple running times. Section 12.5

  • start() — Start the timer.
  • stop() — Stop the timer and record the time in a list.
  • avg() — Return the average time.
  • sum() — Return the sum of time.
  • cumsum() — Return the accumulated time.

class TokenEmbedding

TokenEmbedding(embedding_name)

Token Embedding. Section 15.7

class Trainer(d2l.HyperParameters)

Trainer(max_epochs, num_gpus=0, gradient_clip_val=0)

The base class for training models with data. Section 3.2

  • prepare_data(data)
  • fit(model, data)
  • fit_epoch()
  • prepare_batch(batch)
  • prepare_model(model)
  • clip_gradients(grad_clip_val, model)

class TransformerEncoder(d2l.Encoder)

TransformerEncoder(vocab_size, num_hiddens, ffn_num_hiddens, num_heads, num_blks, dropout, use_bias=False)

The Transformer encoder. Section 11.7

  • forward(X, valid_lens)

class TransformerEncoderBlock(nn.Block)

TransformerEncoderBlock(num_hiddens, ffn_num_hiddens, num_heads, dropout, use_bias=False)

The Transformer encoder block. Section 11.7

  • forward(X, valid_lens)

class VOCSegDataset(gluon.data.Dataset)

VOCSegDataset(is_train, crop_size, voc_dir)

A customized dataset to load the VOC dataset. Section 14.9

  • normalize_image(img)
  • filter(imgs)

class Vocab

Vocab(tokens=[], min_freq=0, reserved_tokens=[])

Vocabulary for text. Section 9.2

  • to_tokens(indices)
  • unk()

23.8.2 Functions

accuracy

accuracy(y_hat, y)

Compute the number of correct predictions. Section 23.7

add_to_class

add_to_class(Class)

Register functions as methods in created class. Section 3.2

assign_anchor_to_bbox

assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5)

Assign closest ground-truth bounding boxes to anchor boxes. Section 14.4

batchify

batchify(data)

Return a minibatch of examples for skip-gram with negative sampling. Section 15.3

bbox_to_rect

bbox_to_rect(bbox, color)

Convert bounding box to matplotlib format. Section 14.3

bleu

bleu(pred_seq, label_seq, k)

Compute the BLEU. Section 10.7.6

box_center_to_corner

box_center_to_corner(boxes)

Convert from (center, width, height) to (upper-left, lower-right). Section 14.3

box_corner_to_center

box_corner_to_center(boxes)

Convert from (upper-left, lower-right) to (center, width, height). Section 14.3

box_iou

box_iou(boxes1, boxes2)

Compute pairwise IoU across two lists of anchor or bounding boxes. Section 14.4

build_array_nmt

build_array_nmt(lines, vocab, num_steps)

Transform text sequences of machine translation into minibatches. Section 23.7

check_len

check_len(a, n)

Check the length of a list. Section 9.5

check_shape

check_shape(a, shape)

Check the shape of a tensor. Section 9.5

copyfile

copyfile(filename, target_dir)

Copy a file into a target directory. Section 14.13

corr2d

corr2d(X, K)

Compute 2D cross-correlation. Section 7.2

cpu

cpu()

Get the CPU device. Section 6.7

download

download(url, folder='../data', sha1_hash=None)

Download a file to folder and return the local filepath. Section 23.7

download_extract

download_extract(name, folder=None)

Download and extract a zip/tar file. Section 23.7

evaluate_accuracy_gpu

evaluate_accuracy_gpu(net, data_iter, device=None)

Compute the accuracy for a model on a dataset using a GPU. Section 23.7

evaluate_loss

evaluate_loss(net, data_iter, loss)

Evaluate the loss of a model on the given dataset. Section 23.7

extract

extract(filename, folder=None)

Extract a zip/tar file into folder. Section 23.7

get_centers_and_contexts

get_centers_and_contexts(corpus, max_window_size)

Return center words and context words in skip-gram. Section 15.3

get_dataloader_workers

get_dataloader_workers()

Use 4 processes to read the data. Section 23.7

get_fashion_mnist_labels

get_fashion_mnist_labels(labels)

Return text labels for the Fashion-MNIST dataset. Section 23.7

get_negatives

get_negatives(all_contexts, vocab, counter, K)

Return noise words in negative sampling. Section 15.3

get_tokens_and_segments

get_tokens_and_segments(tokens_a, tokens_b=None)

Get tokens of the BERT input sequence and their segment IDs. Section 15.8

gpu

gpu(i=0)

Get a GPU device. Section 6.7

grad_clipping

grad_clipping(net, theta)

Clip the gradient. Section 23.7

init_cnn

init_cnn(module)

Initialize weights for CNNs. Section 7.6

init_seq2seq

init_seq2seq(module)

Initialize weights for sequence-to-sequence learning. Section 10.7

linreg

linreg(X, w, b)

The linear regression model. Section 23.7

load_array

load_array(data_arrays, batch_size, is_train=True)

Construct a PyTorch data iterator. Section 23.7

load_data_bananas

load_data_bananas(batch_size)

Load the banana detection dataset. Section 14.6

load_data_fashion_mnist

load_data_fashion_mnist(batch_size, resize=None)

Download the Fashion-MNIST dataset and then load it into memory. Section 23.7

load_data_imdb

load_data_imdb(batch_size, num_steps=500)

Return data iterators and the vocabulary of the IMDb review dataset. Section 16.1

load_data_nmt

load_data_nmt(batch_size, num_steps, num_examples=600)

Return the iterator and the vocabularies of the translation dataset. Section 23.7

load_data_ptb

load_data_ptb(batch_size, max_window_size, num_noise_words)

Download the PTB dataset and then load it into memory. Section 15.3

load_data_snli

load_data_snli(batch_size, num_steps=50)

Download the SNLI dataset and return data iterators and vocabulary. Section 16.4

load_data_voc

load_data_voc(batch_size, crop_size)

Load the VOC semantic segmentation dataset. Section 14.9

load_data_wiki

load_data_wiki(batch_size, max_len)

Load the WikiText-2 dataset. Section 15.9

masked_softmax

masked_softmax(X, valid_lens)

Perform softmax operation by masking elements on the last axis. Section 11.3

multibox_detection

multibox_detection(cls_probs, offset_preds, anchors, nms_threshold=0.5, pos_threshold=0.009999999)

Predict bounding boxes using non-maximum suppression. Section 14.4

multibox_prior

multibox_prior(data, sizes, ratios)

Generate anchor boxes with different shapes centered on each pixel. Section 14.4

multibox_target

multibox_target(anchors, labels)

Label anchor boxes using ground-truth bounding boxes. Section 14.4

nms

nms(boxes, scores, iou_threshold)

Sort confidence scores of predicted bounding boxes. Section 14.4

num_gpus

num_gpus()

Get the number of available GPUs. Section 6.7

offset_boxes

offset_boxes(anchors, assigned_bb, eps=1e-06)

Transform for anchor box offsets. Section 14.4

offset_inverse

offset_inverse(anchors, offset_preds)

Predict bounding boxes based on anchor boxes with predicted offsets. Section 14.4

plot

plot(X, Y=None, xlabel=None, ylabel=None, legend=[], xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None)

Plot data points. Section 2.4

predict_sentiment

predict_sentiment(net, vocab, sequence)

Predict the sentiment of a text sequence. Section 16.2

predict_seq2seq

predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, device, save_attention_weights=False)

Predict for sequence to sequence. Section 23.7

predict_snli

predict_snli(net, vocab, premise, hypothesis)

Predict the logical relationship between the premise and hypothesis. Section 16.5

preprocess_nmt

preprocess_nmt(text)

Preprocess the English-French dataset. Section 23.7

read_csv_labels

read_csv_labels(fname)

Read fname to return a filename to label dictionary. Section 14.13

read_data_bananas

read_data_bananas(is_train=True)

Read the banana detection dataset images and labels. Section 14.6

read_data_nmt

read_data_nmt()

Load the English-French dataset. Section 23.7

read_imdb

read_imdb(data_dir, is_train)

Read the IMDb review dataset text sequences and labels. Section 16.1

read_ptb

read_ptb()

Load the PTB dataset into a list of text lines. Section 15.3

read_snli

read_snli(data_dir, is_train)

Read the SNLI dataset into premises, hypotheses, and labels. Section 16.4

read_voc_images

read_voc_images(voc_dir, is_train=True)

Read all VOC feature and label images. Section 14.9

reorg_test

reorg_test(data_dir)

Organize the testing set for data loading during prediction. Section 14.13

reorg_train_valid

reorg_train_valid(data_dir, labels, valid_ratio)

Split the validation set out of the original training set. Section 14.13

resnet18

resnet18(num_classes, in_channels=1)

A slightly modified ResNet-18 model. Section 13.6

sequence_mask

sequence_mask(X, valid_len, value=0)

Mask irrelevant entries in sequences. Section 23.7

set_axes

set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)

Set the axes for matplotlib. Section 2.4

set_figsize

set_figsize(figsize=(3.5, 2.5))

Set the figure size for matplotlib. Section 2.4

sgd

sgd(params, lr, batch_size)

Minibatch stochastic gradient descent. Section 23.7

show_bboxes

show_bboxes(axes, bboxes, labels=None, colors=None)

Show bounding boxes. Section 14.4

show_heatmaps

show_heatmaps(matrices, xlabel, ylabel, titles=None, figsize=(2.5, 2.5), cmap='Reds')

Show heatmaps of matrices. Section 11.1

show_images

show_images(imgs, num_rows, num_cols, titles=None, scale=1.5)

Plot a list of images. Section 23.7

show_list_len_pair_hist

show_list_len_pair_hist(legend, xlabel, ylabel, xlist, ylist)

Plot the histogram for list length pairs. Section 10.5

show_trace_2d

show_trace_2d(f, results)

Show the trace of 2D variables during optimization. Section 12.3

split_batch

split_batch(X, y, devices)

Split X and y into multiple devices. Section 13.5

squared_loss

squared_loss(y_hat, y)

Squared loss. Section 23.7

subsample

subsample(sentences, vocab)

Subsample high-frequency words. Section 15.3

synthetic_data

synthetic_data(w, b, num_examples)

Generate y = Xw + b + noise. Section 23.7

tokenize

tokenize(lines, token='word')

Split text lines into word or character tokens. Section 23.7

tokenize_nmt

tokenize_nmt(text, num_examples=None)

Tokenize the English-French dataset. Section 23.7

train_2d

train_2d(trainer, steps=20, f_grad=None)

Optimize a 2D objective function with a customized trainer. Section 12.3

train_batch_ch13

train_batch_ch13(net, X, y, loss, trainer, devices)

Train for a minibatch with multiple GPUs (defined in Chapter 13). Section 14.1

train_ch13

train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices=d2l.try_all_gpus())

Train a model with multiple GPUs (defined in Chapter 13). Section 14.1

train_ch6

train_ch6(net, train_iter, test_iter, num_epochs, lr, device)

Train a model with a GPU (defined in Chapter 6). Section 23.7

train_seq2seq

train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device)

Train a model for sequence to sequence. Section 23.7

truncate_pad

truncate_pad(line, num_steps, padding_token)

Truncate or pad sequences. Section 23.7

try_all_gpus

try_all_gpus()

Return all available GPUs, or [cpu(),] if no GPU exists. Section 6.7

try_gpu

try_gpu(i=0)

Return gpu(i) if exists, otherwise return cpu(). Section 6.7

update_D

update_D(X, Z, net_D, net_G, loss, trainer_D)

Update discriminator. Section 20.1

update_G

update_G(Z, net_D, net_G, loss, trainer_G)

Update generator. Section 20.1

use_svg_display

use_svg_display()

Use the svg format to display a plot in Jupyter. Section 2.4

voc_colormap2label

voc_colormap2label()

Build the mapping from RGB to class indices for VOC labels. Section 14.9

voc_label_indices

voc_label_indices(colormap, colormap2label)

Map any RGB values in VOC labels to their class indices. Section 14.9

voc_rand_crop

voc_rand_crop(feature, label, height, width)

Randomly crop both feature and label images. Section 14.9

accuracy

accuracy(y_hat, y)

Compute the number of correct predictions. Section 23.7

add_to_class

add_to_class(Class)

Register functions as methods in created class. Section 3.2

batchify

batchify(data)

Return a minibatch of examples for skip-gram with negative sampling. Section 15.3

bbox_to_rect

bbox_to_rect(bbox, color)

Convert bounding box to matplotlib format. Section 14.3

bleu

bleu(pred_seq, label_seq, k)

Compute the BLEU. Section 10.7.6

box_center_to_corner

box_center_to_corner(boxes)

Convert from (center, width, height) to (upper-left, lower-right). Section 14.3

box_corner_to_center

box_corner_to_center(boxes)

Convert from (upper-left, lower-right) to (center, width, height). Section 14.3

build_array_nmt

build_array_nmt(lines, vocab, num_steps)

Transform text sequences of machine translation into minibatches. Section 23.7

check_len

check_len(a, n)

Check the length of a list. Section 9.5

check_shape

check_shape(a, shape)

Check the shape of a tensor. Section 9.5

copyfile

copyfile(filename, target_dir)

Copy a file into a target directory. Section 14.13

corr2d

corr2d(X, K)

Compute 2D cross-correlation. Section 7.2

cpu

cpu()

Get the CPU device. Section 6.7

download

download(url, folder='../data', sha1_hash=None)

Download a file to folder and return the local filepath. Section 23.7

download_extract

download_extract(name, folder=None)

Download and extract a zip/tar file. Section 23.7

evaluate_accuracy

evaluate_accuracy(net, data_iter)

Compute the accuracy for a model on a dataset. Section 23.7

evaluate_loss

evaluate_loss(net, data_iter, loss)

Evaluate the loss of a model on the given dataset. Section 23.7

extract

extract(filename, folder=None)

Extract a zip/tar file into folder. Section 23.7

get_centers_and_contexts

get_centers_and_contexts(corpus, max_window_size)

Return center words and context words in skip-gram. Section 15.3

get_fashion_mnist_labels

get_fashion_mnist_labels(labels)

Return text labels for the Fashion-MNIST dataset. Section 23.7

get_negatives

get_negatives(all_contexts, vocab, counter, K)

Return noise words in negative sampling. Section 15.3

get_tokens_and_segments

get_tokens_and_segments(tokens_a, tokens_b=None)

Get tokens of the BERT input sequence and their segment IDs. Section 15.8

gpu

gpu(i=0)

Get a GPU device. Section 6.7

grad_clipping

grad_clipping(grads, theta)

Clip the gradient. Section 23.7

linreg

linreg(X, w, b)

The linear regression model. Section 23.7

load_array

load_array(data_arrays, batch_size, is_train=True)

Construct a TensorFlow data iterator. Section 23.7

load_data_fashion_mnist

load_data_fashion_mnist(batch_size, resize=None)

Download the Fashion-MNIST dataset and then load it into memory. Section 23.7

load_data_nmt

load_data_nmt(batch_size, num_steps, num_examples=600)

Return the iterator and the vocabularies of the translation dataset. Section 23.7

masked_softmax

masked_softmax(X, valid_lens)

Perform softmax operation by masking elements on the last axis. Section 11.3

num_gpus

num_gpus()

Get the number of available GPUs. Section 6.7

offset_boxes

offset_boxes(anchors, assigned_bb, eps=1e-06)

Transform for anchor box offsets. Section 14.4

offset_inverse

offset_inverse(anchors, offset_preds)

Predict bounding boxes based on anchor boxes with predicted offsets. Section 14.4

plot

plot(X, Y=None, xlabel=None, ylabel=None, legend=[], xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None)

Plot data points. Section 2.4

predict_seq2seq

predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, save_attention_weights=False)

Predict for sequence to sequence. Section 23.7

preprocess_nmt

preprocess_nmt(text)

Preprocess the English-French dataset. Section 23.7

read_csv_labels

read_csv_labels(fname)

Read fname to return a filename to label dictionary. Section 14.13

read_data_nmt

read_data_nmt()

Load the English-French dataset. Section 23.7

read_imdb

read_imdb(data_dir, is_train)

Read the IMDb review dataset text sequences and labels. Section 16.1

read_ptb

read_ptb()

Load the PTB dataset into a list of text lines. Section 15.3

read_snli

read_snli(data_dir, is_train)

Read the SNLI dataset into premises, hypotheses, and labels. Section 16.4

reorg_test

reorg_test(data_dir)

Organize the testing set for data loading during prediction. Section 14.13

reorg_train_valid

reorg_train_valid(data_dir, labels, valid_ratio)

Split the validation set out of the original training set. Section 14.13

sequence_mask

sequence_mask(X, valid_len, value=0)

Mask irrelevant entries in sequences. Section 23.7

set_axes

set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)

Set the axes for matplotlib. Section 2.4

set_figsize

set_figsize(figsize=(3.5, 2.5))

Set the figure size for matplotlib. Section 2.4

sgd

sgd(params, grads, lr, batch_size)

Minibatch stochastic gradient descent. Section 23.7

show_bboxes

show_bboxes(axes, bboxes, labels=None, colors=None)

Show bounding boxes. Section 14.4

show_heatmaps

show_heatmaps(matrices, xlabel, ylabel, titles=None, figsize=(2.5, 2.5), cmap='Reds')

Show heatmaps of matrices. Section 11.1

show_images

show_images(imgs, num_rows, num_cols, titles=None, scale=1.5)

Plot a list of images. Section 23.7

show_list_len_pair_hist

show_list_len_pair_hist(legend, xlabel, ylabel, xlist, ylist)

Plot the histogram for list length pairs. Section 10.5

show_trace_2d

show_trace_2d(f, results)

Show the trace of 2D variables during optimization. Section 12.3

squared_loss

squared_loss(y_hat, y)

Squared loss. Section 23.7

subsample

subsample(sentences, vocab)

Subsample high-frequency words. Section 15.3

synthetic_data

synthetic_data(w, b, num_examples)

Generate y = Xw + b + noise. Section 23.7

tokenize

tokenize(lines, token='word')

Split text lines into word or character tokens. Section 23.7

tokenize_nmt

tokenize_nmt(text, num_examples=None)

Tokenize the English-French dataset. Section 23.7

train_2d

train_2d(trainer, steps=20, f_grad=None)

Optimize a 2D objective function with a customized trainer. Section 12.3

train_ch6

train_ch6(net_fn, train_iter, test_iter, num_epochs, lr, device)

Train a model with a GPU (defined in Chapter 6). Section 23.7

train_seq2seq

train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device)

Train a model for sequence to sequence. Section 23.7

truncate_pad

truncate_pad(line, num_steps, padding_token)

Truncate or pad sequences. Section 23.7

try_all_gpus

try_all_gpus()

Return all available GPUs, or [cpu(),] if no GPU exists. Section 6.7

try_gpu

try_gpu(i=0)

Return gpu(i) if exists, otherwise return cpu(). Section 6.7

update_D

update_D(X, Z, net_D, net_G, loss, optimizer_D)

Update discriminator. Section 20.1

update_G

update_G(Z, net_D, net_G, loss, optimizer_G)

Update generator. Section 20.1

use_svg_display

use_svg_display()

Use the svg format to display a plot in Jupyter. Section 2.4

add_to_class

add_to_class(Class)

Register functions as methods in created class. Section 3.2

assign_anchor_to_bbox

assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5)

Assign closest ground-truth bounding boxes to anchor boxes. Section 14.4

batchify

batchify(data)

Return a minibatch of examples for skip-gram with negative sampling. Section 15.3

bbox_to_rect

bbox_to_rect(bbox, color)

Convert bounding box to matplotlib format. Section 14.3

bleu

bleu(pred_seq, label_seq, k)

Compute the BLEU. Section 10.7.6

box_center_to_corner

box_center_to_corner(boxes)

Convert from (center, width, height) to (upper-left, lower-right). Section 14.3

box_corner_to_center

box_corner_to_center(boxes)

Convert from (upper-left, lower-right) to (center, width, height). Section 14.3

box_iou

box_iou(boxes1, boxes2)

Compute pairwise IoU across two lists of anchor or bounding boxes. Section 14.4

check_len

check_len(a, n)

Check the length of a list. Section 9.5

check_shape

check_shape(a, shape)

Check the shape of a tensor. Section 9.5

copyfile

copyfile(filename, target_dir)

Copy a file into a target directory. Section 14.13

corr2d

corr2d(X, K)

Compute 2D cross-correlation. Section 7.2

cpu

cpu()

Get the CPU device. Section 6.7

download

download(url, folder='../data', sha1_hash=None)

Download a file to folder and return the local filepath. Section 23.7

download_extract

download_extract(name, folder=None)

Download and extract a zip/tar file. Section 23.7

evaluate_loss

evaluate_loss(net, data_iter, loss)

Evaluate the loss of a model on the given dataset. Section 23.7

extract

extract(filename, folder=None)

Extract a zip/tar file into folder. Section 23.7

get_centers_and_contexts

get_centers_and_contexts(corpus, max_window_size)

Return center words and context words in skip-gram. Section 15.3

get_negatives

get_negatives(all_contexts, vocab, counter, K)

Return noise words in negative sampling. Section 15.3

get_tokens_and_segments

get_tokens_and_segments(tokens_a, tokens_b=None)

Get tokens of the BERT input sequence and their segment IDs. Section 15.8

gpu

gpu(i=0)

Get a GPU device. Section 6.7

linreg

linreg(X, w, b)

The linear regression model. Section 23.7

load_array

load_array(data_arrays, batch_size, is_train=True)

Construct a JAX-compatible data iterator. Section 23.7

load_data_bananas

load_data_bananas(batch_size)

Load the banana detection dataset. Section 14.6

load_data_imdb

load_data_imdb(batch_size, num_steps=500)

Return data iterators and the vocabulary of the IMDb review dataset. Section 16.1

load_data_ptb

load_data_ptb(batch_size, max_window_size, num_noise_words)

Download the PTB dataset and then load it into memory. Section 15.3

load_data_snli

load_data_snli(batch_size, num_steps=50)

Download the SNLI dataset and return data iterators and vocabulary. Section 16.4

load_data_voc

load_data_voc(batch_size, crop_size)

Load the VOC semantic segmentation dataset. Section 14.9

load_data_wiki

load_data_wiki(batch_size, max_len)

Load the WikiText-2 dataset. Section 15.9

masked_softmax

masked_softmax(X, valid_lens)

Perform softmax operation by masking elements on the last axis. Section 11.3

multibox_detection

multibox_detection(cls_probs, offset_preds, anchors, nms_threshold=0.5, pos_threshold=0.009999999)

Predict bounding boxes using non-maximum suppression. Section 14.4

multibox_prior

multibox_prior(data, sizes, ratios)

Generate anchor boxes with different shapes centered on each pixel. Section 14.4

multibox_target

multibox_target(anchors, labels)

Label anchor boxes using ground-truth bounding boxes. Section 14.4

nms

nms(boxes, scores, iou_threshold)

Sort confidence scores of predicted bounding boxes. Section 14.4

num_gpus

num_gpus()

Get the number of available GPUs. Section 6.7

offset_boxes

offset_boxes(anchors, assigned_bb, eps=1e-06)

Transform for anchor box offsets. Section 14.4

offset_inverse

offset_inverse(anchors, offset_preds)

Predict bounding boxes based on anchor boxes with predicted offsets. Section 14.4

plot

plot(X, Y=None, xlabel=None, ylabel=None, legend=[], xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None)

Plot data points. Section 2.4

predict_sentiment

predict_sentiment(net, params, vocab, sequence)

Predict the sentiment of a text sequence. Section 16.2

predict_snli

predict_snli(net, params, vocab, premise, hypothesis)

Predict the logical relationship between the premise and hypothesis. Section 16.5

read_csv_labels

read_csv_labels(fname)

Read fname to return a filename to label dictionary. Section 14.13

read_data_bananas

read_data_bananas(is_train=True)

Read the banana detection dataset images and labels. Section 14.6

read_imdb

read_imdb(data_dir, is_train)

Read the IMDb review dataset text sequences and labels. Section 16.1

read_ptb

read_ptb()

Load the PTB dataset into a list of text lines. Section 15.3

read_snli

read_snli(data_dir, is_train)

Read the SNLI dataset into premises, hypotheses, and labels. Section 16.4

read_voc_images

read_voc_images(voc_dir, is_train=True)

Read all VOC feature and label images. Section 14.9

reorg_test

reorg_test(data_dir)

Organize the testing set for data loading during prediction. Section 14.13

reorg_train_valid

reorg_train_valid(data_dir, labels, valid_ratio)

Split the validation set out of the original training set. Section 14.13

set_axes

set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)

Set the axes for matplotlib. Section 2.4

set_figsize

set_figsize(figsize=(3.5, 2.5))

Set the figure size for matplotlib. Section 2.4

show_bboxes

show_bboxes(axes, bboxes, labels=None, colors=None)

Show bounding boxes. Section 14.4

show_heatmaps

show_heatmaps(matrices, xlabel, ylabel, titles=None, figsize=(2.5, 2.5), cmap='Reds')

Show heatmaps of matrices. Section 11.1

show_images

show_images(imgs, num_rows, num_cols, titles=None, scale=1.5)

Plot a list of images. Section 23.7

show_list_len_pair_hist

show_list_len_pair_hist(legend, xlabel, ylabel, xlist, ylist)

Plot the histogram for list length pairs. Section 10.5

show_trace_2d

show_trace_2d(f, results)

Show the trace of 2D variables during optimization. Section 12.3

split_batch

split_batch(X, y, num_devices)

Split X and y across devices by reshaping. Section 13.5

squared_loss

squared_loss(y_hat, y)

Squared loss. Section 23.7

subsample

subsample(sentences, vocab)

Subsample high-frequency words. Section 15.3

tokenize

tokenize(lines, token='word')

Split text lines into word or character tokens. Section 23.7

train_2d

train_2d(trainer, steps=20, f_grad=None)

Optimize a 2D objective function with a customized trainer. Section 12.3

train_batch_ch13

train_batch_ch13(state, X, y, net, loss_fn)

Train for a minibatch with JAX (defined in Chapter 13). Section 14.1

train_ch13

train_ch13(net, train_iter, test_iter, loss_fn, state, num_epochs)

Train a model with JAX (defined in Chapter 13). Section 14.1

truncate_pad

truncate_pad(line, num_steps, padding_token)

Truncate or pad sequences. Section 23.7

try_all_gpus

try_all_gpus()

Return all available GPUs, or [cpu(),] if no GPU exists. Section 6.7

try_gpu

try_gpu(i=0)

Return gpu(i) if exists, otherwise return cpu(). Section 6.7

update_D

update_D(X, Z, net_D, net_G, params_D, params_G, loss_fn, opt_state_D, optimizer_D)

Update discriminator. Section 20.1

update_G

update_G(Z, net_D, net_G, params_D, params_G, loss_fn, opt_state_G, optimizer_G)

Update generator. Section 20.1

use_svg_display

use_svg_display()

Use the svg format to display a plot in Jupyter. Section 2.4

voc_colormap2label

voc_colormap2label()

Build the mapping from RGB to class indices for VOC labels. Section 14.9

voc_label_indices

voc_label_indices(colormap, colormap2label)

Map any RGB values in VOC labels to their class indices. Section 14.9

voc_rand_crop

voc_rand_crop(feature, label, height, width)

Randomly crop both feature and label images. Section 14.9

accuracy

accuracy(y_hat, y)

Compute the number of correct predictions. Section 23.7

add_to_class

add_to_class(Class)

Register functions as methods in created class. Section 3.2

assign_anchor_to_bbox

assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5)

Assign closest ground-truth bounding boxes to anchor boxes. Section 14.4

batchify

batchify(data)

Return a minibatch of examples for skip-gram with negative sampling. Section 15.3

bbox_to_rect

bbox_to_rect(bbox, color)

Convert bounding box to matplotlib format. Section 14.3

bleu

bleu(pred_seq, label_seq, k)

Compute the BLEU. Section 10.7.6

box_center_to_corner

box_center_to_corner(boxes)

Convert from (center, width, height) to (upper-left, lower-right). Section 14.3

box_corner_to_center

box_corner_to_center(boxes)

Convert from (upper-left, lower-right) to (center, width, height). Section 14.3

box_iou

box_iou(boxes1, boxes2)

Compute pairwise IoU across two lists of anchor or bounding boxes. Section 14.4

build_array_nmt

build_array_nmt(lines, vocab, num_steps)

Transform text sequences of machine translation into minibatches. Section 23.7

check_len

check_len(a, n)

Check the length of a list. Section 9.5

check_shape

check_shape(a, shape)

Check the shape of a tensor. Section 9.5

copyfile

copyfile(filename, target_dir)

Copy a file into a target directory. Section 14.13

corr2d

corr2d(X, K)

Compute 2D cross-correlation. Section 7.2

cpu

cpu()

Get the CPU device. Section 6.7

download

download(url, folder='../data', sha1_hash=None)

Download a file to folder and return the local filepath. Section 23.7

download_extract

download_extract(name, folder=None)

Download and extract a zip/tar file. Section 23.7

evaluate_accuracy

evaluate_accuracy(net, data_iter)

Compute the accuracy for a model on a dataset. Section 23.7

evaluate_accuracy_gpu

evaluate_accuracy_gpu(net, data_iter, device=None)

Compute the accuracy for a model on a dataset using a GPU. Section 23.7

evaluate_accuracy_gpus

evaluate_accuracy_gpus(net, data_iter, split_f=d2l.split_batch)

Compute the accuracy for a model on a dataset using multiple GPUs. Section 13.6

evaluate_loss

evaluate_loss(net, data_iter, loss)

Evaluate the loss of a model on the given dataset. Section 23.7

extract

extract(filename, folder=None)

Extract a zip/tar file into folder. Section 23.7

get_centers_and_contexts

get_centers_and_contexts(corpus, max_window_size)

Return center words and context words in skip-gram. Section 15.3

get_dataloader_workers

get_dataloader_workers()

Use 4 processes to read the data except for Windows. Section 23.7

get_fashion_mnist_labels

get_fashion_mnist_labels(labels)

Return text labels for the Fashion-MNIST dataset. Section 23.7

get_negatives

get_negatives(all_contexts, vocab, counter, K)

Return noise words in negative sampling. Section 15.3

get_tokens_and_segments

get_tokens_and_segments(tokens_a, tokens_b=None)

Get tokens of the BERT input sequence and their segment IDs. Section 15.8

gpu

gpu(i=0)

Get a GPU device. Section 6.7

linreg

linreg(X, w, b)

The linear regression model. Section 23.7

load_array

load_array(data_arrays, batch_size, is_train=True)

Construct a Gluon data iterator. Section 23.7

load_data_bananas

load_data_bananas(batch_size)

Load the banana detection dataset. Section 14.6

load_data_fashion_mnist

load_data_fashion_mnist(batch_size, resize=None)

Download the Fashion-MNIST dataset and then load it into memory. Section 23.7

load_data_imdb

load_data_imdb(batch_size, num_steps=500)

Return data iterators and the vocabulary of the IMDb review dataset. Section 16.1

load_data_nmt

load_data_nmt(batch_size, num_steps, num_examples=600)

Return the iterator and the vocabularies of the translation dataset. Section 23.7

load_data_ptb

load_data_ptb(batch_size, max_window_size, num_noise_words)

Download the PTB dataset and then load it into memory. Section 15.3

load_data_snli

load_data_snli(batch_size, num_steps=50)

Download the SNLI dataset and return data iterators and vocabulary. Section 16.4

load_data_voc

load_data_voc(batch_size, crop_size)

Load the VOC semantic segmentation dataset. Section 14.9

load_data_wiki

load_data_wiki(batch_size, max_len)

Load the WikiText-2 dataset. Section 15.9

masked_softmax

masked_softmax(X, valid_lens)

Perform softmax operation by masking elements on the last axis. Section 11.3

multibox_detection

multibox_detection(cls_probs, offset_preds, anchors, nms_threshold=0.5, pos_threshold=0.009999999)

Predict bounding boxes using non-maximum suppression. Section 14.4

multibox_prior

multibox_prior(data, sizes, ratios)

Generate anchor boxes with different shapes centered on each pixel. Section 14.4

multibox_target

multibox_target(anchors, labels)

Label anchor boxes using ground-truth bounding boxes. Section 14.4

nms

nms(boxes, scores, iou_threshold)

Sort confidence scores of predicted bounding boxes. Section 14.4

num_gpus

num_gpus()

Get the number of available GPUs. Section 6.7

offset_boxes

offset_boxes(anchors, assigned_bb, eps=1e-06)

Transform for anchor box offsets. Section 14.4

offset_inverse

offset_inverse(anchors, offset_preds)

Predict bounding boxes based on anchor boxes with predicted offsets. Section 14.4

plot

plot(X, Y=None, xlabel=None, ylabel=None, legend=[], xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None)

Plot data points. Section 2.4

predict_sentiment

predict_sentiment(net, vocab, sequence)

Predict the sentiment of a text sequence. Section 16.2

predict_seq2seq

predict_seq2seq(net, src_sentence, src_vocab, tgt_vocab, num_steps, device, save_attention_weights=False)

Predict for sequence to sequence. Section 23.7

predict_snli

predict_snli(net, vocab, premise, hypothesis)

Predict the logical relationship between the premise and hypothesis. Section 16.5

preprocess_nmt

preprocess_nmt(text)

Preprocess the English-French dataset. Section 23.7

read_csv_labels

read_csv_labels(fname)

Read fname to return a filename to label dictionary. Section 14.13

read_data_bananas

read_data_bananas(is_train=True)

Read the banana detection dataset images and labels. Section 14.6

read_data_nmt

read_data_nmt()

Load the English-French dataset. Section 23.7

read_imdb

read_imdb(data_dir, is_train)

Read the IMDb review dataset text sequences and labels. Section 16.1

read_ptb

read_ptb()

Load the PTB dataset into a list of text lines. Section 15.3

read_snli

read_snli(data_dir, is_train)

Read the SNLI dataset into premises, hypotheses, and labels. Section 16.4

read_voc_images

read_voc_images(voc_dir, is_train=True)

Read all VOC feature and label images. Section 14.9

reorg_test

reorg_test(data_dir)

Organize the testing set for data loading during prediction. Section 14.13

reorg_train_valid

reorg_train_valid(data_dir, labels, valid_ratio)

Split the validation set out of the original training set. Section 14.13

resnet18

resnet18(num_classes)

A slightly modified ResNet-18 model. Section 13.6

set_axes

set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)

Set the axes for matplotlib. Section 2.4

set_figsize

set_figsize(figsize=(3.5, 2.5))

Set the figure size for matplotlib. Section 2.4

sgd

sgd(params, lr, batch_size)

Minibatch stochastic gradient descent. Section 23.7

show_bboxes

show_bboxes(axes, bboxes, labels=None, colors=None)

Show bounding boxes. Section 14.4

show_heatmaps

show_heatmaps(matrices, xlabel, ylabel, titles=None, figsize=(2.5, 2.5), cmap='Reds')

Show heatmaps of matrices. Section 11.1

show_images

show_images(imgs, num_rows, num_cols, titles=None, scale=1.5)

Plot a list of images. Section 23.7

show_list_len_pair_hist

show_list_len_pair_hist(legend, xlabel, ylabel, xlist, ylist)

Plot the histogram for list length pairs. Section 10.5

show_trace_2d

show_trace_2d(f, results)

Show the trace of 2D variables during optimization. Section 12.3

split_batch

split_batch(X, y, devices)

Split X and y into multiple devices. Section 13.5

split_batch_multi_inputs

split_batch_multi_inputs(X, y, devices)

Split multi-input X and y into multiple devices. Section 16.5

squared_loss

squared_loss(y_hat, y)

Squared loss. Section 23.7

subsample

subsample(sentences, vocab)

Subsample high-frequency words. Section 15.3

synthetic_data

synthetic_data(w, b, num_examples)

Generate y = Xw + b + noise. Section 23.7

tokenize

tokenize(lines, token='word')

Split text lines into word or character tokens. Section 23.7

tokenize_nmt

tokenize_nmt(text, num_examples=None)

Tokenize the English-French dataset. Section 23.7

train_2d

train_2d(trainer, steps=20, f_grad=None)

Optimize a 2D objective function with a customized trainer. Section 12.3

train_batch_ch13

train_batch_ch13(net, features, labels, loss, trainer, devices, split_f=d2l.split_batch)

Train for a minibatch with multiple GPUs (defined in Chapter 13). Section 14.1

train_ch13

train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs, devices=d2l.try_all_gpus(), split_f=d2l.split_batch)

Train a model with multiple GPUs (defined in Chapter 13). Section 14.1

train_ch6

train_ch6(net, train_iter, test_iter, num_epochs, lr, device)

Train a model with a GPU (defined in Chapter 6). Section 23.7

train_seq2seq

train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device)

Train a model for sequence to sequence. Section 23.7

truncate_pad

truncate_pad(line, num_steps, padding_token)

Truncate or pad sequences. Section 23.7

try_all_gpus

try_all_gpus()

Return all available GPUs, or [cpu(),] if no GPU exists. Section 6.7

try_gpu

try_gpu(i=0)

Return gpu(i) if exists, otherwise return cpu(). Section 6.7

update_D

update_D(X, Z, net_D, net_G, loss, trainer_D)

Update discriminator. Section 20.1

update_G

update_G(Z, net_D, net_G, loss, trainer_G)

Update generator. Section 20.1

use_svg_display

use_svg_display()

Use the svg format to display a plot in Jupyter. Section 2.4

voc_colormap2label

voc_colormap2label()

Build the mapping from RGB to class indices for VOC labels. Section 14.9

voc_label_indices

voc_label_indices(colormap, colormap2label)

Map any RGB values in VOC labels to their class indices. Section 14.9

voc_rand_crop

voc_rand_crop(feature, label, height, width)

Randomly crop both feature and label images. Section 14.9