Quaternion Knowledge Graph Embeddings

Shuai Zhang, Yi Tay, Lina Yao, Qi Liu

Introduction

Knowledge graphs (KGs) live at the heart of many semantic applications (e.g., question answering, search, and natural language processing). KGs enable not only powerful relational reasoning but also the ability to learn structural representations. Reasoning with KGs have been an extremely productive research direction, with many innovations leading to improvements to many downstream applications. However, real-world KGs are usually incomplete. As such, completing KGs and predicting missing links between entities have gained growing interest. Learning low-dimensional representations of entities and relations for KGs is an effective solution for this task.

There are numerous benefits of this formulation. (1) The Hamilton operator provides a greater extent of expressiveness compared to the complex Hermitian operator and the inner product in Euclidean space. The Hamilton operator forges inter-latent interactions between all of r,i,j,kr,\textbf{i},\textbf{j},\textbf{k}, resulting in a highly expressive model. (2) Quaternion representations are highly desirable for parameterizing smooth rotation and spatial transformations in vector space. They are generally considered robust to sheer/scaling noise and perturbations (i.e., numerically stable rotations) and avoid the problem of Gimbal locks. Moreover, quaternion rotations have two planes of rotationA plane of rotation is an abstract object used to describe or visualize rotations in space. while complex rotations only work on single plane, giving the model more degrees of freedom. (3) Our QuatE framework subsumes the ComplEx method, concurrently inheriting its attractive properties such as its ability to model symmetry, anti-symmetry, and inversion. (4) Our model can maintain equal or even less parameterization, while outperforming previous work.

Experimental results demonstrate that our method achieves state-of-the-art performance on four well-established knowledge graph completion benchmarks (WN18, FB15K, WN18RR, and FB15K-237).

Related Work

Knowledge graph embeddings have attracted intense research focus in recent years, and a myriad of embedding methodologies have been proposed. We roughly divide previous work into translational models and semantic matching models based on the scoring function, i.e. the composition over head & tail entities and relations.

Translational methods popularized by TransE (Bordes et al., 2013) are widely used embedding methods, which interpret relation vectors as translations in vector space, i.e., head+relationtailhead+\textit{relation}\approx tail. A number of models aiming to improve TransE are proposed subsequently. TransH (Wang et al., 2014) introduces relation-specific hyperplanes with a normal vector. TransR (Lin et al., 2015) further introduces relation-specific space by modelling entities and relations in distinct space with a shared projection matrix. TransD (Ji et al., 2015) uses independent projection vectors for each entity and relation and can reduce the amount of calculation compared to TransR. TorusE (Ebisu and Ichise, 2018) defines embeddings and distance function in a compact Lie group, torus, and shows better accuracy and scalability. The recent state-of-the-art, RotatE (Sun et al., 2019), proposes a rotation-based translational method with complex-valued embeddings.

On the other hand, semantic matching models include bilinear models such as RESCAL (Nickel et al., 2011), DistMult (Yang et al., 2014), HolE (Nickel et al., 2016), and ComplEx (Trouillon et al., 2016), and neural-network-based models. These methods measure plausibility by matching latent semantics of entities and relations. In RESCAL, each relation is represented with a square matrix, while DistMult replaces it with a diagonal matrix in order to reduce the complexity. SimplE (Kazemi and Poole, 2018) is also a simple yet effective bilinear approach for knowledge graph embedding. HolE explores the holographic reduced representations and makes use of circular correlation to capture rich interactions between entities. ComplEx embeds entities and relations in complex space and utilizes Hermitian product to model the antisymmetric patterns, which has shown to be immensely helpful in learning KG representations. The scoring function of ComplEx is isomorphic to that of HolE (Trouillon and Nickel, 2017). Neural networks based methods have also been adopted, e.g., Neural Tensor Network (Socher et al., 2013) and ER-MLP (Dong et al., 2014) are two representative neural network based methodologies. More recently, convolution neural networks (Dettmers et al., 2018), graph convolutional networks (Schlichtkrull et al., 2018), and deep memory networks (Wang et al., 2018) also show promising performance on this task.

Different from previous work, QuatE takes the advantages (e.g., its geometrical meaning and rich representation capability, etc.) of quaternion representations to enable rich and expressive semantic matching between head and tail entities, assisted by relational rotation quaternions. Our framework subsumes DistMult and ComplEx, with the capability to generalize to more advanced hypercomplex spaces. QuatE utilizes the concept of geometric rotation. Unlike the RotatE which has only one plane of rotation, there are two planes of rotation in QuatE. QuatE is a semantic matching model while RotatE is a translational model. We also point out that the composition property introduced in TransE and RotatE can have detrimental effects on the KG embedding task.

Quaternion is a hypercomplex number system firstly described by Hamilton (Hamilton, 1844) with applications in a wide variety of areas including astronautics, robotics, computer visualisation, animation and special effects in movies, and navigation. Lately, Quaternions have attracted attention in the field of machine learning. Quaternion recurrent neural networks (QRNNs) obtain better performance with fewer number of free parameters than traditional RNNs on the phoneme recognition task. Quaternion representations are also useful for enhancing the performance of convolutional neural networks on multiple tasks such as automatic speech recognition (Parcollet et al., ) and image classification (Gaudet and Maida, 2018; Parcollet et al., 2018a). Quaternion multiplayer perceptron (Parcollet et al., 2016) and quaternion autoencoders (Parcollet et al., 2017) also outperform standard MLP and autoencoder. In a nutshell, the major motivation behind these models is that quaternions enable the neural networks to code latent inter- and intra-dependencies between multidimensional input features, thus, leading to more compact interactions and better representation capability.

Hamilton’s Quaternions

Conjugate: The conjugate of a quaternion QQ is defined as Qˉ=abicjdk\bar{Q}=a-b\textbf{i}-c\textbf{j}-d\textbf{k}.

Norm: The norm of a quaternion is defined as Q=a2+b2+c2+d2|Q|=\sqrt{a^{2}+b^{2}+c^{2}+d^{2}}.

Inner Product: The quaternion inner product between Q1=a1+b1i+c1j+d1kQ_{1}=a_{1}+b_{1}\textbf{i}+c_{1}\textbf{j}+d_{1}\textbf{k} and Q2=a2+b2i+c2j+d2kQ_{2}=a_{2}+b_{2}\textbf{i}+c_{2}\textbf{j}+d_{2}\textbf{k} is obtained by taking the inner products between corresponding scalar and imaginary components and summing up the four inner products:

Hamilton Product (Quaternion Multiplication): The Hamilton product is composed of all the standard multiplications of factors in quaternions and follows the distributive law, defined as:

which determines another quaternion. Hamilton product is not commutative. Spatial rotations can be modelled with quaternions Hamilton product. Multiplying a quaternion, Q2Q_{2}, by another quaternion Q1Q_{1}, has the effect of scaling Q1Q_{1} by the magnitude of Q2Q_{2} followed by a special type of rotation in four dimensions. As such, we can also rewrite the above equation as:

Method

Suppose that we have a knowledge graph G\mathcal{G} consisting of NN entities and MM relations. E\mathcal{E} and R\mathcal{R} denote the sets of entities and relations, respectively. The training set consists of triplets (h,r,t)(h,r,t), where h,tEh,t\in\mathcal{E} and rRr\in\mathcal{R}. We use Ω\Omega and Ω=E×R×EΩ\Omega^{\prime}=\mathcal{E}\times\mathcal{R}\times\mathcal{E}-\Omega to denote the set of observed triplets and the set of unobserved triplets, respectively. Yhrt{1,1}Y_{hrt}\in\{-1,1\} represents the corresponding label of the triplet (h,r,t)(h,r,t). The goal of knowledge graph embeddings is to embed entities and relations to a continuous low-dimensional space, while preserving graph relations and semantics.

In this paper, we propose learning effective representations for entities and relations with quaternions. We leverage the expressive rotational capability of quaternions. Unlike RotatE which has only one plane of rotation (i.e., complex plane, shown in Figure 1(a)), QuatE has two planes of rotation. Compared to Euler angles, quaternion can avoid the problem of gimbal lock (loss of one degree of freedom). Quaternions are also more efficient and numerically stable than rotation matrices. The proposed method can be summarized into two steps: (1) rotate the head quaternion using the unit relation quaternion; (2) take the quaternion inner product between the rotated head quaternion and the tail quaternion to score each triplet. If a triplet exists in the KG, the model will rotate the head entity with the relation to make the angle between head and tail entities smaller so the product can be maximized. Otherwise, we can make the head and tail entity be orthogonal so that their product becomes zero.

We first normalize the relation quaternion WrW_{r} to a unit quaternion Wr=p+qi+uj+vkW_{r}^{\triangleleft}=p+q\textbf{i}+u\textbf{j}+v\textbf{k} to eliminate the scaling effect by dividing WrW_{r} by its norm:

We visualize a unit quaternion in Figure 1(c) by projecting it into a 3D space. We keep the unit hypersphere which passes through i,j,k\textbf{i},\textbf{j},\textbf{k} in place. The unit quaternion can be project in, on, or out of the unit hypersphere depending on the value of the real part.

Secondly, we rotate the head entity QhQ_{h} by doing Hamilton product between it and WrW_{r}^{\triangleleft}:

where \circ denotes the element-wise multiplication between two vectors. Right-multiplication by a unit quaternion is a right-isoclinic rotation on Quaternion QhQ_{h}. We can also swap QhQ_{h} and WrW_{r}^{\triangleleft} and do a left-isoclinic rotation, which does not fundamentally change the geometrical meaning. Isoclinic rotation is a special case of double plane rotation where the angles for each plane are equal.

We apply the quaternion inner product as the scoring function:

Following Trouillon et al. (2016), we formulate the task as a classification problem, and the model parameters are learned by minimizing the following regularized logistic loss:

For parameters initilaization, we can adopt the initialization algorithm in (Parcollet et al., 2018b) tailored for quaternion-valued networks to speed up model efficiency and convergence (Glorot and Bengio, 2010). The initialization of entities and relations follows the rule:

where wreal,wi,wj,wkw_{real},w_{i},w_{j},w_{k} denote the scalar and imaginary coefficients, respectively. θ\theta is randomly generated from the interval [π,π][-\pi,\pi]. QimgQ_{img}^{\triangleleft} is a normalized quaternion, whose scalar part is zero. φ\varphi is randomly generated from the interval [12k,12k][-\frac{1}{\sqrt{2k}},\frac{1}{\sqrt{2k}}], reminiscent to the He initialization (He et al., 2015). This initialization method is optional.

2 Discussion

Table 1 summarizes several popular knowledge graph embedding models, including scoring functions, parameters, and time complexities. TransE, HolE, and DistMult use Euclidean embeddings, while ComplEx and RotatE operate in the complex space. In contrast, our model operates in the quaternion space.

Capability in Modeling Symmetry, Antisymmetry and Inversion. The flexibility and representational power of quaternions enable us to model major relation patterns at ease. Similar to ComplEx, our model can model both symmetry (r(x,y)r(y,x))(r(x,y)\Rightarrow r(y,x)) and antisymmetry (r(x,y)r(y,x))(r(x,y)\Rightarrow\urcorner r(y,x)) relations. The symmetry property of QuatE can be proved by setting the imaginary parts of WrW_{r} to zero. One can easily check that the scoring function is antisymmetric when the imaginary parts are nonzero.

As for the inversion pattern (r1(x,y)r2(y,x))(r_{1}(x,y)\Rightarrow r_{2}(y,x)) , we can utilize the conjugation of quaternions. Conjugation is an involution and is its own inverse. One can easily check that:

The detailed proof of antisymmetry and inversion can be found in the appendix.

Composition patterns are commonplace in knowledge graphs (Lao et al., 2011; Neelakantan et al., 2015). Both transE and RotatE have fixed composition methods (Sun et al., 2019). TransE composes two relations using the addition (r1+r2r_{1}+r_{2}) and RotatE uses the Hadamard product (r1r2r_{1}\circ r_{2}). We argue that it is unreasonable to fix the composition patterns, as there might exist multiple composition patterns even in a single knowledge graph. For example, suppose there are three persons x,y,z"``x,y,z". If yy is the elder sister (denoted as r1r_{1}) of xx and zz is the elder brother (denoted as r2r_{2}) of yy, we can easily infer that zz is the elder brother of xx. The relation between zz and xx is r2r_{2} instead of r1+r2r_{1}+r_{2} or r1r2r_{1}\circ r_{2}, violating the two composition methods of TransE and RotatE. In QuatE, the composition patterns are not fixed. The relation between zz and xx is not only determined by relations r1r_{1} and r2r_{2} but also simultaneously influenced by entity embeddings.

Connection to DistMult and ComplEx. Quaternions have more degrees of freedom compared to complex numbers. Here we show that the QuatE framework can be seen as a generalization of ComplEx. If we set the coefficients of the imaginary units j and k to zero, we get complex embeddings as in ComplEx and the Hamilton product will also degrade to complex number multiplication. We further remove the normalization of the relational quaternion, obtaining the following equation:

where a,b,c=kakbkck\langle a,b,c\rangle=\sum_{k}a_{k}b_{k}c_{k} denotes standard component-wise multi-linear dot product. Equation 10 recovers the form of ComplEx. This framework brings another mathematical interpretation for ComplEx instead of just taking the real part of the Hermitian product. Another interesting finding is that Hermitian product is not necessary to formulate the scoring function of ComplEx.

If we remove the imaginary parts of all quaternions and remove the normalization step, the scoring function becomes ϕ(h,r,t)=ah,ar,at\phi(h,r,t)=\langle a_{h},a_{r},a_{t}\rangle, degrading to DistMult in this case.

Experiments and Results

Datasets Description: We conducted experiments on four widely used benchmarks, WN18, FB15K, WN18RR and FB15K-237, of which the statistics are summarized in Table 2. WN18 (Bordes et al., 2013) is extracted from WordNethttps://wordnet.princeton.edu/, a lexical database for English language, where words are interlinked by means of conceptual-semantic and lexical relations. WN18RR (Dettmers et al., 2018) is a subset of WN18, with inverse relations removed. FB15K (Bordes et al., 2013) contains relation triples from Freebase, a large tuple database with structured general human knowledge. FB15K-237 (Toutanova and Chen, 2015) is a subset of FB15K, with inverse relations removed.

Evaluation Protocol: Three popular evaluation metrics are used, including Mean Rank (MR), Mean Reciprocal Rank (MRR), and Hit ratio with cut-off values n=1,3,10n=1,3,10. MR measures the average rank of all correct entities with a lower value representing better performance. MRR is the average inverse rank for correct entities. Hit@n measures the proportion of correct entities in the top nn entities. Following Bordes et al. (2013), filtered results are reported to avoid possibly flawed evaluation.

Baselines: We compared QuatE with a number of strong baselines. For Translational Distance Models, we reported TransE (Bordes et al., 2013) and two recent extensions, TorusE (Ebisu and Ichise, 2018) and RotatE (Sun et al., 2019); For Semantic Matching Models, we reported DistMult (Yang et al., 2014), HolE (Nickel et al., 2016), ComplEx (Trouillon et al., 2016) , SimplE (Kazemi and Poole, 2018), ConvE (Dettmers et al., 2018), R-GCN (Schlichtkrull et al., 2018), and KNGE (ConvE based) (Wang et al., 2018).

Implementation Details: We implemented our model using pytorchhttps://pytorch.org/ and tested it on a single GPU. The hyper-parameters are determined by grid search. The best models are selected by early stopping on the validation set. In general, the embedding size kk is tuned amongst {50,100,200,250,300}\{50,100,200,250,300\}. Regularization rate λ1\lambda_{1} and λ2\lambda_{2} are searched in {0,0.01,0.05,0.1,0.2}\{0,0.01,0.05,0.1,0.2\}. Learning rate is fixed to 0.10.1 without further tuning. The number of negatives (#neg\#neg) per training sample is selected from {1,5,10,20}\{1,5,10,20\}. We create 1010 batches for all the datasets. For most baselines, we report the results in the original papers, and exceptions are provided with references. For RotatE (without self-adversarial negative sampling), we use the best hyper-parameter settings provided in the paper to reproduce the results. We also report the results of RotatE with self-adversarial negative sampling and denote it as a-RotatE. Note that we report three versions of QuatE: including QuatE with/without type constraints, QuatE with N3 regularization and reciprocal learning. Self-adversarial negative sampling (Sun et al., 2019) is not used for QuatE. All hyper-parameters of QuatE are provided in the appendix.

2 Results

The empirical results on four datasets are reported in Table 3 and Table 4. QuatE performs extremely competitively compared to the existing state-of-the-art models across all metrics. As a quaternion-valued method, QuatE outperforms the two representative complex-valued models ComplEx and RotatE. The performance gains over RotatE also confirm the advantages of quaternion rotation over rotation in the complex plane.

On the WN18 dataset, QuatE outperforms all the baselines on all metrics except Hit@10. R-GCN+ achieves high value on Hit@10, yet is surpassed by most models on the other four metrics. The four recent models NKGE, TorusE, RotaE, and a-RotatE achieves comparable results. QuatE also achieves the best results on the FB15K dataset, while the second best results scatter amongst RotatE, a-RotatE and DistMult. We are well-aware of the good results of DistMult reported in (Kadlec et al., 2017), yet they used a very large negative sampling size (i.e., 10001000, 20002000). The results also demonstrate that QuatE can effectively capture the symmetry, antisymmetry and inversion patterns since they account for a large portion of the relations in these two datasets.

As shown in Table 4, QuatE achieves a large performance gain over existing state-of-the-art models on the two datasets where trivial inverse relations are removed. On WN18RR in which there are a number of symmetry relations, a-RotatE is the second best, while other baselines are relatively weaker. The key competitors on the dataset FB15K-237 where a large number of composition patterns exist are NKGE and a-RotatE. Table 5 summarizes the MRR for each relation on WN18RR, confirming the superior representation capability of quaternion in modelling different types of relation. Methods with fixed composition patterns such as TransE and RotatE are relatively weak at times.

We can also apply N3 regularization and reciprocal learning approaches (Lacroix et al., 2018) to QuatE. Results are shown in Table 3 and Table 4 as QuatE2\text{QuatE}^{2}. It is observed that using N3 and reciprocal learning could boost the performances greatly, especially on FB15K and FB15K-237. We found that the N3 regularization method can reduce the norm of relations and entities embeddings so that we do not apply relation normalization here. However, same as the method in (Lacroix et al., 2018), QuatE2\text{QuatE}^{2} requires a large embedding dimension.

3 Model Analysis

Number of Free Parameters Comparison. Table 6 shows the amount of parameters comparison between QuatE1\text{QuatE}^{1} and two recent competitive baselines: RotatE and TorusE. Note that QuatE3\text{QuatE}^{3} uses almost the same number of free parameters as QuatE1\text{QuatE}^{1}. TorusE uses a very large embedding dimension 1000010000 for both WN18 and FB15K. This number is even close to the entities amount of FB15K which we think is not preferable since our original intention is to embed entities and relations to a lower dimensional space. QuatE reduces the parameter size of the complex-valued counterpart RotatE largely. This is more significant on datasets without trivial inverse relations, saving up to 80%80\% parameters while maintaining superior performance.

Ablation Study on Quaternion Normalization. We remove the normalization step in QuatE and use the original relation quaternion WrW_{r} to project head entity. From Table 7, we clearly observe that normalizing the relation to unit quaternion is a critical step for the embedding performance. This is likely because scaling effects in nonunit quaternions are detrimental.

Hamilton Products between Head and Tail Entities. We reformulate the scoring function of QuatE following the original formulate of ComplEx. We do Hamilton product between head and tail quaternions and consider the relation quaternion as weight. Thus, we have ϕ(h,r,t)=Wr(QhQt)\phi(h,r,t)=W_{r}\cdot(Q_{h}\otimes Q_{t}). As a result, the geometric property of relational rotation is lost, which leads to poor performance as shown in Table 7.

Additional Rotational Quaternion for Tail Entity. We hypothesize that adding an additional relation quaternion to tail entity might bring the model more representation capability. So we revise the scoring function to (QhWr)(QtVr)(Q_{h}\otimes W_{r}^{\triangleleft})\cdot(Q_{t}\otimes V_{r}^{\triangleleft}), where VrV_{r} represents the rotational quaternion for tail entity. From Table 7, we observe that it achieves competitive results without extensive tuning. However, it might cause some losses of efficiency.

Conclusion

In this paper, we design a new knowledge graph embedding model which operates on the quaternion space with well-defined mathematical and physical meaning. Our model is advantageous with its capability in modelling several key relation patterns, expressiveness with higher degrees of freedom as well as its good generalization. Empirical experimental evaluations on four well-established datasets show that QuatE achieves an overall state-of-the-art performance, outperforming multiple recent strong baselines, with even fewer free parameters.

This research was partially supported by grant ONRG NICOP N62909-19-1-2009

References

Appendix

In order to prove the antisymmetry pattern, we need to prove the following inequality when imaginary components are nonzero:

We can easily see that those two terms are not equal as the signs for some terms are not the same.

To prove the inversion pattern, we need to prove that:

We can easily check the equality of these two terms.

2 Hyperparameters Settings

We list the best hyperparameters setting of QuatE on the benchmark datasets:

Hyperparameters for QuatE1\textbf{QuatE}^{1} without type constraints:

WN18: k=300,λ1=0.05,λ2=0.05,#neg=10k=300,\lambda_{1}=0.05,\lambda_{2}=0.05,\#neg=10

FB15K: k=200,λ1=0.05,λ2=0.05,#neg=10k=200,\lambda_{1}=0.05,\lambda_{2}=0.05,\#neg=10

WN18RR: k=100,λ1=0.1,λ2=0.1,#neg=1k=100,\lambda_{1}=0.1,\lambda_{2}=0.1,\#neg=1

FB15K-237: k=100,λ1=0.3,λ2=0.3,#neg=10k=100,\lambda_{1}=0.3,\lambda_{2}=0.3,\#neg=10

Hyperparameters for QuatE2\textbf{QuatE}^{2} with N3 regularization and reciprocal learning, without type constraints:

Hyperparameters for QuatE3\textbf{QuatE}^{3} with type constraint:

WN18: k=250,λ1=0.05,λ2=0,#neg=10k=250,\lambda_{1}=0.05,\lambda_{2}=0,\#neg=10

FB15K: k=200,λ1=0.1,λ2=0,#neg=20k=200,\lambda_{1}=0.1,\lambda_{2}=0,\#neg=20

WN18RR: k=100,λ1=0.1,λ2=0.1,#neg=1k=100,\lambda_{1}=0.1,\lambda_{2}=0.1,\#neg=1

FB15K-237: k=100,λ1=0.2,λ2=0.2,#neg=10k=100,\lambda_{1}=0.2,\lambda_{2}=0.2,\#neg=10

Number of epochs. The number of epochs needed of QuatE and RotatE are shown in Table 8.

3 Octonion for Knowledge Graph embedding

Apart from Quaternion, we can also extend our framework to Octonions (hypercomplex number with one real part and seven imaginary parts) and even Sedenions (hypercomplex number with one real part and fifteen imaginary parts). Here, we use OctonionE to denote the method with Octonion embeddings and details are given in the following text.

The conjugate of Octonion is defined as: O1ˉ=x0x1e1x2e2x3e3x4e4x5e5x6e6x7e7\bar{O_{1}}=x_{0}-x_{1}\textbf{e}_{1}-x_{2}\textbf{e}_{2}-x_{3}\textbf{e}_{3}-x_{4}\textbf{e}_{4}-x_{5}\textbf{e}_{5}-x_{6}\textbf{e}_{6}-x_{7}\textbf{e}_{7}.

The norm of Octonion is defined as: O1=x02+x12+x22+x32+x42+x52+x62+x72|O_{1}|=\sqrt{x_{0}^{2}+x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2}+x_{5}^{2}+x_{6}^{2}+x_{7}^{2}}.

If we have another Octonion: O2=y0+y1e1+y2e2+y3e3+y4e4+y5e5+y6e6+y7e7O_{2}=y_{0}+y_{1}\textbf{e}_{1}+y_{2}\textbf{e}_{2}+y_{3}\textbf{e}_{3}+y_{4}\textbf{e}_{4}+y_{5}\textbf{e}_{5}+y_{6}\textbf{e}_{6}+y_{7}\textbf{e}_{7}. We derive the multiplication rule with the Fano Plane.

We can also consider Octonions as a combination of two Quaternions. The scoring functions of OctonionE remains the same as QuatE.

The results of OctonionE on dataset WN18 and WN18RR are given below. We observe that OctonionE performs equally to QuatE. It seems that extending the model to Octonion space does not give additional benefits. Octonions lose some algebraic properties such as associativity, which might bring some side effects to the model.