timesead.models.prediction.gdn

Classes

GraphLayer

Base class for creating message passing layers.

OutLayer

Base class for all neural network modules.

GNNLayer

Base class for all neural network modules.

GDN

Base class for all neural network modules.

GDNAnomalyDetector

Base class for all neural network modules.

Functions

build_fc_edge_index(→ torch.Tensor)

get_batch_edge_index(org_edge_index, batch_num, node_num)

fallback_knn_graph(embedding, k)

Module Contents

class timesead.models.prediction.gdn.GraphLayer(in_channels, out_channels, heads=1, concat=True, negative_slope=0.2, dropout=0, bias=True, inter_dim=-1, **kwargs)

Bases: torch_geometric.nn.MessagePassing

Base class for creating message passing layers.

Message passing layers follow the form

\[\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 \(\bigoplus\) denotes a differentiable, permutation invariant function, e.g., sum, mean, min, max or mul, and \(\gamma_{\mathbf{\Theta}}\) and \(\phi_{\mathbf{\Theta}}\) denote differentiable functions such as MLPs. See here for the accompanying tutorial.

Parameters:
  • aggr (str or [str] or Aggregation, optional) – The aggregation scheme to use, e.g., "sum" "mean", "min", "max" or "mul". In addition, can be any 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 None, the MessagePassing instantiation is expected to implement its own aggregation logic via aggregate(). (default: "add")

  • aggr_kwargs (Dict[str, Any], optional) – Arguments passed to the respective aggregation function in case it gets automatically resolved. (default: None)

  • flow (str, optional) – The flow direction of message passing ("source_to_target" or "target_to_source"). (default: "source_to_target")

  • node_dim (int, optional) – The axis along which to propagate. (default: -2)

  • decomposed_layers (int, optional) – 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 Reddit dataset) for common GNN models such as GCN, GraphSAGE, 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 decomposed_layers depends both on the specific graph dataset and available hardware resources. A value of 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: 1)

Initialize internal Module state, shared by both nn.Module and ScriptModule.

in_channels
out_channels
heads = 1
concat = True
negative_slope = 0.2
dropout = 0
__alpha__ = None
lin
att_i
att_j
att_em_i
att_em_j
reset_parameters()

Resets all learnable parameters of the module.

forward(x, edge_index, embedding, return_attention_weights=False)

Runs the forward pass of the module.

message(x_i, x_j, edge_index_i, size_i, embedding, edges, return_attention_weights)

Constructs messages from node \(j\) to node \(i\) in analogy to \(\phi_{\mathbf{\Theta}}\) for each edge in edge_index. This function can take any argument as input which was initially passed to propagate(). Furthermore, tensors passed to propagate() can be mapped to the respective nodes \(i\) and \(j\) by appending _i or _j to the variable name, .e.g. x_i and x_j.

__repr__()
timesead.models.prediction.gdn.build_fc_edge_index(num_nodes: int) torch.Tensor
Parameters:

num_nodes (int)

Return type:

torch.Tensor

timesead.models.prediction.gdn.get_batch_edge_index(org_edge_index, batch_num, node_num)
class timesead.models.prediction.gdn.OutLayer(in_num: int, hidden_dims: Sequence[int] = (512,))

Bases: 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 to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • in_num (int)

  • hidden_dims (Sequence[int])

Initialize internal Module state, shared by both nn.Module and ScriptModule.

mlp
forward(x)
class timesead.models.prediction.gdn.GNNLayer(in_channel, out_channel, inter_dim=0, heads=1, node_num=100)

Bases: 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 to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

gnn
bn
relu
leaky_relu
forward(x, edge_index, embedding=None, node_num=0)
timesead.models.prediction.gdn.fallback_knn_graph(embedding: torch.Tensor, k: int)
Parameters:
class timesead.models.prediction.gdn.GDN(node_num: int, input_dim: int, dim=64, out_layer_hidden_dims: Sequence[int] = (64,), topk=15, dropout_prob: float = 0.2)

Bases: 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 to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • node_num (int)

  • input_dim (int)

  • out_layer_hidden_dims (Sequence[int])

  • dropout_prob (float)

(Comments by Tobias) Terminology is a bit confusing here, so I’ll add some explanations.

Parameters:
  • node_num (int) – This is the number of features in the dataset, i.e., D!

  • input_dim (int) – This is the length of a TS window, i.e, T!

  • dim – The dimensionality of the embedding.

  • out_layer_hidden_dims (Sequence[int]) – Hidden dimensions for fully connected output layer

  • topk – Number of edges that should be kept in the graph construction.

  • dropout_prob (float)

edge_index_sets
embedding
bn_outlayer_in
gnn_layers
node_embedding = None
topk = 15
learned_graph = None
out_layer
cache_edge_index_sets = [None]
cache_embed_index = None
dp
init_params()
forward(inputs: Tuple[torch.Tensor, Ellipsis]) torch.Tensor
Parameters:

inputs (Tuple[torch.Tensor, Ellipsis])

Return type:

torch.Tensor

class timesead.models.prediction.gdn.GDNAnomalyDetector(model: GDN, epsilon: float = 1e-07)

Bases: 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 to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:

Initialize internal Module state, shared by both nn.Module and ScriptModule.

model
epsilon = 1e-07
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.

Parameters:

inputs (Tuple[torch.Tensor, Ellipsis]) – tuple of input tensors

Returns:

Tensor of shape (B,) that contains the anomaly scores for this batch

Return type:

torch.Tensor

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.

Parameters:

inputs (Tuple[torch.Tensor, Ellipsis]) – tuple of input tensors

Returns:

Tensor of shape (N,) that contains the anomaly scores for this batch

Return type:

torch.Tensor

fit(dataset: torch.utils.data.DataLoader) None

Fit this anomaly detector on a dataset. Note that we assume only normal data here.

Parameters:

dataset (torch.utils.data.DataLoader) – A dataset

Return type:

None

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.

Parameters:

targets (Tuple[torch.Tensor, Ellipsis]) – tuple of target tensors

Returns:

Tensor of shape (B,) that contains the ground truth labels for this batch

Return type:

torch.Tensor