API CheatSheet

The following APIs are applicable for all detector models for easy use.

Key Attributes of a fitted model:

See base class definition below:

deepod.core.base_model module

Base class for deep Anomaly detection models some functions are adapted from the pyod library @Author: Hongzuo Xu <hongzuoxu@126.com, xuhongzuo13@nudt.edu.cn>

class deepod.core.base_model.BaseDeepAD(model_name, data_type='tabular', network='MLP', epochs=100, batch_size=64, lr=0.001, n_ensemble=1, seq_len=100, stride=1, epoch_steps=-1, prt_steps=10, device='cuda', contamination=0.1, verbose=1, random_state=42)[source]

Bases: object

Abstract class for deep outlier detection models

Parameters:
  • data_type (str, optional (default='tabular')) – Data type, choice = [‘tabular’, ‘ts’]

  • network (str, optional (default='MLP')) – network structure for different data structures

  • 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

  • n_ensemble (int or str, optional (default=1)) – Number of ensemble size

  • 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’)

  • 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,

  • contamination (float in (0., 0.5), optional (default=0.1)) – The amount of contamination of the data set, i.e. the proportion of outliers in the data set. Used when fitting to define the threshold on the decision function.

  • verbose (int, optional (default=1)) – Verbosity mode

  • random_state (int, optional (default=42)) – the seed used by the random

decision_scores_

The outlier scores of the training data. The higher, the more abnormal. Outliers tend to have higher scores. This value is available once the detector is fitted.

Type:

numpy array of shape (n_samples,)

threshold_

The threshold is based on contamination. It is the n_samples * contamination most abnormal samples in decision_scores_. The threshold is calculated for generating binary outlier labels.

Type:

float

labels_

The binary labels of the training data. 0 stands for inliers and 1 for outliers/anomalies. It is generated by applying threshold_ on decision_scores_.

Type:

int, either 0 or 1

decision_function(X, return_rep=False)[source]

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)[source]

for any updating operation after decision function

epoch_update()[source]

for any updating operation after each training epoch

fit(X, y=None)[source]

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)[source]

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

abstract inference_forward(batch_x, net, criterion)[source]

define forward step in inference

abstract inference_prepare(X)[source]

define test_loader

classmethod load_model(path)[source]
load_ray_checkpoint(best_config, best_checkpoint)[source]
predict(X, return_confidence=False)[source]

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.

save_model(path)[source]
static set_seed(seed)[source]
set_tuned_net(config)[source]
static set_tuned_params()[source]
abstract training_forward(batch_x, net, criterion)[source]

define forward step in training

abstract training_prepare(X, y)[source]

define train_loader, net, and criterion