RelGraphConvLayer

class graphstorm.model.RelGraphConvLayer(in_feat, out_feat, rel_names, num_bases, *, edge_feat_name=None, edge_feat_mp_op='concat', weight=True, bias=True, activation=None, self_loop=True, dropout=0.0, num_ffn_layers_in_gnn=0, ffn_activation=<function relu>, norm=None)

Bases: Module

Relational graph convolution layer from Modeling Relational Data with Graph Convolutional Networks.

A generic module for computing convolution on heterogeneous graphs.

The relational graph convolution layer applies GraphConv on the heterogeneous graphs, which reads the features from source nodes and writes the updated ones to destination nodes. If multiple relations have the same destination node types, their results are aggregated by the specified method. If the heterogeneous graph has no edge, the corresponding module will not be called.

Mathematically for the GraphConv it is defined as follows:

\[h_i^{(l+1)} = \sigma(b^{(l)} + \sum_{j\in\mathcal{N}(i)}\frac{1}{c_{ji}}h_j^{(l)}W^{(l)})\]

where \(\mathcal{N}(i)\) is the set of neighbors of node \(i\), \(c_{ji}\) is the product of the square root of node degrees (i.e., \(c_{ji} = \sqrt{|\mathcal{N}(j)|}\sqrt{|\mathcal{N}(i)|}\)), and \(\sigma\) is an activation function.

Note

The implementation of RelGraphConvLayer selects right as the norm, which divides the aggregated messages by each node’s in-degrees, equivalent to averaging the received messages.

Examples:

# suppose graph and input_feature are ready
from graphstorm.model import RelGraphConvLayer

layer = RelGraphConvLayer(
        in_feat=h_dim, out_feat=h_dim, rel_names=g.canonical_etypes,
        edge_feat_name, edge_feat_mp_op,
        num_bases=num_bases, self_loop,
        dropout, num_ffn_layers_in_gnn,
        ffn_activation, norm)
h = layer(g, input_feature)

Changed in version 0.4.0: Add two new arguments edge_feat_name and edge_feat_mp_op in v0.4.0 to support edge features in RGCN conv layer.

Parameters

in_feat: int

Input feature size.

out_feat: int

Output feature size.

rel_names: list of tuple

Relation type list in the format of [(‘src_ntyp1’, ‘etype1’, ‘dst_ntype1’), …].

num_bases: int

Number of bases. If is None, use number of relation types. Default: None.

edge_feat_name: dict of list of str

User provided edge feature names in the format of {etype1:[feat1, feat2, …], etype2:[…], …}, or None if not provided.

edge_feat_mp_op: str

The operation method to combine source node embeddings with edge embeddings in message passing. Options include concat, add, sub, mul, and div. concat operation will concatenate the source node features with edge features; add operation will add the source node features with edge features together; sub operation will subtract the source node features by edge features; mul operation will multiply the source node features with edge features; and div operation will divide the source node features by edge features.

weight: bool

Whether to apply a linear layer after message passing. Default: True.

bias: bool

Whether to add bias. Default: True.

activation: callable

Activation function. Default: None.

self_loop: bool

Whether to include self loop message. Default: True.

dropout: float

Dropout rate. Default: 0.

num_ffn_layers_in_gnn: int

Number of fnn layers between gnn layers. Default: 0.

ffn_activation: torch.nn.functional

Activation for ffn. Default: relu.

norm: str

Normalization methods. Options:batch, layer, and None. Default: None, meaning no normalization.

forward(g, n_h, e_h=None)

RGCN layer forward computation.

Parameters

g: DGLHeteroGraph

Input DGL heterogenous graph.

n_h: dict of Tensor

Node features for each node type in the format of {ntype: tensor}.

e_h: dict of Tensor

edge features for each edge type in the format of {etype: tensor}. Default is None.

Returns

dict of Tensor: New node embeddings for each node type in the format of {ntype: tensor}.

Changed in version 0.4.0: Change inputs into n_h and e_h in v0.4.0 to support edge feature in RGCN layer.