class MonotonicAffineTransformationInteraction(base, initial_bias=0.0, trainable_bias=True, initial_scale=1.0, trainable_scale=True)[source]

Bases: pykeen.nn.modules.Interaction[pykeen.typing.HeadRepresentation, pykeen.typing.RelationRepresentation, pykeen.typing.TailRepresentation]

An adapter of interaction functions which adds a scalar (trainable) monotonic affine transformation of the score.

\[score(h, r, t) = \alpha \cdot score'(h, r, t) + \beta\]

This adapter is useful for losses such as BCE, where there is a fixed decision threshold, or margin-based losses, where the margin is not be treated as hyper-parameter, but rather a trainable parameter. This is particularly useful, if the value range of the score function is not known in advance, and thus choosing an appropriate margin becomes difficult.

Monotonicity is required to preserve the ordering of the original scoring function, and thus ensures that more plausible triples are still more plausible after the transformation.

For example, we can add a bias to a distance-based interaction function to enable positive values:

>>> base = TransEInteraction(p=2)
>>> interaction = MonotonicAffineTransformationInteraction(base=base, trainable_bias=True, trainable_scale=False)

When combined with BCE loss, we can geometrically think about predicting a (soft) sphere at \(h + r\) with radius equal to the bias of the transformation. When we add a trainable scale, the model can control the “softness” of the decision boundary itself.

Initialize the interaction.

  • base (Interaction[~HeadRepresentation, ~RelationRepresentation, ~TailRepresentation]) – The base interaction.

  • initial_bias (float) – The initial value for the bias.

  • trainable_bias (bool) – Whether the bias should be trainable.

  • initial_scale (float) – >0 The initial value for the scale. Must be strictly positive.

  • trainable_scale (bool) – Whether the scale should be trainable.

Methods Summary

forward(h, r, t)

Compute broadcasted triple scores given broadcasted representations for head, relation and tails.


Reset parameters the interaction function may have.

Methods Documentation

forward(h, r, t)[source]

Compute broadcasted triple scores given broadcasted representations for head, relation and tails.

  • h (~HeadRepresentation) – shape: (batch_size, num_heads, 1, 1, *) The head representations.

  • r (~RelationRepresentation) – shape: (batch_size, 1, num_relations, 1, *) The relation representations.

  • t (~TailRepresentation) – shape: (batch_size, 1, 1, num_tails, *) The tail representations.

Return type



shape: (batch_size, num_heads, num_relations, num_tails) The scores.


Reset parameters the interaction function may have.