TransHInteraction

class TransHInteraction(p: int, power_norm: bool = False)[source]

Bases: NormBasedInteraction[Tensor, tuple[Tensor, Tensor], Tensor]

The norm-based TransH interaction function.

This model extends TransEInteraction by applying the translation from head to tail entity in a relation-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 \(\mathbf{r}_{w} \in \mathbb{R}^d\) and a vector \(\mathbf{r}_{d} \in \mathbb{R}^d\) that lies in the hyperplane. To obtain a plausibility score, the head representation \(\mathbf{h} \in \mathbb{R}^d\), and the tail embedding \(\mathbf{t} \in \mathbb{R}^d\) are first projected onto the relation-specific hyperplane:

\[ \begin{align}\begin{aligned}\mathbf{h}_{r} = \mathbf{h} - \mathbf{r}_{w}^T \mathbf{h} \mathbf{r}_w\\\mathbf{t}_{r} = \mathbf{t} - \mathbf{r}_{w}^T \mathbf{t} \mathbf{r}_w\end{aligned}\end{align} \]

Then, the projected representations are used to compute the score as in TransEInteraction:

\[-\|\textbf{h}_{r} + \textbf{r}_d - \textbf{t}_{r}\|_{p}^2\]

Initialize the norm-based interaction function.

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

relation_shape

The symbolic shapes for relation representations

Methods Summary

forward(h, r, t)

Evaluate the interaction function.

Attributes Documentation

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

The symbolic shapes for relation representations

Methods Documentation

forward(h: Tensor, r: tuple[Tensor, Tensor], t: 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 (Tensor) – shape: (*batch_dims, d) The head representations.

  • r (tuple[Tensor, Tensor]) – shape: (*batch_dims, d) The relation representations.

  • t (Tensor) – shape: (*batch_dims, d) The tail representations.

Returns:

shape: batch_dims The scores.

Return type:

Tensor