Add code comments for documentation

This commit is contained in:
Yiyuan Yang 2023-11-21 19:46:51 +00:00 committed by GitHub
parent e50de0729c
commit 96c34e76a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 653 additions and 212 deletions

View File

@ -19,71 +19,84 @@ class DevNetTS(BaseDeepAD):
Deviation Networks for Weakly-supervised Anomaly Detection (KDD'19) Deviation Networks for Weakly-supervised Anomaly Detection (KDD'19)
:cite:`pang2019deep` :cite:`pang2019deep`
Parameters Deviation Networks (DevNet) designed for weakly-supervised anomaly detection.
---------- This implementation is based on the architecture presented in the KDD'19 paper:
epochs: int, optional (default=100) "Deviation Networks for Weakly-supervised Anomaly Detection" by Pang et al.
Number of training epochs
batch_size: int, optional (default=64) Args:
Number of samples in a mini-batch
hidden_dims (Union[list, str, int], optional):
lr: float, optional (default=1e-3) The dimensions for the hidden layers. Can be a list of integers, a string of comma-separated integers, or a single integer.
Learning rate
rep_dim: int, optional (default=128)
it is for consistency, unused in this model
hidden_dims: list, str or int, optional (default='100,50')
Number of neural units in hidden layers
- If list, each item is a layer - If list, each item is a layer
- If str, neural units of hidden layers are split by comma - If str, neural units of hidden layers are split by comma
- If int, number of neural units of single hidden layer - If int, number of neural units of single hidden layer
- Defaults to '100,50'.
act: str, optional (default='ReLU')
activation layer name act (str, optional):
choice = ['ReLU', 'LeakyReLU', 'Sigmoid', 'Tanh'] Activation function to use. Choices include 'ReLU', 'LeakyReLU', 'Sigmoid', 'Tanh'. Default is 'ReLU'.
bias: bool, optional (default=False) bias (bool, optional):
Additive bias in linear layer Whether to include a bias term in the linear layers. Default is False.
n_heads: int, optional(default=8): n_heads (int, optional):
number of head in multi-head attention Number of heads in multi-head attention. Only used when network is 'transformer'. Default is 8.
used when network='transformer', deprecated in other networks
pos_encoding (str, optional):
d_model: int, optional (default=64) The type of positional encoding to use. Only relevant when network is 'transformer'. Choices are 'fixed' or 'learnable'. Default is 'fixed'.
number of dimensions in Transformer
used when network='transformer', deprecated in other networks norm (str, optional):
Normalization method in the Transformer. Only relevant when network is 'transformer'. Choices are 'LayerNorm' or 'BatchNorm'. Default is 'LayerNorm'.
pos_encoding: str, optional (default='fixed')
manner of positional encoding, deprecated in other networks epochs (int, optional):
choice = ['fixed', 'learnable'] Number of training epochs. Default is 100.
norm: str, optional (default='BatchNorm') batch_size (int, optional):
manner of norm in Transformer, deprecated in other networks Batch size for training. Default is 64.
choice = ['LayerNorm', 'BatchNorm']
lr (float, optional):
margin: float, optional (default=5.) Learning rate for the optimizer. Default is 1e-3.
margin value used in the deviation loss function
network (str, optional):
l: int, optional (default=5000.) Type of network architecture to use. Default is 'Transformer'.
the size of samples of the Gaussian distribution used in the deviation loss function
seq_len (int, optional):
epoch_steps: int, optional (default=-1) Length of input sequences for models that require it. Default is 100.
Maximum steps in an epoch
- If -1, all the batches will be processed stride (int, optional):
Stride of the convolutional layers. Default is 1.
prt_steps: int, optional (default=10)
Number of epoch intervals per printing rep_dim (int, optional):
The representation dimension. Unused in this model but kept for consistency. Default is 128.
device: str, optional (default='cuda')
torch device, d_model (int, optional):
The number of expected features in the transformer model. Only used when network is 'transformer'. Default is 512.
verbose: int, optional (default=1)
Verbosity mode attn (str, optional):
Type of attention to use. Only used when network is 'transformer'. Default is 'self_attn'.
random_state int, optional (default=42)
the seed used by the random margin (float, optional):
Margin for the deviation loss function. Default is 5.
l (int, optional):
The size of the sample for the Gaussian distribution in the deviation loss function. Default is 5000.
epoch_steps (int, optional):
Maximum number of steps per epoch. If -1, all batches will be processed. Default is -1.
prt_steps (int, optional):
Number of epoch intervals for printing during training. Default is 10.
device (str, optional):
The device to use for training ('cuda' or 'cpu'). Default is 'cuda'.
verbose (int, optional):
Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch. Default is 2.
random_state (int, optional):
Seed for the random number generator for reproducibility. Default is 42.
""" """
def __init__(self, epochs=100, batch_size=64, lr=1e-3, def __init__(self, epochs=100, batch_size=64, lr=1e-3,
network='Transformer', seq_len=100, stride=1, network='Transformer', seq_len=100, stride=1,
rep_dim=128, hidden_dims='100,50', act='ReLU', bias=False, rep_dim=128, hidden_dims='100,50', act='ReLU', bias=False,
@ -91,6 +104,9 @@ class DevNetTS(BaseDeepAD):
margin=5., l=5000, margin=5., l=5000,
epoch_steps=-1, prt_steps=10, device='cuda', epoch_steps=-1, prt_steps=10, device='cuda',
verbose=2, random_state=42): verbose=2, random_state=42):
"""
Initialize the DevNetTS.
"""
super(DevNetTS, self).__init__( super(DevNetTS, self).__init__(
data_type='ts', model_name='DevNet', epochs=epochs, batch_size=batch_size, lr=lr, data_type='ts', model_name='DevNet', epochs=epochs, batch_size=batch_size, lr=lr,
network=network, seq_len=seq_len, stride=stride, network=network, seq_len=seq_len, stride=stride,
@ -115,6 +131,31 @@ class DevNetTS(BaseDeepAD):
return return
def training_prepare(self, X, y): def training_prepare(self, X, y):
"""
Prepares the data and model for training by creating a balanced data loader,
initializing the network, and setting up the loss criterion.
Args:
X (np.ndarray):
The input features for training.
y (np.ndarray):
The target labels for training, where 1 indicates an anomaly.
Returns:
train_loader (DataLoader):
A DataLoader with balanced mini-batches for training.
net (nn.Module):
The initialized neural network model.
criterion (Loss):
The loss function used during training.
"""
# loader: balanced loader, a mini-batch contains a half of normal data and a half of anomalies # loader: balanced loader, a mini-batch contains a half of normal data and a half of anomalies
n_anom = np.where(y == 1)[0].shape[0] n_anom = np.where(y == 1)[0].shape[0]
n_norm = self.n_samples - n_anom n_norm = self.n_samples - n_anom
@ -153,12 +194,47 @@ class DevNetTS(BaseDeepAD):
return train_loader, net, criterion return train_loader, net, criterion
def inference_prepare(self, X): def inference_prepare(self, X):
"""
Prepares the data for inference.
Args:
X (Tensor):
The input features for inference.
Returns:
test_loader (DataLoader):
A DataLoader for inference.
"""
test_loader = DataLoader(X, batch_size=self.batch_size, test_loader = DataLoader(X, batch_size=self.batch_size,
drop_last=False, shuffle=False) drop_last=False, shuffle=False)
self.criterion.reduction = 'none' self.criterion.reduction = 'none'
return test_loader return test_loader
def training_forward(self, batch_x, net, criterion): def training_forward(self, batch_x, net, criterion):
"""
Performs a forward pass during training.
Args:
batch_x (tuple):
A batch of input features and target labels.
net (nn.Module):
The neural network model.
criterion (Loss):
The loss function used during training.
Returns:
loss (Tensor):
The computed loss for the batch.
"""
batch_x, batch_y = batch_x batch_x, batch_y = batch_x
batch_x = batch_x.float().to(self.device) batch_x = batch_x.float().to(self.device)
batch_y = batch_y.to(self.device) batch_y = batch_y.to(self.device)
@ -167,6 +243,30 @@ class DevNetTS(BaseDeepAD):
return loss return loss
def inference_forward(self, batch_x, net, criterion): def inference_forward(self, batch_x, net, criterion):
"""
Performs a forward pass during inference.
Args:
batch_x (Tensor):
A batch of input features.
net (nn.Module):
The neural network model.
criterion (Loss):
The loss function used during training. Not used.
Returns:
batch_z (Tensor):
The batch of input features (unmodified).
s (Tensor):
The computed scores for the batch.
"""
batch_x = batch_x.float().to(self.device) batch_x = batch_x.float().to(self.device)
s = net(batch_x) s = net(batch_x)
s = s.view(-1) s = s.view(-1)

View File

@ -20,7 +20,63 @@ class DeepIsolationForestTS(BaseDeepAD):
""" """
Deep isolation forest for anomaly detection (TKDE'23) Deep isolation forest for anomaly detection (TKDE'23)
Implementation of a Deep Isolation Forest model for time-series anomaly detection, as described in TKDE'23.
This model combines deep learning methods for dimensionality reduction with the traditional Isolation Forest
algorithm to detect anomalies in time-series data.
Args:
epochs (int, optional):
Number of training epochs. Default is 100.
batch_size (int, optional):
Batch size for training. Default is 1000.
lr (float, optional):
Learning rate for the optimizer. Default is 1e-3.
seq_len (int, optional):
Length of the input sequences. Default is 100.
stride (int, optional):
Stride of the sliding window over the time series. Default is 1.
hidden_dims (str, optional):
String representation of the hidden layer dimensions, separated by commas.
bias (bool, optional):
If True, adds a bias term to the layers of the neural network. Default is False.
n_ensemble (int, optional):
Number of ensemble models to train.
n_estimators (int, optional):
Number of base estimators in the Isolation Forest. Default is 6.
max_samples (int, optional):
Maximum number of samples to draw to train each base estimator. Default is 256.
n_jobs (int, optional):
Number of jobs to run in parallel for Isolation Forest training. Default is 1.
epoch_steps (int, optional):
Number of steps per epoch. If -1, all batches will be processed.
prt_steps (int, optional):
Interval of epochs at which to print progress updates.
device (str, optional):
Device to use for training ('cuda' or 'cpu'). Default is 'cuda'.
verbose (int, optional):
Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch.
random_state (int, optional):
Seed for random number generation for reproducibility. Default is 42.
""" """
def __init__(self, def __init__(self,
epochs=100, batch_size=1000, lr=1e-3, epochs=100, batch_size=1000, lr=1e-3,
seq_len=100, stride=1, seq_len=100, stride=1,
@ -29,6 +85,10 @@ class DeepIsolationForestTS(BaseDeepAD):
max_samples=256, n_jobs=1, max_samples=256, n_jobs=1,
epoch_steps=-1, prt_steps=10, device='cuda', epoch_steps=-1, prt_steps=10, device='cuda',
verbose=2, random_state=42): verbose=2, random_state=42):
"""
Initializes the Deep Isolation Forest Time-Series model with the specified hyperparameters.
"""
super(DeepIsolationForestTS, self).__init__( super(DeepIsolationForestTS, self).__init__(
model_name='DIF', data_type='ts', network='DilatedConv', model_name='DIF', data_type='ts', network='DilatedConv',
epochs=epochs, batch_size=batch_size, lr=lr, epochs=epochs, batch_size=batch_size, lr=lr,
@ -54,23 +114,23 @@ class DeepIsolationForestTS(BaseDeepAD):
def fit(self, X, y=None): def fit(self, X, y=None):
""" """
Fit detector. y is ignored in unsupervised methods. Fits the Deep Isolation Forest model on the provided time-series data.
Parameters Args:
----------
X : numpy array of shape (n_samples, n_features) X (np.ndarray):
The input samples. The input samples of shape (n_samples, n_features).
y (np.ndarray, optional):
Target values of shape (n_samples, ) (ignored in unsupervised training).
y : numpy array of shape (n_samples, ) Returns:
Not used in unsupervised methods, present for API consistency by convention.
used in (semi-/weakly-) supervised methods self:
The fitted estimator.
Returns
-------
self : object
Fitted estimator.
""" """
X_seqs = get_sub_seqs(X, seq_len=self.seq_len, stride=self.stride) X_seqs = get_sub_seqs(X, seq_len=self.seq_len, stride=self.stride)
y_seqs = get_sub_seqs(y, seq_len=self.seq_len, stride=self.stride) if y is not None else None y_seqs = get_sub_seqs(y, seq_len=self.seq_len, stride=self.stride) if y is not None else None
self.train_data = X_seqs self.train_data = X_seqs
@ -119,22 +179,23 @@ class DeepIsolationForestTS(BaseDeepAD):
return self return self
def decision_function(self, X): def decision_function(self, X):
"""Predict raw anomaly scores of X using the fitted detector. """
Predict raw anomaly scores of X using the fitted detector.
The anomaly score of an input sample is computed based on the fitted The anomaly score of an input sample is computed based on the fitted
detector. For consistency, outliers are assigned with detector. For consistency, outliers are assigned with
higher anomaly scores. higher anomaly scores.
Parameters Args:
----------
X : numpy array of shape (n_samples, n_features) X (np.ndarray):
The input samples. Sparse matrices are accepted only The input samples of shape (n_samples, n_features). Sparse matrices are accepted only if they are supported by the base estimator.
if they are supported by the base estimator.
Returns:
anomaly_scores (np.ndarray):
The anomaly score of the input samples with the shape of (n_samples,).
Returns
-------
anomaly_scores : numpy array of shape (n_samples,)
The anomaly score of the input samples.
""" """
if self.verbose >= 1: if self.verbose >= 1:
@ -164,6 +225,30 @@ class DeepIsolationForestTS(BaseDeepAD):
@staticmethod @staticmethod
def _deep_transfer(X, net, batch_size, device): def _deep_transfer(X, net, batch_size, device):
"""
Transfers the input data through the network to obtain reduced representations.
Args:
X (np.ndarray):
The input samples to be reduced.
net (nn.Module):
The neural network model for dimensionality reduction.
batch_size (int):
Batch size for processing.
device (str):
The device on which to perform computations.
Returns:
x_reduced (np.ndarray):
The reduced representation of the input samples.
"""
x_reduced = [] x_reduced = []
loader = DataLoader(dataset=X, batch_size=batch_size, drop_last=False, pin_memory=True, shuffle=False) loader = DataLoader(dataset=X, batch_size=batch_size, drop_last=False, pin_memory=True, shuffle=False)
for batch_x in loader: for batch_x in loader:

View File

@ -18,71 +18,71 @@ from collections import Counter
class DeepSADTS(BaseDeepAD): class DeepSADTS(BaseDeepAD):
""" Deep Semi-supervised Anomaly Detection (ICLR'20) """ Deep Semi-supervised Anomaly Detection (ICLR'20)
:cite:`ruff2020dsad` :cite:`ruff2020dsad`
This model extends the semi-supervised anomaly detection framework to time-series datasets, aiming
to detect anomalies by learning a representation of the data in a lower-dimensional hypersphere.
Parameters Args:
----------
data_type: str, optional (default='tabular')
Data type
epochs: int, optional (default=100) data_type (str, optional):
Number of training epochs The type of data, here it's defaulted to 'ts' (time-series).
batch_size: int, optional (default=64) epochs (int, optional):
Number of samples in a mini-batch The number of epochs for training, default is 100.
lr: float, optional (default=1e-3) batch_size (int, optional):
Learning rate The size of the mini-batch for training, default is 64.
network: str, optional (default='MLP') lr (float, optional):
network structure for different data structures The learning rate for the optimizer, default is 1e-3.
rep_dim: int, optional (default=128) network (str, optional):
Dimensionality of the representation space The type of network architecture to use, default is 'TCN'.
hidden_dims: list, str or int, optional (default='100,50') rep_dim (int, optional):
Number of neural units in hidden layers The size of the representation dimension, default is 128.
- If list, each item is a layer
- If str, neural units of hidden layers are split by comma hidden_dims (Union[list, str, int], optional):
- If int, number of neural units of single hidden layer The dimensions for hidden layers. It can be a list, a comma-separated string, or a single integer. Default is '100,50'.
- If list, each item is a layer
act: str, optional (default='ReLU') - If str, neural units of hidden layers are split by comma
activation layer name - If int, number of neural units of single hidden layer
choice = ['ReLU', 'LeakyReLU', 'Sigmoid', 'Tanh']
act (str, optional):
bias: bool, optional (default=False) The activation function to use. Possible values are 'ReLU', 'LeakyReLU', 'Sigmoid', 'Tanh', default is 'ReLU'.
Additive bias in linear layer
bias (bool, optional):
n_heads: int, optional(default=8): Whether to include a bias term in the layers, default is False.
number of head in multi-head attention
used when network='transformer', deprecated in other networks n_heads (int, optional):
The number of heads in a multi-head attention mechanism, default is 8.
d_model: int, optional (default=64)
number of dimensions in Transformer d_model (int, optional):
used when network='transformer', deprecated in other networks The number of dimensions in the transformer model, default is 512.
pos_encoding: str, optional (default='fixed') attn (str, optional):
manner of positional encoding, deprecated in other networks The type of attention mechanism used, default is 'self_attn'.
choice = ['fixed', 'learnable']
pos_encoding (str, optional):
norm: str, optional (default='BatchNorm') The type of positional encoding used in the transformer model, default is 'fixed'.
manner of norm in Transformer, deprecated in other networks
choice = ['LayerNorm', 'BatchNorm'] norm (str, optional):
The type of normalization used in the transformer model, default is 'LayerNorm'.
epoch_steps: int, optional (default=-1)
Maximum steps in an epoch epoch_steps (int, optional):
- If -1, all the batches will be processed The maximum number of steps per epoch, default is -1, indicating that all batches will be processed.
prt_steps: int, optional (default=10) prt_steps (int, optional):
Number of epoch intervals per printing The number of epoch intervals for printing progress, default is 10.
device: str, optional (default='cuda') device (str, optional):
torch device, The device to use for training and inference, default is 'cuda'.
verbose: int, optional (default=1) verbose (int, optional):
Verbosity mode The verbosity mode, default is 2.
random_state int, optional (default=42) random_state (int, optional):
the seed used by the random The seed for the random number generator, default is 42.
""" """
@ -92,6 +92,10 @@ class DeepSADTS(BaseDeepAD):
n_heads=8, d_model=512, attn='self_attn', pos_encoding='fixed', norm='LayerNorm', n_heads=8, d_model=512, attn='self_attn', pos_encoding='fixed', norm='LayerNorm',
epoch_steps=-1, prt_steps=10, device='cuda', epoch_steps=-1, prt_steps=10, device='cuda',
verbose=2, random_state=42): verbose=2, random_state=42):
"""
Initializes the DeepSADTS model with the provided parameters.
"""
super(DeepSADTS, self).__init__( super(DeepSADTS, self).__init__(
data_type='ts', model_name='DeepSAD', epochs=epochs, batch_size=batch_size, lr=lr, data_type='ts', model_name='DeepSAD', epochs=epochs, batch_size=batch_size, lr=lr,
network=network, seq_len=seq_len, stride=stride, network=network, seq_len=seq_len, stride=stride,
@ -116,6 +120,30 @@ class DeepSADTS(BaseDeepAD):
return return
def training_prepare(self, X, y): def training_prepare(self, X, y):
"""
Prepares the model for training by setting up data loaders, initializing the network, and defining the loss criterion.
Args:
X (np.ndarray):
The input feature matrix for training.
y (np.ndarray):
The target labels where 1 indicates known anomalies.
Returns:
train_loader (DataLoader):
The data loader for training.
net (nn.Module):
The neural network for feature extraction.
criterion (Loss):
The loss function used for training.
"""
# By following the original paper, # By following the original paper,
# use -1 to denote known anomalies, and 1 to denote known inliers # use -1 to denote known anomalies, and 1 to denote known inliers
known_anom_id = np.where(y == 1) known_anom_id = np.where(y == 1)
@ -166,12 +194,48 @@ class DeepSADTS(BaseDeepAD):
return train_loader, net, criterion return train_loader, net, criterion
def inference_prepare(self, X): def inference_prepare(self, X):
"""
Prepares the model for inference by setting up data loaders.
Args:
X (np.ndarray):
The input feature matrix for inference.
Returns:
test_loader (DataLoader):
The data loader for inference.
"""
test_loader = DataLoader(X, batch_size=self.batch_size, test_loader = DataLoader(X, batch_size=self.batch_size,
drop_last=False, shuffle=False) drop_last=False, shuffle=False)
self.criterion.reduction = 'none' self.criterion.reduction = 'none'
return test_loader return test_loader
def training_forward(self, batch_x, net, criterion): def training_forward(self, batch_x, net, criterion):
"""
Performs a forward training pass.
Args:
batch_x (tuple):
A batch of input data and labels.
net (nn.Module):
The neural network model.
criterion (Loss):
The loss function.
Returns:
loss (torch.Tensor):
The computed loss for the batch.
"""
batch_x, batch_y = batch_x batch_x, batch_y = batch_x
# from collections import Counter # from collections import Counter
@ -186,13 +250,57 @@ class DeepSADTS(BaseDeepAD):
return loss return loss
def inference_forward(self, batch_x, net, criterion): def inference_forward(self, batch_x, net, criterion):
"""
Performs a forward inference pass.
Args:
batch_x (torch.Tensor):
A batch of input data.
net (nn.Module):
The neural network model.
criterion (Loss):
The loss function used to calculate the anomaly score.
Returns:
batch_z (torch.Tensor):
The encoded batch of data in the feature space.
s (torch.Tensor):
The anomaly scores for the batch.
"""
batch_x = batch_x.float().to(self.device) batch_x = batch_x.float().to(self.device)
batch_z = net(batch_x) batch_z = net(batch_x)
s = criterion(batch_z) s = criterion(batch_z)
return batch_z, s return batch_z, s
def _set_c(self, net, dataloader, eps=0.1): def _set_c(self, net, dataloader, eps=0.1):
"""Initializing the center for the hypersphere""" """
Initializes the center 'c' for the hypersphere.
Args:
net (nn.Module):
The neural network model.
dataloader (DataLoader):
The data loader to compute the center from.
eps (float):
A small value to ensure 'c' is away from zero, default is 0.1.
Returns:
c (torch.Tensor):
The initialized center of the hypersphere.
"""
net.eval() net.eval()
z_ = [] z_ = []
with torch.no_grad(): with torch.no_grad():

View File

@ -14,68 +14,86 @@ class DeepSVDDTS(BaseDeepAD):
""" """
Deep One-class Classification for Anomaly Detection (ICML'18) Deep One-class Classification for Anomaly Detection (ICML'18)
:cite:`ruff2018deepsvdd` :cite:`ruff2018deepsvdd`
Parameters Args:
----------
epochs: int, optional (default=100)
Number of training epochs
batch_size: int, optional (default=64)
Number of samples in a mini-batch
lr: float, optional (default=1e-3)
Learning rate
network: str, optional (default='MLP')
network structure for different data structures
seq_len: int, optional (default=100)
Size of window used to create subsequences from the data
deprecated when handling tabular data (network=='MLP')
stride: int, optional (default=1)
number of time points the window will move between two subsequences
deprecated when handling tabular data (network=='MLP')
rep_dim: int, optional (default=128)
Dimensionality of the representation space
hidden_dims: list, str or int, optional (default='100,50')
Number of neural units in hidden layers
- If list, each item is a layer
- If str, neural units of hidden layers are split by comma
- If int, number of neural units of single hidden layer
act: str, optional (default='ReLU')
activation layer name
choice = ['ReLU', 'LeakyReLU', 'Sigmoid', 'Tanh']
bias: bool, optional (default=False)
Additive bias in linear layer
epoch_steps: int, optional (default=-1)
Maximum steps in an epoch
- If -1, all the batches will be processed
prt_steps: int, optional (default=10)
Number of epoch intervals per printing
device: str, optional (default='cuda')
torch device,
verbose: int, optional (default=1)
Verbosity mode
random_state int, optional (default=42)
the seed used by the random
epochs (int, optional):
Number of training epochs. Default is 100.
batch_size (int, optional):
Number of samples in a mini-batch. Default is 64.
lr (float, optional):
Learning rate. Default is 1e-5.
network (str, optional):
Network structure for different data structures. Default is 'Transformer'.
seq_len (int, optional):
Size of window used to create subsequences from the data. Default is 30.
stride (int, optional):
Number of time points the window moves between subsequences. Default is 10.
rep_dim (int, optional):
Dimensionality of the representation space. Default is 64.
hidden_dims (Union[list, str, int], optional):
Dimensions for hidden layers. Default is '512'.
- If list, each item is a layer
- If str, neural units of hidden layers are split by comma
- If int, number of neural units of single hidden layer
act (str, optional):
Activation layer name. Choices are ['ReLU', 'LeakyReLU', 'Sigmoid', 'Tanh']. Default is 'GELU'.
bias (bool, optional):
Whether to add a bias term in linear layers. Default is False.
n_heads (int, optional):
Number of heads in multi-head attention. Default is 8.
d_model (int, optional):
Number of dimensions in Transformer model. Default is 512.
attn (str, optional):
Type of attention mechanism. Default is 'self_attn'.
pos_encoding (str, optional):
Manner of positional encoding. Default is 'fixed'.
norm (str, optional):
Manner of normalization in Transformer. Default is 'LayerNorm'.
epoch_steps (int, optional):
Maximum steps in an epoch. Default is -1.
prt_steps (int, optional):
Number of epoch intervals per printing. Default is 10.
device (str, optional):
Torch device. Default is 'cuda'.
verbose (int, optional):
Verbosity mode. Default is 2.
random_state (int, optional):
Seed used by the random number generator. Default is 42.
""" """
def __init__(self, epochs=100, batch_size=64, lr=1e-5, def __init__(self, epochs=100, batch_size=64, lr=1e-5,
network='Transformer', seq_len=30, stride=10, network='Transformer', seq_len=30, stride=10,
rep_dim=64, hidden_dims='512', act='GELU', bias=False, rep_dim=64, hidden_dims='512', act='GELU', bias=False,
n_heads=8, d_model=512, attn='self_attn', pos_encoding='fixed', norm='LayerNorm', n_heads=8, d_model=512, attn='self_attn', pos_encoding='fixed', norm='LayerNorm',
epoch_steps=-1, prt_steps=10, device='cuda', epoch_steps=-1, prt_steps=10, device='cuda',
verbose=2, random_state=42): verbose=2, random_state=42):
"""
Initializes the DeepSVDDTS model with the specified parameters.
"""
super(DeepSVDDTS, self).__init__( super(DeepSVDDTS, self).__init__(
model_name='DeepSVDD', data_type='ts', epochs=epochs, batch_size=batch_size, lr=lr, model_name='DeepSVDD', data_type='ts', epochs=epochs, batch_size=batch_size, lr=lr,
network=network, seq_len=seq_len, stride=stride, network=network, seq_len=seq_len, stride=stride,
@ -99,6 +117,30 @@ class DeepSVDDTS(BaseDeepAD):
return return
def training_prepare(self, X, y): def training_prepare(self, X, y):
"""
Prepares the training process by setting up data loaders and initializing the network and loss criterion.
Args:
X (torch.Tensor):
Input tensor of the features.
y (torch.Tensor):
Input tensor of the labels.
Returns:
train_loader (DataLoader):
DataLoader for the training data.
net (nn.Module):
Initialized neural network model.
criterion (DSVDDLoss):
Loss function for DeepSVDD.
"""
train_loader = DataLoader(X, batch_size=self.batch_size, shuffle=True) train_loader = DataLoader(X, batch_size=self.batch_size, shuffle=True)
network_params = { network_params = {
@ -131,25 +173,104 @@ class DeepSVDDTS(BaseDeepAD):
return train_loader, net, criterion return train_loader, net, criterion
def inference_prepare(self, X): def inference_prepare(self, X):
"""
Prepares the model for inference by setting up data loaders.
Args:
X (torch.Tensor):
Input tensor of the features for inference.
Returns:
test_loader (DataLoader):
DataLoader for inference.
"""
test_loader = DataLoader(X, batch_size=self.batch_size, test_loader = DataLoader(X, batch_size=self.batch_size,
drop_last=False, shuffle=False) drop_last=False, shuffle=False)
self.criterion.reduction = 'none' self.criterion.reduction = 'none'
return test_loader return test_loader
def training_forward(self, batch_x, net, criterion): def training_forward(self, batch_x, net, criterion):
"""
Performs a forward pass during training.
Args:
batch_x (torch.Tensor):
Batch of input data.
net (nn.Module):
The neural network model.
criterion (DSVDDLoss):
Loss function for DeepSVDD.
Returns:
loss (torch.Tensor):
Computed loss for the batch.
"""
batch_x = batch_x.float().to(self.device) batch_x = batch_x.float().to(self.device)
z = net(batch_x) z = net(batch_x)
loss = criterion(z) loss = criterion(z)
return loss return loss
def inference_forward(self, batch_x, net, criterion): def inference_forward(self, batch_x, net, criterion):
"""
Performs a forward pass during inference.
Args:
batch_x (torch.Tensor):
Batch of input data.
net (nn.Module):
The neural network model.
criterion (DSVDDLoss):
Loss function for DeepSVDD to calculate anomaly score.
Returns:
batch_z (torch.Tensor):
The encoded batch of data in the feature space.
s (torch.Tensor):
The anomaly scores for the batch.
"""
batch_x = batch_x.float().to(self.device) batch_x = batch_x.float().to(self.device)
batch_z = net(batch_x) batch_z = net(batch_x)
s = criterion(batch_z) s = criterion(batch_z)
return batch_z, s return batch_z, s
def _set_c(self, net, dataloader, eps=0.1): def _set_c(self, net, dataloader, eps=0.1):
"""Initializing the center for the hypersphere""" """
Initializes the center 'c' for the hypersphere in the representation space.
Args:
net (nn.Module):
The neural network model.
dataloader (DataLoader):
DataLoader for the data to compute the center from.
eps (float, optional):
Small value to ensure 'c' is away from zero. Default is 0.1.
Returns:
c (torch.Tensor):
The initialized center of the hypersphere.
"""
net.eval() net.eval()
z_ = [] z_ = []
with torch.no_grad(): with torch.no_grad():
@ -167,25 +288,52 @@ class DeepSVDDTS(BaseDeepAD):
class DSVDDLoss(torch.nn.Module): class DSVDDLoss(torch.nn.Module):
""" """
Parameters Custom loss function for Deep Support Vector Data Description (Deep SVDD).
----------
c: torch.Tensor This loss function computes the distance between each data point in the representation
Center of the pre-defined hyper-sphere in the representation space space and the center of the hypersphere and aims to minimize this distance for normal data points.
reduction: str, optional (default='mean') Args:
choice = [``'none'`` | ``'mean'`` | ``'sum'``]
- If ``'none'``: no reduction will be applied; c (torch.Tensor):
- If ``'mean'``: the sum of the output will be divided by the number of The center of the hypersphere in the representation space.
elements in the output;
- If ``'sum'``: the output will be summed reduction (str, optional):
Specifies the reduction to apply to the output. Choices are 'none', 'mean', 'sum'. Default is 'mean'.
- If ``'none'``: no reduction will be applied;
- If ``'mean'``: the sum of the output will be divided by the number of elements in the output;
- If ``'sum'``: the output will be summed
""" """
def __init__(self, c, reduction='mean'): def __init__(self, c, reduction='mean'):
"""
Initializes the DSVDDLoss with the hypersphere center and reduction method.
"""
super(DSVDDLoss, self).__init__() super(DSVDDLoss, self).__init__()
self.c = c self.c = c
self.reduction = reduction self.reduction = reduction
def forward(self, rep, reduction=None): def forward(self, rep, reduction=None):
"""
Calculates the Deep SVDD loss for a batch of representations.
Args:
rep (torch.Tensor):
The representation of the batch of data.
reduction (str, optional):
The reduction method to apply. If None, will use the specified 'reduction' attribute. Default is None.
Returns:
loss (torch.Tensor):
The calculated loss based on the representations and the center 'c'.
"""
loss = torch.sum((rep - self.c) ** 2, dim=1) loss = torch.sum((rep - self.c) ** 2, dim=1)
if reduction is None: if reduction is None: