RandomLinkSplit
¶
RandomLinkSplit(
num_val: Union[int, float] = 0.1,
num_test: Union[int, float] = 0.2,
is_undirected: bool = False,
add_negative_train_samples: bool = True,
neg_sampling_ratio: float = 1.0,
)
Bases: BaseTransform
Randomly splits the edges of a graph into training, validation and test sets for link prediction.
Three graphs are returned. Each one has
edge_index: the edges to use for message passing;edge_label_index: the[2, num_labels]edges to predict, positive edges first, then negative edges;edge_label: the[num_labels]float32 labels of these edges, 1 for positive (existing) edges and 0 for negative (sampled) ones.
There is no leakage between the splits, as in PyG:
- the training graph uses the training edges both for message passing and as positive edges to predict;
- the validation graph predicts the validation edges, using only the training edges for message passing;
- the test graph predicts the test edges, using the training and validation edges for message passing.
The negative edges are sampled once among the node pairs that are not
connected in the full graph (see
negative_sampling()) and shared
out between the splits, so they do not overlap either. Per-edge attributes
(e.g. edge_features) follow the message passing edges, and the number
of nodes of each graph is set explicitly to that of the input graph.
Randomness comes from mx.random (mx.random.permutation for the
split, a NumPy generator seeded from mx.random for the negative
edges), so results are reproducible with mx.random.seed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_val
|
Union[int, float]
|
Number of validation edges, or ratio of the edges if given
as a float. Default: |
0.1
|
num_test
|
Union[int, float]
|
Number of test edges, or ratio of the edges if given as a
float. Default: |
0.2
|
is_undirected
|
bool
|
If |
False
|
add_negative_train_samples
|
bool
|
Whether to add negative edges to the
training graph. Set it to |
True
|
neg_sampling_ratio
|
float
|
Number of negative edges per positive edge.
Default: |
1.0
|
Example:
graph = GraphData(
edge_index=mx.array(
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]]
),
node_features=mx.ones((10, 4)),
)
train, val, test = RandomLinkSplit(num_val=0.1, num_test=0.2)(graph)
train.edge_index.shape, train.edge_label_index.shape
# (2, 7), (2, 14)
val.edge_index.shape, val.edge_label_index.shape
# (2, 7), (2, 2)
test.edge_index.shape, test.edge_label_index.shape
# (2, 8), (2, 4)
Methods:
| Name | Description |
|---|---|
process |
Splits the edges of |
Source code in mlx_graphs/transforms/splits.py
process
¶
process(data: GraphData) -> tuple[GraphData, GraphData, GraphData]
Splits the edges of data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GraphData
|
The graph to split. |
required |
Returns:
| Type | Description |
|---|---|
tuple[GraphData, GraphData, GraphData]
|
A tuple |
Source code in mlx_graphs/transforms/splits.py
from mlx_graphs.transforms import RandomLinkSplit · mlx_graphs/transforms/splits.py