stgraph.nn.pytorch.static package

Submodules

stgraph.nn.pytorch.static.gat_conv module

class stgraph.nn.pytorch.static.gat_conv.GATConv(in_feats, out_feats, num_heads, feat_drop=0.0, attn_drop=0.0, negative_slope=0.2, activation=None)[source]

Bases: Module

forward(graph, feat)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reset_parameters()[source]

Reinitialize learnable parameters.

training: bool

stgraph.nn.pytorch.static.gcn_conv module

Graph Convolutional Network Layer.

class stgraph.nn.pytorch.static.gcn_conv.GCNConv(in_channels: int, out_channels: int, activation: Callable[..., torch.Tensor] | None = None, bias: bool = True)[source]

Bases: Module

Graph Convolutional Network Layer.

Vertex-centric implementation for Graph Convolutional Network (GCN) layer as described in Semi-supervised Classification with Graph Convolutional Networks.

A multi-layer GCN model has the following layer-wise propagation rule

\[H^{(l+1)} = \sigma \left( \tilde{D}^{-1/2} \tilde{A} \tilde{D}^{-1/2} H^{(l)} W^{(l)} \right)\]
  • \(H^{(l)}\): Matrix of activations in the \(l\)-th layer; \(H^{(0)} = X\) is the input feature matrix.

  • \(\sigma\): Activation function (e.g., ReLU).

  • \(\tilde{A} = A + I_N\): Adjacency matrix of the graph with added self-connections.

  • \(I_N\): Identity matrix.

  • \(\tilde{D}_{ii} = \sum_j \tilde{A}_{ij}\): Degree matrix of \(\tilde{A}\).

  • \(W^{(l)}\): Trainable weight matrix for the \(l\)-th layer.

Vertex-Centric Formula

The vertex-centric implementation can be achieved by aggregating all the features of the neighbouring nodes of the central node

\[h^{(l+1)} = \left( \sum_{\text{nb} \in \text{innbs}(v)} \text{nb}_{h^{(l)}} \cdot \text{nb}_{\text{norm}} \cdot \text{weight}_{\text{nb,v}} \right) \cdot v_{\text{norm}}\]
  • \(h^{(l)}\): Activations of central-node in the \(l\)-th layer.

  • \(\text{innbs}(v)\): In-neighbours of central-node \(v\).

  • \(\text{weight}_{\text{nb,v}}\): Weight of edge from \(nb\) to \(v\). In case no edge weights are present, it is set to 1

  • \(norm\): Node wise normalization factor, \(v_{\text{norm}} = \text{in_degrees(v)}^{-0.5}\).

Node Data

The following node data needs to be set using StaticGraph.set_ndata before calling the forward() method.

Node Property

Description

Type

norm

Node-wise normalization factor

A PyTorch Tensor of shape (num_nodes, 1), where dim=1 contains the node-wise normalization factor

Parameters:
  • in_channels (int) – Size of input sample passed into the layer

  • out_channels (int) – Size of output sample outputted by the layer

  • activation (optional) – Non-linear activation function provided by PyTorch

  • bias (bool, optional) – If set to True, learnable bias parameters are added to the layer

forward(graph: StaticGraph, h: Tensor, edge_weight: Tensor | None = None) Tensor[source]

Execute a single forward pass for the GCN layer.

Runs a single forward pass using the vertex-centric implementation of the GCN layer.

Parameters:
  • graph (StaticGraph) – A StaticGraph graph object

  • h (Tensor) – Input for the GCN forward pass

  • edge_weight (Tensor, optional) – Edge weights for each edge in the graph

Returns:

The output after executing the GCN forward pass

Return type:

Tensor

Raises:
  • KeyError – If norm n_data is not present for the graph

  • ValueError – If norm n_data passed is not of the shape (num_nodes, 1)

Example

Example usage:

# Defining a method to run forward pass with multiple GCN layers

def forward(input: Tensor, layers: List[GCNConv], graph: StaticGraph):
    h = input
    for layer in layers:
        h = layer.forward(graph, h)
    return h
reset_parameters() None[source]

Reset the learnable weight and bias parameters.

The weight parameter is initialized using a Xavier Uniform distribution. The bias parameter is initialized by setting all values to zero.

training: bool

Module contents

State-of-the-art Static Graph Neural Networks written for PyTorch backend.