Dive into Deep Learning
Table Of Contents
Dive into Deep Learning
Table Of Contents

14.7. d2l API Document

14.7.1. Basic and Plotting

The base module contains some basic functions/classes for d2l

d2l.base.try_gpu()[source]

If GPU is available, return mx.gpu(0); else return mx.cpu().

d2l.base.try_all_gpus()[source]

Return all available GPUs, or [mx.cpu()] if there is no GPU.

class d2l.base.Benchmark(prefix=None)[source]

Benchmark programs.

The image module contains functions for plotting

d2l.figure.bbox_to_rect(bbox, color)[source]

Convert bounding box to matplotlib format.

d2l.figure.semilogy(x_vals, y_vals, x_label, y_label, x2_vals=None, y2_vals=None, legend=None, figsize=(3.5, 2.5))[source]

Plot x and log(y).

d2l.figure.set_figsize(figsize=(3.5, 2.5))[source]

Set matplotlib figure size.

d2l.figure.show_bboxes(axes, bboxes, labels=None, colors=None)[source]

Show bounding boxes.

d2l.figure.show_images(imgs, num_rows, num_cols, scale=2)[source]

Plot a list of images.

d2l.figure.show_trace_2d(f, res)[source]

Show the trace of 2D variables during optimization.

d2l.figure.use_svg_display()[source]

Use svg format to display plot in jupyter.

14.7.2. Loading Data

The data module contains functions/classes to load and (pre)process data sets

d2l.data.base.data_iter_consecutive(corpus_indices, batch_size, num_steps, ctx=None)[source]

Sample mini-batches in a consecutive order from sequential data.

d2l.data.base.data_iter_random(corpus_indices, batch_size, num_steps, ctx=None)[source]

Sample mini-batches in a random order from sequential data.

d2l.data.base.get_data_ch7()[source]

Get the data set used in Chapter 7.

d2l.data.base.load_data_time_machine(num_examples=10000)[source]

Load the time machine data set (available in the English book).

d2l.data.base.mkdir_if_not_exist(path)[source]

Make a directory if it does not exist.

d2l.data.fashion_mnist.get_fashion_mnist_labels(labels)[source]

Get text labels for Fashion-MNIST.

d2l.data.fashion_mnist.load_data_fashion_mnist(batch_size, resize=None, root='~/.mxnet/datasets/fashion-mnist')[source]

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

d2l.data.fashion_mnist.show_fashion_mnist(images, labels)[source]

Plot Fashion-MNIST images with labels.

d2l.data.imdb.load_data_imdb(batch_size, max_len=500)[source]

Download an IMDB dataset, return the vocabulary and iterators.

d2l.data.pikachu.load_data_pikachu(batch_size, edge_size=256)[source]

Download the pikachu dataset and load it into memory.

d2l.data.voc.download_voc_pascal(data_dir='../data')[source]

Download the Pascal VOC2012 Dataset.

class d2l.data.voc.VOCSegDataset(is_train, crop_size, voc_dir, colormap2label)[source]

The Pascal VOC2012 Dataset.

d2l.data.voc.read_voc_images(root='../data/VOCdevkit/VOC2012', is_train=True)[source]

Read VOC images.

14.7.3. Building Neural Networks

The model module contains neural network building blocks

d2l.model.corr2d(X, K)[source]

Compute 2D cross-correlation.

d2l.model.linreg(X, w, b)[source]

Linear regression.

class d2l.model.Residual(num_channels, use_1x1conv=False, strides=1, **kwargs)[source]

The residual block.

forward(X)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.
d2l.model.resnet18(num_classes)[source]

The ResNet-18 model.

class d2l.model.RNNModel(rnn_layer, vocab_size, **kwargs)[source]

RNN model.

forward(inputs, state)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.
class d2l.model.Encoder(**kwargs)[source]

The base encoder interface for the encoder-decoder architecture.

forward(X, *args)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.
class d2l.model.Decoder(**kwargs)[source]

The base decoder interface for the encoder-decoder archtecture.

forward(X, state)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.
class d2l.model.EncoderDecoder(encoder, decoder, **kwargs)[source]

The base class for the encoder-decoder architecture.

forward(enc_X, dec_X, *args)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.
class d2l.model.DotProductAttention(dropout, **kwargs)[source]
forward(query, key, value, valid_length=None)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.
class d2l.model.MLPAttention(units, dropout, **kwargs)[source]
forward(query, key, value, valid_length)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.
class d2l.model.Seq2SeqEncoder(vocab_size, embed_size, num_hiddens, num_layers, dropout=0, **kwargs)[source]
forward(X, *args)[source]

Overrides to implement forward computation using NDArray. Only accepts positional arguments.

*args : list of NDArray
Input tensors.

14.7.4. Training

The train module contains functions for neural network training

d2l.train.evaluate_accuracy(data_iter, net, ctx=[cpu(0)])[source]

Evaluate accuracy of a model on the given data set.

d2l.train.squared_loss(y_hat, y)[source]

Squared loss.

d2l.train.grad_clipping(params, theta, ctx)[source]

Clip the gradient.

d2l.train.grad_clipping_gluon(model, theta, ctx)[source]

Clip the gradient for a Gluon model.

d2l.train.sgd(params, lr, batch_size)[source]

Mini-batch stochastic gradient descent.

d2l.train.train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs)[source]

Train and evaluate a model.

d2l.train.train_2d(trainer)[source]

Optimize the objective function of 2D variables with a customized trainer.

d2l.train.train_and_predict_rnn(rnn, get_params, init_rnn_state, num_hiddens, corpus_indices, vocab, ctx, is_random_iter, num_epochs, num_steps, lr, clipping_theta, batch_size, prefixes)[source]

Train an RNN model and predict the next item in the sequence.

d2l.train.train_and_predict_rnn_gluon(model, num_hiddens, corpus_indices, vocab, ctx, num_epochs, num_steps, lr, clipping_theta, batch_size, prefixes)[source]

Train a Gluon RNN model and predict the next item in the sequence.

d2l.train.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params=None, lr=None, trainer=None)[source]

Train and evaluate a model with CPU.

d2l.train.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)[source]

Train and evaluate a model with CPU or GPU.

d2l.train.train_ch9(trainer_fn, states, hyperparams, features, labels, batch_size=10, num_epochs=2)[source]

Train a linear regression model.

d2l.train.train_gluon_ch9(trainer_name, trainer_hyperparams, features, labels, batch_size=10, num_epochs=2)[source]

Train a linear regression model with a given Gluon trainer.

d2l.train.predict_sentiment(net, vocab, sentence)[source]

Predict the sentiment of a given sentence.

d2l.train.train_ch7(model, data_iter, lr, num_epochs, ctx)[source]

Train an encoder-decoder model

d2l.train.translate_ch7(model, src_sentence, src_vocab, tgt_vocab, max_len, ctx)[source]

Translate based on an encoder-decoder model with greedy search.

14.7.5. Prediciting