deep-river 0.2.8__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {deep_river-0.2.8 → deep_river-0.3.0}/PKG-INFO +32 -11
- {deep_river-0.2.8 → deep_river-0.3.0}/README.md +27 -6
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/__version__.py +1 -1
- deep_river-0.3.0/deep_river/anomaly/__init__.py +23 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/anomaly/ae.py +181 -23
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/anomaly/probability_weighted_ae.py +96 -6
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/anomaly/rolling_ae.py +115 -5
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/anomaly/scaler.py +1 -2
- deep_river-0.3.0/deep_river/base.py +700 -0
- deep_river-0.3.0/deep_river/classification/__init__.py +23 -0
- deep_river-0.3.0/deep_river/classification/classifier.py +598 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/classification/rolling_classifier.py +200 -12
- deep_river-0.3.0/deep_river/classification/zoo.py +277 -0
- deep_river-0.3.0/deep_river/regression/__init__.py +21 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/regression/multioutput.py +117 -8
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/regression/regressor.py +118 -6
- deep_river-0.3.0/deep_river/regression/rolling_regressor.py +436 -0
- deep_river-0.3.0/deep_river/regression/zoo.py +234 -0
- deep_river-0.3.0/deep_river/utils/estimator_checks.py +70 -0
- deep_river-0.3.0/deep_river/utils/inspect.py +7 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/tensor_conversion.py +47 -19
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/test_estimators.py +7 -3
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/test_tensor_conversion.py +14 -14
- {deep_river-0.2.8 → deep_river-0.3.0}/pyproject.toml +4 -4
- deep_river-0.2.8/deep_river/anomaly/__init__.py +0 -17
- deep_river-0.2.8/deep_river/base.py +0 -312
- deep_river-0.2.8/deep_river/classification/__init__.py +0 -13
- deep_river-0.2.8/deep_river/classification/classifier.py +0 -344
- deep_river-0.2.8/deep_river/classification/zoo.py +0 -264
- deep_river-0.2.8/deep_river/regression/__init__.py +0 -17
- deep_river-0.2.8/deep_river/regression/rolling_regressor.py +0 -222
- deep_river-0.2.8/deep_river/regression/zoo.py +0 -167
- deep_river-0.2.8/deep_river/utils/estimator_checks.py +0 -21
- {deep_river-0.2.8 → deep_river-0.3.0}/LICENSE +0 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/__init__.py +0 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/__init__.py +0 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/hooks.py +0 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/layer_adaptation.py +0 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/params.py +0 -0
- {deep_river-0.2.8 → deep_river-0.3.0}/deep_river/utils/test_layer_adaptation.py +0 -0
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: deep_river
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Online Deep Learning for river
|
|
5
|
-
Home-page: https://github.com/online-ml/deep-river
|
|
6
5
|
License: BSD-3-Clause
|
|
7
6
|
Keywords: online-learning,deep-learning,data-streams
|
|
8
7
|
Author: Cedric Kulbach
|
|
@@ -18,15 +17,16 @@ Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
|
18
17
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
19
18
|
Requires-Dist: mkdocstrings[python] (>=0.19.0)
|
|
20
19
|
Requires-Dist: numpy (>=1.26.4,<1.27.0)
|
|
21
|
-
Requires-Dist: ordered-set (>=4.1.0,<4.2.0)
|
|
22
20
|
Requires-Dist: pandas (>=2.2.2,<2.3.0)
|
|
23
21
|
Requires-Dist: pytkdocs[numpy-style] (>=0.5.0)
|
|
24
22
|
Requires-Dist: river
|
|
25
23
|
Requires-Dist: scikit-learn (>=1.5.0,<1.6.0)
|
|
26
|
-
Requires-Dist:
|
|
24
|
+
Requires-Dist: sortedcontainers (>=2.4.0,<3.0.0)
|
|
25
|
+
Requires-Dist: torch (==2.2.2)
|
|
27
26
|
Requires-Dist: torchviz (>=0.0.2,<0.1.0)
|
|
28
27
|
Requires-Dist: tqdm (>=4.66.5,<4.67.0)
|
|
29
28
|
Project-URL: Documentation, https://github.com/online-ml/deep-river
|
|
29
|
+
Project-URL: Homepage, https://github.com/online-ml/deep-river
|
|
30
30
|
Project-URL: Repository, https://github.com/online-ml/deep-river
|
|
31
31
|
Description-Content-Type: text/markdown
|
|
32
32
|
|
|
@@ -42,6 +42,7 @@ Description-Content-Type: text/markdown
|
|
|
42
42
|
<img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/deep-river">
|
|
43
43
|
<img alt="GitHub" src="https://img.shields.io/github/license/online-ml/deep-river">
|
|
44
44
|
<a href="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a"><img src="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a/status.svg"></a>
|
|
45
|
+
<a href="https://doi.org/10.5281/zenodo.14601980"><img src="https://zenodo.org/badge/DOI/10.5281/zenodo.14601980.svg" alt="DOI"></a>
|
|
45
46
|
</p>
|
|
46
47
|
<p align="center">
|
|
47
48
|
deep-river is a Python library for online deep learning.
|
|
@@ -102,7 +103,7 @@ For further examples check out the <a href="https://online-ml.github.io/deep-riv
|
|
|
102
103
|
|
|
103
104
|
>>> model_pipeline = compose.Pipeline(
|
|
104
105
|
... preprocessing.StandardScaler(),
|
|
105
|
-
... classification.
|
|
106
|
+
... classification.ClassifierInitialized(module=MyModule(10), loss_fn='binary_cross_entropy', optimizer_fn='adam')
|
|
106
107
|
... )
|
|
107
108
|
|
|
108
109
|
>>> dataset = datasets.Phishing()
|
|
@@ -124,7 +125,7 @@ Accuracy: 0.7264
|
|
|
124
125
|
>>> from river import stream
|
|
125
126
|
>>> from sklearn import datasets
|
|
126
127
|
>>> from torch import nn
|
|
127
|
-
>>> from deep_river.regression.multioutput import
|
|
128
|
+
>>> from deep_river.regression.multioutput import MultiTargetRegressorInitialized
|
|
128
129
|
|
|
129
130
|
>>> class MyModule(nn.Module):
|
|
130
131
|
... def __init__(self, n_features):
|
|
@@ -142,8 +143,8 @@ Accuracy: 0.7264
|
|
|
142
143
|
... )
|
|
143
144
|
>>> model = compose.Pipeline(
|
|
144
145
|
... preprocessing.StandardScaler(),
|
|
145
|
-
...
|
|
146
|
-
... module=MyModule,
|
|
146
|
+
... MultiTargetRegressorInitialized(
|
|
147
|
+
... module=MyModule(10),
|
|
147
148
|
... loss_fn='mse',
|
|
148
149
|
... lr=0.3,
|
|
149
150
|
... optimizer_fn='sgd',
|
|
@@ -158,7 +159,7 @@ MicroAverage(MAE): 34.31
|
|
|
158
159
|
### Anomaly Detection
|
|
159
160
|
|
|
160
161
|
```python
|
|
161
|
-
>>> from deep_river.anomaly import
|
|
162
|
+
>>> from deep_river.anomaly import AutoencoderInitialized
|
|
162
163
|
>>> from river import metrics
|
|
163
164
|
>>> from river.datasets import CreditCard
|
|
164
165
|
>>> from torch import nn
|
|
@@ -183,7 +184,7 @@ MicroAverage(MAE): 34.31
|
|
|
183
184
|
... X = self.linear2(X)
|
|
184
185
|
... return self.sigmoid(X)
|
|
185
186
|
|
|
186
|
-
>>> ae =
|
|
187
|
+
>>> ae = AutoencoderInitialized(module=MyAutoEncoder(10), lr=0.005)
|
|
187
188
|
>>> scaler = MinMaxScaler()
|
|
188
189
|
>>> model = Pipeline(scaler, ae)
|
|
189
190
|
|
|
@@ -197,6 +198,26 @@ Rolling ROCAUC: 0.8901
|
|
|
197
198
|
|
|
198
199
|
```
|
|
199
200
|
|
|
201
|
+
## 💬 Citation
|
|
202
|
+
|
|
203
|
+
To acknowledge the use of the `DeepRiver` library in your research, please refer to our [paper](https://joss.theoj.org/papers/10.21105/joss.07226) published on Journal of Open Source Software (JOSS):
|
|
204
|
+
|
|
205
|
+
```bibtex
|
|
206
|
+
@article{Kulbach2025,
|
|
207
|
+
doi = {10.21105/joss.07226},
|
|
208
|
+
url = {https://doi.org/10.21105/joss.07226},
|
|
209
|
+
year = {2025},
|
|
210
|
+
publisher = {The Open Journal},
|
|
211
|
+
volume = {10},
|
|
212
|
+
number = {105},
|
|
213
|
+
pages = {7226},
|
|
214
|
+
author = {Cedric Kulbach and Lucas Cazzonelli and Hoang-Anh Ngo and Max Halford and Saulo Martiello Mastelini},
|
|
215
|
+
title = {DeepRiver: A Deep Learning Library for Data Streams},
|
|
216
|
+
journal = {Journal of Open Source Software}
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
|
|
200
221
|
## 🏫 Affiliations
|
|
201
222
|
|
|
202
223
|
<p align="center">
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
<img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/deep-river">
|
|
11
11
|
<img alt="GitHub" src="https://img.shields.io/github/license/online-ml/deep-river">
|
|
12
12
|
<a href="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a"><img src="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a/status.svg"></a>
|
|
13
|
+
<a href="https://doi.org/10.5281/zenodo.14601980"><img src="https://zenodo.org/badge/DOI/10.5281/zenodo.14601980.svg" alt="DOI"></a>
|
|
13
14
|
</p>
|
|
14
15
|
<p align="center">
|
|
15
16
|
deep-river is a Python library for online deep learning.
|
|
@@ -70,7 +71,7 @@ For further examples check out the <a href="https://online-ml.github.io/deep-riv
|
|
|
70
71
|
|
|
71
72
|
>>> model_pipeline = compose.Pipeline(
|
|
72
73
|
... preprocessing.StandardScaler(),
|
|
73
|
-
... classification.
|
|
74
|
+
... classification.ClassifierInitialized(module=MyModule(10), loss_fn='binary_cross_entropy', optimizer_fn='adam')
|
|
74
75
|
... )
|
|
75
76
|
|
|
76
77
|
>>> dataset = datasets.Phishing()
|
|
@@ -92,7 +93,7 @@ Accuracy: 0.7264
|
|
|
92
93
|
>>> from river import stream
|
|
93
94
|
>>> from sklearn import datasets
|
|
94
95
|
>>> from torch import nn
|
|
95
|
-
>>> from deep_river.regression.multioutput import
|
|
96
|
+
>>> from deep_river.regression.multioutput import MultiTargetRegressorInitialized
|
|
96
97
|
|
|
97
98
|
>>> class MyModule(nn.Module):
|
|
98
99
|
... def __init__(self, n_features):
|
|
@@ -110,8 +111,8 @@ Accuracy: 0.7264
|
|
|
110
111
|
... )
|
|
111
112
|
>>> model = compose.Pipeline(
|
|
112
113
|
... preprocessing.StandardScaler(),
|
|
113
|
-
...
|
|
114
|
-
... module=MyModule,
|
|
114
|
+
... MultiTargetRegressorInitialized(
|
|
115
|
+
... module=MyModule(10),
|
|
115
116
|
... loss_fn='mse',
|
|
116
117
|
... lr=0.3,
|
|
117
118
|
... optimizer_fn='sgd',
|
|
@@ -126,7 +127,7 @@ MicroAverage(MAE): 34.31
|
|
|
126
127
|
### Anomaly Detection
|
|
127
128
|
|
|
128
129
|
```python
|
|
129
|
-
>>> from deep_river.anomaly import
|
|
130
|
+
>>> from deep_river.anomaly import AutoencoderInitialized
|
|
130
131
|
>>> from river import metrics
|
|
131
132
|
>>> from river.datasets import CreditCard
|
|
132
133
|
>>> from torch import nn
|
|
@@ -151,7 +152,7 @@ MicroAverage(MAE): 34.31
|
|
|
151
152
|
... X = self.linear2(X)
|
|
152
153
|
... return self.sigmoid(X)
|
|
153
154
|
|
|
154
|
-
>>> ae =
|
|
155
|
+
>>> ae = AutoencoderInitialized(module=MyAutoEncoder(10), lr=0.005)
|
|
155
156
|
>>> scaler = MinMaxScaler()
|
|
156
157
|
>>> model = Pipeline(scaler, ae)
|
|
157
158
|
|
|
@@ -165,6 +166,26 @@ Rolling ROCAUC: 0.8901
|
|
|
165
166
|
|
|
166
167
|
```
|
|
167
168
|
|
|
169
|
+
## 💬 Citation
|
|
170
|
+
|
|
171
|
+
To acknowledge the use of the `DeepRiver` library in your research, please refer to our [paper](https://joss.theoj.org/papers/10.21105/joss.07226) published on Journal of Open Source Software (JOSS):
|
|
172
|
+
|
|
173
|
+
```bibtex
|
|
174
|
+
@article{Kulbach2025,
|
|
175
|
+
doi = {10.21105/joss.07226},
|
|
176
|
+
url = {https://doi.org/10.21105/joss.07226},
|
|
177
|
+
year = {2025},
|
|
178
|
+
publisher = {The Open Journal},
|
|
179
|
+
volume = {10},
|
|
180
|
+
number = {105},
|
|
181
|
+
pages = {7226},
|
|
182
|
+
author = {Cedric Kulbach and Lucas Cazzonelli and Hoang-Anh Ngo and Max Halford and Saulo Martiello Mastelini},
|
|
183
|
+
title = {DeepRiver: A Deep Learning Library for Data Streams},
|
|
184
|
+
journal = {Journal of Open Source Software}
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
|
|
168
189
|
## 🏫 Affiliations
|
|
169
190
|
|
|
170
191
|
<p align="center">
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from .ae import Autoencoder, AutoencoderInitialized
|
|
2
|
+
from .probability_weighted_ae import (
|
|
3
|
+
ProbabilityWeightedAutoencoder,
|
|
4
|
+
ProbabilityWeightedAutoencoderInitialized,
|
|
5
|
+
)
|
|
6
|
+
from .rolling_ae import RollingAutoencoder, RollingAutoencoderInitialized
|
|
7
|
+
from .scaler import AnomalyMeanScaler, AnomalyMinMaxScaler, AnomalyStandardScaler
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
This module contains the anomaly detection algorithms for the
|
|
11
|
+
deep_river package.
|
|
12
|
+
"""
|
|
13
|
+
__all__ = [
|
|
14
|
+
"Autoencoder",
|
|
15
|
+
"RollingAutoencoder",
|
|
16
|
+
"ProbabilityWeightedAutoencoder",
|
|
17
|
+
"AnomalyStandardScaler",
|
|
18
|
+
"AnomalyMeanScaler",
|
|
19
|
+
"AnomalyMinMaxScaler",
|
|
20
|
+
"AutoencoderInitialized",
|
|
21
|
+
"ProbabilityWeightedAutoencoderInitialized",
|
|
22
|
+
"RollingAutoencoderInitialized",
|
|
23
|
+
]
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import warnings
|
|
1
2
|
from typing import Any, Callable, Dict, Type, Union
|
|
2
3
|
|
|
3
4
|
import numpy as np
|
|
@@ -6,7 +7,7 @@ import torch
|
|
|
6
7
|
from river.anomaly.base import AnomalyDetector
|
|
7
8
|
from torch import nn
|
|
8
9
|
|
|
9
|
-
from deep_river.base import DeepEstimator
|
|
10
|
+
from deep_river.base import DeepEstimator, DeepEstimatorInitialized
|
|
10
11
|
from deep_river.utils import dict2tensor
|
|
11
12
|
from deep_river.utils.layer_adaptation import expand_layer
|
|
12
13
|
from deep_river.utils.tensor_conversion import df2tensor
|
|
@@ -15,6 +16,8 @@ from deep_river.utils.tensor_conversion import df2tensor
|
|
|
15
16
|
class _TestAutoencoder(torch.nn.Module):
|
|
16
17
|
def __init__(self, n_features, latent_dim=3):
|
|
17
18
|
super().__init__()
|
|
19
|
+
self.n_features = n_features
|
|
20
|
+
self.latent_dim = latent_dim
|
|
18
21
|
self.linear1 = nn.Linear(n_features, latent_dim)
|
|
19
22
|
self.nonlin = torch.nn.LeakyReLU()
|
|
20
23
|
self.linear2 = nn.Linear(latent_dim, n_features)
|
|
@@ -108,6 +111,12 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
108
111
|
seed: int = 42,
|
|
109
112
|
**kwargs,
|
|
110
113
|
):
|
|
114
|
+
warnings.warn(
|
|
115
|
+
"This is deprecated and will be removed in future releases. "
|
|
116
|
+
"Please instead use the AutoencoderInitialized class and "
|
|
117
|
+
"initialize the module beforehand"
|
|
118
|
+
)
|
|
119
|
+
|
|
111
120
|
super().__init__(
|
|
112
121
|
module=module,
|
|
113
122
|
loss_fn=loss_fn,
|
|
@@ -147,15 +156,10 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
147
156
|
Most estimators pass the full test suite. However, in some cases,
|
|
148
157
|
some estimators might not
|
|
149
158
|
be able to pass certain checks.
|
|
150
|
-
|
|
151
|
-
Returns
|
|
152
|
-
-------
|
|
153
|
-
set
|
|
154
|
-
Set of checks to skip during unit testing.
|
|
155
159
|
"""
|
|
156
160
|
return set()
|
|
157
161
|
|
|
158
|
-
def learn_one(self, x: dict, y: Any = None, **kwargs) ->
|
|
162
|
+
def learn_one(self, x: dict, y: Any = None, **kwargs) -> None:
|
|
159
163
|
"""
|
|
160
164
|
Performs one step of training with a single example.
|
|
161
165
|
|
|
@@ -165,19 +169,12 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
165
169
|
Input example.
|
|
166
170
|
|
|
167
171
|
**kwargs
|
|
168
|
-
|
|
169
|
-
Returns
|
|
170
|
-
-------
|
|
171
|
-
Autoencoder
|
|
172
|
-
The model itself.
|
|
173
172
|
"""
|
|
174
173
|
if not self.module_initialized:
|
|
175
174
|
self._update_observed_features(x)
|
|
176
175
|
self.initialize_module(x=x, **self.kwargs)
|
|
177
176
|
self._adapt_input_dim(x)
|
|
178
|
-
|
|
179
|
-
dict2tensor(x, features=self.observed_features, device=self.device)
|
|
180
|
-
)
|
|
177
|
+
self._learn(dict2tensor(x, features=self.observed_features, device=self.device))
|
|
181
178
|
|
|
182
179
|
def _learn(self, x: torch.Tensor) -> "Autoencoder":
|
|
183
180
|
self.module.train()
|
|
@@ -219,7 +216,7 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
219
216
|
loss = self.loss_func(x_pred, x_t).item()
|
|
220
217
|
return loss
|
|
221
218
|
|
|
222
|
-
def learn_many(self, X: pd.DataFrame) ->
|
|
219
|
+
def learn_many(self, X: pd.DataFrame) -> None:
|
|
223
220
|
"""
|
|
224
221
|
Performs one step of training with a batch of examples.
|
|
225
222
|
|
|
@@ -227,12 +224,6 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
227
224
|
----------
|
|
228
225
|
X
|
|
229
226
|
Input batch of examples.
|
|
230
|
-
|
|
231
|
-
Returns
|
|
232
|
-
-------
|
|
233
|
-
Autoencoder
|
|
234
|
-
The model itself.
|
|
235
|
-
|
|
236
227
|
"""
|
|
237
228
|
if not self.module_initialized:
|
|
238
229
|
|
|
@@ -241,7 +232,7 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
241
232
|
|
|
242
233
|
self._adapt_input_dim(X)
|
|
243
234
|
X_t = df2tensor(X, features=self.observed_features, device=self.device)
|
|
244
|
-
|
|
235
|
+
self._learn(X_t)
|
|
245
236
|
|
|
246
237
|
def score_many(self, X: pd.DataFrame) -> np.ndarray:
|
|
247
238
|
"""
|
|
@@ -292,3 +283,170 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
292
283
|
len(self.observed_features),
|
|
293
284
|
output=True,
|
|
294
285
|
)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class AutoencoderInitialized(DeepEstimatorInitialized, AnomalyDetector):
|
|
289
|
+
"""
|
|
290
|
+
Represents an initialized autoencoder for anomaly detection and feature learning.
|
|
291
|
+
|
|
292
|
+
This class is built upon the DeepEstimatorInitialized and AnomalyDetector
|
|
293
|
+
base classes. It provides methods for performing unsupervised learning
|
|
294
|
+
through an autoencoder mechanism. The primary objective of the class is
|
|
295
|
+
to train the autoencoder on input data and compute anomaly scores based
|
|
296
|
+
on the reconstruction error. It supports learning on individual examples
|
|
297
|
+
or entire batches of data.
|
|
298
|
+
|
|
299
|
+
Attributes
|
|
300
|
+
----------
|
|
301
|
+
is_feature_incremental : bool
|
|
302
|
+
Indicates whether the model is designed to increment features dynamically.
|
|
303
|
+
module : torch.nn.Module
|
|
304
|
+
The PyTorch model representing the autoencoder architecture.
|
|
305
|
+
loss_fn : Union[str, Callable]
|
|
306
|
+
Specifies the loss function to compute the reconstruction error.
|
|
307
|
+
optimizer_fn : Union[str, Callable]
|
|
308
|
+
Specifies the optimizer to be used for training the autoencoder.
|
|
309
|
+
lr : float
|
|
310
|
+
The learning rate for optimization.
|
|
311
|
+
device : str
|
|
312
|
+
The device on which the model is loaded and trained (e.g., "cpu",
|
|
313
|
+
"cuda").
|
|
314
|
+
seed : int
|
|
315
|
+
Random seed for ensuring reproducibility.
|
|
316
|
+
"""
|
|
317
|
+
|
|
318
|
+
def __init__(
|
|
319
|
+
self,
|
|
320
|
+
module: torch.nn.Module,
|
|
321
|
+
loss_fn: Union[str, Callable] = "mse",
|
|
322
|
+
optimizer_fn: Union[str, Callable] = "sgd",
|
|
323
|
+
lr: float = 1e-3,
|
|
324
|
+
is_feature_incremental: bool = False,
|
|
325
|
+
device: str = "cpu",
|
|
326
|
+
seed: int = 42,
|
|
327
|
+
**kwargs,
|
|
328
|
+
):
|
|
329
|
+
super().__init__(
|
|
330
|
+
module=module,
|
|
331
|
+
loss_fn=loss_fn,
|
|
332
|
+
optimizer_fn=optimizer_fn,
|
|
333
|
+
lr=lr,
|
|
334
|
+
is_feature_incremental=is_feature_incremental,
|
|
335
|
+
device=device,
|
|
336
|
+
seed=seed,
|
|
337
|
+
**kwargs,
|
|
338
|
+
)
|
|
339
|
+
self.is_feature_incremental = is_feature_incremental
|
|
340
|
+
|
|
341
|
+
@classmethod
|
|
342
|
+
def _unit_test_params(cls):
|
|
343
|
+
"""
|
|
344
|
+
Returns a dictionary of parameters to be used for unit testing
|
|
345
|
+
the respective class.
|
|
346
|
+
|
|
347
|
+
Yields
|
|
348
|
+
-------
|
|
349
|
+
dict
|
|
350
|
+
Dictionary of parameters to be used for unit testing
|
|
351
|
+
the respective class.
|
|
352
|
+
"""
|
|
353
|
+
|
|
354
|
+
yield {
|
|
355
|
+
"module": _TestAutoencoder(30, 3),
|
|
356
|
+
"loss_fn": "mse",
|
|
357
|
+
"optimizer_fn": "sgd",
|
|
358
|
+
"is_feature_incremental": False,
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
@classmethod
|
|
362
|
+
def _unit_test_skips(self) -> set:
|
|
363
|
+
"""
|
|
364
|
+
Indicates which checks to skip during unit testing.
|
|
365
|
+
Most estimators pass the full test suite. However, in some cases,
|
|
366
|
+
some estimators might not
|
|
367
|
+
be able to pass certain checks.
|
|
368
|
+
"""
|
|
369
|
+
return set()
|
|
370
|
+
|
|
371
|
+
def learn_one(self, x: dict, y: Any = None, **kwargs) -> None:
|
|
372
|
+
"""
|
|
373
|
+
Performs one step of training with a single example.
|
|
374
|
+
|
|
375
|
+
Parameters
|
|
376
|
+
----------
|
|
377
|
+
x
|
|
378
|
+
Input example.
|
|
379
|
+
|
|
380
|
+
**kwargs
|
|
381
|
+
"""
|
|
382
|
+
self._update_observed_features(x)
|
|
383
|
+
self._learn(self._dict2tensor(x))
|
|
384
|
+
|
|
385
|
+
def score_one(self, x: dict) -> float:
|
|
386
|
+
"""
|
|
387
|
+
Returns an anomaly score for the provided example in the form of
|
|
388
|
+
the autoencoder's reconstruction error.
|
|
389
|
+
|
|
390
|
+
Parameters
|
|
391
|
+
----------
|
|
392
|
+
x
|
|
393
|
+
Input example.
|
|
394
|
+
|
|
395
|
+
Returns
|
|
396
|
+
-------
|
|
397
|
+
float
|
|
398
|
+
Anomaly score for the given example. Larger values indicate
|
|
399
|
+
more anomalous examples.
|
|
400
|
+
|
|
401
|
+
"""
|
|
402
|
+
|
|
403
|
+
self._update_observed_features(x)
|
|
404
|
+
x_t = self._dict2tensor(x)
|
|
405
|
+
self.module.eval()
|
|
406
|
+
with torch.inference_mode():
|
|
407
|
+
x_pred = self.module(x_t)
|
|
408
|
+
loss = self.loss_func(x_pred, x_t).item()
|
|
409
|
+
return loss
|
|
410
|
+
|
|
411
|
+
def learn_many(self, X: pd.DataFrame) -> None:
|
|
412
|
+
"""
|
|
413
|
+
Performs one step of training with a batch of examples.
|
|
414
|
+
|
|
415
|
+
Parameters
|
|
416
|
+
----------
|
|
417
|
+
X
|
|
418
|
+
Input batch of examples.
|
|
419
|
+
"""
|
|
420
|
+
|
|
421
|
+
self._update_observed_features(X)
|
|
422
|
+
X_t = self._df2tensor(X)
|
|
423
|
+
self._learn(X_t)
|
|
424
|
+
|
|
425
|
+
def score_many(self, X: pd.DataFrame) -> np.ndarray:
|
|
426
|
+
"""
|
|
427
|
+
Returns an anomaly score for the provided batch of examples in
|
|
428
|
+
the form of the autoencoder's reconstruction error.
|
|
429
|
+
|
|
430
|
+
Parameters
|
|
431
|
+
----------
|
|
432
|
+
x
|
|
433
|
+
Input batch of examples.
|
|
434
|
+
|
|
435
|
+
Returns
|
|
436
|
+
-------
|
|
437
|
+
float
|
|
438
|
+
Anomaly scores for the given batch of examples. Larger values
|
|
439
|
+
indicate more anomalous examples.
|
|
440
|
+
"""
|
|
441
|
+
self._update_observed_features(X)
|
|
442
|
+
x_t = self._df2tensor(X)
|
|
443
|
+
|
|
444
|
+
self.module.eval()
|
|
445
|
+
with torch.inference_mode():
|
|
446
|
+
x_pred = self.module(x_t)
|
|
447
|
+
loss = torch.mean(
|
|
448
|
+
self.loss_func(x_pred, x_t, reduction="none"),
|
|
449
|
+
dim=list(range(1, x_t.dim())),
|
|
450
|
+
)
|
|
451
|
+
score = loss.cpu().detach().numpy()
|
|
452
|
+
return score
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import math
|
|
2
|
+
import warnings
|
|
2
3
|
from typing import Any, Callable, Type, Union
|
|
3
4
|
|
|
4
5
|
import pandas as pd
|
|
@@ -117,9 +118,7 @@ class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
|
117
118
|
self.rolling_mean = utils.Rolling(stats.Mean(), window_size=window_size)
|
|
118
119
|
self.rolling_var = utils.Rolling(stats.Var(), window_size=window_size)
|
|
119
120
|
|
|
120
|
-
def learn_one(
|
|
121
|
-
self, x: dict, y: Any = None, **kwargs
|
|
122
|
-
) -> "ProbabilityWeightedAutoencoder":
|
|
121
|
+
def learn_one(self, x: dict, y: Any = None, **kwargs) -> None:
|
|
123
122
|
"""
|
|
124
123
|
Performs one step of training with a single example,
|
|
125
124
|
scaling the employed learning rate based on the outlier
|
|
@@ -147,7 +146,6 @@ class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
|
147
146
|
x_pred = self.module(x_t)
|
|
148
147
|
loss = self.loss_func(x_pred, x_t)
|
|
149
148
|
self._apply_loss(loss)
|
|
150
|
-
return self
|
|
151
149
|
|
|
152
150
|
def _apply_loss(self, loss):
|
|
153
151
|
losses_numpy = loss.detach().numpy()
|
|
@@ -169,7 +167,7 @@ class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
|
169
167
|
loss.backward()
|
|
170
168
|
self.optimizer.step()
|
|
171
169
|
|
|
172
|
-
def learn_many(self, X: pd.DataFrame) ->
|
|
170
|
+
def learn_many(self, X: pd.DataFrame) -> None:
|
|
173
171
|
if not self.module_initialized:
|
|
174
172
|
self._update_observed_features(X)
|
|
175
173
|
self.initialize_module(x=X, **self.kwargs)
|
|
@@ -183,4 +181,96 @@ class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
|
183
181
|
dim=list(range(1, X_t.dim())),
|
|
184
182
|
)
|
|
185
183
|
self._apply_loss(loss)
|
|
186
|
-
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class ProbabilityWeightedAutoencoderInitialized(ae.AutoencoderInitialized):
|
|
187
|
+
""" """
|
|
188
|
+
|
|
189
|
+
def __init__(
|
|
190
|
+
self,
|
|
191
|
+
module: torch.nn.Module,
|
|
192
|
+
loss_fn: Union[str, Callable] = "mse",
|
|
193
|
+
optimizer_fn: Union[str, Callable] = "sgd",
|
|
194
|
+
lr: float = 1e-3,
|
|
195
|
+
device: str = "cpu",
|
|
196
|
+
seed: int = 42,
|
|
197
|
+
skip_threshold: float = 0.9,
|
|
198
|
+
window_size=250,
|
|
199
|
+
**kwargs,
|
|
200
|
+
):
|
|
201
|
+
warnings.warn(
|
|
202
|
+
"This is deprecated and will be removed in future releases. "
|
|
203
|
+
"Please instead use the AutoencoderInitialized class and "
|
|
204
|
+
"initialize the module beforehand"
|
|
205
|
+
)
|
|
206
|
+
super().__init__(
|
|
207
|
+
module=module,
|
|
208
|
+
loss_fn=loss_fn,
|
|
209
|
+
optimizer_fn=optimizer_fn,
|
|
210
|
+
lr=lr,
|
|
211
|
+
device=device,
|
|
212
|
+
seed=seed,
|
|
213
|
+
**kwargs,
|
|
214
|
+
)
|
|
215
|
+
self.window_size = window_size
|
|
216
|
+
self.skip_threshold = skip_threshold
|
|
217
|
+
self.rolling_mean = utils.Rolling(stats.Mean(), window_size=window_size)
|
|
218
|
+
self.rolling_var = utils.Rolling(stats.Var(), window_size=window_size)
|
|
219
|
+
|
|
220
|
+
def learn_one(self, x: dict, y: Any = None, **kwargs) -> None:
|
|
221
|
+
"""
|
|
222
|
+
Performs one step of training with a single example,
|
|
223
|
+
scaling the employed learning rate based on the outlier
|
|
224
|
+
probability estimate of the input example.
|
|
225
|
+
|
|
226
|
+
Parameters
|
|
227
|
+
----------
|
|
228
|
+
**kwargs
|
|
229
|
+
x
|
|
230
|
+
Input example.
|
|
231
|
+
|
|
232
|
+
Returns
|
|
233
|
+
-------
|
|
234
|
+
ProbabilityWeightedAutoencoder
|
|
235
|
+
The autoencoder itself.
|
|
236
|
+
"""
|
|
237
|
+
|
|
238
|
+
self._update_observed_features(x)
|
|
239
|
+
x_t = self._dict2tensor(x)
|
|
240
|
+
|
|
241
|
+
self.module.train()
|
|
242
|
+
x_pred = self.module(x_t)
|
|
243
|
+
loss = self.loss_func(x_pred, x_t)
|
|
244
|
+
self._apply_loss(loss)
|
|
245
|
+
|
|
246
|
+
def _apply_loss(self, loss):
|
|
247
|
+
losses_numpy = loss.detach().numpy()
|
|
248
|
+
mean = self.rolling_mean.get()
|
|
249
|
+
var = self.rolling_var.get() if self.rolling_var.get() > 0 else 1
|
|
250
|
+
if losses_numpy.ndim == 0:
|
|
251
|
+
self.rolling_mean.update(losses_numpy)
|
|
252
|
+
self.rolling_var.update(losses_numpy)
|
|
253
|
+
else:
|
|
254
|
+
for loss_numpy in range(len(losses_numpy)):
|
|
255
|
+
self.rolling_mean.update(loss_numpy)
|
|
256
|
+
self.rolling_var.update(loss_numpy)
|
|
257
|
+
|
|
258
|
+
loss_scaled = (losses_numpy - mean) / math.sqrt(var)
|
|
259
|
+
prob = ndtr(loss_scaled)
|
|
260
|
+
loss = torch.tensor((self.skip_threshold - prob) / self.skip_threshold) * loss
|
|
261
|
+
|
|
262
|
+
self.optimizer.zero_grad()
|
|
263
|
+
loss.backward()
|
|
264
|
+
self.optimizer.step()
|
|
265
|
+
|
|
266
|
+
def learn_many(self, X: pd.DataFrame) -> None:
|
|
267
|
+
self._update_observed_features(X)
|
|
268
|
+
X_t = self._df2tensor(X)
|
|
269
|
+
|
|
270
|
+
self.module.train()
|
|
271
|
+
x_pred = self.module(X)
|
|
272
|
+
loss = torch.mean(
|
|
273
|
+
self.loss_func(x_pred, X_t, reduction="none"),
|
|
274
|
+
dim=list(range(1, X_t.dim())),
|
|
275
|
+
)
|
|
276
|
+
self._apply_loss(loss)
|