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:
ModuleA negative sampler.
Initialize the negative sampler with the given entities.
- Parameters:
mapped_triples (
LongTensor) – the positive training triplesnum_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 infilter_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 frompykeen.sampling.filteringis used.filterer_kwargs (
Optional[Mapping[str,Any]]) – Additional keyword-based arguments passed to the filterer upon construction.
Attributes Summary
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 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 frompositive_batch[*bi, :].
- classmethod get_normalized_name()[source]
Get the normalized name of the negative sampler.
- Return type:
- sample(positive_batch)[source]
Generate negative samples from the positive batch.
- Parameters:
positive_batch (
LongTensor) – shape: (batch_size, 3) The positive triples.- Return type:
- Returns:
A pair (negative_batch, filter_mask) where
negative_batch: shape: (batch_size, num_negatives, 3) The negative batch.
negative_batch[i, :, :]contains the negative examples generated frompositive_batch[i, :].filter_mask: shape: (batch_size, num_negatives) An optional filter mask. True where negative samples are valid.