HeteroConv
¶
HeteroConv(convs: dict[EdgeType, Module], aggr: HeteroAggregations = 'sum')
Bases: Module
Wrapper that runs one graph convolution per edge type of a heterogeneous graph and combines the results per destination node type.
For every edge type (src, rel, dst) the corresponding layer computes new
embeddings for the dst nodes from the src nodes. When several edge
types point to the same node type (e.g. author -> paper and
paper -> paper), their outputs are combined with aggr.
Edge types between two different node types are called with a
(src_features, dst_features) tuple, so the wrapped layer must support
bipartite input (e.g. SAGEConv, with
node_features_dim=(src_dim, dst_dim) if the sizes differ). Edge types
within one node type are called with a single array.
Node types that no edge type points to are not in the output. Edge types
missing from edge_index_dict, or whose source or destination node type
is missing from x_dict, are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
convs
|
dict[EdgeType, Module]
|
A dict mapping each edge type |
required |
aggr
|
HeteroAggregations
|
How to combine the outputs of edge types that share a
destination node type: |
'sum'
|
Example:
import mlx.core as mx
from mlx_graphs.nn import HeteroConv, SAGEConv
x_dict = {"author": mx.ones((4, 16)), "paper": mx.ones((5, 8))}
edge_index_dict = {
("author", "writes", "paper"): mx.array([[0, 1, 3], [0, 0, 4]]),
("paper", "cites", "paper"): mx.array([[1, 2], [0, 0]]),
("paper", "rev_writes", "author"): mx.array([[0, 0, 4], [0, 1, 3]]),
}
conv = HeteroConv(
{
("author", "writes", "paper"): SAGEConv((16, 8), 32),
("paper", "cites", "paper"): SAGEConv(8, 32),
("paper", "rev_writes", "author"): SAGEConv((8, 16), 32),
},
aggr="sum",
)
out = conv(x_dict, edge_index_dict)
>>> {k: v.shape for k, v in out.items()}
{'paper': (5, 32), 'author': (4, 32)}
Methods:
| Name | Description |
|---|---|
__call__ |
Computes new node embeddings for every destination node type. |
Source code in mlx_graphs/nn/conv/hetero_conv.py
__call__
¶
__call__(
x_dict: dict[str, array],
edge_index_dict: dict[EdgeType, array],
**kwargs_dict: dict[EdgeType, Any],
) -> dict[str, array]
Computes new node embeddings for every destination node type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_dict
|
dict[str, array]
|
Node features per node type. |
required |
edge_index_dict
|
dict[EdgeType, array]
|
Edge index of shape |
required |
**kwargs_dict
|
dict[EdgeType, Any]
|
Optional extra per-edge-type arguments for the wrapped
layers, passed as dicts keyed by edge type and named
|
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, array]
|
Node embeddings per destination node type. |
Source code in mlx_graphs/nn/conv/hetero_conv.py
from mlx_graphs.nn import HeteroConv · mlx_graphs/nn/conv/hetero_conv.py