NegativeSampler

class NegativeSampler(triples_factory, num_negs_per_pos=None, filtered=False, filterer=None, filterer_kwargs=None)[source]

Bases: abc.ABC

A negative sampler.

Initialize the negative sampler with the given entities.

Parameters
  • triples_factory (CoreTriplesFactory) – The factory holding the positive training triples

  • 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.

  • filterer (Union[str, Filterer, Type[Filterer], None]) – If filtered is set to True, this can be used to choose which filter module from pykeen.sampling.filtering is used.

  • filterer_kwargs (Optional[Mapping[str, Any]]) – Additional keyword-based arguments passed to the filterer upon construction.

Attributes Summary

hpo_default

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

Methods Summary

corrupt_batch(positive_batch)

Generate negative samples from the positive batch without application of any filter.

get_normalized_name()

Get the normalized name of the negative sampler.

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, 'log': True, 'low': 1, 'type': <class 'int'>}}

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

Methods Documentation

abstract corrupt_batch(positive_batch)[source]

Generate negative samples from the positive batch without application of any filter.

Parameters

positive_batch (LongTensor) – shape: (batch_size, 3) The positive triples.

Return type

LongTensor

Returns

shape: (batch_size, num_negs_per_pos, 3) The negative triples. result[i, :, :] contains the negative examples generated from positive_batch[i, :].

classmethod get_normalized_name()[source]

Get the normalized name of the negative sampler.

Return type

str

sample(positive_batch)[source]

Generate negative samples from the positive batch.

Parameters

positive_batch (LongTensor) – shape: (batch_size, 3) The positive triples.

Return type

Tuple[LongTensor, Optional[BoolTensor]]

Returns

A pair (negative_batch, filter_mask) where

  1. negative_batch: shape: (batch_size, num_negatives, 3) The negative batch. negative_batch[i, :, :] contains the negative examples generated from positive_batch[i, :].

  2. filter_mask: shape: (batch_size, num_negatives) An optional filter mask. True where negative samples are valid.