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 TransRInteraction 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 \(\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

TransD additionally enforces \(\|\mathbf{h}\|, \|\mathbf{r}\|, \|\mathbf{t}\| \leq 1\).

Initialize the interaction module.

See also

The parameters p and power_norm are directly passed to NormBasedInteraction

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

entity_shape

The symbolic shapes for entity representations

relation_shape

The symbolic shapes for relation representations

Methods Summary

forward(h, r, t)

Evaluate the interaction function.

Attributes Documentation

entity_shape: Sequence[str] = ('d', 'd')

The symbolic shapes for entity representations

relation_shape: Sequence[str] = ('e', 'e')

The symbolic shapes for relation representations

Methods Documentation

forward(h: tuple[Tensor, Tensor], r: tuple[Tensor, Tensor], t: tuple[Tensor, Tensor]) Tensor[source]

Evaluate the interaction function.

See also

Interaction.forward for 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_dims The scores.

Return type:

Tensor