to_hetero_with_bases
¶
to_hetero_with_bases(
module: Module,
metadata: Metadata,
num_bases: int,
in_channels: Optional[dict[str, int]] = None,
input_map: Optional[dict[str, str]] = None,
input_dims: Optional[dict[NodeType, int]] = None,
) -> ToHeteroModel
Converts a homogeneous GNN model into its heterogeneous equivalent via
the basis-decomposition technique of the "Modeling Relational Data with Graph Convolutional Networks" paper
(PyG's to_hetero_with_bases).
Every message passing layer \(f_{\theta}^{(\ell)}\) is copied
num_bases times, and every edge type \(r\) gets learned
coefficients \(a^{(\ell)}_{r,b}\) (edge_type_weight). Message
passing runs on one graph made of all node and edge types:
where every message of an edge of type \(r\) is scaled by
\(a^{(\ell)}_{r,b}\) (message() of the copies is wrapped, and the
fused SpMM path is disabled for them). The number of parameters grows with
num_bases instead of with the number of edge types, which helps with
many relations. Unlike to_hetero(), the other modules (Linear,
normalizations, ...) are shared by all node types, and all node types
need the same number of features: give in_channels (with
input_dims) to project node-level inputs of every type to a common size
first, with one bias-free linear layer per node type (PyG's
LinearAlign).
The converted model is called and returns its outputs like
to_hetero()'s. Layers must not add self-loops (the messages would
not belong to any edge type) and must compute their messages with
message().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
The homogeneous model to convert (copied). |
required |
metadata
|
Metadata
|
The node types and edge types of the graph, e.g.
|
required |
num_bases
|
int
|
The number of bases (copies of every message passing layer). |
required |
in_channels
|
Optional[dict[str, int]]
|
Optional |
None
|
input_map
|
Optional[dict[str, str]]
|
Optional |
None
|
input_dims
|
Optional[dict[NodeType, int]]
|
Number of input features per node type, needed with
|
None
|
Returns:
| Type | Description |
|---|---|
ToHeteroModel
|
Example:
from mlx_graphs.nn import GraphSAGE, to_hetero_with_bases
model = to_hetero_with_bases(
GraphSAGE(32, 32, num_layers=2, out_features_dim=4),
data.metadata(),
num_bases=3,
in_channels={"node_features": 32},
input_dims=data.num_node_features,
)
out = model(data.edge_index_dict, data.node_features_dict)
References
Source code in mlx_graphs/nn/to_hetero.py
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 | |
from mlx_graphs.nn import to_hetero_with_bases · mlx_graphs/nn/to_hetero.py