Skip to content

FastRGCNConv

FastRGCNConv(
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    num_relations: int,
    num_bases: Optional[int] = None,
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
)

Bases: RGCNConv

Relational graph convolution layer with the same semantics as RGCNConv, from the "Modeling Relational Data with Graph Convolutional Networks" paper.

\[ \mathbf{h}_i = \mathbf{W}_{\textrm{root}} \mathbf{x}_i + \sum_{r \in \mathcal{R}} \bigoplus_{j \in \mathcal{N}_r(i)} \mathbf{W}_r \mathbf{x}_j \]

Unlike RGCNConv, which aggregates the neighbors of each relation before applying the relation weights, this layer applies the weight of its relation to every edge (one batched matmul over the edges, as PyG's FastRGCNConv). This avoids aggregating per (node, relation) pair, at the cost of gathering one [node_features_dim, out_features_dim] weight matrix per edge, so it is best suited to small graphs and feature sizes. Only the "add" and "mean" aggregations are supported.

Parameters:

Name Type Description Default
node_features_dim Union[int, Tuple[int, int]]

Size of input node features, or a (src_dim, dst_dim) tuple for bipartite graphs

required
out_features_dim int

Size of output node embeddings

required
num_relations int

Number of relations (edge types)

required
num_bases Optional[int]

If set, uses basis decomposition with this number of bases. Default: None

None
root_weight bool

Whether to add the transformed features of the node itself to the output. Default: True

True
bias bool

Whether to add a learnable bias to the output. Default: True

True
**kwargs

Arguments of MessagePassing, i.e. aggr ("mean" by default, or "add")

{}

Example:

import mlx.core as mx
from mlx_graphs.nn import FastRGCNConv

conv = FastRGCNConv(16, 32, num_relations=3, num_bases=2)

edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]])
edge_type = mx.array([0, 0, 1, 2, 2])
node_features = mx.random.uniform(low=0, high=1, shape=(5, 16))

h = conv(edge_index, node_features, edge_type)

>>> h.shape
(5, 32)
Source code in mlx_graphs/nn/conv/fast_rgcn_conv.py
def __init__(
    self,
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    num_relations: int,
    num_bases: Optional[int] = None,
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
):
    super().__init__(
        node_features_dim,
        out_features_dim,
        num_relations,
        num_bases=num_bases,
        root_weight=root_weight,
        bias=bias,
        **kwargs,
    )
    if self.aggr not in ("add", "mean"):
        raise ValueError(
            f"FastRGCNConv only supports the 'add' and 'mean' aggregations, "
            f"got '{self.aggr}'."
        )

from mlx_graphs.nn import FastRGCNConv · mlx_graphs/nn/conv/fast_rgcn_conv.py