paddlets.models.anomaly.dl.anomaly_base
- class AnomalyBaseModel(in_chunk_len: int, sampling_stride: int = 1, loss_fn: ~typing.Optional[~typing.Callable[[...], ~paddle.Tensor]] = None, anomaly_score_fn: ~typing.Optional[~typing.Callable[[...], ~typing.List[float]]] = None, threshold: ~typing.Optional[float] = None, threshold_fn: ~typing.Optional[~typing.Callable[[...], float]] = None, threshold_coeff: float = 1.0, pred_adjust: bool = False, pred_adjust_fn: ~typing.Optional[~typing.Callable[[...], ~numpy.ndarray]] = None, optimizer_fn: ~typing.Callable[[...], ~paddle.optimizer.optimizer.Optimizer] = <class 'paddle.optimizer.adam.Adam'>, optimizer_params: ~typing.Dict[str, ~typing.Any] = {'learning_rate': 0.001}, eval_metrics: ~typing.List[str] = [], callbacks: ~typing.List[~paddlets.models.common.callbacks.callbacks.Callback] = [], batch_size: int = 128, max_epochs: int = 10, verbose: int = 1, patience: int = 4, seed: ~typing.Union[None, int] = None)[source]
Bases:
ABCPaddleTS deep time series anomaly detection framework, all time series models based on paddlepaddle implementation need to inherit this class.
- 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]|None) – Loss function.
anomaly_score_fn (Callable[..., List[float]]|None) – The method to get anomaly score.
threshold (float|None) – The threshold to judge anomaly.
threshold_fn (Callable[..., float]|None) – The method to get anomaly threshold.
threshold_coeff (float) – The coefficient of threshold.
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_fn (Callable[..., Optimizer]) – Optimizer algorithm.
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.
- _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]|None
- _anomaly_score_fn
The method to get anomaly score.
- Type
Callable[…, paddle.Tensor]|None
- _threshold
The threshold to judge anomaly.
- Type
float|None
- _threshold_fn
The method to get anomaly threshold.
- Type
Callable[…, paddle.Tensor]|None
- _threshold_coeff
The coefficient of threshold.
- Type
float
- _pred_adjust
Whether to adjust the pred label according to the real label.
- Type
float
- _pred_adjust_fn
The method to adjust pred label.
- Type
Callable[…, np.ndarray]|None
- _optimizer_fn
Optimizer algorithm.
- Type
Callable[…, Optimizer]
- _optimizer_params
Optimizer parameters.
- Type
Dict[str, Any]
- _eval_metrics
Evaluation metrics of model.
- Type
List[str]
- _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
- _stop_training
- Type
bool
- _fit_params
Infer parameters by TSdataset automatically.
- Type
Dict[str, Any]
- _network
Network structure.
- Type
paddle.nn.Layer
- _optimizer
Optimizer.
- Type
Optimizer
- _metrics_names
List of metric names.
- Type
List[str]
- _metric_container_dict
Dict of metric container.
- Type
Dict[str, MetricContainer]
- _callback_container
Container holding a list of callbacks.
- Type
- 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.
- predict(tsdataset: TSDataset, **predict_kwargs) TSDataset[source]
Get anomaly label on a batch. the result are output as tsdataset.
- Parameters
tsdataset (TSDataset) – Data to be predicted.
**predict_kwargs – Additional arguments for _predict.
- Returns
TSDataset.
- predict_score(tsdataset: TSDataset, **predict_kwargs) TSDataset[source]
Get anomaly score on a batch. the result are output as tsdataset.
- Parameters
tsdataset (TSDataset) – Data to be predicted.
**predict_kwargs – Additional arguments for _predict.
- Returns
TSDataset.
- save(path: str, network_model: bool = False, dygraph_to_static: bool = True, batch_size: Union[None, int] = None) None[source]
Saves a AnomalyBaseModel instance to a disk file.
1> A AnomalyBaseModel (or any child classes inherited from AnomalyBaseModel) instance have a set of member variables, they can be divided into 3 categories: pickle-serializable members (e.g. python built-in type such as int, str, dict, etc.), paddle-related pickle-not-serializable members (e.g. paddle.nn.Layer, paddle.optimizer.Optimizer), paddle-not-related pickle-not-serializable members.
2> To call this method, self._network and self._optimizer must not be None.
- Parameters
path (str) – A path string containing a model file name.
network_model (bool) – Save network model structure and parameters separately for Paddle Inference or not, default False.
dygraph_to_static (bool) – Change network from dygraph to static or not, it works when network_model==True, default True.
batch_size (int) – The fixed batch size for the param input_spec of network_model save, it works when network_model==True, default None.
- Raises
ValueError –
- static load(path: str) AnomalyBaseModel[source]
Loads a AnomalyBaseModel from a file.
As optimizer does not affect the model prediction results, currently optimizer will NOT be loaded.
- Parameters
path (str) – A path string containing a model file name.
- Returns
the loaded AnomalyBaseModel instance.
- Return type