基础负采样器

class BasicNegativeSampler(*, corruption_scheme: Collection[Literal['head', 'relation', 'tail']] | None = None, **kwargs)[source]

基础类:NegativeSampler

一个基本的负采样器。

这个负采样器通过根据选择的破坏方案替换\(h\)\(r\)\(t\)来破坏正三元组\((h,r,t) \in \mathcal{K}\)。破坏方案可以包含\(h\)\(r\)\(t\)或这些的任何子集。

步骤:

  1. 随机(均匀地)确定对于正三元组 \((h,r,t) \in \mathcal{K}\) 中的 \(h\)\(r\)\(t\) 是否应被破坏。

  2. 随机(均匀)采样一个实体 \(e \in \mathcal{E}\) 或关系 \(r' \in \mathcal{R}\) 以选择破坏三元组。

    • 如果之前选择了\(h\),那么损坏的三元组是\((e,r,t)\)

    • 如果之前选择了\(r\),那么损坏的三元组是\((h,r',t)\)

    • 如果之前选择了\(t\),那么损坏的三元组是\((h,r,e)\)

  3. 如果 filtered 设置为 True,所有作为实际正三元组存在的被提议的损坏三元组 \((h,r,t) \in \mathcal{K}\) 将被移除。

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

Parameters:
  • corruption_scheme (Collection[Literal['head', 'relation', 'tail']] | None) – 哪些边(‘h’, ‘r’, ‘t’)应该被破坏。默认为头和尾(‘h’, ‘t’)。

  • kwargs – 传递给pykeen.sampling.NegativeSampler的额外基于关键字的参数。

方法总结

corrupt_batch(positive_batch)

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

方法文档

corrupt_batch(positive_batch: Tensor) Tensor[source]

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

Parameters:

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

Returns:

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

Return type:

Tensor