HGTLoader
¶
HGTLoader(
data: HeteroGraphData,
num_samples: Union[list[int], dict[str, list[int]]],
input_nodes: Union[str, tuple[str, Optional[array]]],
batch_size: int = 1,
shuffle: bool = False,
sampler: Optional[Iterable[int]] = None,
)
Bases: _HeteroNeighborLoader
Heterogeneous mini-batch loader using the layer-wise sampling of
"Heterogeneous Graph Transformer"
(HGSampling), as PyG's HGTLoader.
Instead of a number of neighbors per node (NeighborLoader), each
hop samples a node budget per node type: every node type keeps a budget
of candidate nodes, adjacent to the nodes sampled so far. Each sampled
node adds 1 / degree to the budget of each of its (not yet sampled)
neighbors, for every edge type pointing to it (nodes with more than 50
neighbors only add 1 / 50 to 50 random ones). At hop l,
num_samples[node_type][l] nodes of each type are drawn from its budget
without replacement, with probability proportional to the squared budget,
which favours nodes connected to many sampled nodes. The mini-batch is
the subgraph induced by the sampled nodes (for destination nodes with more
than 50 neighbors, edges to 50 random ones, as in PyG).
Sampling runs on the host with NumPy (seed it with np.random.seed).
The first batch_size nodes of the seed node type are the seed nodes,
so the loss is computed on them:
for batch in loader:
out = model(batch.edge_index_dict, batch.node_features_dict)
n = batch.batch_size
loss = loss_fn(out["paper"][:n], batch.node_labels_dict["paper"][:n])
Each batch is a HeteroGraphData with the same
attributes as the batches of NeighborLoader: edge_index_dict
(local ids per node type), node_features_dict, node_labels_dict,
edge_features_dict, n_id_dict (global id of every sampled node),
e_id_dict (original position of every sampled edge), batch_size,
input_type, num_nodes_dict, num_sampled_nodes_dict (number of
nodes added at each hop, starting with the seeds), and the per-node
attributes of data (e.g. paper_train_mask) sliced to the sampled
nodes. Every node type and edge type is present, possibly empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
HeteroGraphData
|
The full graph. |
required |
num_samples
|
Union[list[int], dict[str, list[int]]]
|
Number of nodes to sample per hop, either one list for
every node type (e.g. |
required |
input_nodes
|
Union[str, tuple[str, Optional[array]]]
|
The seed node type and which of its nodes to iterate
over: |
required |
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
|
sampler
|
Optional[Iterable[int]]
|
Iterable yielding positions in |
None
|
Example:
from mlx_graphs.datasets import DBLP
from mlx_graphs.loaders import HGTLoader
data = DBLP()[0]
loader = HGTLoader(
data,
num_samples={
"author": [512] * 2,
"paper": [512] * 2,
"term": [256] * 2,
"conference": [20] * 2,
},
input_nodes=("author", data.author_train_mask),
batch_size=128,
shuffle=True,
)
batch = next(iter(loader))
Methods:
| Name | Description |
|---|---|
sample |
Samples the subgraph of a mini-batch. |
Source code in mlx_graphs/loaders/hgt_loader.py
sample
¶
Samples the subgraph of a mini-batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed_nodes
|
ndarray
|
Global ids of the (distinct) seed nodes of
|
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
as NumPy arrays
|
the sampled nodes per node type (seeds first), |
dict
|
the induced edges in local ids, their original edge positions, and |
dict
|
the number of nodes added per hop. |
Source code in mlx_graphs/loaders/hgt_loader.py
from mlx_graphs.loaders import HGTLoader · mlx_graphs/loaders/hgt_loader.py