timesead.models.reconstruction ============================== .. py:module:: timesead.models.reconstruction Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/timesead/models/reconstruction/anom_trans/index /autoapi/timesead/models/reconstruction/autoformer/index /autoapi/timesead/models/reconstruction/etsformer/index /autoapi/timesead/models/reconstruction/fedformer/index /autoapi/timesead/models/reconstruction/genad/index /autoapi/timesead/models/reconstruction/lstm_ae/index /autoapi/timesead/models/reconstruction/mscred/index /autoapi/timesead/models/reconstruction/stgat/index /autoapi/timesead/models/reconstruction/tcn_ae/index /autoapi/timesead/models/reconstruction/timesnet/index /autoapi/timesead/models/reconstruction/usad/index Classes ------- .. autoapisummary:: timesead.models.reconstruction.LSTMAE timesead.models.reconstruction.LSTMAEMalhotra2016 timesead.models.reconstruction.LSTMAEMirza2018 timesead.models.reconstruction.LSTMAEAnomalyDetector timesead.models.reconstruction.MSCRED timesead.models.reconstruction.MSCREDLoss timesead.models.reconstruction.MSCREDAnomalyDetector timesead.models.reconstruction.MSCREDAnomalyDetectorOrig timesead.models.reconstruction.SignatureMatrixTransform timesead.models.reconstruction.TCNAE timesead.models.reconstruction.TCNAEAnomalyDetector timesead.models.reconstruction.USADModel timesead.models.reconstruction.BasicAE timesead.models.reconstruction.USADDecoder1Loss timesead.models.reconstruction.USADDecoder2Loss timesead.models.reconstruction.USADAnomalyDetector timesead.models.reconstruction.AnomalyTransformer timesead.models.reconstruction.AnomTransf_Loss timesead.models.reconstruction.AnomTransf_Trainer timesead.models.reconstruction.AnomTransf_AnomalyDetector timesead.models.reconstruction.TimesNet timesead.models.reconstruction.Autoformer timesead.models.reconstruction.FEDformer timesead.models.reconstruction.ETSformer Package Contents ---------------- .. py:class:: LSTMAE(input_dimension: int, hidden_dimensions=None, latent_pooling: Union[str, Callable] = 'last', decoder_class: Type[LSTMAEDecoder] = LSTMAEDecoderReverse, return_latent: bool = False) Bases: :py:obj:`timesead.models.common.AE`, :py:obj:`timesead.models.BaseModel` Generic LSTMAE implementation Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:method:: encode(x: torch.Tensor) -> List[torch.Tensor] .. py:method:: forward(inputs: Tuple[torch.Tensor]) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] :param inputs: Tuple with a single tensor of shape (T, B, D) :return: tensor of shape (T, B, D) .. py:class:: LSTMAEMalhotra2016(input_dimension: int, hidden_dimensions=None) Bases: :py:obj:`LSTMAE` Implementation of Malhotra 2016 (https://arxiv.org/pdf/1607.00148.pdf, default parameters) Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:class:: LSTMAEMirza2018(input_dimension: int, hidden_dimensions: List[int] = [64], latent_pooling: str = 'mean') Bases: :py:obj:`LSTMAE` Mirza 2018 (http://repository.bilkent.edu.tr/bitstream/handle/11693/50234/Computer_network_intrusion_detection_using_sequential_LSTM_neural_networks_autoencoders.pdf?sequence=1) Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: sigmoid .. py:method:: forward(inputs: Tuple[torch.Tensor]) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]] :param inputs: Tuple with a single tensor of shape (T, B, D) :return: tensor of shape (T, B, D) .. py:class:: LSTMAEAnomalyDetector(model: LSTMAE) Bases: :py:obj:`timesead.models.common.AnomalyDetector` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: model .. py:method:: fit(dataset: torch.utils.data.DataLoader) -> None Fit this anomaly detector on a dataset. Note that we assume only normal data here. :param dataset: A dataset .. py:method:: compute_online_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Compute the online anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (B,) that contains the anomaly scores for this batch .. py:method:: compute_offline_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor :abstractmethod: Compute the offline anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (N,) that contains the anomaly scores for this batch .. py:method:: format_online_targets(targets: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Format the labels for a batch of targets. The output tensor must have the same shape as the output of `compute_online_anomaly_score` when called with the corresponding inputs for this batch. :param targets: tuple of target tensors :return: Tensor of shape (B,) that contains the ground truth labels for this batch .. py:class:: MSCRED(n_features: int, in_channels: int, c_out: int = 256, small_model: bool = False, chi: float = 5.0) Bases: :py:obj:`timesead.models.BaseModel` input is signature matrices of shape (Seq_len, Batch, Channel, Height, Width) Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: small_model :value: False .. py:attribute:: chi :value: 5.0 .. py:attribute:: enc1 .. py:attribute:: lstm1 .. py:method:: forward(inputs: Tuple[torch.Tensor]) -> torch.Tensor .. py:class:: MSCREDLoss Bases: :py:obj:`timesead.optim.loss.Loss` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: mse_loss .. py:method:: forward(predictions: Tuple[torch.Tensor, Ellipsis], targets: Tuple[torch.Tensor, Ellipsis], *args, **kwargs) -> torch.Tensor .. py:class:: MSCREDAnomalyDetector(model: MSCRED) Bases: :py:obj:`timesead.models.common.MSEReconstructionAnomalyDetector` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool This is what Florian uses, but in the paper they describe sth. completely different. They compute the number of badly reconstructed entries in the signature matrix (i.e., higher than some threshold) and use that count as the anomaly score .. py:method:: compute_online_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Compute the online anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (B,) that contains the anomaly scores for this batch .. py:method:: compute_offline_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor :abstractmethod: Compute the offline anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (N,) that contains the anomaly scores for this batch .. py:class:: MSCREDAnomalyDetectorOrig(model: MSCRED, error_threshold: float = 0.5) Bases: :py:obj:`timesead.models.common.AnomalyDetector` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: model .. py:attribute:: error_threshold :value: 0.5 .. py:method:: compute_online_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Compute the online anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (B,) that contains the anomaly scores for this batch .. py:method:: compute_offline_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor :abstractmethod: Compute the offline anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (N,) that contains the anomaly scores for this batch .. py:method:: fit(dataset: torch.utils.data.DataLoader) -> None Fit this anomaly detector on a dataset. Note that we assume only normal data here. :param dataset: A dataset .. py:method:: format_online_targets(targets: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Format the labels for a batch of targets. The output tensor must have the same shape as the output of `compute_online_anomaly_score` when called with the corresponding inputs for this batch. :param targets: tuple of target tensors :return: Tensor of shape (B,) that contains the ground truth labels for this batch .. py:class:: SignatureMatrixTransform(parent: timesead.data.transforms.Transform, wins: Tuple[int] = (10, 30, 60), seg_interval: int = 10, h: int = 5) Bases: :py:obj:`timesead.data.transforms.WindowTransform` .. py:attribute:: wins :value: (10, 30, 60) .. py:attribute:: seg_interval :value: 10 .. py:attribute:: h :value: 5 .. py:property:: num_features :type: Union[int, Tuple[int, Ellipsis]] .. py:class:: TCNAE(input_dimension: int, dilations: List[int] = (1, 2, 4, 8, 16), nb_filters: Union[int, List[int]] = 20, kernel_size: int = 20, nb_stacks: int = 1, padding: str = 'same', dropout_rate: float = 0.0, filters_conv1d: int = 8, activation_conv1d: Union[str, Callable] = 'linear', latent_sample_rate: int = 42, pooler: Type[torch.nn.Module] = torch.nn.AvgPool1d) Bases: :py:obj:`timesead.models.BaseModel` A class used to represent the Temporal Convolutional Autoencoder (TCN-AE). Loss for this is logcosh :param ts_dimension: The dimension of the time series (default is 1) :type ts_dimension: int :param dilations: The dilation rates used in the TCN-AE model (default is (1, 2, 4, 8, 16)) :type dilations: tuple :param nb_filters: The number of filters used in the dilated convolutional layers. All dilated conv. layers use the same number of filters (default is 20) :type nb_filters: int .. py:attribute:: tcn_enc .. py:attribute:: conv1d .. py:attribute:: activation :value: 'linear' .. py:attribute:: pooler .. py:attribute:: tcn_dec .. py:attribute:: linear .. py:method:: forward(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor :param inputs: Tuple with single Tensor of shape (B, T, D) :return: .. py:class:: TCNAEAnomalyDetector(model: TCNAE, offline_window_size: int = 128) Bases: :py:obj:`timesead.models.common.AnomalyDetector` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: model .. py:attribute:: offline_window_size :value: 128 .. py:method:: fit(dataset: torch.utils.data.DataLoader) -> None Fit this anomaly detector on a dataset. Note that we assume only normal data here. :param dataset: A dataset .. py:method:: compute_online_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Compute the online anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (B,) that contains the anomaly scores for this batch .. py:method:: compute_offline_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Compute the offline anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (N,) that contains the anomaly scores for this batch .. py:method:: format_online_targets(targets: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Format the labels for a batch of targets. The output tensor must have the same shape as the output of `compute_online_anomaly_score` when called with the corresponding inputs for this batch. :param targets: tuple of target tensors :return: Tensor of shape (B,) that contains the ground truth labels for this batch .. py:class:: USADModel(w_size: int, z_size: int) Bases: :py:obj:`timesead.models.BaseModel` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: encoder .. py:attribute:: decoder1 .. py:attribute:: decoder2 .. py:method:: forward(inputs: Tuple[torch.Tensor, Ellipsis]) -> Tuple[torch.Tensor, Ellipsis] :param inputs: Tuple with one tensor of shape (B, T, D) :return: .. py:method:: grouped_parameters() .. py:class:: BasicAE(w_size: int, z_size: int = 40) Bases: :py:obj:`timesead.models.BaseModel` What I believe to be the basic "AE" model from the USAD paper Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: encoder .. py:attribute:: decoder .. py:method:: forward(inputs: Tuple[torch.Tensor]) -> torch.Tensor .. py:class:: USADDecoder1Loss(size_average=None, reduce=None, reduction: str = 'mean') Bases: :py:obj:`timesead.optim.loss.Loss` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:method:: forward(predictions: Tuple[torch.Tensor, Ellipsis], targets: Tuple[torch.Tensor, Ellipsis], *args, **kwargs) -> torch.Tensor .. py:class:: USADDecoder2Loss(size_average=None, reduce=None, reduction: str = 'mean') Bases: :py:obj:`timesead.optim.loss.Loss` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:method:: forward(predictions: Tuple[torch.Tensor, Ellipsis], targets: Tuple[torch.Tensor, Ellipsis], *args, **kwargs) -> torch.Tensor .. py:class:: USADAnomalyDetector(model: USADModel, alpha: float = 0.5) Bases: :py:obj:`timesead.models.common.AnomalyDetector` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: model .. py:attribute:: alpha :value: 0.5 .. py:attribute:: beta :value: 0.5 .. py:method:: fit(dataset: torch.utils.data.DataLoader) -> None Fit this anomaly detector on a dataset. Note that we assume only normal data here. :param dataset: A dataset .. py:method:: compute_online_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Compute the online anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (B,) that contains the anomaly scores for this batch .. py:method:: compute_offline_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor :abstractmethod: Compute the offline anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (N,) that contains the anomaly scores for this batch .. py:method:: format_online_targets(targets: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Format the labels for a batch of targets. The output tensor must have the same shape as the output of `compute_online_anomaly_score` when called with the corresponding inputs for this batch. :param targets: tuple of target tensors :return: Tensor of shape (B,) that contains the ground truth labels for this batch .. py:class:: AnomalyTransformer(win_size: int, input_dim: int, d_model: int = 512, n_heads: int = 8, e_layers: int = 3, d_ff: int = 512, dropout: float = 0.0, activation: str = 'gelu', output_attention: bool = True) Bases: :py:obj:`timesead.models.BaseModel` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: output_attention :value: True .. py:attribute:: embedding .. py:attribute:: encoder .. py:attribute:: projection .. py:method:: forward(x: torch.Tensor) -> Tuple[torch.Tensor, Ellipsis] .. py:class:: AnomTransf_Loss(lamb: float = 3.0) Bases: :py:obj:`timesead.optim.loss.Loss` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: lamb :value: 3.0 .. py:attribute:: mse_loss .. py:method:: calc_association_disc(series: List[torch.Tensor], priors: List[torch.Tensor]) -> Tuple[torch.Tensor, torch.Tensor] .. py:method:: forward(predictions: Tuple[torch.Tensor, Ellipsis], targets: Tuple[torch.Tensor, Ellipsis] = None, *args, **kwargs) -> torch.Tensor .. py:class:: AnomTransf_Trainer(*args, **kwargs) Bases: :py:obj:`timesead.optim.trainer.Trainer` .. py:method:: validate_batch(network: torch.nn.Module, val_metrics: Dict[str, Callable], b_inputs: Tuple[torch.Tensor, Ellipsis], b_targets: Tuple[torch.Tensor, Ellipsis], *args, **kwargs) -> Dict[str, float] .. py:method:: train_batch(network: AnomalyTransformer, losses: List[timesead.optim.loss.Loss], optimizers: List[torch.optim.Optimizer], epoch: int, num_epochs: int, b_inputs: Tuple[torch.Tensor, Ellipsis], b_targets: Tuple[torch.Tensor, Ellipsis]) -> List[float] .. py:class:: AnomTransf_AnomalyDetector(model: AnomalyTransformer) Bases: :py:obj:`timesead.models.common.AnomalyDetector` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: model .. py:method:: compute_online_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Compute the online anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (B,) that contains the anomaly scores for this batch .. py:method:: compute_offline_anomaly_score(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor :abstractmethod: Compute the offline anomaly score for a batch of inputs. The output tensor must have the same shape as the output of `format_targets` when called with the corresponding targets for this batch. This method expects a window (or a batch of windows) as its input and should return a score for the last point in the window. :param inputs: tuple of input tensors :return: Tensor of shape (N,) that contains the anomaly scores for this batch .. py:method:: fit(dataset: torch.utils.data.DataLoader) -> None Fit this anomaly detector on a dataset. Note that we assume only normal data here. :param dataset: A dataset .. py:method:: format_online_targets(targets: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor Format the labels for a batch of targets. The output tensor must have the same shape as the output of `compute_online_anomaly_score` when called with the corresponding inputs for this batch. :param targets: tuple of target tensors :return: Tensor of shape (B,) that contains the ground truth labels for this batch .. py:class:: TimesNet(window_size: int, input_dim: int, top_k: int = 5, d_model: int = 64, d_ff: int = 64, num_kernels: int = 8, e_layers: int = 2, dropout: float = 0.1) Bases: :py:obj:`timesead.models.BaseModel` Paper link: https://openreview.net/pdf?id=ju_Uqw384Oq Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: seq_len .. py:attribute:: model .. py:attribute:: enc_embedding .. py:attribute:: layer :value: 2 .. py:attribute:: layer_norm .. py:attribute:: projection .. py:method:: forward(inputs: Tuple[torch.Tensor, Ellipsis]) -> Tuple[torch.Tensor, Ellipsis] .. py:class:: Autoformer(window_size: int, input_dim: int, moving_avg: int = 25, model_dim: int = 128, dropout: float = 0.1, attention_factor: int = 1, num_heads: int = 8, fcn_dim: int = 128, activation: str = 'gelu', encoder_layers: int = 3) Bases: :py:obj:`timesead.models.BaseModel` Autoformer is the first method to achieve the series-wise connection, with inherent O(LlogL) complexity Paper link: https://openreview.net/pdf?id=I55UqU-M11y Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: seq_len .. py:attribute:: decomp .. py:attribute:: enc_embedding .. py:attribute:: encoder .. py:attribute:: projection .. py:method:: forward(inputs: Tuple[torch.Tensor, Ellipsis]) -> Tuple[torch.Tensor, Ellipsis] .. py:class:: FEDformer(window_size: int, input_dim: int, moving_avg: int = 25, model_dim: int = 128, dropout: float = 0.1, num_heads: int = 8, fcn_dim: int = 128, activation: str = 'gelu', encoder_layers: int = 3, version: str = 'fourier', mode_select: str = 'random', modes: int = 32) Bases: :py:obj:`timesead.models.BaseModel` FEDformer performs the attention mechanism on frequency domain and achieved O(N) complexity Paper link: https://proceedings.mlr.press/v162/zhou22g.html version: str, for FEDformer, there are two versions to choose, options: [Fourier, Wavelets]. mode_select: str, for FEDformer, there are two mode selection method, options: [random, low]. modes: int, modes to be selected. .. py:attribute:: seq_len .. py:attribute:: version :value: 'fourier' .. py:attribute:: mode_select :value: 'random' .. py:attribute:: modes :value: 32 .. py:attribute:: decomp .. py:attribute:: enc_embedding .. py:attribute:: encoder .. py:attribute:: projection .. py:method:: forward(inputs: Tuple[torch.Tensor, Ellipsis]) -> Tuple[torch.Tensor, Ellipsis] .. py:class:: ETSformer(window_size: int, input_dim: int, model_dim: int = 128, dropout: float = 0.1, num_heads: int = 8, fcn_dim: int = 128, encoder_layers: int = 3, activation: str = 'gelu', top_k: int = 5) Bases: :py:obj:`timesead.models.BaseModel` Paper link: https://arxiv.org/abs/2202.01381 Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: seq_len .. py:attribute:: enc_embedding .. py:attribute:: encoder .. py:attribute:: decoder .. py:method:: forward(inputs: Tuple[torch.Tensor, Ellipsis]) -> Tuple[torch.Tensor, Ellipsis]