paddlets.models.anomaly.dl.anomaly_transformer

This implementation is based on the article Anomaly Transformer: Time Series Anomaly Detection with Association Discrepancy .

Base model features

Basic architecture: A network with stacking anomaly Attention.

Anomaly Attention have two branch, one is self attention, another one is Gaussian kernel.

The backward is for computing of the the prior-association loss and the series-association loss, distinguishing anomaly data in raw data.

class AnomalyTransformer(in_chunk_len: int, sampling_stride: int = 1, loss_fn: ~typing.Callable[[...], ~paddle.Tensor] = <function series_prior_loss>, optimizer_fn: ~typing.Callable[[...], ~paddle.optimizer.optimizer.Optimizer] = <class 'paddle.optimizer.adam.Adam'>, threshold_fn: ~typing.Callable[[...], float] = <function anomaly_get_threshold>, criterion: ~typing.Callable[[...], ~paddle.Tensor] = MSELoss(), threshold: ~typing.Optional[float] = None, threshold_coeff: float = 1.0, anomaly_score_fn: ~typing.Optional[~typing.Callable[[...], ~typing.List[float]]] = None, pred_adjust: bool = True, pred_adjust_fn: ~typing.Callable[[...], ~numpy.ndarray] = <function result_adjust>, optimizer_params: ~typing.Dict[str, ~typing.Any] = {'learning_rate': 0.0001}, eval_metrics: ~typing.List[str] = [], callbacks: ~typing.List[~paddlets.models.common.callbacks.callbacks.Callback] = [], batch_size: int = 32, max_epochs: int = 100, verbose: int = 1, patience: int = 10, seed: ~typing.Union[None, int] = None, temperature: int = 50, k: int = 3, anormly_ratio=1, d_model: int = 512, n_heads: int = 8, e_layers: int = 3, d_ff: int = 512, dropout: float = 0.0, activation: ~typing.Callable[[...], ~paddle.Tensor] = <function gelu>, output_attention=True)[source]

Bases: AnomalyBaseModel

Anomaly Transformer network for anomaly detection.

Parameters
  • in_chunk_len (int) – The size of the loopback window, i.e. the number of time steps feed to the model.

  • sampling_stride (int) – Sampling intervals between two adjacent samples.

  • loss_fn (Callable[..., paddle.Tensor]) – Loss function.

  • optimizer_fn (Callable[..., Optimizer]) – Optimizer algorithm.

  • threshold_fn (Callable[..., float]|None) – The method to get anomaly threshold.

  • criterion (Callable[..., paddle.Tensor]) – Loss function for for the reconstruction loss.

  • threshold (float|None) – The threshold to judge anomaly.

  • threshold_coeff (float) – The coefficient of threshold.

  • anomaly_score_fn (Callable[..., List[float]]|None) – The method to get anomaly score.

  • pred_adjust (bool) – Whether to adjust the pred label according to the real label.

  • pred_adjust_fn (Callable[..., np.ndarray]|None) – The method to adjust pred label.

  • optimizer_params (Dict[str, Any]) – Optimizer parameters.

  • eval_metrics (List[str]) – Evaluation metrics of model.

  • callbacks (List[Callback]) – Customized callback functions.

  • batch_size (int) – Number of samples per batch.

  • max_epochs (int) – Max epochs during training.

  • verbose (int) – Verbosity mode.

  • patience (int) – Number of epochs to wait for improvement before terminating.

  • seed (int|None) – Global random seed.

  • temperature (int|float) – A parameter to adjust series loss and prior loss.

  • k (int) – The optimization is to enlarge the association discrepancy.

  • anormly_ratio (int|float) – The Proportion of Anomaly data in train set and test set.

  • d_model (int) – The expected feature size for the input of the anomaly transformer.

  • n_heads (int) – The number of heads in multi-head attention.

  • e_layers (int) – The number of attentionLayer layers to be stacked.

  • d_ff (int) – The Number of channels for FFN layers.

  • dropout (float) – Dropout regularization parameter.

  • activation (Callable[..., paddle.Tensor]) – The activation function for AnomalyAttention.

  • output_attention (float) – Whether to output series, prior and sigma.

_in_chunk_len

The size of the loopback window, i.e. the number of time steps feed to the model.

Type

int

_sampling_stride

Sampling intervals between two adjacent samples.

Type

int

_loss_fn

Loss function.

Type

Callable[…, paddle.Tensor]

_optimizer_fn

Optimizer algorithm.

Type

Callable[…, Optimizer]

_threshold_fn

The method to get anomaly threshold.

Type

Callable[…, float]|None

_criterion

Loss function for for the reconstruction loss.

Type

Callable[…, paddle.Tensor]

_threshold

The threshold to judge anomaly.

Type

float|None

_threshold_coeff

The coefficient of threshold.

Type

float

_anomaly_score_fn

The method to get anomaly score.

Type

Callable[…, List[float]]|None

_pred_adjust

Whether to adjust the pred label according to the real label.

Type

bool

_pred_adjust_fn

The method to adjust pred label.

Type

Callable[…, np.ndarray]|None

_optimizer_params

Optimizer parameters.

Type

Dict[str, Any]

_eval_metrics

Evaluation metrics of model.

Type

List[str]

_callbacks

Customized callback functions.

Type

List[Callback]

_batch_size

Number of samples per batch.

Type

int

_max_epochs

Max epochs during training.

Type

int

_verbose

Verbosity mode.

Type

int

_patience

Number of epochs to wait for improvement before terminating.

Type

int

_seed

Global random seed.

Type

int|None

_temperature

A parameter to adjust series loss and prior loss.

Type

int|float

_k

The optimization is to enlarge the association discrepancy.

Type

int

_anormly_ratio

The Proportion of Anomaly data in train set and test set.

Type

int|float

_d_model

The expected feature size for the input of the anomaly transformer.

Type

int

_n_heads

The number of heads in multi-head attention.

Type

int

_e_layers

The number of attentionLayer layers to be stacked.

Type

int

_d_ff

The Number of channels for FFN layers.

Type

int

_dropout

Dropout regularization parameter.

Type

float

_activation

The activation function for AnomalyAttention.

Type

Callable[…, paddle.Tensor]

_output_attention

whether to output series, prior and sigma.

Type

float

_adjust_lr

Dynamic Learning Rate Adjustment.

Type

function

predict(test_dataset: TSDataset, train_dataset: TSDataset = None) TSDataset[source]

Get anomaly label on a batch. the result are output as tsdataset.

Parameters
  • train_dataset (TSDataset) – Train set.

  • test_dataset (TSDataset) – Data to be predicted.

Returns

TSDataset.

predict_score(tsdataset: TSDataset) TSDataset[source]

Get anomaly score on a batch. the result are output as tsdataset.

Parameters

tsdataset (TSDataset) – Data to be predicted.

Returns

TSDataset.

fit(train_tsdataset: TSDataset, valid_tsdataset: Optional[TSDataset] = None)[source]
Train a neural network stored in self._network,

Using train_dataloader for training data and valid_dataloader for validation.

Parameters
  • train_tsdataset (TSDataset) – Train set.

  • valid_tsdataset (TSDataset|None) – Eval set, used for early stopping.