TransD

class TransD(triples_factory, embedding_dim=50, relation_dim=30, loss=None, preferred_device=None, random_seed=None, regularizer=None, entity_initializer=<function xavier_uniform_>, relation_initializer=<pykeen.utils.compose object>, entity_constrainer=<function clamp_norm>, relation_constrainer=<function clamp_norm>)[source]

Bases: pykeen.models.base.EntityRelationEmbeddingModel

An implementation of TransD from [ji2015].

TransD is an extension of pykeen.models.TransR that, like TransR, considers entities and relations as objects living in different vector spaces. However, instead of performing the same relation-specific projection for all entity embeddings, entity-relation-specific projection matrices \(\textbf{M}_{r,h}, \textbf{M}_{t,h} \in \mathbb{R}^{k \times d}\) are constructed.

To do so, all head entities, tail entities, and relations are represented by two vectors, \(\textbf{e}_h, \hat{\textbf{e}}_h, \textbf{e}_t, \hat{\textbf{e}}_t \in \mathbb{R}^d\) and \(\textbf{r}_r, \hat{\textbf{r}}_r \in \mathbb{R}^k\), respectively. The first set of embeddings is used for calculating the entity-relation-specific projection matrices:

\[ \begin{align}\begin{aligned}\textbf{M}_{r,h} = \hat{\textbf{r}}_r \hat{\textbf{e}}_h^{T} + \tilde{\textbf{I}}\\\textbf{M}_{r,t} = \hat{\textbf{r}}_r \hat{\textbf{e}}_t^{T} + \tilde{\textbf{I}}\end{aligned}\end{align} \]

where \(\tilde{\textbf{I}} \in \mathbb{R}^{k \times d}\) is a \(k \times d\) matrix with ones on the diagonal and zeros elsewhere. Next, \(\textbf{e}_h\) and \(\textbf{e}_t\) are projected into the relation space by means of the constructed projection matrices. Finally, the plausibility score for \((h,r,t) \in \mathbb{K}\) is given by:

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

See also

Initialize the entity embedding model.

See also

Constructor of the base class pykeen.models.Model

Attributes Summary

hpo_default

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

Methods Summary

interaction_function(h, h_p, r, r_p, t, t_p)

Evaluate the interaction function for given embeddings.

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': 256, 'low': 16, 'q': 16, 'type': <class 'int'>}, 'relation_dim': {'high': 256, 'low': 16, 'q': 16, 'type': <class 'int'>}}

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

Methods Documentation

static interaction_function(h, h_p, r, r_p, t, t_p)[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.

  • h_p (FloatTensor) – shape: (batch_size, num_entities, d_e) Head projections.

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

  • r_p (FloatTensor) – shape: (batch_size, num_entities, d_r) Relation projections.

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

  • t_p (FloatTensor) – shape: (batch_size, num_entities, d_e) Tail projections.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities) The scores.

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.