Module

The Module class is an extension of the torch.nn.Module object. This class implements scikit-learn-like fit() and predict() methods to automatically use nn.Module objects for training and predicting labels. This module also automatically keeps track of the progress during fitting and predicting.

class module.Module(*args, **kwargs)[source]

Extention of nn.Module that adds fit and predict methods Can be used for automatic training.

progress

Used to track progress of fit and predict methods

Type:Progress()

Initialization

Module.__init__(*args, **kwargs)[source]

Only calls super method nn.Module with given arguments.

Fit

To train a Module, we use the fit() method.

Module.fit(X, y, epochs=10, batch_size=32, learning_rate=0.01, criterion=<sphinx.ext.autodoc.importer._MockObject object>, optimizer=<sphinx.ext.autodoc.importer._MockObject object>, variable=False, verbose=True, **kwargs)[source]

Train the module with given parameters

Parameters:
  • X (torch.Tensor) – Tensor to train with
  • y (torch.Tensor) – Target tensor
  • epochs (int, default=10) – Number of epochs to train with
  • batch_size (int, default=32) – Default batch size to use for training
  • learning_rate (float, default=0.01) – Learning rate to use for optimizer
  • criterion (nn.Loss, default=nn.NLLLoss()) – Loss function to use
  • optimizer (optim.Optimizer, default=optim.SGD) – Optimizer to use for training
  • variable (boolean, default=False) – If True, accept inputs of variable length
  • verbose (boolean, default=True) – If True, prints training progress
Returns:

result – Returns self

Return type:

self

Predict

A module can also predict() outputs for given inputs. Usually this method is called after fitting.

Module.predict(X, batch_size=32, variable=False, verbose=True, **kwargs)[source]

Makes prediction based on input data X. Default implementation just uses the module forward(X) method, often the predict method will be overwritten to fit the specific needs of the module.

Parameters:
  • X (torch.Tensor) – Tensor from which to make prediction
  • batch_size (int, default=32) – Batch size in which to predict items in X
  • variable (boolean, default=False) – If True, accept inputs of variable length
  • verbose (boolean, default=True) – If True, print progress of prediction
Returns:

result – Resulting prediction

Return type:

torch.Tensor

Fit-Predict

Sometimes we want to fit() and predict() on the same data. This can easily be achieved using the fit_predict() method.

Module.fit_predict(X, y, epochs=10, batch_size=32, learning_rate=0.01, criterion=<sphinx.ext.autodoc.importer._MockObject object>, optimizer=<sphinx.ext.autodoc.importer._MockObject object>, variable=False, verbose=True, **kwargs)[source]

Train the module with given parameters

Parameters:
  • X (torch.Tensor) – Tensor to train with
  • y (torch.Tensor) – Target tensor
  • epochs (int, default=10) – Number of epochs to train with
  • batch_size (int, default=32) – Default batch size to use for training
  • learning_rate (float, default=0.01) – Learning rate to use for optimizer
  • criterion (nn.Loss, default=nn.NLLLoss) – Loss function to use
  • optimizer (optim.Optimizer, default=optim.SGD) – Optimizer to use for training
  • variable (boolean, default=False) – If True, accept inputs of variable length
  • verbose (boolean, default=True) – If True, prints training progress
Returns:

result – Resulting prediction

Return type:

torch.Tensor