GraphConv
¶
GraphConv(
node_features_dim: Union[int, Tuple[int, int]],
out_features_dim: int,
bias: bool = True,
**kwargs,
)
Bases: MessagePassing
Graph convolution layer from "Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks" paper.
where \(\mathbf{x}_i\) represents the input features of node \(i\),
\(e_{j,i}\) is the weight of the edge from node \(j\) to node
\(i\) (1 if no weights are given), \(\bigoplus\) denotes the
aggregation function, set by default to add and \(\mathbf{h}_i\) is
the computed embedding of node \(i\).
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.
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 |
bias
|
bool
|
Whether to add a bias to the output |
True
|
**kwargs
|
Arguments of |
{}
|
Example:
import mlx.core as mx
from mlx_graphs.nn import GraphConv
edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]])
node_features = mx.ones((5, 16))
conv = GraphConv(16, 32)
h = conv(edge_index, node_features)
>>> h.shape
(5, 32)
# Weighted edges and another aggregation
conv = GraphConv(16, 32, aggr="mean")
h = conv(edge_index, node_features, edge_weights=mx.ones((5,)))
# Bipartite: 5 authors (16 features) -> 2 papers (8 features)
conv = GraphConv((16, 8), 32)
edge_index = mx.array([[0, 1, 4], [0, 0, 1]]) # author -> paper
h_paper = conv(edge_index, (mx.ones((5, 16)), mx.ones((2, 8))))
>>> h_paper.shape
(2, 32)
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the forward pass of GraphConv. |
Source code in mlx_graphs/nn/conv/graph_conv.py
__call__
¶
__call__(
edge_index: array,
node_features: Union[array, Tuple[array, array]],
edge_weights: Optional[array] = None,
) -> array
Computes the forward pass of GraphConv.
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_weights
|
Optional[array]
|
Optional scalar weight per edge, of shape |
None
|
Returns:
| Type | Description |
|---|---|
array
|
The computed node embeddings, one row per destination node |
Source code in mlx_graphs/nn/conv/graph_conv.py
from mlx_graphs.nn import GraphConv · mlx_graphs/nn/conv/graph_conv.py