alibi_detect.models.tensorflow.losses module

alibi_detect.models.tensorflow.losses.elbo(y_true, y_pred, cov_full=None, cov_diag=None, sim=None)[source]

Compute ELBO loss. The covariance matrix can be specified by passing the full covariance matrix, the matrix diagonal, or a scale identity multiplier. Only one of these should be specified. If none are specified, the identity matrix is used.

Parameters:
  • y_true (Tensor) – Labels.

  • y_pred (Tensor) – Predictions.

  • cov_full (Optional[Tensor]) – Full covariance matrix.

  • cov_diag (Optional[Tensor]) – Diagonal (variance) of covariance matrix.

  • sim (Optional[float]) – Scale identity multiplier.

Return type:

Tensor

Returns:

ELBO loss value.

Example

>>> import tensorflow as tf
>>> from alibi_detect.models.tensorflow.losses import elbo
>>> y_true = tf.constant([[0.0, 1.0], [1.0, 0.0]])
>>> y_pred = tf.constant([[0.1, 0.9], [0.8, 0.2]])
>>> # Specifying scale identity multiplier
>>> elbo(y_true, y_pred, sim=1.0)
>>> # Specifying covariance matrix diagonal
>>> elbo(y_true, y_pred, cov_diag=tf.ones(2))
>>> # Specifying full covariance matrix
>>> elbo(y_true, y_pred, cov_full=tf.eye(2))
alibi_detect.models.tensorflow.losses.loss_adv_ae(x_true, x_pred, model=None, model_hl=None, w_model=1.0, w_recon=0.0, w_model_hl=None, temperature=1.0)[source]

Loss function used for AdversarialAE.

Parameters:
  • x_true (Tensor) – Batch of instances.

  • x_pred (Tensor) – Batch of reconstructed instances by the autoencoder.

  • model (Optional[Model]) – A trained tf.keras model with frozen layers (layers.trainable = False).

  • model_hl (Optional[list]) – List with tf.keras models used to extract feature maps and make predictions on hidden layers.

  • w_model (float) – Weight on model prediction loss term.

  • w_recon (float) – Weight on MSE reconstruction error loss term.

  • w_model_hl (Optional[list]) – Weights assigned to the loss of each model in model_hl.

  • temperature (float) – Temperature used for model prediction scaling. Temperature <1 sharpens the prediction probability distribution.

Return type:

Tensor

Returns:

Loss value.

alibi_detect.models.tensorflow.losses.loss_aegmm(x_true, x_pred, z, gamma, w_energy=0.1, w_cov_diag=0.005)[source]

Loss function used for OutlierAEGMM.

Parameters:
  • x_true (Tensor) – Batch of instances.

  • x_pred (Tensor) – Batch of reconstructed instances by the autoencoder.

  • z (Tensor) – Latent space values.

  • gamma (Tensor) – Membership prediction for mixture model components.

  • w_energy (float) – Weight on sample energy loss term.

  • w_cov_diag (float) – Weight on covariance regularizing loss term.

Return type:

Tensor

Returns:

Loss value.

alibi_detect.models.tensorflow.losses.loss_distillation(x_true, y_pred, model=None, loss_type='kld', temperature=1.0)[source]

Loss function used for Model Distillation.

Parameters:
  • x_true (Tensor) – Batch of data points.

  • y_pred (Tensor) – Batch of prediction from the distilled model.

  • model (Optional[Model]) – tf.keras model.

  • loss_type (str) – Type of loss for distillation. Supported ‘kld’, ‘xent.

  • temperature (float) – Temperature used for model prediction scaling. Temperature <1 sharpens the prediction probability distribution.

Return type:

Tensor

Returns:

Loss value.

alibi_detect.models.tensorflow.losses.loss_vaegmm(x_true, x_pred, z, gamma, w_recon=1e-07, w_energy=0.1, w_cov_diag=0.005, cov_full=None, cov_diag=None, sim=0.05)[source]

Loss function used for OutlierVAEGMM.

Parameters:
  • x_true (Tensor) – Batch of instances.

  • x_pred (Tensor) – Batch of reconstructed instances by the variational autoencoder.

  • z (Tensor) – Latent space values.

  • gamma (Tensor) – Membership prediction for mixture model components.

  • w_recon (float) – Weight on elbo loss term.

  • w_energy (float) – Weight on sample energy loss term.

  • w_cov_diag (float) – Weight on covariance regularizing loss term.

  • cov_full (Optional[Tensor]) – Full covariance matrix.

  • cov_diag (Optional[Tensor]) – Diagonal (variance) of covariance matrix.

  • sim (float) – Scale identity multiplier.

Return type:

Tensor

Returns:

Loss value.