TransH
- class TransH(*, embedding_dim=50, scoring_fct_norm=2, entity_initializer=<function uniform_>, relation_initializer=<function uniform_>, **kwargs)[source]
Bases:
pykeen.models.base.EntityRelationEmbeddingModel
An implementation of TransH [wang2014].
This model extends
pykeen.models.TransE
by applying the translation from head to tail entity in a relational-specific hyperplane in order to address its inability to model one-to-many, many-to-one, and many-to-many relations.In TransH, each relation is represented by a hyperplane, or more specifically a normal vector of this hyperplane \(\textbf{w}_{r} \in \mathbb{R}^d\) and a vector \(\textbf{d}_{r} \in \mathbb{R}^d\) that lies in the hyperplane. To compute the plausibility of a triple \((h,r,t)\in \mathbb{K}\), the head embedding \(\textbf{e}_h \in \mathbb{R}^d\) and the tail embedding \(\textbf{e}_t \in \mathbb{R}^d\) are first projected onto the relation-specific hyperplane:
\[ \begin{align}\begin{aligned}\textbf{e'}_{h,r} = \textbf{e}_h - \textbf{w}_{r}^\top \textbf{e}_h \textbf{w}_r\\\textbf{e'}_{t,r} = \textbf{e}_t - \textbf{w}_{r}^\top \textbf{e}_t \textbf{w}_r\end{aligned}\end{align} \]where \(\textbf{h}, \textbf{t} \in \mathbb{R}^d\). Then, the projected embeddings are used to compute the score for the triple \((h,r,t)\):
\[f(h, r, t) = -\|\textbf{e'}_{h,r} + \textbf{d}_r - \textbf{e'}_{t,r}\|_{p}^2\]See also
OpenKE implementation of TransH
Initialize TransH.
- 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 usually1
or2.
.entity_initializer (
Union
[str
,Callable
[[FloatTensor
],FloatTensor
],None
]) – Entity initializer function. Defaults totorch.nn.init.uniform_()
relation_initializer (
Union
[str
,Callable
[[FloatTensor
],FloatTensor
],None
]) – Relation initializer function. Defaults totorch.nn.init.uniform_()
kwargs – Remaining keyword arguments to forward to
pykeen.models.EntityRelationEmbeddingModel
Attributes Summary
The default strategy for optimizing the model's hyper-parameters
The settings used by [wang2014] for TransH
Methods Summary
Has to be called after each parameter update.
regularize_if_necessary
(*tensors)Update the regularizer's term given some tensors, if regularization is requested.
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
- regularizer_default_kwargs: ClassVar[Mapping[str, Any]] = {'epsilon': 1e-05, 'weight': 0.05}
The settings used by [wang2014] for TransH
Methods Documentation
- regularize_if_necessary(*tensors)[source]
Update the regularizer’s term given some tensors, if regularization is requested.
- Return type
- 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.