BernoulliNegativeSampler

class BernoulliNegativeSampler(*, mapped_triples, **kwargs)[source]

Bases: NegativeSampler

An implementation of the Bernoulli negative sampling approach proposed by [wang2014].

The probability of corrupting the head \(h\) or tail \(t\) in a relation \((h,r,t) \in \mathcal{K}\) is determined by global properties of the relation \(r\):

  • \(r\) is one-to-many (e.g. motherOf): a higher probability is assigned to replace \(h\)

  • \(r\) is many-to-one (e.g. bornIn): a higher probability is assigned to replace \(t\).

More precisely, for each relation \(r \in \mathcal{R}\), the average number of tails per head (tph) and heads per tail (hpt) are first computed.

Then, the head corruption probability \(p_r\) is defined as \(p_r = \frac{tph}{tph + hpt}\). The tail corruption probability is defined as \(1 - p_r = \frac{hpt}{tph + hpt}\).

For each triple \((h,r,t) \in \mathcal{K}\), the head is corrupted with probability \(p_r\) and the tail is corrupted with probability \(1 - p_r\).

If filtered is set to True, all proposed corrupted triples that also exist as actual positive triples \((h,r,t) \in \mathcal{K}\) will be removed.

Initialize the bernoulli negative sampler with the given entities.

Parameters

Methods Summary

corrupt_batch(positive_batch)

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

Methods Documentation

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, :].