Saving and Loading
Last updated
Was this helpful?
Last updated
Was this helpful?
Alibi Detect includes support for saving and loading detectors to disk. To save a detector, simply call the save_detector
method and provide a path to a directory (a new one will be created if it doesn't exist):
To load a previously saved detector, use the load_detector
method and provide it with the path to the detector's directory:
Detectors can be saved using two formats:
Config format: For drift detectors, by default save_detector
serializes the detector via a config file named config.toml
, stored in filepath
. The format is human-readable, which makes the config files useful for record keeping, and allows a detector to be edited before it is reloaded. For more details, see .
Legacy format: Outlier and adversarial detectors are saved to files stored within filepath
. Drift detectors can also be saved in this legacy format by running save_detector
with legacy=True
. Loading is performed in the same way, by simply running load_detector(filepath)
.
To save a clean (stateless) detector, it should be reset before saving:
The following tables list the current state of save/load support for each detector. Adding full support for the remaining detectors is in the .
Alibi Detect drift detectors offer the option to perform with user-defined machine learning models:
Additionally, some detectors are built upon models directly, for example the drift detector requires a model
to be passed as an argument:
In order for a detector to be saveable and loadable, any models contained within it (or referenced within a ) must fall within the family of supported models:
Alibi Detect supports serialization of any TensorFlow model that can be serialized to the format. Custom objects should be pre-registered with .
PyTorch models are serialized by saving the using the module. Therefore, Alibi Detect should support any PyTorch model that can be saved and loaded with torch.save(..., pickle_module=dill)
and torch.load(..., pickle_module=dill)
.
Scikit-learn models are serialized using . Any scikit-learn model that is a subclass of is supported, including models following the scikit-learn API.
are stateful, with their state updated each timestep t
(each time .predict()
is called). {func}~alibi_detect.saving.save_detector
will save the state of online detectors to disk if t > 0
. At load time, {func}~alibi_detect.saving.load_detector
will load this state. For example: