MarginalDistributionBaseline

class MarginalDistributionBaseline(triples_factory: CoreTriplesFactory, entity_margin: bool = True, relation_margin: bool = True)[source]

Bases: EvaluationOnlyModel

Score based on marginal distributions.

To predict scores for the tails, we make the following simplification of \(P(t | h, r)\):

\[P(t | h, r) \sim P(t | h) * P(t | r)\]

Depending on the settings, we either set \(P(t | *) = \frac{1}{n}\) where \(n\) is the number of entities, or estimate them by counting occurrences in the training triples.

Note

This model cannot make use of GPU acceleration, since internally it uses scipy’s sparse matrices.

Initialize the model.

Parameters:
  • triples_factory (CoreTriplesFactory) – The triples factory containing the training triples.

  • entity_margin (bool) – whether to compute entity-specific marginal distributions

  • relation_margin (bool) – whether to compute relation-specific marginal distributions

If you set entity_margin=False and relation_margin=False, it will lead to a uniform distribution, i.e. equal scores for all entities.

Methods Summary

score_h(rt_batch, **kwargs)

Forward pass using left side (head) prediction.

score_t(hr_batch, **kwargs)

Forward pass using right side (tail) prediction.

Methods Documentation

score_h(rt_batch: Tensor, **kwargs) Tensor[source]

Forward pass using left side (head) prediction.

This method calculates the score for all possible heads for each (relation, tail) pair.

Parameters:
  • rt_batch (Tensor) – shape: (batch_size, 2), dtype: long The indices of (relation, tail) pairs.

  • slice_size – >0 The divisor for the scoring function when using slicing.

  • mode – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.

  • heads – shape: (num_heads,) | (batch_size, num_heads) head entity indices to score against. If None, scores against all entities (from the given mode).

Returns:

shape: (batch_size, num_heads), dtype: float For each r-t pair, the scores for all possible heads.

Return type:

Tensor

score_t(hr_batch: Tensor, **kwargs) Tensor[source]

Forward pass using right side (tail) prediction.

This method calculates the score for all possible tails for each (head, relation) pair.

Parameters:
  • hr_batch (Tensor) – shape: (batch_size, 2), dtype: long The indices of (head, relation) pairs.

  • slice_size – >0 The divisor for the scoring function when using slicing.

  • mode – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.

  • tails – shape: (num_tails,) | (batch_size, num_tails) tail entity indices to score against. If None, scores against all entities (from the given mode).

Returns:

shape: (batch_size, num_tails), dtype: float For each h-r pair, the scores for all possible tails.

Return type:

Tensor