TransDInteraction
- class TransDInteraction(p: int = 2, power_norm: bool = True)[source]
Bases:
NormBasedInteraction[tuple[Tensor,Tensor],tuple[Tensor,Tensor],tuple[Tensor,Tensor]]The TransD interaction function.
TransD is an extension of
TransRInteractionthat, 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 \(\mathbf{M}_{r, h}, \mathbf{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, \(\mathbf{h}_v, \mathbf{h}_p, \mathbf{t}_v, \mathbf{t}_p \in \mathbb{R}^d\) and \(\mathbf{r}_p, \mathbf{r}_v \in \mathbb{R}^k\), respectively.
The first set of representations is used for calculating the entity-relation-specific projection matrices:
\[ \begin{align}\begin{aligned}\mathbf{M}_{r, h} &=& \mathbf{r}_p \mathbf{h}_p^{T} + \tilde{\mathbf{I}}\\\mathbf{M}_{r, t} &=& \mathbf{r}_p \mathbf{t}_p^{T} + \tilde{\mathbf{I}}\end{aligned}\end{align} \]where \(\tilde{\mathbf{I}} \in \mathbb{R}^{k \times d}\) is a \(k \times d\) matrix with ones on the diagonal and zeros elsewhere. Next, \(\mathbf{h}_v\) and \(\mathbf{t}_v\) are projected into the relation space by means of the constructed projection matrices, before calculating a distance similar to
TransEInteraction:\[-\|c(\mathbf{M}_{r, h} \mathbf{h}_v) + \mathbf{r}_v - c(\mathbf{M}_{r, t} \mathbf{t}_v)\|_{2}^2\]where \(c\) enforces the constraint \(\|\cdot\| \leq 1\).
Note
TransDadditionally enforces \(\|\mathbf{h}\|, \|\mathbf{r}\|, \|\mathbf{t}\| \leq 1\).Initialize the interaction module.
See also
The parameters
pandpower_normare directly passed toNormBasedInteraction- Parameters:
p (int) – The norm used with
torch.linalg.vector_norm(). Typically is 1 or 2.power_norm (bool) – Whether to use the p-th power of the \(L_p\) norm. It has the advantage of being differentiable around 0, and numerically more stable.
Attributes Summary
The symbolic shapes for entity representations
The symbolic shapes for relation representations
Methods Summary
forward(h, r, t)Evaluate the interaction function.
Attributes Documentation
Methods Documentation
- forward(h: tuple[Tensor, Tensor], r: tuple[Tensor, Tensor], t: tuple[Tensor, Tensor]) Tensor[source]
Evaluate the interaction function.
See also
Interaction.forwardfor a detailed description about the generic batched form of the interaction function.- Parameters:
h (tuple[Tensor, Tensor]) – shape:
(*batch_dims, d)and(*batch_dims, d)The head representations.r (tuple[Tensor, Tensor]) – shape:
(*batch_dims, e)and(*batch_dims, e)The relation representations.t (tuple[Tensor, Tensor]) – shape:
(*batch_dims, d)and(*batch_dims, d)The tail representations.
- Returns:
shape:
batch_dimsThe scores.- Return type: