Skip to content

Commit ecb3bcc

Browse files
Rajakavitha1gitbook-bot
authored andcommitted
GITBOOK-4: No subject
1 parent 4a902f3 commit ecb3bcc

File tree

1 file changed

+42
-56
lines changed

1 file changed

+42
-56
lines changed

docs-gb/overview/saving.md

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Saving and Loading
22

3-
Alibi Detect includes support for saving and loading detectors to disk. To
4-
save a detector, simply call the `save_detector` method and provide a path to a directory (a new
5-
one will be created if it doesn't exist):
3+
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):
64

75
```python
86
from alibi_detect.od import OutlierVAE
@@ -14,8 +12,7 @@ filepath = './my_detector/'
1412
save_detector(od, filepath)
1513
```
1614

17-
To load a previously saved detector, use the `load_detector` method and provide it with the path to the detector's
18-
directory:
15+
To load a previously saved detector, use the `load_detector` method and provide it with the path to the detector's directory:
1916

2017
```python
2118
from alibi_detect.saving import load_detector
@@ -24,31 +21,20 @@ filepath = './my_detector/'
2421
od = load_detector(filepath)
2522
```
2623

27-
```{warning}
28-
When loading a saved detector, a warning will be issued if the runtime alibi-detect version is
29-
different from the version used to save the detector. **It is highly recommended to use the same
30-
alibi-detect, Python and dependency versions as were used to save the detector to avoid potential
31-
bugs and incompatibilities**.
32-
```
24+
{% hint style="info" %}
25+
**Note**: When loading a saved detector, a warning will be issued if the runtime alibi-detect version is different from the version used to save the detector. **It is highly recommended to use the same alibi-detect, Python and dependency versions as were used to save the detector to avoid potential bugs and incompatibilities**.
26+
{% endhint %}
3327

3428
## Formats
35-
Detectors can be saved using two formats:
36-
37-
- **Config format**: For drift detectors, by default `save_detector` serializes the detector via a config file named `config.toml`,
38-
stored in `filepath`. The [TOML](https://toml.io/en/) format is human-readable, which makes the config files useful for
39-
record keeping, and allows a detector to be edited before it is reloaded. For more details, see
40-
[Detector Configuration Files](config_files.md).
4129

42-
- **Legacy format**: Outlier and adversarial detectors are saved to [dill](https://dill.readthedocs.io/en/latest/dill.html) files stored
43-
within `filepath`. Drift detectors can also be saved in this legacy format by running `save_detector` with
44-
`legacy=True`. Loading is performed in the same way, by simply running `load_detector(filepath)`.
30+
Detectors can be saved using two formats:
4531

32+
* **Config format**: For drift detectors, by default `save_detector` serializes the detector via a config file named `config.toml`, stored in `filepath`. The [TOML](https://toml.io/en/) 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 [Detector Configuration Files](config\_files.md).
33+
* **Legacy format**: Outlier and adversarial detectors are saved to [dill](https://dill.readthedocs.io/en/latest/dill.html) 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)`.
4634

4735
## Supported detectors
4836

49-
The following tables list the current state of save/load support for each detector. Adding full support
50-
for the remaining detectors is in the [Roadmap](roadmap.md).
51-
37+
The following tables list the current state of save/load support for each detector. Adding full support for the remaining detectors is in the [Roadmap](roadmap.md).
5238

5339
````{tab-set}
5440
@@ -97,58 +83,58 @@ for the remaining detectors is in the [Roadmap](roadmap.md).
9783
```
9884
````
9985

100-
(supported_models)=
86+
{% tabs %}
87+
{% tab title="Drift detectors" %}
88+
89+
{% endtab %}
90+
91+
{% tab title="Outlier detectors" %}
92+
93+
{% endtab %}
94+
95+
{% tab title="Adversarial detectors " %}
96+
| Detector | Legacy save/load | Config save/load |
97+
| ------------------ | ----------------- | ---------------- |
98+
| Adversarial AE |||
99+
| Model distillation |||
100+
{% endtab %}
101+
{% endtabs %}
102+
101103
## Supported ML models
102104

103-
Alibi Detect drift detectors offer the option to perform [preprocessing](../cd/background.md#input-preprocessing)
104-
with user-defined machine learning models:
105+
Alibi Detect drift detectors offer the option to perform [preprocessing](../cd/background.md#input-preprocessing) with user-defined machine learning models:
105106

106107
```python
107108
model = ... # A TensorFlow model
108109
preprocess_fn = partial(preprocess_drift, model=model, batch_size=128)
109110
cd = MMDDrift(x_ref, backend='tensorflow', p_val=.05, preprocess_fn=preprocess_fn)
110111
```
111112

112-
Additionally, some detectors are built upon models directly,
113-
for example the [Classifier](../cd/methods/classifierdrift.ipynb) drift detector requires a `model` to be passed
114-
as an argument:
113+
Additionally, some detectors are built upon models directly, for example the [Classifier](../cd/methods/classifierdrift.ipynb) drift detector requires a `model` to be passed as an argument:
115114

116115
```python
117116
cd = ClassifierDrift(x_ref, model, backend='sklearn', p_val=.05, preds_type='probs')
118117
```
119118

120-
In order for a detector to be saveable and loadable, any models contained within it (or referenced within a
121-
[detector configuration file](config_files.md#specifying-artefacts)) must fall within the family of supported models:
122-
123-
````{tab-set}
119+
In order for a detector to be saveable and loadable, any models contained within it (or referenced within a [detector configuration file](config\_files.md#specifying-artefacts)) must fall within the family of supported models:
124120

125-
```{tab-item} TensorFlow
126-
Alibi Detect supports serialization of any TensorFlow model that can be serialized to the
127-
[HDF5](https://www.tensorflow.org/guide/keras/save_and_serialize#keras_h5_format) format.
128-
Custom objects should be pre-registered with
129-
[register_keras_serializable](https://www.tensorflow.org/api_docs/python/tf/keras/utils/register_keras_serializable).
130-
```
121+
{% tabs %}
122+
{% tab title="TensorFlow" %}
123+
Alibi Detect supports serialization of any TensorFlow model that can be serialized to the [HDF5](https://www.tensorflow.org/guide/keras/save\_and\_serialize#keras\_h5\_format) format. Custom objects should be pre-registered with [register\_keras\_serializable](https://www.tensorflow.org/api\_docs/python/tf/keras/utils/register\_keras\_serializable).
124+
{% endtab %}
131125

132-
```{tab-item} PyTorch
133-
PyTorch models are serialized by saving the [entire model](https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-entire-model)
134-
using the [dill](https://dill.readthedocs.io/en/latest/index.html) module. Therefore, Alibi Detect should support any PyTorch
135-
model that can be saved and loaded with `torch.save(..., pickle_module=dill)` and `torch.load(..., pickle_module=dill)`.
136-
```
126+
{% tab title="PyTorch" %}
127+
PyTorch models are serialized by saving the [entire model](https://pytorch.org/tutorials/beginner/saving\_loading\_models.html#save-load-entire-model) using the [dill](https://dill.readthedocs.io/en/latest/index.html) 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)`.
128+
{% endtab %}
137129

138-
```{tab-item} Scikit-learn
139-
Scikit-learn models are serialized using [joblib](https://joblib.readthedocs.io/en/latest/persistence.html).
140-
Any scikit-learn model that is a subclass of {py:class}`sklearn.base.BaseEstimator` is supported, including
141-
[xgboost](https://xgboost.readthedocs.io/en/latest/python/python_api.html#module-xgboost.sklearn) models following
142-
the scikit-learn API.
143-
```
144-
````
130+
{% tab title="Scikit-learn" %}
131+
Scikit-learn models are serialized using [joblib](https://joblib.readthedocs.io/en/latest/persistence.html). Any scikit-learn model that is a subclass of [`sklearn.base.BaseEstimator`](https://scikit-learn.org/stable/modules/generated/sklearn.base.BaseEstimator.html#sklearn.base.BaseEstimator) is supported, including [xgboost](https://xgboost.readthedocs.io/en/latest/python/python\_api.html#module-xgboost.sklearn) models following the scikit-learn API.
132+
{% endtab %}
133+
{% endtabs %}
145134

146135
## Online detectors
147136

148-
[Online drift detectors](../cd/methods.md#online) are stateful, with their state updated each timestep `t` (each time
149-
`.predict()` is called). {func}`~alibi_detect.saving.save_detector` will save the state of online
150-
detectors to disk if `t > 0`. At load time, {func}`~alibi_detect.saving.load_detector` will load this state.
151-
For example:
137+
[Online drift detectors](../cd/methods.md#online) 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:
152138

153139
```python
154140
from alibi_detect.cd import LSDDDriftOnline
@@ -173,4 +159,4 @@ To save a clean (stateless) detector, it should be reset before saving:
173159
```python
174160
dd.reset_state() # reset to t=0
175161
save_detector(dd, filepath) # save the detector without state
176-
```
162+
```

0 commit comments

Comments
 (0)