HGTConv
¶
HGTConv(
node_features_dim: Union[int, Dict[str, int]],
out_features_dim: int,
metadata: Metadata,
heads: int = 1,
)
Bases: Module
The Heterogeneous Graph Transformer (HGT) operator from the "Heterogeneous Graph Transformer" paper.
Every node type \(\tau\) has its own key, query and value projections \(\mathbf{K}_\tau, \mathbf{Q}_\tau, \mathbf{V}_\tau\), and every edge type \(\phi = (s, r, d)\) its own per-head key and value matrices \(\mathbf{W}^{\textrm{att}}_{\phi}, \mathbf{W}^{\textrm{msg}}_{\phi}\) and prior \(\mu_{\phi}\). For each head, the attention of a node \(i\) over all of its neighbors \(j\) (of any edge type) is
and the messages \(\mathbf{V}_{s} \mathbf{x}_j \mathbf{W}^{\textrm{msg}}_{\phi(j,i)}\) are summed with these weights. The heads are concatenated and passed through a GELU and a node-type specific linear layer. When the input and output sizes match, the result is mixed with the input with a learned gate \(\sigma(s_\tau)\).
Node and edge types are given as in HeteroConv: node features as a
dict keyed by node type and edge indices as a dict keyed by
(src, rel, dst) tuples. Only node types that are the destination of an
edge type of metadata are in the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
Union[int, Dict[str, int]]
|
Size of input node features, the same for every node type, or a dict mapping each node type to its size |
required |
out_features_dim
|
int
|
Size of output node embeddings. Must be divisible by
|
required |
metadata
|
Metadata
|
The node types and the |
required |
heads
|
int
|
Number of attention heads. Default: |
1
|
Example:
import mlx.core as mx
from mlx_graphs.nn import HGTConv
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]]),
}
metadata = (list(x_dict), list(edge_index_dict))
conv = HGTConv({"author": 16, "paper": 8}, 32, metadata, heads=4)
out = conv(x_dict, edge_index_dict)
>>> {k: v.shape for k, v in out.items()}
{'author': (4, 32), 'paper': (5, 32)}
References
Methods:
| Name | Description |
|---|---|
__call__ |
Computes new node embeddings for every destination node type. |
Source code in mlx_graphs/nn/conv/hgt_conv.py
__call__
¶
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 |
Returns:
| Type | Description |
|---|---|
Dict[str, array]
|
Node embeddings per destination node type. |
Source code in mlx_graphs/nn/conv/hgt_conv.py
from mlx_graphs.nn import HGTConv · mlx_graphs/nn/conv/hgt_conv.py