timesead.models.prediction.gdn ============================== .. py:module:: timesead.models.prediction.gdn Classes ------- .. autoapisummary:: timesead.models.prediction.gdn.GraphLayer timesead.models.prediction.gdn.OutLayer timesead.models.prediction.gdn.GNNLayer timesead.models.prediction.gdn.GDN timesead.models.prediction.gdn.GDNAnomalyDetector Functions --------- .. autoapisummary:: timesead.models.prediction.gdn.build_fc_edge_index timesead.models.prediction.gdn.get_batch_edge_index timesead.models.prediction.gdn.fallback_knn_graph Module Contents --------------- .. py:class:: GraphLayer(in_channels, out_channels, heads=1, concat=True, negative_slope=0.2, dropout=0, bias=True, inter_dim=-1, **kwargs) Bases: :py:obj:`torch_geometric.nn.MessagePassing` Base class for creating message passing layers. Message passing layers follow the form .. math:: \mathbf{x}_i^{\prime} = \gamma_{\mathbf{\Theta}} \left( \mathbf{x}_i, \bigoplus_{j \in \mathcal{N}(i)} \, \phi_{\mathbf{\Theta}} \left(\mathbf{x}_i, \mathbf{x}_j,\mathbf{e}_{j,i}\right) \right), where :math:`\bigoplus` denotes a differentiable, permutation invariant function, *e.g.*, sum, mean, min, max or mul, and :math:`\gamma_{\mathbf{\Theta}}` and :math:`\phi_{\mathbf{\Theta}}` denote differentiable functions such as MLPs. See `here `__ for the accompanying tutorial. :param aggr: The aggregation scheme to use, *e.g.*, :obj:`"sum"` :obj:`"mean"`, :obj:`"min"`, :obj:`"max"` or :obj:`"mul"`. In addition, can be any :class:`~torch_geometric.nn.aggr.Aggregation` module (or any string that automatically resolves to it). If given as a list, will make use of multiple aggregations in which different outputs will get concatenated in the last dimension. If set to :obj:`None`, the :class:`MessagePassing` instantiation is expected to implement its own aggregation logic via :meth:`aggregate`. (default: :obj:`"add"`) :type aggr: str or [str] or Aggregation, optional :param aggr_kwargs: Arguments passed to the respective aggregation function in case it gets automatically resolved. (default: :obj:`None`) :type aggr_kwargs: Dict[str, Any], optional :param flow: The flow direction of message passing (:obj:`"source_to_target"` or :obj:`"target_to_source"`). (default: :obj:`"source_to_target"`) :type flow: str, optional :param node_dim: The axis along which to propagate. (default: :obj:`-2`) :type node_dim: int, optional :param decomposed_layers: The number of feature decomposition layers, as introduced in the `"Optimizing Memory Efficiency of Graph Neural Networks on Edge Computing Platforms" `_ paper. Feature decomposition reduces the peak memory usage by slicing the feature dimensions into separated feature decomposition layers during GNN aggregation. This method can accelerate GNN execution on CPU-based platforms (*e.g.*, 2-3x speedup on the :class:`~torch_geometric.datasets.Reddit` dataset) for common GNN models such as :class:`~torch_geometric.nn.models.GCN`, :class:`~torch_geometric.nn.models.GraphSAGE`, :class:`~torch_geometric.nn.models.GIN`, etc. However, this method is not applicable to all GNN operators available, in particular for operators in which message computation can not easily be decomposed, *e.g.* in attention-based GNNs. The selection of the optimal value of :obj:`decomposed_layers` depends both on the specific graph dataset and available hardware resources. A value of :obj:`2` is suitable in most cases. Although the peak memory usage is directly associated with the granularity of feature decomposition, the same is not necessarily true for execution speedups. (default: :obj:`1`) :type decomposed_layers: int, optional Initialize internal Module state, shared by both nn.Module and ScriptModule. .. py:attribute:: in_channels .. py:attribute:: out_channels .. py:attribute:: heads :value: 1 .. py:attribute:: concat :value: True .. py:attribute:: negative_slope :value: 0.2 .. py:attribute:: dropout :value: 0 .. py:attribute:: __alpha__ :value: None .. py:attribute:: lin .. py:attribute:: att_i .. py:attribute:: att_j .. py:attribute:: att_em_i .. py:attribute:: att_em_j .. py:method:: reset_parameters() Resets all learnable parameters of the module. .. py:method:: forward(x, edge_index, embedding, return_attention_weights=False) Runs the forward pass of the module. .. py:method:: message(x_i, x_j, edge_index_i, size_i, embedding, edges, return_attention_weights) Constructs messages from node :math:`j` to node :math:`i` in analogy to :math:`\phi_{\mathbf{\Theta}}` for each edge in :obj:`edge_index`. This function can take any argument as input which was initially passed to :meth:`propagate`. Furthermore, tensors passed to :meth:`propagate` can be mapped to the respective nodes :math:`i` and :math:`j` by appending :obj:`_i` or :obj:`_j` to the variable name, *.e.g.* :obj:`x_i` and :obj:`x_j`. .. py:method:: __repr__() .. py:function:: build_fc_edge_index(num_nodes: int) -> torch.Tensor .. py:function:: get_batch_edge_index(org_edge_index, batch_num, node_num) .. py:class:: OutLayer(in_num: int, hidden_dims: Sequence[int] = (512, )) Bases: :py:obj:`torch.nn.Module` 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:: mlp .. py:method:: forward(x) .. py:class:: GNNLayer(in_channel, out_channel, inter_dim=0, heads=1, node_num=100) Bases: :py:obj:`torch.nn.Module` 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:: gnn .. py:attribute:: bn .. py:attribute:: relu .. py:attribute:: leaky_relu .. py:method:: forward(x, edge_index, embedding=None, node_num=0) .. py:function:: fallback_knn_graph(embedding: torch.Tensor, k: int) .. py:class:: GDN(node_num: int, input_dim: int, dim=64, out_layer_hidden_dims: Sequence[int] = (64, ), topk=15, dropout_prob: float = 0.2) 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 (Comments by Tobias) Terminology is a bit confusing here, so I'll add some explanations. :param node_num: This is the number of features in the dataset, i.e., D! :param input_dim: This is the length of a TS window, i.e, T! :param dim: The dimensionality of the embedding. :param out_layer_hidden_dims: Hidden dimensions for fully connected output layer :param topk: Number of edges that should be kept in the graph construction. .. py:attribute:: edge_index_sets .. py:attribute:: embedding .. py:attribute:: bn_outlayer_in .. py:attribute:: gnn_layers .. py:attribute:: node_embedding :value: None .. py:attribute:: topk :value: 15 .. py:attribute:: learned_graph :value: None .. py:attribute:: out_layer .. py:attribute:: cache_edge_index_sets :value: [None] .. py:attribute:: cache_embed_index :value: None .. py:attribute:: dp .. py:method:: init_params() .. py:method:: forward(inputs: Tuple[torch.Tensor, Ellipsis]) -> torch.Tensor .. py:class:: GDNAnomalyDetector(model: GDN, epsilon: float = 1e-07) Bases: :py:obj:`timesead.models.common.PredictionAnomalyDetector` 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:: epsilon :value: 1e-07 .. 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:: 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