20.8. d2l API Document
Open the notebook in Colab
Open the notebook in Colab
Open the notebook in Colab
Open the notebook in SageMaker Studio Lab

The implementations of the following members of the d2l package and sections where they are defined and explained can be found in the source file.

20.8.1. Models

class d2l.torch.Module(plot_train_per_epoch=2, plot_valid_per_epoch=1)[source]

Bases: Module, HyperParameters

Defined in Section 3.2

apply_init(inputs, init=None)[source]

Defined in Section 6.4

configure_optimizers()[source]

Defined in Section 4.3

forward(X)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

loss(y_hat, y)[source]
plot(key, value, train)[source]

Plot a point in animation.

training: bool
training_step(batch)[source]
validation_step(batch)[source]
class d2l.torch.LinearRegressionScratch(num_inputs, lr, sigma=0.01)[source]

Bases: Module

Defined in Section 3.4

configure_optimizers()[source]

Defined in Section 3.4

forward(X)[source]

The linear regression model.

Defined in Section 3.4

loss(y_hat, y)[source]

Defined in Section 3.4

training: bool
class d2l.torch.LinearRegression(lr)[source]

Bases: Module

Defined in Section 3.5

configure_optimizers()[source]

Defined in Section 3.5

forward(X)[source]

The linear regression model.

Defined in Section 3.5

get_w_b()[source]

Defined in Section 3.5

loss(y_hat, y)[source]

Defined in Section 3.5

training: bool
class d2l.torch.Classifier(plot_train_per_epoch=2, plot_valid_per_epoch=1)[source]

Bases: Module

Defined in Section 4.3

accuracy(Y_hat, Y, averaged=True)[source]

Compute the number of correct predictions.

Defined in Section 4.3

layer_summary(X_shape)[source]

Defined in Section 7.6

loss(Y_hat, Y, averaged=True)[source]

Defined in Section 4.5

training: bool
validation_step(batch)[source]

20.8.2. Data

class d2l.torch.DataModule(root='../data', num_workers=4)[source]

Bases: HyperParameters

Defined in Section 3.2

get_dataloader(train)[source]
get_tensorloader(tensors, train, indices=slice(0, None, None))[source]

Defined in Section 3.3

train_dataloader()[source]
val_dataloader()[source]
class d2l.torch.SyntheticRegressionData(w, b, noise=0.01, num_train=1000, num_val=1000, batch_size=32)[source]

Bases: DataModule

Defined in Section 3.3

get_dataloader(train)[source]

Defined in Section 3.3

class d2l.torch.FashionMNIST(batch_size=64, resize=(28, 28))[source]

Bases: DataModule

Defined in Section 4.2

get_dataloader(train)[source]

Defined in Section 4.2

text_labels(indices)[source]

Return text labels.

Defined in Section 4.2

visualize(batch, nrows=1, ncols=8, labels=[])[source]

Defined in Section 4.2

20.8.3. Trainer

class d2l.torch.Trainer(max_epochs, num_gpus=0, gradient_clip_val=0)[source]

Bases: HyperParameters

Defined in Section 3.2

clip_gradients(grad_clip_val, model)[source]

Defined in Section 9.5

fit(model, data)[source]
fit_epoch()[source]

Defined in Section 3.4

prepare_batch(batch)[source]

Defined in Section 6.7

prepare_data(data)[source]
prepare_model(model)[source]

Defined in Section 6.7

class d2l.torch.SGD(params, lr)[source]

Bases: HyperParameters

Defined in Section 3.4

step()[source]
zero_grad()[source]

20.8.4. Utilities

d2l.torch.add_to_class(Class)[source]

Defined in Section 3.2

d2l.torch.cpu()[source]

Defined in Section 6.7

d2l.torch.gpu(i=0)[source]

Defined in Section 6.7

d2l.torch.num_gpus()[source]

Defined in Section 6.7

class d2l.torch.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)[source]

Bases: HyperParameters

Plot data points in animation.

Defined in Section 3.2

draw(x, y, label, every_n=1)[source]

Defined in Section 20.7

class d2l.torch.HyperParameters[source]

Bases: object

save_hyperparameters(ignore=[])[source]

Save function arguments into class attributes.

Defined in Section 20.7