RGCNConv
¶
RGCNConv(
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: MessagePassing
Relational graph convolution layer from "Modeling Relational Data with Graph Convolutional Networks" paper.
where \(\mathcal{R}\) is the set of relations (edge types),
\(\mathcal{N}_r(i)\) are the neighbors of node \(i\) under relation
\(r\) and \(\bigoplus\) denotes the aggregation function, set by default
to mean, i.e. \(\frac{1}{|\mathcal{N}_r(i)|} \sum_{j \in \mathcal{N}_r(i)}\).
With num_bases set, the relation weights share a small set of basis matrices
\(\mathbf{V}_b\) (basis decomposition), which reduces the number of
parameters when there are many relations:
The layer also works on bipartite graphs: pass node_features as a
(src_features, dst_features) tuple and, if the two sets have different
feature sizes, node_features_dim as a (src_dim, dst_dim) tuple. The
output has one row per destination node.
Note
Small inputs are aggregated in one shot into a dense
[num_nodes, num_relations, node_features_dim] array. When that array would
be both larger than the per-edge features and larger than
RGCNConv.dense_max_elements elements, the layer instead loops over the
relations, each time using only the edges of that relation, so memory stays
proportional to the number of edges and nodes. Both paths give the same
result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
Union[int, Tuple[int, int]]
|
Size of input node features, or a |
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
|
root_weight
|
bool
|
Whether to add the transformed features of the node itself
to the output. Default: |
True
|
bias
|
bool
|
Whether to add a learnable bias to the output. Default: |
True
|
**kwargs
|
Arguments of |
{}
|
Example:
import mlx.core as mx
from mlx_graphs.nn.conv.rgcn_conv import RGCNConv
conv = RGCNConv(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)
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the forward pass of RGCNConv. |
Source code in mlx_graphs/nn/conv/rgcn_conv.py
__call__
¶
__call__(
edge_index: array,
node_features: Union[array, Tuple[array, array]],
edge_type: array,
) -> array
Computes the forward pass of RGCNConv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Input edge index of shape |
required |
node_features
|
Union[array, Tuple[array, array]]
|
Input node features, or a |
required |
edge_type
|
array
|
Relation of each edge, integer array of shape |
required |
Returns:
| Type | Description |
|---|---|
array
|
The computed node embeddings, one row per destination node |
Source code in mlx_graphs/nn/conv/rgcn_conv.py
from mlx_graphs.nn import RGCNConv · mlx_graphs/nn/conv/rgcn_conv.py