You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs-gb/overview/saving.md
+42-56Lines changed: 42 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# Saving and Loading
2
2
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):
6
4
7
5
```python
8
6
from alibi_detect.od import OutlierVAE
@@ -14,8 +12,7 @@ filepath = './my_detector/'
14
12
save_detector(od, filepath)
15
13
```
16
14
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:
19
16
20
17
```python
21
18
from alibi_detect.saving import load_detector
@@ -24,31 +21,20 @@ filepath = './my_detector/'
24
21
od = load_detector(filepath)
25
22
```
26
23
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 %}
33
27
34
28
## 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).
41
29
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:
45
31
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)`.
46
34
47
35
## Supported detectors
48
36
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).
52
38
53
39
````{tab-set}
54
40
@@ -97,58 +83,58 @@ for the remaining detectors is in the [Roadmap](roadmap.md).
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:
cd = MMDDrift(x_ref, backend='tensorflow', p_val=.05, preprocess_fn=preprocess_fn)
110
111
```
111
112
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:
115
114
116
115
```python
117
116
cd = ClassifierDrift(x_ref, model, backend='sklearn', p_val=.05, preds_type='probs')
118
117
```
119
118
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:
124
120
125
-
```{tab-item} TensorFlow
126
-
Alibi Detect supports serialization of any TensorFlow model that can be serialized to the
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 %}
131
125
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 %}
137
129
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 %}
145
134
146
135
## Online detectors
147
136
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:
152
138
153
139
```python
154
140
from alibi_detect.cd import LSDDDriftOnline
@@ -173,4 +159,4 @@ To save a clean (stateless) detector, it should be reset before saving:
173
159
```python
174
160
dd.reset_state() # reset to t=0
175
161
save_detector(dd, filepath) # save the detector without state
0 commit comments