# ComplEx¶

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

An implementation of ComplEx [trouillon2016].

ComplEx is an extension of pykeen.models.DistMult that uses complex valued representations for the entities and relations. Entities and relations are represented as vectors $$\textbf{e}_i, \textbf{r}_i \in \mathbb{C}^d$$, and the plausibility score is computed using the Hadamard product:

$f(h,r,t) = Re(\mathbf{e}_h\odot\mathbf{r}_r\odot\mathbf{e}_t)$

Which expands to:

$f(h,r,t) = \left\langle Re(\mathbf{e}_h),Re(\mathbf{r}_r),Re(\mathbf{e}_t)\right\rangle + \left\langle Im(\mathbf{e}_h),Re(\mathbf{r}_r),Im(\mathbf{e}_t)\right\rangle + \left\langle Re(\mathbf{e}_h),Re(\mathbf{r}_r),Im(\mathbf{e}_t)\right\rangle - \left\langle Im(\mathbf{e}_h),Im(\mathbf{r}_r),Im(\mathbf{e}_t)\right\rangle$

where $$Re(\textbf{x})$$ and $$Im(\textbf{x})$$ denote the real and imaginary parts of the complex valued vector $$\textbf{x}$$. Because the Hadamard product is not commutative in the complex space, ComplEx can model anti-symmetric relations in contrast to DistMult.

Official implementation: https://github.com/ttrouill/complex/

Initialize ComplEx.

Parameters

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 regularizer_default_kwargs The LP settings used by [trouillon2016] for ComplEx.

Methods Summary

 interaction_function(h, r, t) Evaluate the interaction function of ComplEx for given embeddings. score_hrt(hrt_batch) Forward pass.

Attributes Documentation

hpo_default: ClassVar[Mapping[str, Any]] = {'embedding_dim': {'high': 300, 'low': 50, 'q': 50, 'type': <class 'int'>}}

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

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

The default parameters for the default loss function class

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

The LP settings used by [trouillon2016] for ComplEx.

Methods Documentation

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

Evaluate the interaction function of ComplEx for given embeddings.

The embeddings have to be in a broadcastable shape.

Parameters
• h (FloatTensor) – Head embeddings.

• r (FloatTensor) – Relation embeddings.

• t (FloatTensor) – Tail embeddings.

Return type

FloatTensor

Returns

shape: (…) The scores.

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.