NegativeSampler

class NegativeSampler(*, mapped_triples, num_entities=None, num_relations=None, num_negs_per_pos=None, filtered=False, filterer=None, filterer_kwargs=None)[source]

Bases: Module

A negative sampler.

Initialize the negative sampler with the given entities.

Parameters:
  • mapped_triples (LongTensor) – the positive training triples

  • num_entities (Optional[int]) – the number of entities. If None, will be inferred from the triples.

  • num_relations (Optional[int]) – the number of relations. If None, will be inferred from the 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_dims, 3) The positive triples.

Return type:

LongTensor

Returns:

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

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.