alibi.models.pytorch.metrics module

This module contains a loss wrapper and a definition of various monitoring metrics used during training. The model to be trained inherits form alibi.explainers.models.pytorch.model.Model and represents a simplified version of the tensorflow.keras API for training and monitoring the model. 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.metrics.AccuracyMetric(name='accuracy')[source]

Bases: Metric

Accuracy monitoring metric.

compute_metric(y_pred, y_true)[source]

Computes accuracy metric given the predicted label and the true label.

Parameters:
  • y_pred (Union[Tensor, ndarray]) – Predicted label.

  • y_true (Union[Tensor, ndarray]) – True label.

Return type:

None

class alibi.models.pytorch.metrics.LossContainer(loss, name)[source]

Bases: object

Loss wrapped to monitor the average loss throughout training.

__call__(y_pred, y_true)[source]

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

Parameters:
  • y_pred (Tensor) – Prediction labels.

  • y_true (Tensor) – True labels.

Return type:

Tensor

Returns:

Loss value.

__init__(loss, name)[source]

Constructor.

Parameters:
  • loss (Callable[[Tensor, Tensor], Tensor]) – Loss function.

  • name (str) – Name of the loss function

reset()[source]

Resets the loss.

result()[source]

Computes the average loss obtain by dividing the cumulated loss by the number of steps

Return type:

Dict[str, float]

Returns:

Average loss.

class alibi.models.pytorch.metrics.Metric(reduction=Reduction.MEAN, name='unknown')[source]

Bases: ABC

Monitoring metric object. Supports two types of reduction: mean and sum.

__init__(reduction=Reduction.MEAN, name='unknown')[source]

Constructor.

Parameters:
  • reduction (Reduction) – Metric’s reduction type. Possible values mean`|`sum. By default mean.

  • name (str) – Name of the metric.

abstract compute_metric(y_pred, y_true)[source]
reset()[source]

Resets the monitoring metric.

result()[source]

Computes the result according to the reduction procedure.

Return type:

Dict[str, float]

Returns:

Monitoring metric.

update_state(values)[source]

Update the state of the metric by summing up the metric values and updating the counts by adding the number of instances for which the metric was computed (first dimension).

class alibi.models.pytorch.metrics.Reduction(value)[source]

Bases: Enum

Reduction operation supported by the monitoring metrics.

MEAN: str = 'mean'
SUM: str = 'sum'