NeighborLoader
¶
NeighborLoader(
data: Union[GraphData, HeteroGraphData, tuple],
num_neighbors: Union[list[int], dict[EdgeType, list[int]]],
input_nodes: Union[None, array, str, tuple[str, Optional[array]]] = None,
batch_size: int = 1,
shuffle: bool = False,
replace: bool = False,
directed: bool = True,
sampler: Optional[Iterable[int]] = None,
time_attr: Optional[str] = None,
input_time: Optional[array] = None,
temporal_strategy: str = "uniform",
disjoint: bool = False,
)
Mini-batch loader for node-level tasks on a single large graph.
Uses neighbor sampling to extract K-hop subgraphs around seed nodes.
Works on both homogeneous and heterogeneous graphs, following PyG's API:
the implementation is picked from the type of data.
GraphData: each iteration yields aGraphDataholding the sampled subgraph with locally re-indexed edges.HeteroGraphData: each iteration yields aHeteroGraphDatawith edges re-indexed locally per node type (sampled withmlx_cluster.hetero_neighbor_sample).- A
(FeatureStore, GraphStore)pair (PyG's remote backend, seeFeatureStore): the graph structure is read from the graph store, and only the rows of the sampled nodes (and edges) are gathered from the feature store at every iteration, so node features can stay on disk (MemmapFeatureStore). Store arrays namedx/y/edge_attrbecomenode_features/node_labels/edge_featuresof the mini-batch. For static, non-disjoint sampling, CSC adjacency stored in aMemmapGraphStoreis shared with the native sampler on MLX builds that support zero-copy host buffers.
Temporal sampling (homogeneous graphs): with time_attr, only the
neighbors whose timestamp is earlier than or equal to the time of their
seed are sampled, as in PyG. time_attr names a node-level timestamp
(the time of a neighbor is then its node's) or an edge-level one (the
time of the edge, which requires input_time). The time of a seed is
input_time if given, else its own node timestamp. As in PyG, every
seed then gets its own subgraph (disjoint=True): a node sampled for
two seeds appears twice, and batch.batch holds the position of the
seed of every node. These mini-batches also hold the per-hop counts
num_sampled_nodes / num_sampled_edges, and seed_time when
input_time is given.
The first batch_size nodes (of the seed node type, for heterogeneous
graphs) are always the seed nodes. During training, compute the loss
only on these nodes:
# homogeneous
out = model(batch.node_features, batch.edge_index)
loss = loss_fn(out[: batch.batch_size], batch.node_labels[: batch.batch_size])
# heterogeneous
out = model(batch.node_features_dict, batch.edge_index_dict)
n = batch.batch_size
loss = loss_fn(out["author"][:n], batch.node_labels_dict["author"][:n])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Union[GraphData, HeteroGraphData, tuple]
|
The full graph, a |
required |
num_neighbors
|
Union[list[int], dict[EdgeType, list[int]]]
|
Number of neighbors to sample per hop. Its length
determines the number of hops; |
required |
input_nodes
|
Union[None, array, str, tuple[str, Optional[array]]]
|
The seed nodes to iterate over.
|
None
|
batch_size
|
int
|
Number of seed nodes per mini-batch. |
1
|
shuffle
|
bool
|
Whether to shuffle the seed nodes at the start of each epoch. |
False
|
replace
|
bool
|
Whether to sample neighbors with replacement. |
False
|
directed
|
bool
|
Whether to only include edges in the sampling direction.
Only |
True
|
sampler
|
Optional[Iterable[int]]
|
Iterable yielding, for each epoch, the positions in
|
None
|
time_attr
|
Optional[str]
|
Name of the node-level or edge-level timestamp attribute
of |
None
|
input_time
|
Optional[array]
|
Optional time of every seed in |
None
|
temporal_strategy
|
str
|
|
'uniform'
|
disjoint
|
bool
|
Whether every seed gets its own subgraph (always the case
with |
False
|
Example:
from mlx_graphs.datasets import DBLP
from mlx_graphs.loaders import NeighborLoader
data = DBLP()[0]
loader = NeighborLoader(
data,
num_neighbors=[10, 5],
input_nodes=("author", data.author_train_mask),
batch_size=128,
shuffle=True,
)
batch = next(iter(loader))
Source code in mlx_graphs/loaders/neighbor_loader.py
from mlx_graphs.loaders import NeighborLoader · mlx_graphs/loaders/neighbor_loader.py