# TuckER¶

class TuckER(triples_factory, embedding_dim=200, automatic_memory_optimization=None, relation_dim=None, loss=None, preferred_device=None, random_seed=None, dropout_0=0.3, dropout_1=0.4, dropout_2=0.5, regularizer=None, apply_batch_normalization=True)[source]

An implementation of TuckEr from [balazevic2019].

TuckER is a linear model that is based on the tensor factorization method Tucker in which a three-mode tensor $$\mathfrak{X} \in \mathbb{R}^{I \times J \times K}$$ is decomposed into a set of factor matrices $$\textbf{A} \in \mathbb{R}^{I \times P}$$, $$\textbf{B} \in \mathbb{R}^{J \times Q}$$, and $$\textbf{C} \in \mathbb{R}^{K \times R}$$ and a core tensor $$\mathfrak{Z} \in \mathbb{R}^{P \times Q \times R}$$ (of lower rank):

$\mathfrak{X} \approx \mathfrak{Z} \times_1 \textbf{A} \times_2 \textbf{B} \times_3 \textbf{C}$

where $$\times_n$$ is the tensor product, with $$n$$ denoting along which mode the tensor product is computed. In TuckER, a knowledge graph is considered as a binary tensor which is factorized using the Tucker factorization where $$\textbf{E} = \textbf{A} = \textbf{C} \in \mathbb{R}^{n_{e} \times d_e}$$ denotes the entity embedding matrix, $$\textbf{R} = \textbf{B} \in \mathbb{R}^{n_{r} \times d_r}$$ represents the relation embedding matrix, and $$\mathfrak{W} = \mathfrak{Z} \in \mathbb{R}^{d_e \times d_r \times d_e}$$ is the core tensor that indicates the extent of interaction between the different factors. The interaction model is defined as:

$f(h,r,t) = \mathfrak{W} \times_1 \textbf{h} \times_2 \textbf{r} \times_3 \textbf{t}$

where $$\textbf{h},\textbf{t}$$ correspond to rows of $$\textbf{E}$$ and $$\textbf{r}$$ to a row of $$\textbf{R}$$.

Initialize the model.

The dropout values correspond to the following dropouts in the model’s score function:

DO_2(BN(DO_0(BN(h)) x_1 DO_1(W x_2 r))) x_3 t

where h,r,t are the head, relation, and tail embedding, W is the core tensor, x_i denotes the tensor product along the i-th mode, BN denotes batch normalization, and DO dropout.

Attributes Summary

 hpo_default The default strategy for optimizing the model’s hyper-parameters loss_default_kwargs The default parameters for the default loss function class

Methods Summary

 score_h(rt_batch) Forward pass using left side (head) prediction. score_hrt(hrt_batch) Forward pass. score_t(hr_batch) Forward pass using right side (tail) prediction.

Attributes Documentation

hpo_default: ClassVar[Mapping[str, Any]] = {'dropout_0': {'high': 0.4, 'low': 0.1, 'type': <class 'float'>}, 'dropout_1': {'high': 0.5, 'low': 0.1, 'type': <class 'float'>}, 'dropout_2': {'high': 0.6, 'low': 0.1, 'type': <class 'float'>}, 'embedding_dim': {'high': 300, 'low': 50, 'q': 50, 'type': <class 'int'>}, 'relation_dim': {'high': 200, 'low': 30, 'q': 25, 'type': <class 'int'>}}

The default strategy for optimizing the model’s hyper-parameters

loss_default_kwargs: ClassVar[Optional[Mapping[str, Any]]] = {}

The default parameters for the default loss function class

Methods Documentation

score_h(rt_batch)[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.

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)[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.

Raises

NotImplementedError – If the method was not implemented for this class.

Return type

FloatTensor

Returns

shape: (batch_size, 1), dtype: float The score for each triple.

score_t(hr_batch)[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.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities), dtype: float For each h-r pair, the scores for all possible tails.