TransR

class TransR(triples_factory, embedding_dim=50, automatic_memory_optimization=None, relation_dim=30, scoring_fct_norm=1, loss=None, preferred_device=None, random_seed=None, regularizer=None)[source]

Bases: pykeen.models.base.EntityRelationEmbeddingModel

An implementation of TransR from [lin2015].

TransR is an extension of pykeen.models.TransH that explicitly considers entities and relations as different objects and therefore represents them in different vector spaces.

For a triple \((h,r,t) \in \mathbb{K}\), the entity embeddings, \(\textbf{e}_h, \textbf{e}_t \in \mathbb{R}^d\), are first projected into the relation space by means of a relation-specific projection matrix \(\textbf{M}_{r} \in \mathbb{R}^{k \times d}\). With relation embedding \(\textbf{r}_r \in \mathbb{R}^k\), the interaction model is defined similarly to TransE with:

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

The following constraints are applied:

  • \(\|\textbf{e}_h\|_2 \leq 1\)

  • \(\|\textbf{r}_r\|_2 \leq 1\)

  • \(\|\textbf{e}_t\|_2 \leq 1\)

  • \(\|\textbf{M}_{r}\textbf{e}_h\|_2 \leq 1\)

  • \(\|\textbf{M}_{r}\textbf{e}_t\|_2 \leq 1\)

Initialize the model.

Attributes Summary

hpo_default

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

Methods Summary

interaction_function(h, r, t, m_r)

Evaluate the interaction function for given embeddings.

post_parameter_update()

Has to be called after each parameter update.

score_h(rt_batch)

Forward pass using left side (head) prediction.

score_hrt(hrt_batch)

Forward pass.

score_t(hr_batch)

Forward pass using right side (tail) prediction.

Attributes Documentation

hpo_default: ClassVar[Mapping[str, Any]] = {'embedding_dim': {'high': 300, 'low': 20, 'q': 50, 'type': <class 'int'>}, 'relation_dim': {'high': 300, 'low': 20, 'q': 50, '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

static interaction_function(h, r, t, m_r)[source]

Evaluate the interaction function for given embeddings.

The embeddings have to be in a broadcastable shape.

Parameters
  • h (FloatTensor) – shape: (batch_size, num_entities, d_e) Head embeddings.

  • r (FloatTensor) – shape: (batch_size, num_entities, d_r) Relation embeddings.

  • t (FloatTensor) – shape: (batch_size, num_entities, d_e) Tail embeddings.

  • m_r (FloatTensor) – shape: (batch_size, num_entities, d_e, d_r) The relation specific linear transformations.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities) The scores.

post_parameter_update()[source]

Has to be called after each parameter update.

Return type

None

score_h(rt_batch)[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.

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)[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.

Raises

NotImplementedError – If the method was not implemented for this class.

Return type

FloatTensor

Returns

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

score_t(hr_batch)[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.

Return type

FloatTensor

Returns

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