BasicNegativeSampler

class BasicNegativeSampler(triples_factory, num_negs_per_pos=None, filtered=False, corruption_scheme=None)[source]

Bases: pykeen.sampling.negative_sampler.NegativeSampler

A basic negative sampler.

This negative sampler that corrupts positive triples \((h,r,t) \in \mathcal{K}\) by replacing either \(h\), \(r\) or \(t\) based on the chosen corruption scheme. The corruption scheme can contain \(h\), \(r\) and \(t\) or any subset of these.

Steps:

  1. Randomly (uniformly) determine whether \(h\), \(r\) or \(t\) shall be corrupted for a positive triple \((h,r,t) \in \mathcal{K}\).

  2. Randomly (uniformly) sample an entity \(e \in \mathcal{E}\) or relation \(r' \in \mathcal{R}\) for selection to corrupt the triple.

    • If \(h\) was selected before, the corrupted triple is \((e,r,t)\)

    • If \(r\) was selected before, the corrupted triple is \((h,r',t)\)

    • If \(t\) was selected before, the corrupted triple is \((h,r,e)\)

  3. If filtered is set to True, all proposed corrupted triples that also exist as actual positive triples \((h,r,t) \in \mathcal{K}\) will be removed.

Initialize the negative sampler with the given entities.

Parameters
  • triples_factory (TriplesFactory) – The factory holding the triples to sample from

  • num_negs_per_pos (Optional[int]) – Number of negative samples to make per positive triple. Defaults to 1.

  • filtered (bool) – Whether proposed corrupted triples that are in the training data should be filtered. Defaults to False. See explanation in filter_negative_triples() for why this is a reasonable default.

  • corruption_scheme (Optional[Collection[str]]) – What sides (‘h’, ‘r’, ‘t’) should be corrupted. Defaults to head and tail (‘h’, ‘t’).

Attributes Summary

hpo_default

The default strategy for optimizing the negative sampler’s hyper-parameters

Methods Summary

sample(positive_batch)

Generate negative samples from the positive batch.

Attributes Documentation

hpo_default: ClassVar[Mapping[str, Mapping[str, Any]]] = {'num_negs_per_pos': {'high': 100, 'low': 1, 'q': 10, 'type': <class 'int'>}}

The default strategy for optimizing the negative sampler’s hyper-parameters

Methods Documentation

sample(positive_batch)[source]

Generate negative samples from the positive batch.

Return type

Tuple[LongTensor, Optional[Tensor]]