deepod.models.TcnED

class deepod.models.TcnED(seq_len=100, stride=1, epochs=10, batch_size=32, lr=0.0001, rep_dim=32, hidden_dims=32, kernel_size=3, act='ReLU', bias=True, dropout=0.2, epoch_steps=-1, prt_steps=1, device='cuda', verbose=2, random_state=42)[source]

An Evaluation of Anomaly Detection and Diagnosis in Multivariate Time Series (TNNLS’21)

Temporal Convolutional Network for Anomaly Detection in Multivariate Time Series.

Parameters:
  • seq_len (int) – The length of the input sequences for the network. Default is 100.

  • stride (int) – The stride of the convolutional operation. Default is 1.

  • epochs (int) – The number of training epochs. Default is 10.

  • batch_size (int) – The batch size used in training. Default is 32.

  • lr (float) – The learning rate for the optimizer. Default is 1e-4.

  • rep_dim (int) – The dimensionality of the latent representation (embedding) layer. Default is 32.

  • hidden_dims (int) – The number of hidden units in each layer. Default is 32.

  • kernel_size (int) – The size of the kernel in the convolutional layers. Default is 3.

  • act (str) – The activation function used in the network. Default is ‘ReLU’.

  • bias (bool) – Whether to use bias in the convolutional layers. Default is True.

  • dropout (float) – The dropout rate used in the network. Default is 0.2.

  • epoch_steps (int) – The number of steps per epoch. Default is -1, indicating use of the full dataset.

  • prt_steps (int) – The interval of epochs at which to print training progress. Default is 1.

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

  • verbose (int) – The verbosity level. Default is 2.

  • random_state (int) – The seed for random number generation. Default is 42.

Methods

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

Initializes the TcnED model with 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)

Conducts a forward inference pass with a batch of data.

inference_prepare(X)

Prepares the model for inference, including setting up the data loader.

load_model(path)

load_ray_checkpoint(best_config, best_checkpoint)

Loads the best model checkpoint from Ray Tune.

predict(X[, return_confidence])

Predict if a particular sample is an outlier or not.

save_model(path)

set_seed(seed)

set_tuned_net(config)

Sets up the network model with tuned hyperparameters.

set_tuned_params()

Defines the grid of hyperparameters for tuning.

training_forward(batch_x, net, criterion)

Conducts a forward training pass with a batch of data.

training_prepare(X[, y])

Sets up the model for training including the data loader, 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]

Conducts a forward inference pass with a batch of data.

Parameters:
  • batch_x (Tensor) – The batch of inference data.

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

  • criterion (callable) – The loss criterion used to compute the error.

Returns:

A tuple containing the output and the error for the inference batch.

Return type:

tuple

inference_prepare(X)[source]

Prepares the model for inference, including setting up the data loader.

Parameters:

X (numpy.ndarray) – The input features for inference.

Returns:

A data loader containing the test dataset.

Return type:

DataLoader

load_ray_checkpoint(best_config, best_checkpoint)[source]

Loads the best model checkpoint from Ray Tune.

Parameters:
  • best_config (dict) – The best configuration found by Ray Tune.

  • best_checkpoint (dict) – The checkpoint data to load.

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.

set_tuned_net(config)[source]

Sets up the network model with tuned hyperparameters.

Parameters:

config (dict) – The configuration dictionary containing the hyperparameters.

Returns:

The initialized network model with the specified hyperparameters.

Return type:

TcnAE

static set_tuned_params()[source]

Defines the grid of hyperparameters for tuning.

Returns:

A configuration dictionary for Ray Tune.

Return type:

dict

training_forward(batch_x, net, criterion)[source]

Conducts a forward training pass with a batch of data.

Parameters:
  • batch_x (Tensor) – The batch of training data.

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

  • criterion (callable) – The loss criterion.

Returns:

The loss for the training batch.

Return type:

Tensor

training_prepare(X, y=None)[source]

Sets up the model for training including the data loader, network, and loss criterion.

Parameters:
  • X (numpy.ndarray) – The input features for training.

  • y (numpy.ndarray, optional) – The target values for training. Defaults to None.

Returns:

A tuple containing the training data loader, network, and loss criterion.

Return type:

tuple