LinkNeighborLoader
¶
LinkNeighborLoader(
data: Union[GraphData, HeteroGraphData],
num_neighbors: Union[list[int], dict[EdgeType, list[int]]],
edge_label_index: Any = None,
edge_label: Optional[array] = None,
neg_sampling: Union[None, str, dict, NegativeSampling] = None,
neg_sampling_ratio: Optional[float] = None,
batch_size: int = 1,
shuffle: bool = False,
replace: bool = False,
directed: bool = True,
sampler: Optional[Iterable[int]] = None,
edge_label_time: Optional[array] = None,
time_attr: Optional[str] = None,
temporal_strategy: str = "uniform",
disjoint: bool = False,
)
Mini-batch loader for link-level tasks (e.g. link prediction) on a
single large graph, following PyG's LinkNeighborLoader.
Iterates over the supervision edges edge_label_index. For each batch
of edges (plus optional negative samples), the source and destination
nodes are used as seeds of a neighbor sampling (as in
NeighborLoader), and the sampled subgraph is returned with the
supervision edges relabeled to local node ids.
Each batch holds, besides the sliced subgraph:
n_id/e_id: global ids of the sampled nodes and original ids of the sampled edges (n_id_dict/e_id_dictfor heterogeneous graphs). The seed nodes come first inn_id.input_id: positions inedge_label_indexof the positive edges of the batch.- Without negative sampling, or with
"binary"negative sampling:edge_label_index([2, num_pos + num_neg], positives first, as local node ids) andedge_label(1for positives and0for negatives whenedge_labelis not given; whenedge_labelis given and contains0, it is incremented by one so that0means "negative"). - With
"triplet"negative sampling:src_index,dst_pos_index([batch_size]) anddst_neg_index([batch_size]or[batch_size, amount]), as local node ids. - For heterogeneous graphs:
input_type, the supervised edge type.edge_label_indexrow 0 indexes nodes of its source type, row 1 nodes of its destination type.
Supervision edges are not removed from the message-passing graph.
Temporal sampling (homogeneous graphs), as in PyG: with
time_attr and edge_label_time, the seeds of every supervision
edge (and of its negatives) only sample neighbors whose timestamp is not
later than the edge's time, and negatives are drawn among nodes whose
timestamp is not later either (node-level time_attr). Every seed
then gets its own subgraph (disjoint=True): the sources come first,
then the destinations, without merging, so edge_label_index is
arange(2 * num_edges).reshape(2, -1) and batch.batch holds, for
every node, the position in the mini-batch of its supervision edge.
Mini-batches also hold edge_label_time (seed_time for
"triplet" negative sampling).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Union[GraphData, HeteroGraphData]
|
The full graph, a |
required |
num_neighbors
|
Union[list[int], dict[EdgeType, list[int]]]
|
Number of neighbors to sample per hop (see
|
required |
edge_label_index
|
Any
|
The supervision edges. For a |
None
|
edge_label
|
Optional[array]
|
Optional label of every supervision edge. |
None
|
neg_sampling
|
Union[None, str, dict, NegativeSampling]
|
Negative sampling configuration: a
|
None
|
neg_sampling_ratio
|
Optional[float]
|
Shortcut for
|
None
|
batch_size
|
int
|
Number of supervision edges per mini-batch. |
1
|
shuffle
|
bool
|
Whether to shuffle the supervision edges at 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 positions in |
None
|
edge_label_time
|
Optional[array]
|
Timestamp of every supervision edge (requires
|
None
|
time_attr
|
Optional[str]
|
Name of the node-level or edge-level timestamp attribute
of |
None
|
temporal_strategy
|
str
|
|
'uniform'
|
disjoint
|
bool
|
Whether every seed gets its own subgraph (always the case
with |
False
|
Example:
from mlx_graphs.loaders import LinkNeighborLoader
loader = LinkNeighborLoader(
data,
num_neighbors=[10, 5],
edge_label_index=train_edge_index,
neg_sampling_ratio=1.0,
batch_size=256,
shuffle=True,
)
for batch in loader:
h = model(batch.node_features, batch.edge_index)
src, dst = batch.edge_label_index
logits = (h[src] * h[dst]).sum(-1)
loss = bce(logits, batch.edge_label)
Attributes:
| Name | Type | Description |
|---|---|---|
edge_label_index |
ndarray
|
The supervision edges (global node ids). |
edge_label |
Optional[array]
|
The labels of the supervision edges (incremented, see above). |
Source code in mlx_graphs/loaders/link_neighbor_loader.py
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
from mlx_graphs.loaders import LinkNeighborLoader · mlx_graphs/loaders/link_neighbor_loader.py