Skip to content

GraphSAINTEdgeSampler

GraphSAINTEdgeSampler(
    data: GraphData,
    batch_size: int,
    num_steps: int = 1,
    sample_coverage: int = 0,
    save_dir: Optional[str] = None,
)

Bases: GraphSAINTSampler

GraphSAINT edge sampler: samples batch_size edges without replacement with probability proportional to 1 / out_degree(source) + 1 / in_degree(target), and takes the subgraph induced by their endpoints.

Note

This follows the edge probabilities of the GraphSAINT paper (and of its reference implementation), which minimize the variance of the normalized aggregation. PyG's implementation instead uses in_degree(source) + out_degree(target), which favors the edges between high-degree nodes, and draws the batch_size edges with replacement.

See GraphSAINTSampler for the arguments and the batch content.

Example:

from mlx_graphs.loaders import GraphSAINTEdgeSampler

loader = GraphSAINTEdgeSampler(data, batch_size=4000, num_steps=5)
Source code in mlx_graphs/loaders/graph_saint.py
def __init__(
    self,
    data: GraphData,
    batch_size: int,
    num_steps: int = 1,
    sample_coverage: int = 0,
    save_dir: Optional[str] = None,
):
    if batch_size < 1:
        raise ValueError(f"`batch_size` must be positive (got {batch_size})")
    for name in ("node_norm", "edge_norm"):
        if getattr(data, name, None) is not None:
            raise ValueError(f"`data` already has a '{name}' attribute.")

    self.data = data
    self.batch_size = batch_size
    self.num_steps = num_steps
    self.sample_coverage = sample_coverage
    self.save_dir = save_dir

    self.N = data.num_nodes
    self.E = data.num_edges
    self._adj = Adjacency(data.edge_index, self.N)
    self._step = 0

    self.node_norm: Optional[np.ndarray] = None
    self.edge_norm: Optional[np.ndarray] = None
    if sample_coverage > 0:
        path = (
            os.path.join(save_dir, self._filename) if save_dir is not None else None
        )
        if path is not None and os.path.exists(path):
            saved = np.load(path)
            self.node_norm, self.edge_norm = saved["node_norm"], saved["edge_norm"]
        else:
            self.node_norm, self.edge_norm = self._compute_norm()
            if path is not None:
                os.makedirs(save_dir, exist_ok=True)  # type: ignore
                np.savez(path, node_norm=self.node_norm, edge_norm=self.edge_norm)

from mlx_graphs.loaders import GraphSAINTEdgeSampler · mlx_graphs/loaders/graph_saint.py