alibi.models.pytorch.model module

This module tries to provided a class wrapper to mimic the TensorFlow API of tensorflow.keras.Model. It is intended to simplify the training of a model through methods like compile, fit and evaluate which allow the user to define custom loss functions, optimizers, evaluation metrics, train a model and evaluate it. Currently it is used internally to test the functionalities for the Pytorch backend. To be discussed if the module will be exposed to the user in future versions.

class alibi.models.pytorch.model.Model(*args: Any, **kwargs: Any)[source]

Bases: Module

compile(optimizer, loss, loss_weights=None, metrics=None)[source]

Compiles a model by setting the optimizer and the loss functions, loss weights and metrics to monitor the training of the model.

Parameters:
  • optimizer (Optimizer) – Optimizer to be used.

  • loss (Union[Callable, List[Callable]]) – Loss function to be used. Can be a list of the loss function which will be weighted and summed up to compute the total loss.

  • loss_weights (Optional[List[float]]) – Weights corresponding to each loss function. Only used if the loss argument is a list.

  • metrics (Optional[List[Metric]]) – Metrics used to monitor the training process.

compute_loss(y_pred, y_true)[source]

Computes the loss given the prediction labels and the true labels.

Parameters:
  • y_pred (Union[Tensor, List[Tensor]]) – Prediction labels.

  • y_true (Union[Tensor, List[Tensor]]) – True labels.

Return type:

Tuple[Tensor, Dict[str, float]]

Returns:

A tuple consisting of the total loss computed as a weighted sum of individual losses and a dictionary of individual losses used of logging.

compute_metrics(y_pred, y_true)[source]

Computes the metrics given the prediction labels and the true labels.

Parameters:
  • y_pred (Union[Tensor, List[Tensor]]) – Prediction labels.

  • y_true (Union[Tensor, List[Tensor]]) – True labels.

Return type:

Dict[str, float]

evaluate(testloader)[source]

Evaluation function. The function reports the evaluation metrics used for monitoring the training loop.

Parameters:

testloader (DataLoader) – Test dataloader.

Return type:

Dict[str, float]

Returns:

Evaluation metrics.

fit(trainloader, epochs)[source]

Fit method. Equivalent of a training loop.

Parameters:
  • trainloader (DataLoader) – Training data loader.

  • epochs (int) – Number of epochs to train the model.

Return type:

Dict[str, float]

Returns:

Final epoch monitoring metrics.

load_weights(path)[source]

Loads the weight of the current model.

Return type:

None

save_weights(path)[source]

Save the weight of the current model.

Return type:

None

test_step(x, y)

Performs a test step.

Parameters:
  • x (Tensor) – Input tensor.

  • y (Union[Tensor, List[Tensor]]) – Label tensor.

train_step(x, y)[source]

Performs a train step.

Parameters:
  • x (Tensor) – Input tensor.

  • y (Union[Tensor, List[Tensor]]) – Label tensor.

Return type:

Dict[str, float]

validate_prediction_labels(y_pred, y_true)[source]

Validates the loss functions, loss weights, training labels and prediction labels.

Parameters:
  • y_pred (Union[Tensor, List[Tensor]]) – Prediction labels.

  • y_true (Union[Tensor, List[Tensor]]) – True labels.