SAGEConv
¶
SAGEConv(
node_features_dim: Union[int, Tuple[int, int]],
out_features_dim: int,
bias: bool = True,
root_weight: bool = True,
**kwargs,
)
Bases: MessagePassing
GraphSAGE convolution layer from "Inductive Representation Learning on Large Graphs" paper.
where \(\mathbf{x}_i\) represents the input features of node \(i\),
\(\bigoplus\) denotes the aggregation function, set by default to mean and
\(\mathbf{h}_i\) is the computed embedding of node \(i\).
The layer also works on bipartite graphs, where messages flow from one set
of nodes to another (e.g. author -> paper in a heterogeneous graph):
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 use bias in the node projection |
True
|
root_weight
|
bool
|
Whether to add the projection \(\mathbf{W}_1
\mathbf{x}_i\) of the node's own features, as in PyG. Without it,
the bias (if any) is added to the neighbor term. Setting
|
True
|
**kwargs
|
Arguments of |
{}
|
Example:
import mlx.core as mx
from mlx_graphs.data.data import GraphData
from mlx_graphs.nn import SAGEConv
graph = GraphData(
edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]]),
node_features = mx.ones((5, 16)),
)
conv = SAGEConv(16, 32)
h = conv(graph.edge_index, graph.node_features)
>>> h
array([[1.65429, -0.376169, 1.04172, ..., -0.919106, 1.42576, 0.490938],
[1.65429, -0.376169, 1.04172, ..., -0.919106, 1.42576, 0.490938],
[1.05823, -0.295776, 0.075439, ..., -0.104383, 0.031947, -0.351897],
[1.65429, -0.376169, 1.04172, ..., -0.919106, 1.42576, 0.490938],
[1.05823, -0.295776, 0.075439, ..., -0.104383, 0.031947, -0.351897]],
dtype=float32)
# Bipartite: 5 authors (16 features) -> 2 papers (8 features)
conv = SAGEConv((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 SAGEConv. |
Attributes:
| Name | Type | Description |
|---|---|---|
root_weight |
bool
|
Whether the projection of the node's own features is added. |
Source code in mlx_graphs/nn/conv/sage_conv.py
root_weight
property
writable
¶
root_weight: bool
Whether the projection of the node's own features is added.
__call__
¶
__call__(
edge_index: array,
node_features: Union[array, Tuple[array, array]],
edge_weights: Optional[array] = None,
) -> array
Computes the forward pass of SAGEConv.
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]
|
Edge weights leveraged in message passing. Default: |
None
|
Returns:
| Type | Description |
|---|---|
array
|
The computed node embeddings, one row per destination node |
Source code in mlx_graphs/nn/conv/sage_conv.py
from mlx_graphs.nn import SAGEConv · mlx_graphs/nn/conv/sage_conv.py