ShaDowKHopSampler
¶
ShaDowKHopSampler(
data: GraphData,
depth: int,
num_neighbors: int,
node_idx: Optional[Union[array, ndarray]] = None,
replace: bool = False,
batch_size: int = 1,
shuffle: bool = False,
)
The ShaDow \(k\)-hop sampler from the "Decoupling the Depth and Scope of Graph Neural Networks"
paper, following PyG's ShaDowKHopSampler.
For every seed (root) node, num_neighbors incoming neighbors are
sampled per node and per hop, for depth hops, and the subgraph
induced by the sampled nodes is extracted. The subgraphs of the
batch_size roots of a mini-batch are kept separate (a node may appear
in several of them) and returned as one block-diagonal
GraphData:
edge_index, node- and edge-level attributes of all subgraphs, with the nodes of each subgraph sorted by global id;batch_indices: subgraph of every node (PyG'sbatch), to pool one representation per subgraph;ptr: start of every subgraph in the node list,[batch_size + 1];root_n_id: position of every root node in the node list;n_id/e_id: global node ids and original edge ids;node_labels: like PyG'sy, the labels of the roots only ([batch_size, ...]), whendata.node_labelsis node-level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GraphData
|
The full graph. |
required |
depth
|
int
|
Number of hops of each subgraph. |
required |
num_neighbors
|
int
|
Number of neighbors sampled per node and per hop.
|
required |
node_idx
|
Optional[Union[array, ndarray]]
|
Root nodes to iterate over, as indices or a boolean mask. Defaults to None (all nodes) |
None
|
replace
|
bool
|
Whether to sample neighbors with replacement. Defaults to False |
False
|
batch_size
|
int
|
Number of roots (subgraphs) per mini-batch. Defaults to 1 |
1
|
shuffle
|
bool
|
Whether to shuffle the roots at each epoch. Defaults to False |
False
|
Example:
from mlx_graphs.loaders import ShaDowKHopSampler
loader = ShaDowKHopSampler(
data, depth=2, num_neighbors=5, node_idx=data.train_mask, batch_size=64
)
for batch in loader:
h = model(batch.node_features, batch.edge_index)
root_h = h[batch.root_n_id]
loss = loss_fn(classifier(root_h), batch.node_labels)
Methods:
| Name | Description |
|---|---|
sample |
Builds the batch of the subgraphs around |
Source code in mlx_graphs/loaders/shadow.py
sample
¶
sample(roots: ndarray) -> GraphData
Builds the batch of the subgraphs around roots (global ids).
Source code in mlx_graphs/loaders/shadow.py
from mlx_graphs.loaders import ShaDowKHopSampler · mlx_graphs/loaders/shadow.py