deep-river 0.2.5__tar.gz → 0.2.7__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.5 → deep_river-0.2.7}/PKG-INFO +108 -9
- {deep_river-0.2.5 → deep_river-0.2.7}/README.md +12 -7
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/__version__.py +1 -1
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/anomaly/ae.py +57 -29
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/anomaly/probability_weighted_ae.py +16 -12
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/anomaly/rolling_ae.py +16 -23
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/anomaly/scaler.py +3 -7
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/base.py +107 -30
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/classification/classifier.py +103 -151
- deep_river-0.2.7/deep_river/classification/rolling_classifier.py +291 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/classification/zoo.py +44 -32
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/regression/multioutput.py +22 -13
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/regression/regressor.py +32 -21
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/regression/rolling_regressor.py +42 -39
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/regression/zoo.py +27 -20
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/utils/__init__.py +1 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/utils/estimator_checks.py +1 -0
- deep_river-0.2.7/deep_river/utils/layer_adaptation.py +239 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/utils/params.py +8 -7
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/utils/tensor_conversion.py +46 -23
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/utils/test_estimators.py +1 -0
- deep_river-0.2.7/deep_river/utils/test_layer_adaptation.py +179 -0
- deep_river-0.2.7/deep_river/utils/test_tensor_conversion.py +156 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river.egg-info/PKG-INFO +109 -10
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river.egg-info/SOURCES.txt +2 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river.egg-info/requires.txt +41 -40
- {deep_river-0.2.5 → deep_river-0.2.7}/setup.py +17 -17
- deep_river-0.2.5/deep_river/classification/rolling_classifier.py +0 -605
- deep_river-0.2.5/deep_river/utils/test_tensor_conversion.py +0 -117
- {deep_river-0.2.5 → deep_river-0.2.7}/LICENSE +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/__init__.py +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/anomaly/__init__.py +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/classification/__init__.py +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/regression/__init__.py +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river/utils/hooks.py +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river.egg-info/dependency_links.txt +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/deep_river.egg-info/top_level.txt +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/pyproject.toml +0 -0
- {deep_river-0.2.5 → deep_river-0.2.7}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: deep_river
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.7
|
|
4
4
|
Summary: Online Deep Learning for river
|
|
5
5
|
Home-page: https://online-ml.github.io/deep-river/
|
|
6
6
|
Author: Cedric Kulbach
|
|
@@ -16,10 +16,104 @@ Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
|
16
16
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
17
17
|
Requires-Python: >=3.6.0
|
|
18
18
|
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: scikit-learn~=1.5.0
|
|
21
|
+
Requires-Dist: torch~=2.2.2
|
|
22
|
+
Requires-Dist: pandas~=2.2.2
|
|
23
|
+
Requires-Dist: numpy~=1.26.4
|
|
24
|
+
Requires-Dist: river~=0.21.2
|
|
25
|
+
Requires-Dist: scipy~=1.13.0
|
|
26
|
+
Requires-Dist: tqdm~=4.66.5
|
|
27
|
+
Requires-Dist: ordered-set~=4.1.0
|
|
28
|
+
Requires-Dist: torchviz~=0.0.2
|
|
19
29
|
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: scikit-learn~=1.5.0; extra == "dev"
|
|
31
|
+
Requires-Dist: torch~=2.2.2; extra == "dev"
|
|
32
|
+
Requires-Dist: pandas~=2.2.2; extra == "dev"
|
|
33
|
+
Requires-Dist: numpy~=1.26.4; extra == "dev"
|
|
34
|
+
Requires-Dist: river~=0.21.2; extra == "dev"
|
|
35
|
+
Requires-Dist: scipy~=1.13.0; extra == "dev"
|
|
36
|
+
Requires-Dist: tqdm~=4.66.5; extra == "dev"
|
|
37
|
+
Requires-Dist: ordered-set~=4.1.0; extra == "dev"
|
|
38
|
+
Requires-Dist: torchviz~=0.0.2; extra == "dev"
|
|
39
|
+
Requires-Dist: graphviz>=0.20.3; extra == "dev"
|
|
40
|
+
Requires-Dist: matplotlib>=3.9.2; extra == "dev"
|
|
41
|
+
Requires-Dist: mypy>=1.11.1; extra == "dev"
|
|
42
|
+
Requires-Dist: pre-commit>=3.8.0; extra == "dev"
|
|
43
|
+
Requires-Dist: pytest>=8.3.2; extra == "dev"
|
|
44
|
+
Requires-Dist: pytest-cov>=5.0.0; extra == "dev"
|
|
45
|
+
Requires-Dist: black>=24.8.0; extra == "dev"
|
|
46
|
+
Requires-Dist: flake8>=7.1.1; extra == "dev"
|
|
47
|
+
Requires-Dist: isort>=5.13.2; extra == "dev"
|
|
48
|
+
Requires-Dist: jupyter>=1.0.0; extra == "dev"
|
|
49
|
+
Requires-Dist: pyupgrade==3.17.0; extra == "dev"
|
|
20
50
|
Provides-Extra: docs
|
|
51
|
+
Requires-Dist: scikit-learn~=1.5.0; extra == "docs"
|
|
52
|
+
Requires-Dist: torch~=2.2.2; extra == "docs"
|
|
53
|
+
Requires-Dist: pandas~=2.2.2; extra == "docs"
|
|
54
|
+
Requires-Dist: numpy~=1.26.4; extra == "docs"
|
|
55
|
+
Requires-Dist: river~=0.21.2; extra == "docs"
|
|
56
|
+
Requires-Dist: scipy~=1.13.0; extra == "docs"
|
|
57
|
+
Requires-Dist: tqdm~=4.66.5; extra == "docs"
|
|
58
|
+
Requires-Dist: ordered-set~=4.1.0; extra == "docs"
|
|
59
|
+
Requires-Dist: torchviz~=0.0.2; extra == "docs"
|
|
60
|
+
Requires-Dist: flask>=2.0.2; extra == "docs"
|
|
61
|
+
Requires-Dist: ipykernel>=6.9.0; extra == "docs"
|
|
62
|
+
Requires-Dist: mike>=0.5.3; extra == "docs"
|
|
63
|
+
Requires-Dist: mkdocs>=1.2.3; extra == "docs"
|
|
64
|
+
Requires-Dist: mkdocs-awesome-pages-plugin>=2.7.0; extra == "docs"
|
|
65
|
+
Requires-Dist: mkdocs-gen-files>=0.3.5; extra == "docs"
|
|
66
|
+
Requires-Dist: mkdocs-charts-plugin>=0.0.8; extra == "docs"
|
|
67
|
+
Requires-Dist: mkdocs-literate-nav>=0.4.1; extra == "docs"
|
|
68
|
+
Requires-Dist: mkdocs-material>=8.1.11; extra == "docs"
|
|
69
|
+
Requires-Dist: mkdocstrings[python]>=0.19.0; extra == "docs"
|
|
70
|
+
Requires-Dist: pytkdocs[numpy-style]>=0.5.0; extra == "docs"
|
|
71
|
+
Requires-Dist: ipython_genutils>=0.1.0; extra == "docs"
|
|
72
|
+
Requires-Dist: mkdocs-jupyter>=0.20.0; extra == "docs"
|
|
73
|
+
Requires-Dist: nbconvert>=6.4.2; extra == "docs"
|
|
74
|
+
Requires-Dist: numpydoc>=1.2; extra == "docs"
|
|
75
|
+
Requires-Dist: spacy>=3.2.2; extra == "docs"
|
|
76
|
+
Requires-Dist: jinja2>=3.0.3; extra == "docs"
|
|
77
|
+
Requires-Dist: dominate; extra == "docs"
|
|
78
|
+
Requires-Dist: jupyter-client; extra == "docs"
|
|
79
|
+
Requires-Dist: mkdocs-charts-plugin; extra == "docs"
|
|
80
|
+
Requires-Dist: python-slugify; extra == "docs"
|
|
81
|
+
Requires-Dist: watermark==2.3.1; extra == "docs"
|
|
21
82
|
Provides-Extra: all
|
|
22
|
-
|
|
83
|
+
Requires-Dist: graphviz>=0.20.3; extra == "all"
|
|
84
|
+
Requires-Dist: matplotlib>=3.9.2; extra == "all"
|
|
85
|
+
Requires-Dist: mypy>=1.11.1; extra == "all"
|
|
86
|
+
Requires-Dist: pre-commit>=3.8.0; extra == "all"
|
|
87
|
+
Requires-Dist: pytest>=8.3.2; extra == "all"
|
|
88
|
+
Requires-Dist: pytest-cov>=5.0.0; extra == "all"
|
|
89
|
+
Requires-Dist: black>=24.8.0; extra == "all"
|
|
90
|
+
Requires-Dist: flake8>=7.1.1; extra == "all"
|
|
91
|
+
Requires-Dist: isort>=5.13.2; extra == "all"
|
|
92
|
+
Requires-Dist: jupyter>=1.0.0; extra == "all"
|
|
93
|
+
Requires-Dist: pyupgrade==3.17.0; extra == "all"
|
|
94
|
+
Requires-Dist: flask>=2.0.2; extra == "all"
|
|
95
|
+
Requires-Dist: ipykernel>=6.9.0; extra == "all"
|
|
96
|
+
Requires-Dist: mike>=0.5.3; extra == "all"
|
|
97
|
+
Requires-Dist: mkdocs>=1.2.3; extra == "all"
|
|
98
|
+
Requires-Dist: mkdocs-awesome-pages-plugin>=2.7.0; extra == "all"
|
|
99
|
+
Requires-Dist: mkdocs-gen-files>=0.3.5; extra == "all"
|
|
100
|
+
Requires-Dist: mkdocs-charts-plugin>=0.0.8; extra == "all"
|
|
101
|
+
Requires-Dist: mkdocs-literate-nav>=0.4.1; extra == "all"
|
|
102
|
+
Requires-Dist: mkdocs-material>=8.1.11; extra == "all"
|
|
103
|
+
Requires-Dist: mkdocstrings[python]>=0.19.0; extra == "all"
|
|
104
|
+
Requires-Dist: pytkdocs[numpy-style]>=0.5.0; extra == "all"
|
|
105
|
+
Requires-Dist: ipython_genutils>=0.1.0; extra == "all"
|
|
106
|
+
Requires-Dist: mkdocs-jupyter>=0.20.0; extra == "all"
|
|
107
|
+
Requires-Dist: nbconvert>=6.4.2; extra == "all"
|
|
108
|
+
Requires-Dist: numpydoc>=1.2; extra == "all"
|
|
109
|
+
Requires-Dist: spacy>=3.2.2; extra == "all"
|
|
110
|
+
Requires-Dist: jinja2>=3.0.3; extra == "all"
|
|
111
|
+
Requires-Dist: dominate; extra == "all"
|
|
112
|
+
Requires-Dist: jupyter-client; extra == "all"
|
|
113
|
+
Requires-Dist: mkdocs-charts-plugin; extra == "all"
|
|
114
|
+
Requires-Dist: python-slugify; extra == "all"
|
|
115
|
+
Requires-Dist: watermark==2.3.1; extra == "all"
|
|
116
|
+
Requires-Dist: dataclasses; python_version == "3.6"
|
|
23
117
|
|
|
24
118
|
|
|
25
119
|
<p align="center">
|
|
@@ -32,6 +126,7 @@ License-File: LICENSE
|
|
|
32
126
|
</a>
|
|
33
127
|
<img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/deep-river">
|
|
34
128
|
<img alt="GitHub" src="https://img.shields.io/github/license/online-ml/deep-river">
|
|
129
|
+
<a href="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a"><img src="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a/status.svg"></a>
|
|
35
130
|
</p>
|
|
36
131
|
<p align="center">
|
|
37
132
|
deep-river is a Python library for online deep learning.
|
|
@@ -98,10 +193,10 @@ For further examples check out the <a href="https://online-ml.github.io/deep-riv
|
|
|
98
193
|
|
|
99
194
|
>>> for x, y in dataset:
|
|
100
195
|
... y_pred = model_pipeline.predict_one(x) # make a prediction
|
|
101
|
-
... metric
|
|
102
|
-
... model_pipeline
|
|
196
|
+
... metric.update(y, y_pred) # update the metric
|
|
197
|
+
... model_pipeline.learn_one(x, y) # make the model learn
|
|
103
198
|
>>> print(f"Accuracy: {metric.get():.4f}")
|
|
104
|
-
Accuracy: 0.
|
|
199
|
+
Accuracy: 0.7264
|
|
105
200
|
|
|
106
201
|
```
|
|
107
202
|
### Multi Target Regression
|
|
@@ -139,7 +234,7 @@ Accuracy: 0.6728
|
|
|
139
234
|
>>> metric = metrics.multioutput.MicroAverage(metrics.MAE())
|
|
140
235
|
>>> ev = evaluate.progressive_val_score(dataset, model, metric)
|
|
141
236
|
>>> print(f"MicroAverage(MAE): {metric.get():.2f}")
|
|
142
|
-
MicroAverage(MAE):
|
|
237
|
+
MicroAverage(MAE): 34.31
|
|
143
238
|
|
|
144
239
|
```
|
|
145
240
|
|
|
@@ -177,11 +272,11 @@ MicroAverage(MAE): 28.36
|
|
|
177
272
|
|
|
178
273
|
>>> for x, y in dataset:
|
|
179
274
|
... score = model.score_one(x)
|
|
180
|
-
... model
|
|
181
|
-
... metric
|
|
275
|
+
... model.learn_one(x=x)
|
|
276
|
+
... metric.update(y, score)
|
|
182
277
|
...
|
|
183
278
|
>>> print(f"ROCAUC: {metric.get():.4f}")
|
|
184
|
-
ROCAUC: 0.
|
|
279
|
+
ROCAUC: 0.7812
|
|
185
280
|
|
|
186
281
|
```
|
|
187
282
|
|
|
@@ -190,3 +285,7 @@ ROCAUC: 0.7447
|
|
|
190
285
|
<p align="center">
|
|
191
286
|
<img src="https://upload.wikimedia.org/wikipedia/de/thumb/4/44/Fzi_logo.svg/1200px-Fzi_logo.svg.png?raw=true" alt="FZI Logo" height="200"/>
|
|
192
287
|
</p>
|
|
288
|
+
|
|
289
|
+
<p align="center">
|
|
290
|
+
<img src="https://lieferbotnet.de/wp-content/uploads/2022/09/LieferBotNet-Logo.png?raw=true" alt="Lieferbot net" height="200"/>
|
|
291
|
+
</p>
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
</a>
|
|
9
9
|
<img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/deep-river">
|
|
10
10
|
<img alt="GitHub" src="https://img.shields.io/github/license/online-ml/deep-river">
|
|
11
|
+
<a href="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a"><img src="https://joss.theoj.org/papers/6a76784f55e8b041d71a7fa776eb386a/status.svg"></a>
|
|
11
12
|
</p>
|
|
12
13
|
<p align="center">
|
|
13
14
|
deep-river is a Python library for online deep learning.
|
|
@@ -74,10 +75,10 @@ For further examples check out the <a href="https://online-ml.github.io/deep-riv
|
|
|
74
75
|
|
|
75
76
|
>>> for x, y in dataset:
|
|
76
77
|
... y_pred = model_pipeline.predict_one(x) # make a prediction
|
|
77
|
-
... metric
|
|
78
|
-
... model_pipeline
|
|
78
|
+
... metric.update(y, y_pred) # update the metric
|
|
79
|
+
... model_pipeline.learn_one(x, y) # make the model learn
|
|
79
80
|
>>> print(f"Accuracy: {metric.get():.4f}")
|
|
80
|
-
Accuracy: 0.
|
|
81
|
+
Accuracy: 0.7264
|
|
81
82
|
|
|
82
83
|
```
|
|
83
84
|
### Multi Target Regression
|
|
@@ -115,7 +116,7 @@ Accuracy: 0.6728
|
|
|
115
116
|
>>> metric = metrics.multioutput.MicroAverage(metrics.MAE())
|
|
116
117
|
>>> ev = evaluate.progressive_val_score(dataset, model, metric)
|
|
117
118
|
>>> print(f"MicroAverage(MAE): {metric.get():.2f}")
|
|
118
|
-
MicroAverage(MAE):
|
|
119
|
+
MicroAverage(MAE): 34.31
|
|
119
120
|
|
|
120
121
|
```
|
|
121
122
|
|
|
@@ -153,11 +154,11 @@ MicroAverage(MAE): 28.36
|
|
|
153
154
|
|
|
154
155
|
>>> for x, y in dataset:
|
|
155
156
|
... score = model.score_one(x)
|
|
156
|
-
... model
|
|
157
|
-
... metric
|
|
157
|
+
... model.learn_one(x=x)
|
|
158
|
+
... metric.update(y, score)
|
|
158
159
|
...
|
|
159
160
|
>>> print(f"ROCAUC: {metric.get():.4f}")
|
|
160
|
-
ROCAUC: 0.
|
|
161
|
+
ROCAUC: 0.7812
|
|
161
162
|
|
|
162
163
|
```
|
|
163
164
|
|
|
@@ -166,3 +167,7 @@ ROCAUC: 0.7447
|
|
|
166
167
|
<p align="center">
|
|
167
168
|
<img src="https://upload.wikimedia.org/wikipedia/de/thumb/4/44/Fzi_logo.svg/1200px-Fzi_logo.svg.png?raw=true" alt="FZI Logo" height="200"/>
|
|
168
169
|
</p>
|
|
170
|
+
|
|
171
|
+
<p align="center">
|
|
172
|
+
<img src="https://lieferbotnet.de/wp-content/uploads/2022/09/LieferBotNet-Logo.png?raw=true" alt="Lieferbot net" height="200"/>
|
|
173
|
+
</p>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Any, Callable, Type, Union
|
|
1
|
+
from typing import Any, Callable, Dict, Type, Union
|
|
2
2
|
|
|
3
3
|
import numpy as np
|
|
4
4
|
import pandas as pd
|
|
@@ -8,6 +8,7 @@ from torch import nn
|
|
|
8
8
|
|
|
9
9
|
from deep_river.base import DeepEstimator
|
|
10
10
|
from deep_river.utils import dict2tensor
|
|
11
|
+
from deep_river.utils.layer_adaptation import expand_layer
|
|
11
12
|
from deep_river.utils.tensor_conversion import df2tensor
|
|
12
13
|
|
|
13
14
|
|
|
@@ -89,11 +90,11 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
89
90
|
|
|
90
91
|
>>> for x, y in dataset:
|
|
91
92
|
... score = model.score_one(x)
|
|
92
|
-
... model
|
|
93
|
-
... metric
|
|
93
|
+
... model.learn_one(x=x)
|
|
94
|
+
... metric.update(y, score)
|
|
94
95
|
...
|
|
95
96
|
>>> print(f"ROCAUC: {metric.get():.4f}")
|
|
96
|
-
ROCAUC: 0.
|
|
97
|
+
ROCAUC: 0.7812
|
|
97
98
|
"""
|
|
98
99
|
|
|
99
100
|
def __init__(
|
|
@@ -102,6 +103,7 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
102
103
|
loss_fn: Union[str, Callable] = "mse",
|
|
103
104
|
optimizer_fn: Union[str, Callable] = "sgd",
|
|
104
105
|
lr: float = 1e-3,
|
|
106
|
+
is_feature_incremental: bool = False,
|
|
105
107
|
device: str = "cpu",
|
|
106
108
|
seed: int = 42,
|
|
107
109
|
**kwargs,
|
|
@@ -111,10 +113,12 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
111
113
|
loss_fn=loss_fn,
|
|
112
114
|
optimizer_fn=optimizer_fn,
|
|
113
115
|
lr=lr,
|
|
116
|
+
is_feature_incremental=is_feature_incremental,
|
|
114
117
|
device=device,
|
|
115
118
|
seed=seed,
|
|
116
119
|
**kwargs,
|
|
117
120
|
)
|
|
121
|
+
self.is_class_incremental = is_feature_incremental
|
|
118
122
|
|
|
119
123
|
@classmethod
|
|
120
124
|
def _unit_test_params(cls):
|
|
@@ -133,6 +137,7 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
133
137
|
"module": _TestAutoencoder,
|
|
134
138
|
"loss_fn": "mse",
|
|
135
139
|
"optimizer_fn": "sgd",
|
|
140
|
+
"is_feature_incremental": True,
|
|
136
141
|
}
|
|
137
142
|
|
|
138
143
|
@classmethod
|
|
@@ -148,13 +153,7 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
148
153
|
set
|
|
149
154
|
Set of checks to skip during unit testing.
|
|
150
155
|
"""
|
|
151
|
-
return
|
|
152
|
-
"check_shuffle_features_no_impact",
|
|
153
|
-
"check_emerging_features",
|
|
154
|
-
"check_disappearing_features",
|
|
155
|
-
"check_predict_proba_one",
|
|
156
|
-
"check_predict_proba_one_binary",
|
|
157
|
-
}
|
|
156
|
+
return set()
|
|
158
157
|
|
|
159
158
|
def learn_one(self, x: dict, y: Any = None, **kwargs) -> "Autoencoder":
|
|
160
159
|
"""
|
|
@@ -173,14 +172,17 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
173
172
|
The model itself.
|
|
174
173
|
"""
|
|
175
174
|
if not self.module_initialized:
|
|
176
|
-
self.
|
|
177
|
-
self.initialize_module(**self.kwargs)
|
|
178
|
-
|
|
175
|
+
self._update_observed_features(x)
|
|
176
|
+
self.initialize_module(x=x, **self.kwargs)
|
|
177
|
+
self._adapt_input_dim(x)
|
|
178
|
+
return self._learn(
|
|
179
|
+
dict2tensor(x, features=self.observed_features, device=self.device)
|
|
180
|
+
)
|
|
179
181
|
|
|
180
182
|
def _learn(self, x: torch.Tensor) -> "Autoencoder":
|
|
181
183
|
self.module.train()
|
|
182
184
|
x_pred = self.module(x)
|
|
183
|
-
loss = self.
|
|
185
|
+
loss = self.loss_func(x_pred, x)
|
|
184
186
|
loss.backward()
|
|
185
187
|
self.optimizer.step()
|
|
186
188
|
self.optimizer.zero_grad()
|
|
@@ -205,14 +207,18 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
205
207
|
"""
|
|
206
208
|
|
|
207
209
|
if not self.module_initialized:
|
|
208
|
-
self.
|
|
209
|
-
self.initialize_module(**self.kwargs)
|
|
210
|
+
self._update_observed_features(x)
|
|
211
|
+
self.initialize_module(x=x, **self.kwargs)
|
|
210
212
|
|
|
211
|
-
|
|
213
|
+
self._adapt_input_dim(x)
|
|
214
|
+
|
|
215
|
+
x_t = dict2tensor(
|
|
216
|
+
x, features=self.observed_features, device=self.device
|
|
217
|
+
)
|
|
212
218
|
self.module.eval()
|
|
213
219
|
with torch.inference_mode():
|
|
214
220
|
x_pred = self.module(x_t)
|
|
215
|
-
loss = self.
|
|
221
|
+
loss = self.loss_func(x_pred, x_t).item()
|
|
216
222
|
return loss
|
|
217
223
|
|
|
218
224
|
def learn_many(self, X: pd.DataFrame) -> "Autoencoder":
|
|
@@ -231,10 +237,13 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
231
237
|
|
|
232
238
|
"""
|
|
233
239
|
if not self.module_initialized:
|
|
234
|
-
|
|
235
|
-
self.
|
|
236
|
-
|
|
237
|
-
|
|
240
|
+
|
|
241
|
+
self._update_observed_features(X)
|
|
242
|
+
self.initialize_module(x=X, **self.kwargs)
|
|
243
|
+
|
|
244
|
+
self._adapt_input_dim(X)
|
|
245
|
+
X_t = df2tensor(X, features=self.observed_features, device=self.device)
|
|
246
|
+
return self._learn(X_t)
|
|
238
247
|
|
|
239
248
|
def score_many(self, X: pd.DataFrame) -> np.ndarray:
|
|
240
249
|
"""
|
|
@@ -253,16 +262,35 @@ class Autoencoder(DeepEstimator, AnomalyDetector):
|
|
|
253
262
|
indicate more anomalous examples.
|
|
254
263
|
"""
|
|
255
264
|
if not self.module_initialized:
|
|
256
|
-
self.
|
|
257
|
-
self.initialize_module(**self.kwargs)
|
|
258
|
-
|
|
265
|
+
self._update_observed_features(X)
|
|
266
|
+
self.initialize_module(x=X, **self.kwargs)
|
|
267
|
+
|
|
268
|
+
self._adapt_input_dim(X)
|
|
269
|
+
X_t = df2tensor(X, features=self.observed_features, device=self.device)
|
|
259
270
|
|
|
260
271
|
self.module.eval()
|
|
261
272
|
with torch.inference_mode():
|
|
262
|
-
X_pred = self.module(
|
|
273
|
+
X_pred = self.module(X_t)
|
|
263
274
|
loss = torch.mean(
|
|
264
|
-
self.
|
|
265
|
-
dim=list(range(1,
|
|
275
|
+
self.loss_func(X_pred, X_t, reduction="none"),
|
|
276
|
+
dim=list(range(1, X_t.dim())),
|
|
266
277
|
)
|
|
267
278
|
score = loss.cpu().detach().numpy()
|
|
268
279
|
return score
|
|
280
|
+
|
|
281
|
+
def _adapt_input_dim(self, x: Dict | pd.DataFrame):
|
|
282
|
+
has_new_feature = self._update_observed_features(x)
|
|
283
|
+
|
|
284
|
+
if has_new_feature and self.is_feature_incremental:
|
|
285
|
+
expand_layer(
|
|
286
|
+
self.input_layer,
|
|
287
|
+
self.input_expansion_instructions,
|
|
288
|
+
len(self.observed_features),
|
|
289
|
+
output=False,
|
|
290
|
+
)
|
|
291
|
+
expand_layer(
|
|
292
|
+
self.output_layer,
|
|
293
|
+
self.output_expansion_instructions,
|
|
294
|
+
len(self.observed_features),
|
|
295
|
+
output=True,
|
|
296
|
+
)
|
|
@@ -8,6 +8,7 @@ from scipy.special import ndtr
|
|
|
8
8
|
|
|
9
9
|
from deep_river.anomaly import ae
|
|
10
10
|
from deep_river.utils import dict2tensor
|
|
11
|
+
from deep_river.utils.tensor_conversion import df2tensor
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
@@ -83,11 +84,11 @@ class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
|
83
84
|
|
|
84
85
|
>>> for x, y in dataset:
|
|
85
86
|
... score = model.score_one(x)
|
|
86
|
-
... model
|
|
87
|
-
... metric
|
|
87
|
+
... model.learn_one(x=x)
|
|
88
|
+
... metric.update(y, score)
|
|
88
89
|
...
|
|
89
90
|
>>> print(f"ROCAUC: {metric.get():.4f}")
|
|
90
|
-
ROCAUC: 0.
|
|
91
|
+
ROCAUC: 0.7572
|
|
91
92
|
"""
|
|
92
93
|
|
|
93
94
|
def __init__(
|
|
@@ -138,13 +139,15 @@ class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
|
138
139
|
The autoencoder itself.
|
|
139
140
|
"""
|
|
140
141
|
if not self.module_initialized:
|
|
141
|
-
self.
|
|
142
|
-
self.initialize_module(**self.kwargs)
|
|
143
|
-
|
|
142
|
+
self._update_observed_features(x)
|
|
143
|
+
self.initialize_module(x=x, **self.kwargs)
|
|
144
|
+
|
|
145
|
+
self._adapt_input_dim(x)
|
|
146
|
+
x_t = dict2tensor(x, self.observed_features, device=self.device)
|
|
144
147
|
|
|
145
148
|
self.module.train()
|
|
146
149
|
x_pred = self.module(x_t)
|
|
147
|
-
loss = self.
|
|
150
|
+
loss = self.loss_func(x_pred, x_t)
|
|
148
151
|
self._apply_loss(loss)
|
|
149
152
|
return self
|
|
150
153
|
|
|
@@ -173,15 +176,16 @@ class ProbabilityWeightedAutoencoder(ae.Autoencoder):
|
|
|
173
176
|
|
|
174
177
|
def learn_many(self, X: pd.DataFrame) -> "ProbabilityWeightedAutoencoder":
|
|
175
178
|
if not self.module_initialized:
|
|
176
|
-
self.
|
|
177
|
-
self.initialize_module(**self.kwargs)
|
|
178
|
-
|
|
179
|
+
self._update_observed_features(X)
|
|
180
|
+
self.initialize_module(x=X, **self.kwargs)
|
|
181
|
+
self._adapt_input_dim(X)
|
|
182
|
+
X_t = df2tensor(X, features=self.observed_features, device=self.device)
|
|
179
183
|
|
|
180
184
|
self.module.train()
|
|
181
185
|
x_pred = self.module(X)
|
|
182
186
|
loss = torch.mean(
|
|
183
|
-
self.
|
|
184
|
-
dim=list(range(1,
|
|
187
|
+
self.loss_func(x_pred, X_t, reduction="none"),
|
|
188
|
+
dim=list(range(1, X_t.dim())),
|
|
185
189
|
)
|
|
186
190
|
self._apply_loss(loss)
|
|
187
191
|
return self
|
|
@@ -26,20 +26,10 @@ class _TestLSTMAutoencoder(nn.Module):
|
|
|
26
26
|
num_layers=n_layers,
|
|
27
27
|
batch_first=batch_first,
|
|
28
28
|
)
|
|
29
|
-
self.decoder = nn.LSTM(
|
|
30
|
-
input_size=hidden_size,
|
|
31
|
-
hidden_size=n_features,
|
|
32
|
-
num_layers=n_layers,
|
|
33
|
-
batch_first=batch_first,
|
|
34
|
-
)
|
|
35
29
|
|
|
36
30
|
def forward(self, x):
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
x_flipped = torch.flip(x[1:], dims=[self.time_axis])
|
|
40
|
-
input = torch.cat((h, x_flipped), dim=self.time_axis)
|
|
41
|
-
x_hat, _ = self.decoder(input)
|
|
42
|
-
return torch.flip(x_hat, dims=[self.time_axis])
|
|
31
|
+
output, (h, c) = self.encoder(x)
|
|
32
|
+
return output
|
|
43
33
|
|
|
44
34
|
|
|
45
35
|
class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
@@ -146,7 +136,7 @@ class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
|
146
136
|
def _learn(self, x: torch.Tensor):
|
|
147
137
|
self.module.train()
|
|
148
138
|
x_pred = self.module(x)
|
|
149
|
-
loss = self.
|
|
139
|
+
loss = self.loss_func(x_pred, x)
|
|
150
140
|
|
|
151
141
|
self.optimizer.zero_grad()
|
|
152
142
|
loss.backward()
|
|
@@ -169,8 +159,8 @@ class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
|
169
159
|
The estimator itself.
|
|
170
160
|
"""
|
|
171
161
|
if not self.module_initialized:
|
|
172
|
-
self.
|
|
173
|
-
self.initialize_module(**self.kwargs)
|
|
162
|
+
self._update_observed_features(x)
|
|
163
|
+
self.initialize_module(x=x, **self.kwargs)
|
|
174
164
|
|
|
175
165
|
self._x_window.append(list(x.values()))
|
|
176
166
|
|
|
@@ -197,8 +187,8 @@ class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
|
197
187
|
The estimator itself.
|
|
198
188
|
"""
|
|
199
189
|
if not self.module_initialized:
|
|
200
|
-
self.
|
|
201
|
-
self.initialize_module(**self.kwargs)
|
|
190
|
+
self._update_observed_features(X)
|
|
191
|
+
self.initialize_module(x=X, **self.kwargs)
|
|
202
192
|
|
|
203
193
|
self._x_window.append(X.values.tolist())
|
|
204
194
|
if len(self._x_window) == self.window_size:
|
|
@@ -209,8 +199,10 @@ class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
|
209
199
|
def score_one(self, x: dict) -> float:
|
|
210
200
|
res = 0.0
|
|
211
201
|
if not self.module_initialized:
|
|
212
|
-
|
|
213
|
-
self.
|
|
202
|
+
|
|
203
|
+
self._update_observed_features(x)
|
|
204
|
+
|
|
205
|
+
self.initialize_module(x=x, **self.kwargs)
|
|
214
206
|
|
|
215
207
|
if len(self._x_window) == self.window_size:
|
|
216
208
|
x_win = self._x_window.copy()
|
|
@@ -219,7 +211,7 @@ class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
|
219
211
|
self.module.eval()
|
|
220
212
|
with torch.inference_mode():
|
|
221
213
|
x_pred = self.module(x_t)
|
|
222
|
-
loss = self.
|
|
214
|
+
loss = self.loss_func(x_pred, x_t)
|
|
223
215
|
res = loss.item()
|
|
224
216
|
|
|
225
217
|
if self.append_predict:
|
|
@@ -228,8 +220,9 @@ class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
|
228
220
|
|
|
229
221
|
def score_many(self, X: pd.DataFrame) -> List[Any]:
|
|
230
222
|
if not self.module_initialized:
|
|
231
|
-
|
|
232
|
-
self.
|
|
223
|
+
|
|
224
|
+
self._update_observed_features(X)
|
|
225
|
+
self.initialize_module(x=X, **self.kwargs)
|
|
233
226
|
|
|
234
227
|
x_win = self._x_window.copy()
|
|
235
228
|
x_win.append(X.values.tolist())
|
|
@@ -242,7 +235,7 @@ class RollingAutoencoder(RollingDeepEstimator, anomaly.base.AnomalyDetector):
|
|
|
242
235
|
with torch.inference_mode():
|
|
243
236
|
x_pred = self.module(X_t)
|
|
244
237
|
loss = torch.mean(
|
|
245
|
-
self.
|
|
238
|
+
self.loss_func(x_pred, x_pred, reduction="none"),
|
|
246
239
|
dim=list(range(1, x_pred.dim())),
|
|
247
240
|
)
|
|
248
241
|
losses = loss.detach().numpy()
|
|
@@ -4,7 +4,7 @@ import numpy as np
|
|
|
4
4
|
from river import base, utils
|
|
5
5
|
from river.anomaly import HalfSpaceTrees
|
|
6
6
|
from river.anomaly.base import AnomalyDetector
|
|
7
|
-
from river.stats import Mean, Min
|
|
7
|
+
from river.stats import Mean, Min, RollingMin
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class AnomalyScaler(base.Wrapper, AnomalyDetector):
|
|
@@ -259,12 +259,8 @@ class AnomalyMinMaxScaler(AnomalyScaler):
|
|
|
259
259
|
super().__init__(anomaly_detector)
|
|
260
260
|
self.rolling = rolling
|
|
261
261
|
self.window_size = window_size
|
|
262
|
-
self.min = (
|
|
263
|
-
|
|
264
|
-
)
|
|
265
|
-
self.max = (
|
|
266
|
-
utils.Rolling(Min(), self.window_size) if self.rolling else Min()
|
|
267
|
-
)
|
|
262
|
+
self.min = RollingMin(self.window_size) if self.rolling else Min()
|
|
263
|
+
self.max = RollingMin(self.window_size) if self.rolling else Min()
|
|
268
264
|
|
|
269
265
|
def score_one(self, *args):
|
|
270
266
|
"""
|