model

class aitoolbox.torchtrain.model.TTModel[source]

Bases: torch.nn.modules.module.Module, abc.ABC

TTModel is an extension of core PyTorch nn.Module

TT in TTModel –> TorchTrain Model

In addition to the common forward() method required by the base nn.Module, the user also needs to implement the additional AIToolbox specific get_loss() and get_predictions() methods.

transfer_model_attributes (list or tuple): additional TTModel attributes which need to be transferred to the TTDataParallel level to enable their use in the transferred/exposed class methods. When coding the model’s __init__() method user should also fill in the string names of attributes that should be transferred in case the model is wrapped for DP/DDP.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

abstract get_loss(batch_data, criterion, device)[source]

Get loss during training stage

Called from fit() in TrainLoop

Executed during training stage where model weights are updated based on the loss returned from this function.

Parameters
  • batch_data – model input data batch

  • criterion – loss criterion

  • device – device on which the model is being trained

Returns

PyTorch loss

get_loss_eval(batch_data, criterion, device)[source]

Get loss during evaluation stage

Called from evaluate_model_loss() in TrainLoop.

The difference compared with get_loss() is that here the backprop weight update is not done. This function is executed in the evaluation stage not training.

For simple examples this function can just call the get_loss() and return its result.

Parameters
  • batch_data – model input data batch

  • criterion – loss criterion

  • device – device on which the model is being trained

Returns

PyTorch loss

abstract get_predictions(batch_data, device)[source]

Get predictions during evaluation stage

Parameters
  • batch_data – model input data batch

  • device – device on which the model is making the prediction

Returns

y_pred.cpu(), y_test.cpu(), metadata

Return type

np.array, np.array, dict

training: bool
class aitoolbox.torchtrain.model.TTBasicModel[source]

Bases: aitoolbox.torchtrain.model.TTModel

Extension of the TTModel abstract class with already implemented simple loss and prediction calculation functions

The pre-implemented get_loss() and get_predictions() will take all the provided data sources from the data loader except the last one as an input to the model. The last data source from the data loader will be treated as the target variable. (*batch_input_data, targets = batch_data)

This base class is mainly meant to be used for simple models. TTBasicModel removes the need to constantly duplicate code in get_loss and get_predictions.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

get_loss(batch_data, criterion, device)[source]

Get loss during training stage

Called from fit() in TrainLoop

Executed during training stage where model weights are updated based on the loss returned from this function.

Parameters
  • batch_data – model input data batch

  • criterion – loss criterion

  • device – device on which the model is being trained

Returns

PyTorch loss

get_predictions(batch_data, device)[source]

Get predictions during evaluation stage

Parameters
  • batch_data – model input data batch

  • device – device on which the model is making the prediction

Returns

y_pred.cpu(), y_test.cpu(), metadata

Return type

np.array, np.array, dict

training: bool
class aitoolbox.torchtrain.model.TTBasicMultiGPUModel[source]

Bases: aitoolbox.torchtrain.model.TTBasicModel

Extension of the TTModel abstract class with already implemented simple loss and prediction calculation functions

which support leveled utilization when training on multi-GPU.

The pre-implemented get_loss() and get_predictions() will take all the provided data sources from the data loader except the last one as an input to the model. The last data source from the data loader will be treated as the target variable. (*batch_input_data, targets = batch_data)

In the case of the get_loss() the inout into the model’s forward() function will also provide targets and criterion arguments in order to enable calculation of the loss inside forward() function.

The forward() function should have the following parameter signature and should finish with:

def forward(*batch_input_data, targets=None, criterion=None):

… predictions calculation via the computational graph …

if criterion is not None:

return criterion(predictions, targets)

else:

return predictions

This base class is mainly meant to be used for simple models. TTBasicModel removes the need to constantly duplicate code in get_loss and get_predictions.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

get_loss(batch_data, criterion, device)[source]

Get loss during training stage

Called from fit() in TrainLoop

Executed during training stage where model weights are updated based on the loss returned from this function.

Parameters
  • batch_data – model input data batch

  • criterion – loss criterion

  • device – device on which the model is being trained

Returns

PyTorch loss

training: bool
class aitoolbox.torchtrain.model.MultiGPUModelWrap(model)[source]

Bases: aitoolbox.torchtrain.model.TTBasicMultiGPUModel

Model wrapper optimizing the model for multi-GPU training by moving the loss calculation to the GPUs

Parameters

model (nn.Module or TTModel) – neural network model. The model should follow the basic PyTorch model definition where the forward() function returns predictions

forward(*input_data, targets=None, criterion=None)[source]

DP friendly forward abstraction on top of the wrapped model’s usual forward() function

Parameters
  • *input_data – whatever input data should be passed into the wrapped model’s forward() function

  • targets – target variables which the model is training to fit

  • criterion – loss function

Returns

PyTorch loss or model output predictions. If loss function criterion is provided this function returns the

calculated loss, otherwise the model output predictions are returned

training: bool
class aitoolbox.torchtrain.model.ModelWrap(model, batch_model_feed_def)[source]

Bases: object

TrainLoop model wrapper combining PyTorch model and model feed definition

NOTE: especially useful in the case when you want to train on multi-GPU where TTModel abstract functions

can’t be used.

ModelWrap can be used as a replacement of TTModel when using the TrainLoop.

Parameters
  • model (nn.Module) – neural network model

  • batch_model_feed_def (AbstractModelFeedDefinition or None) – data prep definition for batched data. This definition prepares the data for each batch that gets than fed into the neural network.