Skip to content

TGBLinkPropPredDataset

TGBLinkPropPredDataset(
    name: str,
    base_dir: Optional[str] = None,
    pre_transform: Optional[Callable] = None,
    transform: Optional[Callable] = None,
)

Bases: _TGBDataset

The dynamic link property prediction datasets of the Temporal Graph Benchmark (TGB), as py-tgb's tgb.linkproppred.dataset_pyg.PyGLinkPropPredDataset.

The dataset is a single TemporalData stream of events sorted by time, with src and dst the nodes, t the timestamps (int64), msg the edge features, w the edge weights and y all ones (the label of a true edge). The events are split chronologically at the 70% and 85% quantiles of the timestamps (train_mask, val_mask, test_mask) and every validation and test event comes with a fixed set of negative destinations (see TGBNegativeEdgeSampler; 999 per event for "tgbl-wiki", 100 for "tgbl-review"), against which it is ranked to compute the mean reciprocal rank (MRR) with the TGBEvaluator.

================= ========== =========== ========== Name #nodes #events #features ================= ========== =========== ========== "tgbl-wiki" 9,227 157,474 172 "tgbl-review" 352,637 4,873,540 1 "tgbl-coin" 638,486 22,809,486 1 "tgbl-comment" 994,790 44,314,507 3 "tgbl-flight" 18,143 67,169,570 16 ================= ========== =========== ==========

The node ids and features are those of py-tgb (the processing of each raw file is the same): "tgbl-wiki" numbers the items after the users and its features are the JODIE Wikipedia features; "tgbl-review" and "tgbl-coin" number the nodes in order of first appearance and their feature is log2 of the edge weight (w, zero weights count as 1); "tgbl-comment" has the number of words (divided by 5,000, twice) and the score of the comments; "tgbl-flight" encodes the call sign and aircraft type code as characters, and has node features (node_feat, the type, continent, region and coordinates of the airports).

Parameters:

Name Type Description Default
name str

Name of the dataset, e.g. "tgbl-wiki"

required
base_dir Optional[str]

Directory where to store dataset files. Default is in the local directory .mlx_graphs_data/.

None
pre_transform Optional[Callable]

A function/transform applied to the TemporalData after processing (it is applied every time the dataset is loaded, as the processed arrays are stored without it)

None
transform Optional[Callable]

A function/transform applied to the TemporalData at every access

None

Example:

from mlx_graphs.datasets import TGBLinkPropPredDataset
from mlx_graphs.loaders import TemporalDataLoader

dataset = TGBLinkPropPredDataset("tgbl-wiki")
data = dataset[0]
>>> TemporalData(src=[157474], dst=[157474], t=[157474],
                 msg=[157474, 172], y=[157474], w=[157474])
train_data = data[dataset.train_mask]
val_data = data[dataset.val_mask]

dataset.load_val_ns()
sampler, evaluator = dataset.negative_sampler, dataset.evaluator
for batch in TemporalDataLoader(val_data, batch_size=200):
    neg_dst, mask = sampler.query_batch_array(
        batch.src, batch.dst, batch.t, split_mode="val"
    )
    # pos_scores [B], neg_scores [B, num_neg] with -inf where ~mask
    ...
evaluator.eval({"y_pred_pos": pos_scores, "y_pred_neg": neg_scores,
                "eval_metric": ["mrr"]})

Methods:

Name Description
get_negative_sampler

Alias of negative_sampler.

load_val_ns

Loads the negative samples of the validation events.

load_test_ns

Loads the negative samples of the test events.

Attributes:

Name Type Description
negative_sampler TGBNegativeEdgeSampler

The TGBNegativeEdgeSampler of the dataset (load its

node_feat Optional[array]

Node features ("tgbl-flight" only), shape [num_nodes, 20].

min_dst_idx int

Smallest destination node id (to sample negative destinations).

max_dst_idx int

Largest destination node id (to sample negative destinations).

Source code in mlx_graphs/datasets/tgb.py
def __init__(
    self,
    name: str,
    base_dir: Optional[str] = None,
    pre_transform: Optional[Callable] = None,
    transform: Optional[Callable] = None,
):
    super().__init__(name, base_dir, pre_transform, transform)
    self._ns_sampler = TGBNegativeEdgeSampler(self.tgb_name)

negative_sampler property

negative_sampler: TGBNegativeEdgeSampler

The TGBNegativeEdgeSampler of the dataset (load its negative samples with load_val_ns() and load_test_ns()).

node_feat property

node_feat: Optional[array]

Node features ("tgbl-flight" only), shape [num_nodes, 20].

min_dst_idx property

min_dst_idx: int

Smallest destination node id (to sample negative destinations).

max_dst_idx property

max_dst_idx: int

Largest destination node id (to sample negative destinations).

get_negative_sampler

get_negative_sampler() -> TGBNegativeEdgeSampler

Alias of negative_sampler.

Source code in mlx_graphs/datasets/tgb.py
def get_negative_sampler(self) -> TGBNegativeEdgeSampler:
    """Alias of :attr:`negative_sampler`."""
    return self._ns_sampler

load_val_ns

load_val_ns()

Loads the negative samples of the validation events.

Source code in mlx_graphs/datasets/tgb.py
def load_val_ns(self):
    """Loads the negative samples of the validation events."""
    self._ns_sampler.load_eval_set(self._ns_file("val"), split_mode="val")

load_test_ns

load_test_ns()

Loads the negative samples of the test events.

Source code in mlx_graphs/datasets/tgb.py
def load_test_ns(self):
    """Loads the negative samples of the test events."""
    self._ns_sampler.load_eval_set(self._ns_file("test"), split_mode="test")

from mlx_graphs.datasets import TGBLinkPropPredDataset · mlx_graphs/datasets/tgb.py