deepod.models.AnomalyTransformer
- class deepod.models.AnomalyTransformer(seq_len=100, stride=1, lr=0.0001, epochs=10, batch_size=32, epoch_steps=20, prt_steps=1, device='cuda', k=3, verbose=2, random_state=42)[source]
Anomaly Transformer: Time Series Anomaly Detection with Association Discrepancy (ICLR’22)
Implements the Anomaly Transformer model for time series anomaly detection based on the paper “Anomaly Transformer: Time Series Anomaly Detection with Association Discrepancy”.
Inherits from BaseDeepAD which contains base functionality for anomaly detection models.
- Parameters:
seq_len (int, optional) – This parameter determines the length of the input sequences for the transformer. Default is 100.
stride (int, optional) – This parameter determines the stride with which the input sequences are sampled. Default is 1.
lr (float, optional) – This parameter sets the learning rate for the optimizer. Default is 0.001.
epochs (int, optional) – This parameter sets the number of epochs for training the model. Default is 10.
batch_size (int, optional) – This parameter sets the size of batches for training and inference. Default is 32.
epoch_steps (int, optional) – This parameter sets the number of steps (batches) per epoch. Default is 20.
prt_steps (int, optional) – This parameter sets the interval of epochs to print training progress. Default is 1.
device (str, optional) – This parameter sets the device to train the model on, ‘cuda’ or ‘cpu’. Default is ‘cuda’.
k (int, optional) – This parameter sets the hyperparameter k for loss calculation. Default is 3.
verbose (int, optional) – This parameter sets the verbosity mode. Default is 2.
random_state (int, optional) – This parameter sets the seed for random number generator for reproducibility. Default is 42.
Methods
__init__([seq_len, stride, lr, epochs, ...])Initializes the AnomalyTransformer model with specified hyperparameters and training settings.
decision_function(X[, return_rep])This method computes the anomaly scores for the given input data.
decision_function_update(z, scores)for any updating operation after decision function
for any updating operation after each training epoch
fit(X[, y])This method is used to train the AnomalyTransformer model on the provided dataset.
fit_auto_hyper(X[, y, X_test, y_test, ...])Fit detector.
inference(dataloader)This method performs inference on the data provided by the dataloader.
inference_forward(batch_x, net, criterion)define forward step in inference
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)This method defines the training process for one epoch.
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]
This method computes the anomaly scores for the given input data. Anomaly scores are a measure of how much a data point deviates from what is considered normal or expected. A higher score indicates a higher likelihood of the data point being anomalous.
- Parameters:
X (np.array, required) – The input data for which the anomaly scores are to be computed. It should be a numpy array where each row represents a different time series and each column represents a different time point.
return_rep (bool, optional) – A flag that determines whether the representations should be returned along with the anomaly scores. These representations are the encoded versions of the input data as learned by the model. They can be useful for further analysis or for visualizing the data in a lower-dimensional space. The default value is False, which means that by default, the representations are not returned.
- Returns:
The anomaly scores for the input data. Each score corresponds to a data point in the input data. The scores are returned as a numpy array.
- Return type:
np.array
- 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]
This method is used to train the AnomalyTransformer model on the provided dataset.
- Parameters:
X (np.array, required) – This is the input data that the model will be trained on. It should be a numpy array where each row represents a different time series and each column represents a different time point.
y (np.array, optional) – These are the true labels for the input data. If provided, they can be used to monitor the training process and adjust the model parameters. However, they are not necessary for the training process and their default value is None.
- Returns:
This method does not return any value. It modifies the state of the AnomalyTransformer object by training it on the provided data.
- Return type:
None
- 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:
- inference(dataloader)[source]
This method performs inference on the data provided by the dataloader. It uses the trained model to generate anomaly scores and predictions for the input data.
- Parameters:
dataloader (DataLoader) – The DataLoader object that provides the data for inference. It should contain the input data that we want to generate anomaly scores and predictions for.
- Returns:
A tuple containing two numpy arrays. The first array contains the anomaly scores for the input data, and the second array contains the predicted labels for the input data.
- anomaly scores (np.array):
An array of anomaly scores for the input data. Each score represents the degree of anomaly of the corresponding data point in the input data.
- predictions (np.array):
An array of predicted labels for the input data. Each label represents the predicted class (normal or anomalous) of the corresponding data point in the input data.
- Return type:
- 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]
This method defines the training process for one epoch. During each epoch, the model is trained on batches of input data provided by the DataLoader. The training process involves forward propagation, loss computation, backpropagation, and optimization steps. The loss function used is the Mean Squared Error (MSE) loss, which measures the average squared difference between the actual and predicted values. The loss is computed for each batch, and the average loss over all batches is returned.
- Parameters:
dataloader (DataLoader) – The DataLoader object that provides batches of input data for training. Each batch is a tensor of shape (batch_size, sequence_length, number_of_features), where batch_size is the number of sequences in a batch, sequence_length is the length of each sequence, and number_of_features is the number of features in the data.
- Returns:
The average loss over all batches in the dataloader. This is a single floating-point number that represents the average of the MSE loss computed for each batch of data.
- Return type: