NegativeSampler

class NegativeSampler(*, mapped_triples: Tensor, num_entities: int | None = None, num_relations: int | None = None, num_negs_per_pos: int | None = None, filtered: bool = False, filterer: str | Filterer | type[Filterer] | None = None, filterer_kwargs: Mapping[str, Any] | None = None)[source]

Bases: Module

A negative sampler.

Initialize the negative sampler with the given entities.

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

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

  • num_relations (int) – the number of relations. If None, will be inferred from the triples.

  • num_negs_per_pos (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 (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 (Mapping[str, Any] | None) – 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: Tensor) Tensor[source]

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

Parameters:

positive_batch (Tensor) – shape: (*batch_dims, 3) The positive triples.

Returns:

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

Return type:

Tensor

classmethod get_normalized_name() str[source]

Get the normalized name of the negative sampler.

Return type:

str

sample(positive_batch: Tensor) tuple[Tensor, Tensor | None][source]

Generate negative samples from the positive batch.

Parameters:

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

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.

Return type:

tuple[Tensor, Tensor | None]