Build negative training examples

A negative sample is a training candidate under a rejection policy, not evidence that a proposition is false. Knowledge-graph negatives replace an endpoint of a positive triple while rejecting known positives. They differ from walk-context negatives used by an embedding objective and from explicit negative epistemic claims. Start with Sampling for graph learning for compact IDs.

Reject known positives

This standalone array example contains drug-a binds protein-x and drug-b binds protein-y. It asks for one tail replacement per positive and tries other positive tails in the batch first. Both crossed pairs are absent from the snapshot and satisfy this fixture’s policy.

Standalone example. Install with python -m pip install "gestaltdb".

 1from tempfile import TemporaryDirectory
 2from gestaltdb.sampling import HardNegativeConfig, SamplerEngine, SamplerSnapshot
 3
 4with TemporaryDirectory() as root:
 5    snapshot = SamplerSnapshot.from_edge_arrays(
 6        f"{root}/snapshot",
 7        external_node_ids=["drug-a", "drug-b", "protein-x", "protein-y"],
 8        external_edge_ids=["ax", "by"], external_relation_ids=["binds"],
 9        src_int=[0, 1], dst_int=[2, 3], rel_int=[0, 0],
10    )
11    engine = SamplerEngine(snapshot, seed=7)
12    negatives, diagnostics = engine.sample_hard_negatives(
13        snapshot.positive_triples,
14        config=HardNegativeConfig(
15            negatives_per_positive=1, in_batch=True, head_probability=0.0,
16            exhaustion_policy="raise",
17        ),
18        return_diagnostics=True,
19    )
20    external = {snapshot.global_triple_to_external(triple)
21                for triple in negatives[:, 0, :]}
22    assert external == {
23        ("drug-a", "binds", "protein-y"), ("drug-b", "binds", "protein-x"),
24    }
25    assert diagnostics["accepted_in_batch"] == 2
26    known = set(map(tuple, snapshot.positive_triples))
27    assert all(tuple(triple) not in known for triple in negatives[:, 0, :])

The output shape is (positive_count, negatives_per_positive, 3) with triples (head, relation, tail) in global compact snapshot IDs. Subgraph batches instead use local endpoint rows; map through node_ids_global before interpreting them. Type compatibility is separate from a node label: snapshots use their recorded semantic type IDs (graph builds default to the kind property).

Choose the rejection and exhaustion policy

  • in_batch=True tries same-relation peer endpoints first. Normal type, availability, duplicate and positive-rejection checks still apply. Context and random fallback run when the peer pool is exhausted.

  • candidate_distribution="degree" weights only the random fallback by incident degree raised to degree_exponent. It does not reweight peer or context proposals; a graph without incident edges falls back to uniform.

  • exhaustion_policy="raise" fails when the requested accepted pool cannot be filled. "repeat" reuses an accepted candidate deterministically; it does not permit known positives or invent an acceptable candidate where none exists.

  • Known-positive rejection is relative to the selected snapshot and policy, not a complete-world truth test or automatic train/validation split.

For temporal snapshots, the default temporal_positive_policy="any_time" rejects positives anywhere in the known history. "at_positive_time" checks each example instant; "window" checks an explicit window. Direct temporal calls require aligned positive_times_us or a temporal context unless future candidates are explicitly allowed. A trailing candidate window constrains availability separately; allow_future_candidates=True is an explicit opt-in, not implied by choosing point-positive rejection. See Time-Aware Hard Negatives for exact window, diagnostic and alignment contracts, and Sample historical graph structure for the positive sampling timeline.

For Skip-gram/context negatives, use ContextNegativeSampler in Random-walk embedding pipelines. It serves a different objective and does not promise knowledge-graph positive rejection.