Interaction

class Interaction[source]

Bases: torch.nn.modules.module.Module, Generic[pykeen.typing.HeadRepresentation, pykeen.typing.RelationRepresentation, pykeen.typing.TailRepresentation], abc.ABC

Base class for interaction functions.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

Attributes Summary

entity_shape

The symbolic shapes for entity representations

relation_shape

The symbolic shapes for relation representations

tail_entity_shape

The symbolic shapes for entity representations for tail entities, if different.

Methods Summary

forward(h, r, t)

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

reset_parameters()

Reset parameters the interaction function may have.

score(h, r, t[, slice_size, slice_dim])

Compute broadcasted triple scores with optional slicing.

score_h(all_entities, r, t[, slice_size])

Score all head entities.

score_hrt(h, r, t)

Score a batch of triples.

score_r(h, all_relations, t[, slice_size])

Score all relations.

score_t(h, r, all_entities[, slice_size])

Score all tail entities.

Attributes Documentation

entity_shape: Sequence[str] = ('d',)

The symbolic shapes for entity representations

relation_shape: Sequence[str] = ('d',)

The symbolic shapes for relation representations

tail_entity_shape: Optional[Sequence[str]] = None

The symbolic shapes for entity representations for tail entities, if different. This is ony relevant for ConvE.

Methods Documentation

abstract forward(h, r, t)[source]

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

Parameters
  • 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

FloatTensor

Returns

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

reset_parameters()[source]

Reset parameters the interaction function may have.

score(h, r, t, slice_size=None, slice_dim=None)[source]

Compute broadcasted triple scores with optional slicing.

Note

At most one of the slice sizes may be not None.

Parameters
  • 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.

  • slice_size (Optional[int]) – The slice size.

  • slice_dim (Optional[str]) – The dimension along which to slice. From {“h”, “r”, “t”}

Return type

FloatTensor

Returns

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

score_h(all_entities, r, t, slice_size=None)[source]

Score all head entities.

Parameters
  • all_entities (~HeadRepresentation) – shape: (num_entities, d_e) The head representations.

  • r (~RelationRepresentation) – shape: (batch_size, d_r) The relation representations.

  • t (~TailRepresentation) – shape: (batch_size, d_e) The tail representations.

  • slice_size (Optional[int]) – The slice size.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities) The scores.

score_hrt(h, r, t)[source]

Score a batch of triples.

Parameters
  • h (~HeadRepresentation) – shape: (batch_size, d_e) The head representations.

  • r (~RelationRepresentation) – shape: (batch_size, d_r) The relation representations.

  • t (~TailRepresentation) – shape: (batch_size, d_e) The tail representations.

Return type

FloatTensor

Returns

shape: (batch_size, 1) The scores.

score_r(h, all_relations, t, slice_size=None)[source]

Score all relations.

Parameters
  • h (~HeadRepresentation) – shape: (batch_size, d_e) The head representations.

  • all_relations (~RelationRepresentation) – shape: (num_relations, d_r) The relation representations.

  • t (~TailRepresentation) – shape: (batch_size, d_e) The tail representations.

  • slice_size (Optional[int]) – The slice size.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities) The scores.

score_t(h, r, all_entities, slice_size=None)[source]

Score all tail entities.

Parameters
  • h (~HeadRepresentation) – shape: (batch_size, d_e) The head representations.

  • r (~RelationRepresentation) – shape: (batch_size, d_r) The relation representations.

  • all_entities (~TailRepresentation) – shape: (num_entities, d_e) The tail representations.

  • slice_size (Optional[int]) – The slice size.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities) The scores.