deepod.models.DeepSVDDTS

class deepod.models.DeepSVDDTS(epochs=100, batch_size=64, lr=1e-05, network='Transformer', seq_len=30, stride=10, rep_dim=64, hidden_dims='512', act='GELU', bias=False, n_heads=8, d_model=512, attn='self_attn', pos_encoding='fixed', norm='LayerNorm', epoch_steps=-1, prt_steps=10, device='cuda', verbose=2, random_state=42)[source]
Deep One-class Classification for Anomaly Detection (ICML’18)

[BRVG+18]

Parameters:
  • 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.

Methods

__init__([epochs, batch_size, lr, network, ...])

Initializes the DeepSVDDTS model with the specified parameters.

decision_function(X[, return_rep])

Predict raw anomaly scores of X using the fitted detector.

decision_function_update(z, scores)

for any updating operation after decision function

epoch_update()

for any updating operation after each training epoch

fit(X[, y])

Fit detector.

fit_auto_hyper(X[, y, X_test, y_test, ...])

Fit detector.

inference_forward(batch_x, net, criterion)

Performs a forward pass during inference.

inference_prepare(X)

Prepares the model for inference by setting up data loaders.

load_model(path)

load_ray_checkpoint(best_config, best_checkpoint)

predict(X[, return_confidence])

Predict if a particular sample is an outlier or not.

save_model(path)

set_seed(seed)

set_tuned_net(config)

set_tuned_params()

training_forward(batch_x, net, criterion)

Performs a forward pass during training.

training_prepare(X, y)

Prepares the training process by setting up data loaders and initializing the network and loss criterion.

decision_function(X, return_rep=False)

Predict raw anomaly scores of X using the fitted detector.

The anomaly score of an input sample is computed based on the fitted detector. For consistency, outliers are assigned with higher anomaly scores.

Parameters:
  • X (numpy array of shape (n_samples, n_features)) – The input samples. Sparse matrices are accepted only if they are supported by the base estimator.

  • return_rep (boolean, optional, default=False) – whether return representations

Returns:

anomaly_scores – The anomaly score of the input samples.

Return type:

numpy array of shape (n_samples,)

decision_function_update(z, scores)

for any updating operation after decision function

epoch_update()

for any updating operation after each training epoch

fit(X, y=None)

Fit detector. y is ignored in unsupervised methods.

Parameters:
  • X (numpy array of shape (n_samples, n_features)) – The input samples.

  • y (numpy array of shape (n_samples, )) – Not used in unsupervised methods, present for API consistency by convention. used in (semi-/weakly-) supervised methods

Returns:

self – Fitted estimator.

Return type:

object

fit_auto_hyper(X, y=None, X_test=None, y_test=None, n_ray_samples=5, time_budget_s=None)

Fit detector. y is ignored in unsupervised methods.

Parameters:
  • X (numpy array of shape (n_samples, n_features)) – The input samples.

  • y (numpy array of shape (n_samples, )) – Not used in unsupervised methods, present for API consistency by convention. used in (semi-/weakly-) supervised methods

  • X_test (numpy array of shape (n_samples, n_features), default=None) – The input testing samples for hyper-parameter tuning.

  • y_test (numpy array of shape (n_samples, ), default=None) – Label of input testing samples for hyper-parameter tuning.

  • n_ray_samples (int, default=5) – Number of times to sample from the hyperparameter space

  • time_budget_s (int, default=None) – Global time budget in seconds after which all trials of Ray are stopped.

Returns:

config – tuned hyper-parameter

Return type:

dict

inference_forward(batch_x, net, criterion)[source]

Performs a forward pass during inference.

Parameters:
  • 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:

The encoded batch of data in the feature space.

s (torch.Tensor):

The anomaly scores for the batch.

Return type:

batch_z (torch.Tensor)

inference_prepare(X)[source]

Prepares the model for inference by setting up data loaders.

Parameters:

X (torch.Tensor) – Input tensor of the features for inference.

Returns:

DataLoader for inference.

Return type:

test_loader (DataLoader)

predict(X, return_confidence=False)

Predict if a particular sample is an outlier or not.

Parameters:
  • X (numpy array of shape (n_samples, n_features)) – The input samples.

  • return_confidence (boolean, optional(default=False)) – If True, also return the confidence of prediction.

Returns:

  • outlier_labels (numpy array of shape (n_samples,)) – For each observation, tells whether it should be considered as an outlier according to the fitted model. 0 stands for inliers and 1 for outliers.

  • confidence (numpy array of shape (n_samples,).) – Only if return_confidence is set to True.

training_forward(batch_x, net, criterion)[source]

Performs a forward pass during training.

Parameters:
  • batch_x (torch.Tensor) – Batch of input data.

  • net (nn.Module) – The neural network model.

  • criterion (DSVDDLoss) – Loss function for DeepSVDD.

Returns:

Computed loss for the batch.

Return type:

loss (torch.Tensor)

training_prepare(X, y)[source]

Prepares the training process by setting up data loaders and initializing the network and loss criterion.

Parameters:
  • X (torch.Tensor) – Input tensor of the features.

  • y (torch.Tensor) – Input tensor of the labels.

Returns:

DataLoader for the training data.

net (nn.Module):

Initialized neural network model.

criterion (DSVDDLoss):

Loss function for DeepSVDD.

Return type:

train_loader (DataLoader)