Skip to content

RandomNodeLoader

RandomNodeLoader(data: GraphData, num_parts: int, shuffle: bool = False)

Splits the nodes of a graph into num_parts random partitions and yields the subgraph induced by each partition, following PyG's RandomNodeLoader.

Useful to train on large graphs that do not fit in memory, when the loss of the edges between partitions is acceptable. With shuffle=True new random partitions are drawn at each epoch, otherwise the partitions are contiguous ranges of node ids.

Each batch is a GraphData with node- and edge-level attributes sliced to the subgraph, plus n_id (global id of every node) and e_id (original id of every edge).

Parameters:

Name Type Description Default
data GraphData

The full graph.

required
num_parts int

Number of partitions (batches per epoch).

required
shuffle bool

Whether to draw new random partitions at each epoch. Defaults to False

False

Example:

from mlx_graphs.loaders import RandomNodeLoader

loader = RandomNodeLoader(data, num_parts=10, shuffle=True)
for batch in loader:
    out = model(batch.node_features, batch.edge_index)
Source code in mlx_graphs/loaders/random_node_loader.py
def __init__(self, data: GraphData, num_parts: int, shuffle: bool = False):
    if num_parts < 1:
        raise ValueError(f"`num_parts` must be positive (got {num_parts})")
    self.data = data
    self.num_parts = num_parts
    self.shuffle = shuffle
    self.num_nodes = data.num_nodes
    self.num_edges = data.num_edges
    self.batch_size = max(1, math.ceil(self.num_nodes / num_parts))
    self._adj = Adjacency(data.edge_index, self.num_nodes)
    self._perm = np.arange(self.num_nodes)
    self._current_index = 0

from mlx_graphs.loaders import RandomNodeLoader · mlx_graphs/loaders/random_node_loader.py