ae
¶
Classes:
Name | Description |
---|---|
Autoencoder |
Wrapper for PyTorch autoencoder models that uses the networks |
AutoencoderInitialized |
Represents an initialized autoencoder for anomaly detection and feature learning. |
Autoencoder
¶
Autoencoder(
module: Type[Module],
loss_fn: Union[str, Callable] = "mse",
optimizer_fn: Union[str, Callable] = "sgd",
lr: float = 0.001,
is_feature_incremental: bool = False,
device: str = "cpu",
seed: int = 42,
**kwargs
)
Bases: DeepEstimator
, AnomalyDetector
Wrapper for PyTorch autoencoder models that uses the networks reconstruction error for scoring the anomalousness of a given example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
Type[Module]
|
Torch Module that builds the autoencoder to be wrapped.
The Module should accept parameter |
required |
loss_fn
|
Union[str, Callable]
|
Loss function to be used for training the wrapped model. Can be a
loss function provided by |
'mse'
|
optimizer_fn
|
Union[str, Callable]
|
Optimizer to be used for training the wrapped model. Can be an
optimizer class provided by |
'sgd'
|
lr
|
float
|
Learning rate of the optimizer. |
0.001
|
device
|
str
|
Device to run the wrapped model on. Can be "cpu" or "cuda". |
'cpu'
|
seed
|
int
|
Random seed to be used for training the wrapped model. |
42
|
**kwargs
|
Parameters to be passed to the |
{}
|
Examples:
>>> from deep_river.anomaly import Autoencoder
>>> from river import metrics
>>> from river.datasets import CreditCard
>>> from torch import nn
>>> import math
>>> from river.compose import Pipeline
>>> from river.preprocessing import MinMaxScaler
>>> class MyAutoEncoder(torch.nn.Module):
... def __init__(self, n_features, latent_dim=3):
... super(MyAutoEncoder, self).__init__()
... self.linear1 = nn.Linear(n_features, latent_dim)
... self.nonlin = torch.nn.LeakyReLU()
... self.linear2 = nn.Linear(latent_dim, n_features)
... self.sigmoid = nn.Sigmoid()
...
... def forward(self, X, **kwargs):
... X = self.linear1(X)
... X = self.nonlin(X)
... X = self.linear2(X)
... return self.sigmoid(X)
>>> ae = Autoencoder(module=MyAutoEncoder, lr=0.005)
>>> scaler = MinMaxScaler()
>>> model = Pipeline(scaler, ae)
>>> for x, y in dataset:
... score = model.score_one(x)
... model.learn_one(x=x)
... metric.update(y, score)
...
>>> print(f"Rolling ROCAUC: {metric.get():.4f}")
Rolling ROCAUC: 0.8901
Methods:
Name | Description |
---|---|
clone |
Clones the estimator. |
draw |
Draws the wrapped model. |
initialize_module |
Parameters |
learn_many |
Performs one step of training with a batch of examples. |
learn_one |
Performs one step of training with a single example. |
score_many |
Returns an anomaly score for the provided batch of examples in |
score_one |
Returns an anomaly score for the provided example in the form of |
Source code in deep_river/anomaly/ae.py
clone
¶
Clones the estimator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_params
|
dict[Any, Any] | None
|
New parameters to be passed to the cloned estimator. |
None
|
include_attributes
|
If True, the attributes of the estimator will be copied to the cloned estimator. This is useful when the estimator is a transformer and the attributes are the learned parameters. |
False
|
Returns:
Type | Description |
---|---|
DeepEstimator
|
The cloned estimator. |
Source code in deep_river/base.py
draw
¶
Draws the wrapped model.
Source code in deep_river/base.py
initialize_module
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
The instance or class or callable to be initialized, e.g.
|
required | |
kwargs
|
dict
|
The keyword arguments to initialize the instance or class. Can be an empty dict. |
{}
|
Returns:
Type | Description |
---|---|
instance
|
The initialized component. |
Source code in deep_river/base.py
learn_many
¶
Performs one step of training with a batch of examples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Input batch of examples. |
required |
Source code in deep_river/anomaly/ae.py
learn_one
¶
Performs one step of training with a single example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict
|
Input example. |
required |
**kwargs
|
|
{}
|
Source code in deep_river/anomaly/ae.py
score_many
¶
Returns an anomaly score for the provided batch of examples in the form of the autoencoder's reconstruction error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Input batch of examples. |
required |
Returns:
Type | Description |
---|---|
float
|
Anomaly scores for the given batch of examples. Larger values indicate more anomalous examples. |
Source code in deep_river/anomaly/ae.py
score_one
¶
Returns an anomaly score for the provided example in the form of the autoencoder's reconstruction error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict
|
Input example. |
required |
Returns:
Type | Description |
---|---|
float
|
Anomaly score for the given example. Larger values indicate more anomalous examples. |
Source code in deep_river/anomaly/ae.py
AutoencoderInitialized
¶
AutoencoderInitialized(
module: Module,
loss_fn: Union[str, Callable] = "mse",
optimizer_fn: Union[str, Callable] = "sgd",
lr: float = 0.001,
is_feature_incremental: bool = False,
device: str = "cpu",
seed: int = 42,
**kwargs
)
Bases: DeepEstimatorInitialized
, AnomalyDetector
Represents an initialized autoencoder for anomaly detection and feature learning.
This class is built upon the DeepEstimatorInitialized and AnomalyDetector base classes. It provides methods for performing unsupervised learning through an autoencoder mechanism. The primary objective of the class is to train the autoencoder on input data and compute anomaly scores based on the reconstruction error. It supports learning on individual examples or entire batches of data.
Attributes:
Name | Type | Description |
---|---|---|
is_feature_incremental |
bool
|
Indicates whether the model is designed to increment features dynamically. |
module |
Module
|
The PyTorch model representing the autoencoder architecture. |
loss_fn |
Union[str, Callable]
|
Specifies the loss function to compute the reconstruction error. |
optimizer_fn |
Union[str, Callable]
|
Specifies the optimizer to be used for training the autoencoder. |
lr |
float
|
The learning rate for optimization. |
device |
str
|
The device on which the model is loaded and trained (e.g., "cpu", "cuda"). |
seed |
int
|
Random seed for ensuring reproducibility. |
Methods:
Name | Description |
---|---|
learn_many |
Performs one step of training with a batch of examples. |
learn_one |
Performs one step of training with a single example. |
score_many |
Returns an anomaly score for the provided batch of examples in |
score_one |
Returns an anomaly score for the provided example in the form of |
Source code in deep_river/anomaly/ae.py
learn_many
¶
Performs one step of training with a batch of examples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
Input batch of examples. |
required |
Source code in deep_river/anomaly/ae.py
learn_one
¶
Performs one step of training with a single example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict
|
Input example. |
required |
**kwargs
|
|
{}
|
Source code in deep_river/anomaly/ae.py
score_many
¶
Returns an anomaly score for the provided batch of examples in the form of the autoencoder's reconstruction error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Input batch of examples. |
required |
Returns:
Type | Description |
---|---|
float
|
Anomaly scores for the given batch of examples. Larger values indicate more anomalous examples. |
Source code in deep_river/anomaly/ae.py
score_one
¶
Returns an anomaly score for the provided example in the form of the autoencoder's reconstruction error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
dict
|
Input example. |
required |
Returns:
Type | Description |
---|---|
float
|
Anomaly score for the given example. Larger values indicate more anomalous examples. |