TuckERInteraction

class TuckERInteraction(embedding_dim: int = 200, relation_dim: int | None = None, head_dropout: float = 0.3, relation_dropout: float = 0.4, head_relation_dropout: float = 0.5, apply_batch_normalization: bool = True, core_initializer: str | Callable[[Tensor], Tensor] | None = None, core_initializer_kwargs: Mapping[str, Any] | None = None)[source]

Bases: Interaction[Tensor, Tensor, Tensor]

The stateful TuckER interaction function.

The interaction function is inspired by the Tucker tensor decomposition. The base form is given as

\[\mathbf{Z} \times_1 \mathbf{h} \times_2 \mathbf{r} \times_3 \mathbf{t} = \sum_{1 \leq i, k \leq d_e, 1 \leq j \leq d_r} \mathbf{Z}_{i, j, k} \cdot \mathbf{h}_{i} \cdot \mathbf{r}_{j} \cdot \mathbf{t}_{k}\]

where \(\mathbf{h}, \mathbf{t} \in \mathbb{R}^{d_e}\) are the head and tail entity representation, \(\mathbf{r} \in \mathbb{R}^{d_r}\) is the relation representation, and \(\mathbf{Z} \in \mathbb{R}^{d_e \times d_r \times d_e}\) is a global parameter, and \(\times_k\) denotes the tensor product along the \(k\)-th dimension.

The implementation further adds BatchNorm1d and Dropout layers at the following locations:

\[\textit{DO}_{hr}(\textit{BN}_{hr}( \textit{DO}_h(\textit{BN}_h(\mathbf{h})) \times_1 \textit{DO}_r(\mathbf{Z} \times_2 \mathbf{r}) ) \times_3 \mathbf{t}\]

The implementation a has complexity of \(\mathcal{O}(d_e^2 d_r)\), and requires \(\mathcal{O}(d_e^2 d_r)\) global trainable parameters.

Initialize the Tucker interaction function.

Parameters:
  • embedding_dim (int) – The entity embedding dimension.

  • relation_dim (int | None) – The relation embedding dimension. Defaults to embedding_dim.

  • head_dropout (float) – The dropout rate applied to the head representations.

  • relation_dropout (float) – The dropout rate applied to the relation representations.

  • head_relation_dropout (float) – The dropout rate applied to the combined head and relation representations.

  • apply_batch_normalization (bool) – Whether to use batch normalization on head representations and the combination of head and relation.

  • core_initializer (Hint[Initializer]) – The core tensor’s initializer, or a hint thereof. Defaults to default_core_initializer.

  • core_initializer_kwargs (OptionalKwargs) – Additional keyword-based parameters for the initializer. Defaults to default_core_initializer_kwargs.

Note

The parameter pair (core_initializer, core_initializer_kwargs) is used for pykeen.nn.init.initializer_resolver

An explanation of resolvers and how to use them is given in https://class-resolver.readthedocs.io/en/latest/.

Attributes Summary

default_core_initializer_kwargs

Methods Summary

default_core_initializer(tensor[, a, b, ...])

Fill the input Tensor with values drawn from the uniform distribution.

forward(h, r, t)

Evaluate the interaction function.

reset_parameters()

Reset parameters the interaction function may have.

Attributes Documentation

default_core_initializer_kwargs: Mapping[str, Any] = {'a': -1.0, 'b': 1.0}

Methods Documentation

static default_core_initializer(tensor: Tensor, a: float = 0.0, b: float = 1.0, generator: Generator | None = None) Tensor

Fill the input Tensor with values drawn from the uniform distribution.

\(\mathcal{U}(a, b)\).

Args:

tensor: an n-dimensional torch.Tensor a: the lower bound of the uniform distribution b: the upper bound of the uniform distribution generator: the torch Generator to sample from (default: None)

Examples:
>>> w = torch.empty(3, 5)
>>> nn.init.uniform_(w)
Parameters:
Return type:

Tensor

forward(h: Tensor, r: 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 (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

reset_parameters()[source]

Reset parameters the interaction function may have.