alibi_detect.models.tensorflow package

class alibi_detect.models.tensorflow.AE(encoder_net, decoder_net, name='ae')[source]

Bases: Model

__init__(encoder_net, decoder_net, name='ae')[source]

Combine encoder and decoder in AE.

Parameters:
  • encoder_net (Model) – Layers for the encoder wrapped in a tf.keras.Sequential class.

  • decoder_net (Model) – Layers for the decoder wrapped in a tf.keras.Sequential class.

  • name (str) – Name of autoencoder model.

call(x)[source]
Return type:

Tensor

class alibi_detect.models.tensorflow.AEGMM(encoder_net, decoder_net, gmm_density_net, n_gmm, recon_features=<function eucl_cosim_features>, name='aegmm')[source]

Bases: Model

__init__(encoder_net, decoder_net, gmm_density_net, n_gmm, recon_features=<function eucl_cosim_features>, name='aegmm')[source]

Deep Autoencoding Gaussian Mixture Model.

Parameters:
  • encoder_net (Model) – Layers for the encoder wrapped in a tf.keras.Sequential class.

  • decoder_net (Model) – Layers for the decoder wrapped in a tf.keras.Sequential class.

  • gmm_density_net (Model) – Layers for the GMM network wrapped in a tf.keras.Sequential class.

  • n_gmm (int) – Number of components in GMM.

  • recon_features (Callable) – Function to extract features from the reconstructed instance by the decoder.

  • name (str) – Name of the AEGMM model.

call(x)[source]
Return type:

Tuple[Tensor, Tensor, Tensor]

class alibi_detect.models.tensorflow.PixelCNN(image_shape, conditional_shape=None, num_resnet=5, num_hierarchies=3, num_filters=160, num_logistic_mix=10, receptive_field_dims=(3, 3), dropout_p=0.5, resnet_activation='concat_elu', l2_weight=0.0, use_weight_norm=True, use_data_init=True, high=255, low=0, dtype=tensorflow.compat.v2.float32, name='PixelCNN')[source]

Bases: Distribution

__init__(image_shape, conditional_shape=None, num_resnet=5, num_hierarchies=3, num_filters=160, num_logistic_mix=10, receptive_field_dims=(3, 3), dropout_p=0.5, resnet_activation='concat_elu', l2_weight=0.0, use_weight_norm=True, use_data_init=True, high=255, low=0, dtype=tensorflow.compat.v2.float32, name='PixelCNN')[source]

Construct Pixel CNN++ distribution.

Parameters:
  • image_shape (tuple) – 3D TensorShape or tuple for the [height, width, channels] dimensions of the image.

  • conditional_shape (Optional[tuple]) – TensorShape or tuple for the shape of the conditional input, or None if there is no conditional input.

  • num_resnet (int) – The number of layers (shown in Figure 2 of [2]) within each highest-level block of Figure 2 of [1].

  • num_hierarchies (int) – The number of highest-level blocks (separated by expansions/contractions of dimensions in Figure 2 of [1].)

  • num_filters (int) – The number of convolutional filters.

  • num_logistic_mix (int) – Number of components in the logistic mixture distribution.

  • receptive_field_dims (tuple) – Height and width in pixels of the receptive field of the convolutional layers above and to the left of a given pixel. The width (second element of the tuple) should be odd. Figure 1 (middle) of [2] shows a receptive field of (3, 5) (the row containing the current pixel is included in the height). The default of (3, 3) was used to produce the results in [1].

  • dropout_p (float) – The dropout probability. Should be between 0 and 1.

  • resnet_activation (str) – The type of activation to use in the resnet blocks. May be ‘concat_elu’, ‘elu’, or ‘relu’.

  • l2_weight (float) – The L2 regularization weight.

  • use_weight_norm (bool) – If True then use weight normalization (works only in Eager mode).

  • use_data_init (bool) – If True then use data-dependent initialization (has no effect if use_weight_norm is False).

  • high (int) – The maximum value of the input data (255 for an 8-bit image).

  • low (int) – The minimum value of the input data.

  • dtype – Data type of the Distribution.

  • name (str) – The name of the Distribution.

class alibi_detect.models.tensorflow.Seq2Seq(encoder_net, decoder_net, threshold_net, n_features, score_fn=tensorflow.math.squared_difference, beta=1.0, name='seq2seq')[source]

Bases: Model

__init__(encoder_net, decoder_net, threshold_net, n_features, score_fn=tensorflow.math.squared_difference, beta=1.0, name='seq2seq')[source]

Sequence-to-sequence model.

Parameters:
  • encoder_net (EncoderLSTM) – Encoder network.

  • decoder_net (DecoderLSTM) – Decoder network.

  • threshold_net (Model) – Regression network used to estimate threshold.

  • n_features (int) – Number of features.

  • score_fn (Callable) – Function used for outlier score.

  • beta (float) – Weight on the threshold estimation loss term.

  • name (str) – Name of the seq2seq model.

call(x)[source]

Forward pass used for teacher-forcing training.

Return type:

Tensor

decode_seq(x)[source]

Sequence decoding and threshold estimation used for inference.

Return type:

Tuple[ndarray, ndarray]

class alibi_detect.models.tensorflow.TransformerEmbedding(model_name_or_path, embedding_type, layers=None)[source]

Bases: Model

__init__(model_name_or_path, embedding_type, layers=None)[source]

Extract text embeddings from transformer models.

Parameters:
  • model_name_or_path (str) – Name of or path to the model.

  • embedding_type (str) –

    Type of embedding to extract. Needs to be one of pooler_output, last_hidden_state, hidden_state or hidden_state_cls.

    From the HuggingFace documentation:

    • pooler_output

      Last layer hidden-state of the first token of the sequence (classification token) further processed by a Linear layer and a Tanh activation function. The Linear layer weights are trained from the next sentence prediction (classification) objective during pre-training. This output is usually not a good summary of the semantic content of the input, you’re often better with averaging or pooling the sequence of hidden-states for the whole input sequence.

    • last_hidden_state

      Sequence of hidden-states at the output of the last layer of the model.

    • hidden_state

      Hidden states of the model at the output of each layer.

    • hidden_state_cls

      See hidden_state but use the CLS token output.

  • layers (Optional[List[int]]) – If “hidden_state” or “hidden_state_cls” is used as embedding type, layers has to be a list with int’s referring to the hidden layers used to extract the embedding.

call(tokens)[source]
Return type:

Tensor

class alibi_detect.models.tensorflow.VAE(encoder_net, decoder_net, latent_dim, beta=1.0, name='vae')[source]

Bases: Model

__init__(encoder_net, decoder_net, latent_dim, beta=1.0, name='vae')[source]

Combine encoder and decoder in VAE.

Parameters:
  • encoder_net (Model) – Layers for the encoder wrapped in a tf.keras.Sequential class.

  • decoder_net (Model) – Layers for the decoder wrapped in a tf.keras.Sequential class.

  • latent_dim (int) – Dimensionality of the latent space.

  • beta (float) – Beta parameter for KL-divergence loss term.

  • name (str) – Name of VAE model.

call(x)[source]
Return type:

Tensor

class alibi_detect.models.tensorflow.VAEGMM(encoder_net, decoder_net, gmm_density_net, n_gmm, latent_dim, recon_features=<function eucl_cosim_features>, beta=1.0, name='vaegmm')[source]

Bases: Model

__init__(encoder_net, decoder_net, gmm_density_net, n_gmm, latent_dim, recon_features=<function eucl_cosim_features>, beta=1.0, name='vaegmm')[source]

Variational Autoencoding Gaussian Mixture Model.

Parameters:
  • encoder_net (Model) – Layers for the encoder wrapped in a tf.keras.Sequential class.

  • decoder_net (Model) – Layers for the decoder wrapped in a tf.keras.Sequential class.

  • gmm_density_net (Model) – Layers for the GMM network wrapped in a tf.keras.Sequential class.

  • n_gmm (int) – Number of components in GMM.

  • latent_dim (int) – Dimensionality of the latent space.

  • recon_features (Callable) – Function to extract features from the reconstructed instance by the decoder.

  • beta (float) – Beta parameter for KL-divergence loss term.

  • name (str) – Name of the VAEGMM model.

call(x)[source]
Return type:

Tuple[Tensor, Tensor, Tensor]

alibi_detect.models.tensorflow.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.eucl_cosim_features(x, y, max_eucl=100.0)[source]

Compute features extracted from the reconstructed instance using the relative Euclidean distance and cosine similarity between 2 tensors.

Parameters:
  • x (Tensor) – Tensor used in feature computation.

  • y (Tensor) – Tensor used in feature computation.

  • max_eucl (float) – Maximum value to clip relative Euclidean distance by.

Return type:

Tensor

Returns:

Tensor concatenating the relative Euclidean distance and cosine similarity features.

alibi_detect.models.tensorflow.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.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.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.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.

alibi_detect.models.tensorflow.resnet(num_blocks, classes=10, input_shape=(32, 32, 3))[source]

Define ResNet.

Parameters:
  • num_blocks (int) – Number of ResNet blocks.

  • classes (int) – Number of classification classes.

  • input_shape (Tuple[int, int, int]) – Input shape of an image.

Return type:

Model

Returns:

ResNet as a tf.keras.Model.

alibi_detect.models.tensorflow.scale_by_instance(x, eps=1e-12)[source]
Return type:

ndarray

alibi_detect.models.tensorflow.trainer(model, loss_fn, x_train, y_train=None, dataset=None, optimizer=tensorflow.keras.optimizers.Adam, loss_fn_kwargs=None, preprocess_fn=None, epochs=20, reg_loss_fn=<function <lambda>>, batch_size=64, buffer_size=1024, verbose=True, log_metric=None, callbacks=None)[source]

Train TensorFlow model.

Parameters:
  • model – Model to train.

  • loss_fn – Loss function used for training.

  • x_train – Training data.

  • y_train – Training labels.

  • dataset – Training dataset which returns (x, y).

  • optimizer – Optimizer used for training.

  • loss_fn_kwargs – Kwargs for loss function.

  • preprocess_fn – Preprocessing function applied to each training batch.

  • epochs – Number of training epochs.

  • reg_loss_fn – Allows an additional regularisation term to be defined as reg_loss_fn(model)

  • batch_size – Batch size used for training.

  • buffer_size – Maximum number of elements that will be buffered when prefetching.

  • verbose – Whether to print training progress.

  • log_metric – Additional metrics whose progress will be displayed if verbose equals True.

  • callbacks – Callbacks used during training.

Submodules