deepod.models.TranAD

class deepod.models.TranAD(seq_len=100, stride=1, lr=0.001, epochs=5, batch_size=128, epoch_steps=20, prt_steps=1, device='cuda', verbose=2, random_state=42)[source]

TranAD: Deep Transformer Networks for Anomaly Detection in Multivariate Time Series Data (VLDB’22)

Parameters:
  • seq_len (int) – The length of the input sequences for the model (default 100).

  • stride (int) – The stride size for the sliding window mechanism (default 1).

  • lr (float) – The learning rate for the optimizer (default 0.001).

  • epochs (int) – The number of epochs to train the model (default 5).

  • batch_size (int) – The size of the batches used during training (default 128).

  • epoch_steps (int) – The number of steps per epoch (default 20).

  • prt_steps (int) – The number of epochs after which to print progress (default 1).

  • device (str) – The device on which to train the model (‘cuda’ or ‘cpu’) (default ‘cuda’).

  • verbose (int) – The verbosity level of the training process (default 2).

  • random_state (int) – The seed used by the random number generator (default 42).

Methods

__init__([seq_len, stride, lr, epochs, ...])

Initializes the TranAD model with the specified parameters for training.

decision_function(X[, return_rep])

Computes anomaly scores for the given time series data.

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

Fits the TranAD model to the given multivariate time series data.

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

Fit detector.

inference(dataloader)

Conducts the inference phase, computing the anomaly scores for the provided data loader.

inference_forward(batch_x, net, criterion)

define forward step in inference

inference_prepare(X)

define test_loader

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(dataloader, epoch)

Conducts a single epoch of training over the provided data loader.

training_forward(batch_x, net, criterion)

define forward step in training

training_prepare(X, y)

define train_loader, net, and criterion

decision_function(X, return_rep=False)[source]

Computes anomaly scores for the given time series data.

Parameters:
  • X (numpy.ndarray) – The input time series data.

  • return_rep (bool, optional) – Flag to determine whether to return the latent representations. Defaults to False.

Returns:

Anomaly scores for each instance in the time series data.

Return type:

numpy.ndarray

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

Fits the TranAD model to the given multivariate time series data.

Parameters:
  • X (numpy.ndarray) – The input time series data.

  • y (numpy.ndarray, optional) – The true labels for the data. This argument is not used.

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

Conducts the inference phase, computing the anomaly scores for the provided data loader.

Parameters:

dataloader (DataLoader) – DataLoader containing the data for inference.

Returns:

An array of loss values and a list of predictions (currently unused).

Return type:

Tuple[numpy.ndarray, list]

inference_forward(batch_x, net, criterion)[source]

define forward step in inference

inference_prepare(X)[source]

define test_loader

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(dataloader, epoch)[source]

Conducts a single epoch of training over the provided data loader.

Parameters:
  • dataloader (DataLoader) – DataLoader containing the training batches.

  • epoch (int) – The current epoch number.

Returns:

The average loss over all batches in this epoch.

Return type:

float

training_forward(batch_x, net, criterion)[source]

define forward step in training

training_prepare(X, y)[source]

define train_loader, net, and criterion