负采样器

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)[源代码]

基础类: Module

一个负采样器。

使用给定的实体初始化负采样器。

Parameters:
  • mapped_triples (Tensor) – 正训练三元组

  • num_entities (int) – 实体的数量。如果为None,将从三元组中推断。

  • num_relations (int) – 关系的数量。如果为None,将从三元组中推断。

  • num_negs_per_pos (int) – 每个正样本三元组生成的负样本数量。默认为1。

  • filtered (bool) – 是否应该过滤掉训练数据中提出的损坏三元组。 默认为 False。有关为什么这是一个合理的默认值的解释,请参见 filter_negative_triples()

  • filterer (Filterer | None) – 如果filtered设置为True,可以使用此选项从 pykeen.sampling.filtering 中选择使用哪个过滤模块。

  • filterer_kwargs (Mapping[str, Any] | None) – 在构造时传递给过滤器的额外基于关键字的参数。

属性摘要

hpo_default

优化负采样器超参数的默认策略

方法总结

corrupt_batch(positive_batch)

从正样本批次中生成负样本,不应用任何过滤器。

get_normalized_name()

获取负采样器的标准化名称。

sample(positive_batch)

从正样本批次中生成负样本。

属性文档

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

优化负采样器超参数的默认策略

方法文档

abstract corrupt_batch(positive_batch: Tensor) Tensor[来源]

从正样本批次中生成负样本,不应用任何过滤器。

Parameters:

positive_batch (Tensor) – 形状: (*batch_dims, 3) 正样本三元组。

Returns:

形状:(*batch_dims, num_negs_per_pos, 3) 负三元组。result[*bi, :, :] 包含从 positive_batch[*bi, :] 生成的负样本。

Return type:

Tensor

classmethod get_normalized_name() str[来源]

获取负采样器的规范化名称。

Return type:

str

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

从正样本批次中生成负样本。

Parameters:

positive_batch (Tensor) – 形状: (batch_size, 3) 正样本三元组。

Returns:

一对 (negative_batch, filter_mask) 其中

  1. negative_batch: 形状: (batch_size, num_negatives, 3) 负样本批次。 negative_batch[i, :, :] 包含从 positive_batch[i, :] 生成的负样本。

  2. filter_mask: 形状: (batch_size, num_negatives) 一个可选的过滤掩码。在负样本有效的地方为 True。

Return type:

tuple[Tensor, Tensor | None]