deepod.models.PReNetTS
- class deepod.models.PReNetTS(epochs=100, batch_size=64, lr=0.001, network='Transformer', seq_len=30, stride=1, rep_dim=128, hidden_dims='512', act='GELU', bias=False, n_heads=8, d_model=512, attn='self_attn', pos_encoding='fixed', norm='BatchNorm', epoch_steps=-1, prt_steps=10, device='cuda', verbose=2, random_state=42)[source]
Deep Weakly-supervised Anomaly Detection (KDD‘23)
- Parameters:
epochs (int) – The number of epochs for training the model. Default is 100.
batch_size (int) – The size of the batch for training. Default is 64.
lr (float) – The learning rate. Default is 1e-3.
network (str) – The type of network used, ‘Transformer’ by default.
seq_len (int) – The length of the input sequences. Default is 30.
stride (int) – The stride for sliding window on data. Default is 1.
rep_dim (int) – The representation dimension. Default is 128.
hidden_dims (str) – The hidden layer dimensions, separated by commas. Default is ‘512’.
act (str) – The activation function. Default is ‘GELU’.
bias (bool) – Whether to use bias in the layers. Default is False.
n_heads (int) – The number of attention heads in a transformer. Default is 8.
d_model (int) – The dimensionality of the transformer model. Default is 512.
attn (str) – The type of attention mechanism. Default is ‘self_attn’.
pos_encoding (str) – The type of position encoding. Default is ‘fixed’.
norm (str) – The type of normalization layer. Default is ‘BatchNorm’.
epoch_steps (int) – The steps per epoch, -1 indicates using the full dataset. Default is -1.
prt_steps (int) – The steps for printing during training. Default is 10.
device (str) – The device for training, ‘cuda’ by default.
verbose (int) – The verbosity level. Default is 2.
random_state (int) – The random state seeding. Default is 42.
Methods
__init__([epochs, batch_size, lr, network, ...])Initializes the PReNetTS 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
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)Processes a single inference batch through the model.
Prepares the model for inference by setting up the test data 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_forward(batch_x, net, criterion)Processes a single training batch through the model.
training_prepare(X, y)Prepares the model for training by setting up the data loader, network, and 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:
- 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_forward(batch_x, net, criterion)[source]
Processes a single inference batch through the model.
- inference_prepare(X)[source]
Prepares the model for inference by setting up the test data loader.
- Parameters:
X (numpy.ndarray) – Test data.
- Returns:
A list of batches for testing.
- 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_forward(batch_x, net, criterion)[source]
Processes a single training batch through the model.
- Parameters:
batch_x (tuple) – A tuple of the batch data.
net (torch.nn.Module) – The network model.
criterion (callable) – The loss criterion.
- Returns:
The computed loss for the batch.
- Return type:
Tensor
- training_prepare(X, y)[source]
Prepares the model for training by setting up the data loader, network, and criterion.
- Parameters:
X (numpy.ndarray) – Training data.
y (numpy.ndarray) – Training labels.
- Returns:
A tuple containing the training data loader, the network model, and the loss criterion.
- Return type: