deepod.models.TimesNet

class deepod.models.TimesNet(seq_len=100, stride=1, lr=0.0001, epochs=10, batch_size=32, epoch_steps=20, prt_steps=1, device='cuda', pred_len=0, e_layers=2, d_model=64, d_ff=64, dropout=0.1, top_k=5, num_kernels=6, verbose=2, random_state=42)[source]

TIMESNET: Temporal 2D-Variation Modeling for General Time Series Analysis (ICLR’23)

This model applies temporal 2D-variation modeling to capture the intrinsic patterns in time series data for general analysis, specifically designed for anomaly detection tasks.

Parameters:
  • seq_len (int) – Length of the input sequences (default 100).

  • stride (int) – Stride for sliding window on data (default 1).

  • lr (float) – Learning rate for optimizer (default 0.0001).

  • epochs (int) – Number of epochs to train the model (default 10).

  • batch_size (int) – Size of batches for training (default 32).

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

  • prt_steps (int) – Interval of epochs for printing training progress (default 1).

  • device (str) – Device to use for training, e.g., ‘cuda’ (default ‘cuda’).

  • pred_len (int) – Prediction length for the model (default 0).

  • e_layers (int) – Number of encoder layers (default 2).

  • d_model (int) – Dimensionality of the model (default 64).

  • d_ff (int) – Dimensionality of the feedforward layer (default 64).

  • dropout (float) – Dropout rate (default 0.1).

  • top_k (int) – Top K frequencies for FFT period finding (default 5).

  • num_kernels (int) – Number of kernels for inception block (default 6).

  • verbose (int) – Verbosity level (default 2).

  • random_state (int) – Seed for random number generation (default 42).

Methods

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

Initializes TimesNet with the provided parameters.

decision_function(X[, return_rep])

Computes the anomaly scores for each sample in X.

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 TimesNet model to the training data.

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

Fit detector.

inference(dataloader)

Performs inference on the data provided by the DataLoader.

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)

Conducts a training pass on the given DataLoader.

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 the anomaly scores for each sample in X.

Parameters:
  • X (numpy.ndarray) – Data to compute anomaly scores for.

  • return_rep (bool, optional) – Flag to determine if representations should be returned (default False).

Returns:

Anomaly scores for each sample in X.

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 TimesNet model to the training data.

Parameters:
  • X (numpy.ndarray) – Training data with shape (samples, features).

  • y (numpy.ndarray, optional) – Training labels. Not used in this method (default None).

Side effects:

Trains the model and updates the model’s weights. Sets the decision_scores_ and labels_ attributes based on training data.

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]

Performs inference on the data provided by the DataLoader.

Parameters:

dataloader (DataLoader) – DataLoader containing the test batches.

Returns:

A tuple containing anomaly scores and predictions for the test data.

Return type:

tuple

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

Conducts a training pass on the given DataLoader.

Parameters:

dataloader (DataLoader) – DataLoader containing the training batches.

Returns:

Average training loss over all batches in the DataLoader.

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