alibi_detect.models.tensorflow package

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

Bases: tensorflow.keras.Model

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

Combine encoder and decoder in AE.

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

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

  • name (str) – Name of autoencoder model.

Return type

None

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: tensorflow.keras.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 (Sequential) – Layers for the encoder wrapped in a tf.keras.Sequential class.

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

  • gmm_density_net (Sequential) – 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.

Return type

None

call(x)[source]
Return type

Tuple[Tensor, Tensor, Tensor]

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: tensorflow.keras.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 (Sequential) – 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.

Return type

None

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.VAE(encoder_net, decoder_net, latent_dim, beta=1.0, name='vae')[source]

Bases: tensorflow.keras.Model

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

Combine encoder and decoder in VAE.

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

  • decoder_net (Sequential) – 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.

Return type

None

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: tensorflow.keras.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 (Sequential) – Layers for the encoder wrapped in a tf.keras.Sequential class.

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

  • gmm_density_net (Sequential) – 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.

Return type

None

call(x)[source]
Return type

Tuple[Tensor, Tensor, Tensor]

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.

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: tensorflow_probability.python.distributions.distribution.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’.

  • 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.

Return type

None

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

Bases: tensorflow.keras.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.

Return type

None

call(tokens)[source]
Return type

Tensor

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.

  • 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.