Skip to main content

Fast temporal negative edge sampler for graph neural networks

Project description

Temporal Negative Edge Sampler

PyPI Latest Release PyPI Downloads

The Problem:

Poursafaei et al. in NeurIPS 2022 described that random negative edges make temporal link prediction task easy. This is because random negatives are often between unrelated node pairs that have never interacted, making them trivially easy for models to distinguish from true positive edges. So they introduced the concept of historical negatives—edges that existed in the past but are inactive at the current time—making the prediction task more realistic and challenging.

Huang et al. in NeurIPS 2023 created a benchmark based on this called TGB (Temporal Graph Benchmark) where in the validation and test sets they included both historical and random negatives.

  • Historical negatives: Edges that existed in the past but are inactive at the current time between a pair of nodes.
  • Random negatives: Edges that never existed in the past between a pair of nodes.

TGB generated 10-20 negative edges per positive edge where 50% is historical and 50% is random.

However, they only released pre-generated pickle files with negatives for their benchmark datasets, and did not publish code to generate such negatives for new datasets or for training-time augmentation.

Negative edges are generated using the following steps:

  1. Group all edges by timestamp, sorted in chronological order.

  2. Iterate through each timestamp group (i.e., a batch of edges sharing the same timestamp):

    • Add the current batch to a streaming graph structure, which keeps track of all previously seen edges (the historical graph).
  3. For each source node in the current batch:

    • Sample n × p historical negative edges:
      • These are destination nodes that have been connected to the source in the past, but are not connected to the source in the current timestamp.
    • Sample n × (1 - p) random negative edges:
      • These are destination nodes that have never been connected to the source in the historical graph (including the current batch).

While the algorithm described above is correct and benchmark-aligned, it is computationally expensive if implemented naively in Python. A naive Python implementation will not scale to large datasets and may run indefinitely for even moderate-sized graphs.

Temporal Negative Edge Sampler is a high-performance library for generating negative edges in temporal link prediction tasks, supporting both historical and random negative sampling. It mirrors the evaluation strategy used in the Temporal Graph Benchmark (TGB), but extends it to training and custom datasets. The core implementation is written in parallelized C++ with Python bindings, making it scalable to large graphs with millions of edges.

API

collect_all_negatives_by_timestamp

Collects negative edges for temporal graph sampling, processing edges grouped by timestamp.

Signature

from temporal_negative_edge_sampler import collect_all_negatives_by_timestamp

collect_all_negatives_by_timestamp(
    sources: np.ndarray,
    targets: np.ndarray,
    timestamps: np.ndarray,
    is_directed: bool,
    num_negatives_per_positive: int,
    historical_negative_percentage: float = 0.5
) -> Tuple[np.ndarray, np.ndarray]

Parameters

  • sources (np.ndarray): Array of source node IDs
  • targets (np.ndarray): Array of target node IDs
  • timestamps (np.ndarray): Array of timestamps for each edge
  • is_directed (bool): Whether the graph is directed
  • num_negatives_per_positive (int): Number of negative edges to sample per positive edge
  • historical_negative_percentage (float, optional): Ratio of historical vs random negatives (default: 0.5)

References:

Poursafaei et al. (2022)
Towards Better Evaluation for Dynamic Link Prediction
Farimah Poursafaei, Shenyang Huang, Kris Pelrine, Reihaneh Rabbany
In NeurIPS 2022 Datasets and Benchmarks Track.
https://arxiv.org/pdf/2207.10128

Huang et al. (2023)
Temporal Graph Benchmark for Machine Learning on Temporal Graphs
Shenyang Huang, Farimah Poursafaei, Jacob Danovitch, Matthias Fey, Weihua Hu, Emanuele Rossi, Jure Leskovec, Michael Bronstein, Guillaume Rabusseau, Reihaneh Rabbany
In NeurIPS 2023 Datasets and Benchmarks Track.
https://arxiv.org/pdf/2307.01026

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

temporal_negative_edge_sampler-0.1.15-cp311-cp311-manylinux_2_34_x86_64.whl (214.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

temporal_negative_edge_sampler-0.1.15-cp310-cp310-manylinux_2_34_x86_64.whl (212.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

temporal_negative_edge_sampler-0.1.15-cp310-cp310-macosx_15_0_arm64.whl (73.8 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

temporal_negative_edge_sampler-0.1.15-cp39-cp39-manylinux_2_34_x86_64.whl (211.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

temporal_negative_edge_sampler-0.1.15-cp38-cp38-manylinux_2_34_x86_64.whl (211.8 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.34+ x86-64

File details

Details for the file temporal_negative_edge_sampler-0.1.15-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for temporal_negative_edge_sampler-0.1.15-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 145bb4ec9245b39ea1f38f273eecfa85f170438e6ad505b91fc43ec348494332
MD5 042d7e5fc44c3588090776fa07f47227
BLAKE2b-256 6e055e37c7a37e6208db93cbafeaec63dd28acb5531a4e660968bc2541604e3c

See more details on using hashes here.

File details

Details for the file temporal_negative_edge_sampler-0.1.15-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for temporal_negative_edge_sampler-0.1.15-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 b6a521d5079ecfad46157f6e9f2c54f011df1acc8efedf409c00c8ef26903eca
MD5 8023e7b01e40dc7f4a59a7802fc2a671
BLAKE2b-256 3fec689f41619e77594ffa07abc59e24ba98306c4fdb35580e1af4d2605af591

See more details on using hashes here.

File details

Details for the file temporal_negative_edge_sampler-0.1.15-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for temporal_negative_edge_sampler-0.1.15-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 6b2271fc88f96637ae9008283400a29018598236a476a0c27e7b2ae46ab3b9ba
MD5 06f0946f257a3718c19f30268bffcf88
BLAKE2b-256 e85c2bb3f50e440953ffafa55f6b6774965a5239198c6c0c6109bae98ccc0194

See more details on using hashes here.

File details

Details for the file temporal_negative_edge_sampler-0.1.15-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for temporal_negative_edge_sampler-0.1.15-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 1a763c171b225f55ef8ccaf1941c080ef8c079f3369d2e974ca15c7a40eb7fd3
MD5 d2bde23093feb537aa7c18d3ebbd4e5d
BLAKE2b-256 1990e3386f8990e6709c2a4b0c2eab09b73536183070bda122e122ef40254cd1

See more details on using hashes here.

File details

Details for the file temporal_negative_edge_sampler-0.1.15-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for temporal_negative_edge_sampler-0.1.15-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 c7d6e62c65c2825da99ae2481fb57e1ce19c3893c79cf39c9b6225ecdf26d295
MD5 52f9889a4ce196840b15b442195df2c6
BLAKE2b-256 e78044acc9281fb4a267793343824e8acd2cbf16f42dbf4732014f4c2cf4b203

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page