HANConv
¶
HANConv(
node_features_dim: Union[int, Dict[str, int]],
out_features_dim: int,
metadata: Metadata,
heads: int = 1,
negative_slope: float = 0.2,
dropout: float = 0.0,
)
Bases: Module
The Heterogeneous Graph Attention Operator from the "Heterogenous Graph Attention Network" paper.
Node features of every node type are first projected to
out_features_dim features. Then, for every edge type
\(\Phi = (s, r, d)\), a node-level multi-head attention (as in GAT, with
attention vectors specific to the edge type) aggregates the neighbors of
each node of type \(d\), followed by a ReLU:
Finally, a semantic-level attention combines the embeddings of all the edge types pointing to the same node type \(d\):
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. Node types that no edge type points to are not
in the output (PyG maps them to None).
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
|
negative_slope
|
float
|
Slope of the LeakyReLU of the node-level attention.
Default: |
0.2
|
dropout
|
float
|
Dropout probability of the node-level attention coefficients.
Default: |
0.0
|
Example:
import mlx.core as mx
from mlx_graphs.nn import HANConv
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 = HANConv({"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)}
Methods:
| Name | Description |
|---|---|
__call__ |
Computes new node embeddings for every destination node type. |
Source code in mlx_graphs/nn/conv/han_conv.py
__call__
¶
__call__(
x_dict: Dict[str, array],
edge_index_dict: Dict[EdgeType, array],
return_semantic_attention_weights: bool = False,
) -> Union[Dict[str, array], Tuple[Dict[str, array], 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 |
return_semantic_attention_weights
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, array], Tuple[Dict[str, array], Dict[str, array]]]
|
Node embeddings per destination node type, and the semantic |
Union[Dict[str, array], Tuple[Dict[str, array], Dict[str, array]]]
|
attention weights if |
Union[Dict[str, array], Tuple[Dict[str, array], Dict[str, array]]]
|
|
Source code in mlx_graphs/nn/conv/han_conv.py
from mlx_graphs.nn import HANConv · mlx_graphs/nn/conv/han_conv.py