TransE

class TransE(*, embedding_dim=50, scoring_fct_norm=1, entity_initializer=<function xavier_uniform_>, entity_constrainer=<function normalize>, relation_initializer=<pykeen.utils.compose object>, relation_constrainer=None, **kwargs)[source]

Bases: pykeen.models.base.EntityRelationEmbeddingModel

An implementation of TransE [bordes2013].

TransE models relations as a translation from head to tail entities in \(\textbf{e}\):

\[\textbf{e}_h + \textbf{e}_r \approx \textbf{e}_t\]

This equation is rearranged and the \(l_p\) norm is applied to create the TransE interaction function.

\[f(h, r, t) = - \|\textbf{e}_h + \textbf{e}_r - \textbf{e}_t\|_{p}\]

While this formulation is computationally efficient, it inherently cannot model one-to-many, many-to-one, and many-to-many relationships. For triples \((h,r,t_1), (h,r,t_2) \in \mathcal{K}\) where \(t_1 \neq t_2\), the model adapts the embeddings in order to ensure \(\textbf{e}_h + \textbf{e}_r \approx \textbf{e}_{t_1}\) and \(\textbf{e}_h + \textbf{e}_r \approx \textbf{e}_{t_2}\) which results in \(\textbf{e}_{t_1} \approx \textbf{e}_{t_2}\).

Initialize TransE.

Parameters
  • embedding_dim (int) – The entity embedding dimension \(d\). Is usually \(d \in [50, 300]\).

  • scoring_fct_norm (int) – The \(l_p\) norm applied in the interaction function. Is usually 1 or 2..

  • entity_initializer (Union[str, Callable[[FloatTensor], FloatTensor], None]) – Entity initializer function. Defaults to pykeen.nn.init.xavier_uniform_()

  • entity_constrainer (Union[str, Callable[[FloatTensor], FloatTensor], None]) – Entity constrainer function. Defaults to torch.nn.init.normalize()

  • relation_initializer (Union[str, Callable[[FloatTensor], FloatTensor], None]) – Relation initializer function. Defaults to pykeen.nn.init.xavier_uniform_norm_()

  • relation_constrainer (Union[str, Callable[[FloatTensor], FloatTensor], None]) – Relation constrainer function. Defaults to none.

  • kwargs – Remaining keyword arguments to forward to pykeen.models.EntityRelationEmbeddingModel

See also

Attributes Summary

hpo_default

The default strategy for optimizing the model's hyper-parameters

Methods Summary

score_h(rt_batch, **kwargs)

Forward pass using left side (head) prediction.

score_hrt(hrt_batch, **kwargs)

Forward pass.

score_t(hr_batch, **kwargs)

Forward pass using right side (tail) prediction.

Attributes Documentation

hpo_default: ClassVar[Mapping[str, Any]] = {'embedding_dim': {'high': 256, 'low': 16, 'q': 16, 'type': <class 'int'>}, 'scoring_fct_norm': {'high': 2, 'low': 1, 'type': <class 'int'>}}

The default strategy for optimizing the model’s hyper-parameters

Methods Documentation

score_h(rt_batch, **kwargs)[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 (LongTensor) – 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.

Return type

FloatTensor

Returns

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

score_hrt(hrt_batch, **kwargs)[source]

Forward pass.

This method takes head, relation and tail of each triple and calculates the corresponding score.

Parameters
  • hrt_batch (LongTensor) – shape: (batch_size, 3), dtype: long The indices of (head, relation, tail) triples.

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

Return type

FloatTensor

Returns

shape: (batch_size, 1), dtype: float The score for each triple.

score_t(hr_batch, **kwargs)[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 (LongTensor) – 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.

Return type

FloatTensor

Returns

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