# DistMult¶

class DistMult(triples_factory, embedding_dim=50, automatic_memory_optimization=None, loss=None, preferred_device=None, random_seed=None, regularizer=None)[source]

An implementation of DistMult from [yang2014].

This model simplifies RESCAL by restricting matrices representing relations as diagonal matrices.

DistMult is a simplification of pykeen.models.RESCAL where the relation matrices $$\textbf{W}_{r} \in \mathbb{R}^{d \times d}$$ are restricted to diagonal matrices:

$f(h,r,t) = \textbf{e}_h^{T} \textbf{W}_r \textbf{e}_t = \sum_{i=1}^{d}(\textbf{e}_h)_i \cdot diag(\textbf{W}_r)_i \cdot (\textbf{e}_t)_i$

Because of its restriction to diagonal matrices, DistMult is more computationally than RESCAL, but at the same time it is less expressive. For instance, it is not able to model anti-symmetric relations, since $$f(h,r, t) = f(t,r,h)$$. This can alternatively be formulated with relation vectors $$\textbf{r}_r \in \mathbb{R}^d$$ and the Hadamard operator and the $$l_1$$ norm.

$f(h,r,t) = \|\textbf{e}_h \odot \textbf{r}_r \odot \textbf{e}_t\|_1$
Note:
• For FB15k, Yang et al. report 2 negatives per each positive.

Initialize DistMult.

Parameters

embedding_dim (int) – The entity embedding dimension $$d$$. Is usually $$d \in [50, 300]$$.

Attributes Summary

 hpo_default The default strategy for optimizing the model’s hyper-parameters regularizer_default_kwargs The LP settings used by [yang2014] for DistMult

Methods Summary

 interaction_function(h, r, t) Evaluate the interaction function for given embeddings. Has to be called after each parameter update. 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]] = {'embedding_dim': {'high': 350, 'low': 50, 'q': 25, 'type': <class 'int'>}}

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

regularizer_default_kwargs: ClassVar[Optional[Mapping[str, Any]]] = {'normalize': True, 'p': 2.0, 'weight': 0.1}

The LP settings used by [yang2014] for DistMult

Methods Documentation

static interaction_function(h, r, t)[source]

Evaluate the interaction function for given embeddings.

The embeddings have to be in a broadcastable shape.

WARNING: Does not ensure forward constraints.

Parameters
• h (FloatTensor) – shape: (…, e) Head embeddings.

• r (FloatTensor) – shape: (…, e) Relation embeddings.

• t (FloatTensor) – shape: (…, e) Tail embeddings.

Return type

FloatTensor

Returns

shape: (…) The scores.

post_parameter_update()[source]

Has to be called after each parameter update.

Return type

None

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.