alibi_detect.saving.schemas module
Pydantic models used by validate_config()
to validate configuration dictionaries.
The resolved kwarg of validate_config()
determines whether the unresolved or
resolved pydantic models are used:
The unresolved models expect any artefacts specified within it to not yet have been resolved. The artefacts are still string references to local filepaths or registries (e.g. x_ref = ‘x_ref.npy’).
The resolved models expect all artefacts to be have been resolved into runtime objects. For example, x_ref should have been resolved into an np.ndarray.
Note
For detector pydantic models, the fields match the corresponding detector’s args/kwargs. Refer to the detector’s api docs for a full description of each arg/kwarg.
- class alibi_detect.saving.schemas.CVMDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the CVMDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
CVMDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.CVMDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the CVMDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
CVMDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.CVMDriftOnlineConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the CVMDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
CVMDriftOnline
documentation for a description of each field.
- class alibi_detect.saving.schemas.CVMDriftOnlineConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the CVMDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
CVMDriftOnline
documentation for a description of each field.
- class alibi_detect.saving.schemas.ChiSquareDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the ChiSquareDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
ChiSquareDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.ChiSquareDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the ChiSquareDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
ChiSquareDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.ClassifierDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the ClassifierDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
ClassifierDrift
documentation for a description of each field.- model: str | ModelConfig
- optimizer: str | OptimizerConfig | None = None
- class alibi_detect.saving.schemas.ClassifierDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the ClassifierDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
ClassifierDrift
documentation for a description of each field.- model: SupportedModel | None = None
- optimizer: SupportedOptimizer | None = None
- class alibi_detect.saving.schemas.ClassifierUncertaintyDriftConfig(*args, **kwargs)[source]
Bases:
DetectorConfig
Unresolved schema for the ClassifierUncertaintyDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
ClassifierUncertaintyDrift
documentation for a description of each field.- model: str | ModelConfig
- tokenizer: str | TokenizerConfig | None = None
- class alibi_detect.saving.schemas.ClassifierUncertaintyDriftConfigResolved(*args, **kwargs)[source]
Bases:
DetectorConfig
Resolved schema for the ClassifierUncertaintyDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
ClassifierUncertaintyDrift
documentation for a description of each field.- model: SupportedModel | None = None
- class alibi_detect.saving.schemas.ContextMMDDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the ContextMMDDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
ContextMMDDrift
documentation for a description of each field.- c_kernel: str | KernelConfig | None = None
- x_kernel: str | KernelConfig | None = None
- class alibi_detect.saving.schemas.ContextMMDDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the MMDDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
MMDDrift
documentation for a description of each field.- c_ref: ndarray
- class alibi_detect.saving.schemas.CustomBaseModel(*args, **kwargs)[source]
Bases:
BaseModel
Base pydantic model schema. The default pydantic settings are set here.
- class alibi_detect.saving.schemas.CustomBaseModelWithKwargs(*args, **kwargs)[source]
Bases:
BaseModel
Base pydantic model schema. The default pydantic settings are set here.
- class alibi_detect.saving.schemas.DeepKernelConfig(*args, **kwargs)[source]
Bases:
CustomBaseModel
Unresolved schema for
DeepKernel
’s.Examples
A
DeepKernel
, with a trainableGaussianRBF
kernel applied to the projected inputs and a custom serialized kernel applied to the raw inputs:[kernel] eps = 0.01 [kernel.kernel_a] src = "@utils.tensorflow.kernels.GaussianRBF" trainable = true [kernel.kernel_b] src = "custom_kernel.dill" sigma = [ 1.2,] trainable = false [kernel.proj] src = "model/"
- eps: float | str = 'trainable'
The proportion (in [0,1]) of weight to assign to the kernel applied to raw inputs. This can be either specified or set to ‘trainable’. Only relevant is kernel_b is not None.
- kernel_a: str | KernelConfig = '@utils.tensorflow.kernels.GaussianRBF'
The kernel to apply to the projected inputs. Defaults to a
GaussianRBF
with trainable bandwidth.
- kernel_b: str | KernelConfig | None = '@utils.tensorflow.kernels.GaussianRBF'
The kernel to apply to the raw inputs. Defaults to a
GaussianRBF
with trainable bandwidth. Set to None in order to use only the deep component (i.e. eps=0).
- proj: str | ModelConfig
The projection to be applied to the inputs before applying kernel_a. This should be a Tensorflow or PyTorch model, specified as an object registry reference, or a
ModelConfig
.
- class alibi_detect.saving.schemas.DetectorConfig(*args, **kwargs)[source]
Bases:
CustomBaseModel
Base detector config schema. Only fields universal across all detectors are defined here.
- class alibi_detect.saving.schemas.DriftDetectorConfig(*args, **kwargs)[source]
Bases:
DetectorConfig
Unresolved base schema for drift detectors.
- input_shape: tuple | None = None
Optionally pass the shape of the input data. Used when saving detectors.
- preprocess_fn: str | PreprocessConfig | None = None
Function to preprocess the data before computing the data drift metrics. A string referencing a serialized function in .dill format, an object registry reference, or a
PreprocessConfig
.
- class alibi_detect.saving.schemas.DriftDetectorConfigResolved(*args, **kwargs)[source]
Bases:
DetectorConfig
Resolved base schema for drift detectors.
- input_shape: tuple | None = None
Optionally pass the shape of the input data. Used when saving detectors.
- class alibi_detect.saving.schemas.EmbeddingConfig(*args, **kwargs)[source]
Bases:
CustomBaseModel
Unresolved schema for text embedding models. Currently, only pre-trained HuggingFace transformer models are supported.
Examples
Using the hidden states at the output of each layer of a TensorFlow BERT base model as text embeddings:
[embedding] flavour = "tensorflow" src = "bert-base-cased" type = "hidden_state" layers = [-1, -2, -3, -4, -5, -6, -7, -8]
- flavour: Literal['tensorflow', 'pytorch'] = 'tensorflow'
Whether the embedding model is a tensorflow or pytorch model.
- layers: List[int] | None = None
List specifying the hidden layers to be used to extract the embedding.
- src: str
Model name e.g. “bert-base-cased”, or a filepath to directory storing the model to extract embeddings from (relative to the config.toml file, or absolute).
- type: Literal['pooler_output', 'last_hidden_state', 'hidden_state', 'hidden_state_cls']
The type of embedding to be loaded. See embedding_type in
TransformerEmbedding
.
- class alibi_detect.saving.schemas.FETDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the FETDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
FETDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.FETDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the FETDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
FETDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.FETDriftOnlineConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the FETDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
FETDriftOnline
documentation for a description of each field.
- class alibi_detect.saving.schemas.FETDriftOnlineConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the FETDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
FETDriftOnline
documentation for a description of each field.
- class alibi_detect.saving.schemas.KSDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the KSDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
KSDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.KSDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the KSDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
KSDrift
documentation for a description of each field. Resolved schema for theKSDrift
detector.
- class alibi_detect.saving.schemas.KernelConfig(*args, **kwargs)[source]
Bases:
CustomBaseModelWithKwargs
Unresolved schema for kernels, to be passed to a detector’s kernel kwarg.
If src specifies a
GaussianRBF
kernel, the sigma, trainable and init_sigma_fn fields are passed to it. Otherwise, all fields except src are passed as kwargs.Examples
A
GaussianRBF
kernel, with three different bandwidths:[kernel] src = "@alibi_detect.utils.tensorflow.GaussianRBF" trainable = false sigma = [0.1, 0.2, 0.3]
A serialized kernel with keyword arguments passed:
[kernel] src = "mykernel.dill" sigma = 0.42 custom_setting = "xyz"
- flavour: Literal['tensorflow', 'pytorch', 'keops']
Whether the kernel is a tensorflow or pytorch kernel.
- init_sigma_fn: str | None = None
Function used to compute the bandwidth sigma. Used when sigma is to be inferred. The function’s signature should match
sigma_median()
. If None, it is set tosigma_median()
.
- sigma: float | List[float] | None = None
Bandwidth used for the kernel. Needn’t be specified if being inferred or trained. Can pass multiple values to eval kernel with and then average.
- class alibi_detect.saving.schemas.LSDDDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the LSDDDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
LSDDDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.LSDDDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the LSDDDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
LSDDDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.LSDDDriftOnlineConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the LSDDDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
LSDDDriftOnline
documentation for a description of each field.
- class alibi_detect.saving.schemas.LSDDDriftOnlineConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the LSDDDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
LSDDDriftOnline
documentation for a description of each field.
- class alibi_detect.saving.schemas.LearnedKernelDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the LearnedKernelDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
LearnedKernelDrift
documentation for a description of each field.- kernel: str | DeepKernelConfig
- optimizer: str | OptimizerConfig | None = None
- class alibi_detect.saving.schemas.LearnedKernelDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the LearnedKernelDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
LearnedKernelDrift
documentation for a description of each field.- optimizer: SupportedOptimizer | None = None
- class alibi_detect.saving.schemas.MMDDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the MMDDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
MMDDrift
documentation for a description of each field.- kernel: str | KernelConfig | None = None
- class alibi_detect.saving.schemas.MMDDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the MMDDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
MMDDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.MMDDriftOnlineConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the MMDDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
MMDDriftOnline
documentation for a description of each field.- kernel: str | KernelConfig | None = None
- class alibi_detect.saving.schemas.MMDDriftOnlineConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the MMDDriftOnline detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
MMDDriftOnline
documentation for a description of each field.
- class alibi_detect.saving.schemas.MetaData(*args, **kwargs)[source]
Bases:
CustomBaseModel
- class alibi_detect.saving.schemas.ModelConfig(*args, **kwargs)[source]
Bases:
CustomBaseModel
Unresolved schema for (ML) models. Note that the model “backend” e.g. ‘tensorflow’, ‘pytorch’, ‘sklearn’, is set by backend in
DetectorConfig
.Examples
A TensorFlow classifier model stored in the model/ directory, with the softmax layer extracted:
[model] flavour = "tensorflow" src = "model/" layer = -1
- custom_objects: dict | None = None
Dictionary of custom objects. Passed to the tensorflow load_model function. This can be used to pass custom registered functions and classes to a model.
- flavour: Literal['tensorflow', 'pytorch', 'sklearn']
Whether the model is a tensorflow, pytorch or sklearn model. XGBoost models following the scikit-learn API are also included under sklearn.
- layer: int | None = None
Optional index of hidden layer to extract. If not None, a
HiddenOutput
orHiddenOutput
model is returned (dependent on flavour). Only applies to ‘tensorflow’ and ‘pytorch’ models.
- class alibi_detect.saving.schemas.OptimizerConfig(*args, **kwargs)[source]
Bases:
CustomBaseModelWithKwargs
Unresolved schema for optimizers. The optimizer dictionary has two possible formats:
1. A configuration dictionary compatible with tf.keras.optimizers.deserialize. For backend=’tensorflow’ only. 2. A dictionary containing only class_name, where this is a string referencing the optimizer name e.g. optimizer.class_name = ‘Adam’. In this case, the tensorflow or pytorch optimizer class of the same name is loaded. For backend=’tensorflow’ and backend=’pytorch’.
Examples
A TensorFlow Adam optimizer:
[optimizer] class_name = "Adam" [optimizer.config] name = "Adam" learning_rate = 0.001 decay = 0.0
A PyTorch Adam optimizer:
[optimizer] class_name = "Adam"
- class alibi_detect.saving.schemas.PreprocessConfig(*args, **kwargs)[source]
Bases:
CustomBaseModel
Unresolved schema for drift detector preprocess functions, to be passed to a detector’s preprocess_fn kwarg. Once loaded, the function is wrapped in a
partial()
, to be evaluated within the detector.If src specifies a generic Python function, the dictionary specified by kwargs is passed to it. Otherwise, if src specifies
preprocess_drift()
(src=’@cd.tensorflow.preprocess.preprocess_drift’), all fields (except kwargs) are passed to it.Examples
Preprocessor with a model, text embedding and tokenizer passed to
preprocess_drift()
:[preprocess_fn] src = "@cd.tensorflow.preprocess.preprocess_drift" batch_size = 32 max_len = 100 tokenizer.src = "tokenizer/" # TokenizerConfig [preprocess_fn.model] # ModelConfig src = "model/" [preprocess_fn.embedding] # EmbeddingConfig src = "embedding/" type = "hidden_state" layers = [-1, -2, -3, -4, -5, -6, -7, -8]
A serialized Python function with keyword arguments passed to it:
[preprocess_fn] src = 'myfunction.dill' kwargs = {'kwarg1'=0.7, 'kwarg2'=true}
- device: Literal['cpu', 'cuda'] | None = None
Device type used. The default None tries to use the GPU and falls back on CPU if needed. Only relevant if src=’@cd.torch.preprocess.preprocess_drift’
- embedding: str | EmbeddingConfig | None = None
A text embedding model. Either a string referencing a HuggingFace transformer model name, an object registry reference, or a
EmbeddingConfig
. If model=None, the embedding is passed topreprocess_drift()
as model. Otherwise, the model is chained to the output of the embedding as an additional preprocessing step.
- kwargs: dict = {}
Dictionary of keyword arguments to be passed to the function specified by src. Only used if src specifies a generic Python function.
- model: str | ModelConfig | None = None
Model used for preprocessing. Either an object registry reference, or a
ModelConfig
.
- preprocess_batch_fn: str | None = None
Optional batch preprocessing function. For example to convert a list of objects to a batch which can be processed by the model.
- src: str = '@cd.tensorflow.preprocess.preprocess_drift'
The preprocessing function. A string referencing a filepath to a serialized function in dill format, or an object registry reference.
- tokenizer: str | TokenizerConfig | None = None
Optional tokenizer for text drift. Either a string referencing a HuggingFace tokenizer model name, or a
TokenizerConfig
.
- class alibi_detect.saving.schemas.RegressorUncertaintyDriftConfig(*args, **kwargs)[source]
Bases:
DetectorConfig
Unresolved schema for the RegressorUncertaintyDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
RegressorUncertaintyDrift
documentation for a description of each field.- model: str | ModelConfig
- tokenizer: str | TokenizerConfig | None = None
- class alibi_detect.saving.schemas.RegressorUncertaintyDriftConfigResolved(*args, **kwargs)[source]
Bases:
DetectorConfig
Resolved schema for the RegressorUncertaintyDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
RegressorUncertaintyDrift
documentation for a description of each field.- model: SupportedModel | None = None
- class alibi_detect.saving.schemas.SpotTheDiffDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the SpotTheDiffDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
SpotTheDiffDrift
documentation for a description of each field.- kernel: str | KernelConfig | None = None
- optimizer: str | OptimizerConfig | None = None
- class alibi_detect.saving.schemas.SpotTheDiffDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the SpotTheDiffDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
SpotTheDiffDrift
documentation for a description of each field.- optimizer: SupportedOptimizer | None = None
- class alibi_detect.saving.schemas.SupportedModel[source]
Bases:
object
Pydantic custom type to check the model is one of the supported types (conditional on what optional deps are installed).
- class alibi_detect.saving.schemas.SupportedOptimizer[source]
Bases:
object
Pydantic custom type to check the optimizer is one of the supported types (conditional on what optional deps are installed).
- class alibi_detect.saving.schemas.TabularDriftConfig(*args, **kwargs)[source]
Bases:
DriftDetectorConfig
Unresolved schema for the TabularDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
TabularDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.TabularDriftConfigResolved(*args, **kwargs)[source]
Bases:
DriftDetectorConfigResolved
Resolved schema for the TabularDrift detector.
Except for the name and meta fields, the fields match the detector’s args and kwargs. Refer to the
TabularDrift
documentation for a description of each field.
- class alibi_detect.saving.schemas.TokenizerConfig(*args, **kwargs)[source]
Bases:
CustomBaseModel
Unresolved schema for text tokenizers. Currently, only pre-trained HuggingFace tokenizer models are supported.
Examples
BERT base tokenizer with additional keyword arguments passed to the HuggingFace
from_pretrained()
method:[tokenizer] src = "bert-base-cased" [tokenizer.kwargs] use_fast = false force_download = true