SlowGuess/NAACL_2022_OCR huggingface dataset:
Jiaxin Bai $^{1}$ , Zihao Wang $^{1}$ , Hongming Zhang $^{2}$ , and Yangqiu Song $^{1}$
$^{1}$ CSE, HKUST
{jbai, zwanggc, yqsong}@cse.ust.hk, hongmzhang@tencent.com
Answering complex logical queries on incomplete knowledge graphs (KGs) with missing edges is a fundamental and important task for knowledge graph reasoning. The query embedding method is proposed to answer these queries by jointly encoding queries and entities to the same embedding space. Then the answer entities are selected according to the similarities between the entity embeddings and the query embedding. As the answers to a complex query are obtained from a combination of logical operations over sub-queries, the embeddings of the answer entities may not always follow a uni-modal distribution in the embedding space. Thus, it is challenging to simultaneously retrieve a set of diverse answers from the embedding space using a single and concentrated query representation such as a vector or a hyperrectangle. To better cope with queries with diversified answers, we propose Query2Particles (Q2P), a complex KG query answering method. Q2P encodes each query into multiple vectors, named particle embeddings. By doing so, the candidate answers can be retrieved from different areas over the embedding space using the maximal similarities between the entity embeddings and any of the particle embeddings. Meanwhile, the corresponding neural logic operations are defined to support its reasoning over arbitrary first-order logic queries. The experiments show that Query2Particles achieves state-of-the-art performance on the complex query answering tasks on FB15k, FB15K-237, and NELL knowledge graphs.
Reasoning over a factual knowledge graph (KG) is the process of deriving new knowledge or conclusions from the existing data in the knowledge graph (Chen et al., 2020). A recently developed sub-task of knowledge graph reasoning is complex query answering, which aims to answer complex queries over large knowledge graphs (Hamilton et al., 2018;
Logical Knowledge Graph Queries	Interpretations
q1 = V7..3V: Win(TuringAward, V) ∧ ¬Citizen(Canada, V) ∧ Graduate(V, V2)	Find where the non-Canadian Turing award laureates graduated from.
q2 = V7..3V: Interacts(V2, V) ∧ (Assoc(V, T1) ∨ Assoc(V, T2) ∨ Assoc(V, T3))	Find the substances that interact with the proteins associated with diseases T1, T2, or T3.
q3 = V7: MovesTo(V7, America) ∧ ¬IsResident(V7, America) ∧ Wins(V7, NobelPrize)	Find entities, who are not American, were the Nobel Prize winners and eventually moved to the US.
Figure 1: The example logical knowledge graph queries and their interpretations in natural language.
Ren et al., 2020; Ren and Leskovec, 2020). Compared to KG completion tasks (Liu et al., 2016; West et al., 2014), complex query answering requires reasoning over multi-hop relations and logical operations. As shown in Figure 1, complex KG queries are defined in predicate logic forms with relation projection operations, existential quantifiers  $\exists$ , logical conjunctions  $\land$ , disjunctions  $\lor$ , and negation  $\neg$ . Answering these queries is challenging because real-world knowledge graphs (KG), such as Freebase (Bollacker et al., 2008), NELL (Carlson et al., 2010), and DBPedia (Bizer et al., 2009), are incomplete. Consequently, sub-graph matching methods cannot be used to find the answers.
To address the challenge raised from the incompleteness of knowledge graphs, the query embedding methods are proposed (Hamilton et al., 2018; Ren et al., 2020; Ren and Leskovec, 2020; Sun et al., 2020). In this line of research, the queries and entities are jointly encoded into the same embedding space, and the answers are retrieved based on similarities between the query embedding and entity embeddings. In general, there are two steps in encoding a query to the vector space. First, a query is parsed into a computational graph with a directed acyclic graph (DAG) structure, as shown in Figure 2 (A). Then, the query representation is iteratively computed following the neural logic operations and relation projections in the DAG.
Although the query embedding methods are robust for dealing with the incompleteness of KGs,
Findings of the Association for Computational Linguistics: NAACL 2022, pages 2703 - 2714
Figure 2: An example of answering a knowledge graph query by using the Query2Particles method. (A) The computational graph corresponds to the query "where did the non-Canadian Turing award laureates graduate from." (B) The Query2Particles encodes each query into a set of vectors, called particle embeddings. The logical operations iteratively compute particle embeddings following the computational graph. The answers are determined by using the maximum similarities between the entity embeddings and any one of the resulting particle embeddings.
the embedding structure used for encoding the queries can be improved. Because of the multi-hop and compositional nature of complex KG queries, a single query may contain multiple sufficiently diverse answers. Thus, the ideal query embedding may follow a multi-modal distribution<sup>1</sup> in the embedding space. For example, the answers to the query, “Find entities, who are not American, were the Nobel Prize winners and eventually moved to the US,” involve intermediate entities with different attributes, such as gender, nationality, research fields, etc. It is difficult to use a single embedding vector to find all final answer embeddings. Box embedding (Ren et al., 2020) partially solved this problem, but for complicated attributes, a single box may be too coarse, and intermediate entities are distributed far away from each other, so they are more like several disjoint clusters rather than a single big region in the embedding space. So for the query embedding methods, the capability to simultaneously encode a set of answers from different areas is necessary.
To better address the diversity of answers, we propose Query2Particles, a new query embedding method for complex query answering. In this approach, each query is encoded into a set of vectors in the embedding space, called particle embeddings. The particle embeddings of a query are iteratively computed by following the computational graph parsed from the query. Then the answers to this query are determined by using the maximum similarities between the entity embeddings and any one of the resulting particle embeddings. Experimental results show that Query2Particle achieves state-of-the-art performance on complex query answering over three standard knowledge graphs: FB15K, FB15k-237, and NELL. Meanwhile, the inference
Figure 3: In the example embedding space, the yellow dots are the answer entities, and the blue dots are the non-answer entities. The purple areas in (B), (C), and (D) demonstrate the neighborhoods of the vector embedding, the box embedding, and the particle embeddings respectively. In this case, the particle embeddings are more suitable for finding the answers clustered in different areas in the embedding space.
speed of Query2Particles is comparable to other query embedding methods and is higher than query decomposition methods on multi-hop queries. Further analysis indicates that the optimal numbers of particles for different query types depend on the structures of the queries. Our experimental code is released on github<sup>2</sup>.
Other query embedding approaches are closely related to our work. These query embedding methods leverage different structures to encode logical KG queries, and they can answer various scopes of logical queries. The GQE method proposed by Hamilton et al. (2018) can answer the conjunctive queries by representing queries as vector representations. Ren et al. (2020) used hyper-rectangles to encode and answer existential positive first-order (EPFO) queries. At the same time, Sun et al. (2020) proposed to improve the faithfulness of the query embedding method by using centroid-sketch representations on EPFO queries. The conjunctive queries and EPFO queries are both subsets of first-order logic (FOL) queries. The Beta Embedding (Ren and Leskovec, 2020) is the first query embed
$^{2}$ https://github.com/HKUST-KnowComp/query2particles
<sup>1</sup>A multi-modal distribution is a distribution with two or more distinct peaks in the probability density function.
ding method that supports a full set of operations in FOL by encoding entities and queries into probabilistic Beta distributions. In a contemporaneous work, Zhang et al. (2021) uses cone embeddings to encode the FOL queries. As shown in Figure 3, compared to these query embedding approaches, the Q2P method can encode the FOL queries to address the diversity of answers. Note that, Ren et al. (2020) proposed to use the disjunctive normal form (DNF) to address the answer diversities resulting from the union operations. This partly solve the problem, but the diversity of the answers is not solely caused by the union operation, but a joint effort of multi-hop projections, intersection, and complement. As a result, using particle embeddings is a more general solution.
Query decomposition (Arakelyan et al., 2020) is another approach to answering complex knowledge graph queries. In this line of research, a complex query is decomposed into atomic queries, and the probabilities of atomic queries are modeled by link predictors. In the inference process, continuous optimization and beam search are used for finding the answers. Meanwhile, the rule and path-based methods (Guo et al., 2016; Xiong et al., 2017; Lin et al., 2018; Guo et al., 2018; Chen et al., 2019) use pre-defined or learned rules to do multi-hop KG reasoning. These methods explicitly model the intermediate entities in the query. Instead, the query embedding methods directly embed the complex query and retrieve the answers without explicit modeling intermediate entities. So the query embedding methods are more scalable to large knowledge graphs and complex query structures.
Neural link predictors (Wang et al., 2014; Trouillon et al., 2016; Dettmers et al., 2018; Sun et al., 2018) are also related to this work. The link predictors learn the distributed representations of entities and relations in embedding space and use different neural structures to classify whether there exists a certain relation between two entities. The link predictors can be used for one-hop queries, but cannot be directly used for answering complex queries.
In this section, we formally define the complex logical knowledge graph queries and the corresponding computational graphs. The knowledge graph reasoning is conducted on a multi-relational knowledge graph  $\mathcal{G} = (\mathcal{V},\mathcal{R})$ , where each vertex  $v\in \mathcal{V}$  represents an entity, and each relation  $r\in \mathcal{R}$  is a
binary function defined as  $r:\mathcal{V}\times \mathcal{V}\to \{0,1\}$  For any  $r\in \mathcal{R}$  , and  $u,v\in \mathcal{V}$  , there is a relation  $r$  between entities  $u$  and  $v$  if and only if  $r(u,v) = 1$
3.1 First-Order Logic Query
The complex knowledge graph query is defined in first-order logic form with logical operators such as existential quantifiers  $\exists$ , conjunctions  $\land$ , disjunctions  $\lor$ , and negations  $\neg$ . In a first-order logic query, there is a set of anchor entities  $V_{a} \in \mathcal{V}$ , existential quantified variables  $V_{1}, V_{2}, \ldots, V_{k} \in \mathcal{V}$  and a unique target variable  $V_{?} \in \mathcal{V}$ . The query intends to find the answers  $V_{?} \in \mathcal{V}$ , such that there simultaneously exist  $V_{1}, V_{2}, \ldots, V_{k} \in \mathcal{V}$  satisfying the logical expression in the query. For each FOL query, it can be converted to a disjunctive normal form, where the query is expressed as a disjunction of several conjunctive expressions:
$$
q \left[ V _ {?} \right] = V _ {?}. \exists V _ {1}, \dots , V _ {k}: c _ {1} \vee c _ {2} \vee \dots \vee c _ {n}, \tag {1}
$$
$$
c _ {i} = e _ {i 1} \wedge e _ {i 2} \wedge \dots \wedge e _ {i m}. \tag {2}
$$
Each  $c_{i}$  represents a conjunctive expression of several literals  $e_{ij}$ , and each  $e_{ij}$  is an atomic or the negation of an atomic expression expressed by any of the following expressions:  $e_{ij} = r(v_{a}, V)$ ,  $e_{ij} = \neg r(v_{a}, V)$ ,  $e_{ij} = r(V, V')$ , or  $e_{ij} = \neg r(V, V')$ . Here  $v_{a} \in V_{a}$  is one of the anchor entities, and  $V, V' \in \{V_{1}, V_{2}, \dots, V_{k}, V_{?}\}$  are distinct variables satisfying  $V \neq V'$ .
3.2 Computational Graph and Operations
As shown in Figure 2 (A), for a first-order query, there is a corresponding computational graph. In the computational graph, each node corresponds to an intermediate query embedding, and each edge corresponds to a neural logic operation to be defined in the following section. Both the input and output of these operations are query embeddings. These operations are used for implicitly modeling different set operations over the intermediate answer sets. These set operations include relational projection, intersection, union, and complement: (1) Relational Projection: Given a set of entities  $A$  and a relation  $r \in R$ , the relational projection will return all entities having relation  $r$  with at least one of entity  $e \in A$ . Namely,  $P_r(A) = \{v \in \mathcal{V} | \exists v' \in A, r(v', v) = 1\}$ ; (2) Intersection: Given sets of entities  $A_1, \ldots, A_n \subset \mathcal{V}$ , this operation computes their intersection  $\cap_{i=1}^{n} A_i$ ; (3) Union: Given several sets of entities  $A_1, \ldots, A_n \subset \mathcal{V}$ , the union operation calculates their union  $\cup_{i=1}^{n} A_i$ ; (4) Complement:
Given a set of entities  $A$ , the complement operation calculates its absolute complement  $\mathcal{V} - A$ .
4 Query2Particles
In this section, we first introduce the particle embeddings structure and the neural logic operations, and then we present the learning of the model.
4.1 Particles Representations of Queries
In Query2Particles, each query is represented as a set of vectors, called particles. For simplicity, a set of particles  $\{p^{(k)}\}_{k = 1}^{K}$  are represented as a matrix  $P$ . All the operations discussed in the following sections are invariant to the permutations of the particle vectors in the matrix. Formally, the particle embeddings  $P\in R^{d\times K}$  are
$$
P = \left[ p ^ {(1)}, p ^ {(2)}, \dots , p ^ {(K)} \right], \tag {3}
$$
where each vector  $p^{(k)}\in R^d$  is a particle vector. As shown in Figure 2, the computations along the computation graph start with the anchor entities, such as "Turing Award". Suppose the entity embedding of an anchor entity  $v$  is denoted as  $e_v\in R^d$ . Then, the initial particle embeddings are computed as the sum of  $e_v$  and a learnable offset matrix  $M\in R^{d\times K}$
$$
P _ {0} = e _ {v} + M. \tag {4}
$$
Here and in the following sections, the addition between the matrix  $M$  and the vector  $e_v$  is defined as the broadcasted element-wise addition.
4.2 Logical Operations
In this sub-section, we define and parameterize four types of neural logic operations: projection, intersection, negation, and union.
4.2.1 Projection
Suppose the  $e_l \in R^d$  is the embedding vector of the relation  $l$ . The relation projection  $f_P$  is expressed as  $P_{i+1} = f_P(P_i, e_l)$ , where the  $P_i$  and  $P_{i+1}$  are input and output particle embeddings. Instead of directly adding the same relation embedding  $e_l$  to all particles in  $P_i$  to model the relation projection following (Bordes et al., 2013), we incorporate multiple neuralized gates (Chung et al., 2014) to individually adjust the relation transition for each particle in  $P_i$ , which are expressed as follows:
$$
Z = \sigma \left(W _ {z} ^ {P} e _ {l} + U _ {z} P _ {i} + b _ {z}\right), \tag {5}
$$
$$
R = \sigma \left(W _ {r} ^ {P} e _ {l} + U _ {r} P _ {i} + b _ {r}\right), \tag {6}
$$
$$
T = \phi \left(W _ {h} ^ {P} e _ {l} + U _ {h} \left(R \odot P _ {i}\right) + b _ {h}\right), \tag {7}
$$
$$
A _ {i} = (1 - Z) \odot P _ {i} + Z \odot T. \tag {8}
$$
Here,  $\sigma$  and  $\phi$  are the sigmoid and hyperbolic tangent functions, and  $\odot$  is the Hadamard product. Also,  $W_{z}^{P}, W_{r}^{P}, W_{h}^{P}, U_{z}, U_{r}, U_{h}$  are parameter matrices.  $T$  is interpreted as the relation transitions for each of the particles given the relation embedding  $e_l$ , and  $Z$  and  $R$  are the update gate and the reset gate used for customizing the relation transitions for each particle. Meanwhile, the relation projection result for each particle should also depend on the positions of other input particles. To allow information exchange among different particles, a scaled dot-product self-attention (Vaswani et al., 2017) module is also incorporated,
$$
P _ {i + 1} = \operatorname {A t t n} \left(W _ {q} ^ {P} A _ {i} ^ {T}, W _ {k} ^ {P} A _ {i} ^ {T}, W _ {v} ^ {P} A _ {i} ^ {T}\right) ^ {T}. \tag {9}
$$
The  $W_{q}^{P}, W_{k}^{P}, W_{v}^{P} \in R^{d \times d}$  are parameters used for modeling the input Query, Key, and Value for the self-attention module Attn. The Attn represents the scaled dot-product self-attention,
$$
\operatorname {A t t n} (Q, K, V) = \operatorname {S o f t m a x} \left(\frac {Q K ^ {T}}{\sqrt {d}}\right) V. \tag {10}
$$
Here, the  $Q$ ,  $K$ , and  $V$  represent the input Query, Key, and Value for this attention layer.
4.2.2 Intersection
The intersection operation  $f_{I}$  is defined on multiple sets of particle embeddings  $\{P_i^{(n)}\}_{n = 1}^N$ . It outputs a single set of particle embeddings  $P_{i + 1} = f_{I}(\{P_{i}^{(n)}\}_{n = 1}^{N})$ . The particles from the  $\{P_i^{(n)}\}_{n = 1}^N$  are first merged into a new matrix  $P_{i} = [P_{i}^{(1)},P_{i}^{(2)},\dots,P_{i}^{(N)}]\in R^{d\times NK}$ , and this matrix  $P_{i}$  serves as the input of the intersection operation. The operation updates the position of each particle according to the positions of other input particles in  $\{P_i^{(n)}\}_{n = 1}^N$ . This process is modeled by the scaled dot-product self-attention followed by a multi-layer perceptron (MLP) layer,
$$
A _ {i} = \operatorname {A t t n} \left(W _ {q} ^ {I} P _ {i} ^ {T}, W _ {k} ^ {I} P _ {i} ^ {T}, W _ {v} ^ {I} P _ {i} ^ {T}\right) ^ {T}, \tag {11}
$$
$$
P _ {i + 1} = \mathrm {M L P} \left(A _ {i}\right). \tag {12}
$$
Here  $W_{q}^{I}, W_{k}^{I}, W_{v}^{I} \in R^{d \times d}$  are parameters for the self-attention layer. The MLP here denotes a multilayer proceptron layer with ReLU activation, and the parameters in the MLP layers in different operations are not shared. To keep the number of particles unchanged, we uniformly sub-sample  $K$  particles out of the  $NK$  particles in  $P_{i+1}$  as the final output of the intersection operation.
(A) Projection
Figure 4: The  $P_{i}$  in blue and the  $P_{i+1}$  in yellow are the input and output particles respectively. (A) The embeddings  $P_{i}$  are projected to  $P_{i+1}$  by the relation  $l$ . (B) The resulting embeddings  $P_{i+1}$  are computed from three sets of particles  $P_{i}$  by the intersection  $f_{I}$ . (C) The output  $P_{i+1}$  are computed from the input  $P_{i}$  by the complement  $f_{C}$ . (D) The  $P_{i+1}$  are directly taken from the all input particles in  $P_{i}$  without any additional parameterization.
(B) Intersection
(C) Complement
(D) Union
4.2.3 Complement
The input of the complement operation is a single set of particle embeddings  $P_{i}$ , and the operation  $f_{C}$  is formulated as  $P_{i + 1} = f_{C}(P_{i})$ . The complement operation updates the position of each particle based on the distributions of other input particles. The operation is then modeled by scaled dot-product attention followed by an MLP layer, and this can be formulated by
$$
A _ {i} = \operatorname {A t t n} \left(W _ {q} ^ {C} P _ {i} ^ {T}, W _ {k} ^ {C} P _ {i} ^ {T}, W _ {v} ^ {C} P _ {i} ^ {T}\right) ^ {T}, \tag {13}
$$
$$
P _ {i + 1} = \operatorname {M L P} \left(A _ {i}\right). \tag {14}
$$
Here, the  $P_{i + 1}\in R^{d\times K}$  are the resulting particle embeddings for the complement operation, and the values in  $W_{q}^{C},W_{k}^{C},W_{v}^{C}\in R^{d\times d}$  are parameters. Intuitively speaking, the proposed structure can model the complement operation by encouraging the particles to move towards the areas that are not occupied by any of the input particles.
4.2.4 Union
The union operation is directly modeled by all the input particles without extra parameterization. In detail, the particles from the input particle embeddings are directly merged into a new set of particles,
$$
f _ {U} \left(\left\{P _ {i} ^ {(n)} \right\} _ {n = 1} ^ {N}\right) = \left[ P _ {i} ^ {(1)}, \dots , P _ {i} ^ {(N)} \right]. \tag {15}
$$
4.3 Scoring
After the particle embeddings  $P_{T} \in R^{d \times K}$  for the target variable of the query  $q$  are computed, the scoring function  $\phi$  between the particle embeddings  $P_{T}$  and each entity embedding  $e_{v}$  is used for calculating the maximal similarities between each particle vectors in  $\{p_{T}^{(k)}\}_{k=1}^{K}$  and entity embedding vector. Here, the inner product is used to compute the similarity scores between vectors, and the overall scoring function is expressed by
$$
\phi \left(P _ {T}, e _ {v}\right) = \max  _ {k \in \{1, 2, \dots , K \}} <   p _ {T} ^ {(k)}, e _ {v} >. \tag {16}
$$
4.4 Learning Query2Particles
To train the Query2Particles model, we compute the normalized probability of the entity  $v$  being the correct answer of query  $q$  by using the softmax function on all similarity scores,
$$
p (v, q) = \frac {\phi \left(P _ {T} , e _ {v}\right)}{\sum_ {v ^ {\prime} \in V} \phi \left(P _ {T} , e _ {v ^ {\prime}}\right)}. \tag {17}
$$
Then we construct the cross-entropy loss from the given probabilities to maximize the log probabilities of all correct query-answer pairs:
$$
L = - \frac {1}{N} \sum_ {i} \log p \left(v ^ {(i)}, q ^ {(i)}\right). \tag {18}
$$
The  $(v^{(i)},q^{(i)})$  denotes is one of the positive queryanswer pairs, and in total there are  $N$  such pairs.
The experiments in this section demonstrate the effectiveness and efficiency of Query2Particles.
The Query2Particles method is evaluated on three commonly used knowledge graphs, FB15K (Bordes et al., 2013), FB15K-237 (Toutanova and Chen, 2015), and NELL995 (Carlson et al., 2010) with the standard training, validation, and testing edges separations. For each of these graphs, the corresponding training graph  $G_{train}$ , validation graph  $G_{valid}$ , and testing graph  $G_{test}$  are created from training edges, training + validation edges, and training + validation + testing edges respectively.
There are two sets of complex logical queries sampled from these knowledge graphs, and the existing methods evaluate their performance on either of them. Specifically, Ren et al. (2020) sample nine different types of existential positive first-order (EPFO) queries. For these queries, five types of them (1p, 2p, 3p, 2i, 3i) are used for training and evaluation in a supervised setting. For the rest of four types of queries (2u, up, ip, pi), they
Figure 5: The query structures used for training and evaluation. For brevity, the  $p$ ,  $i$ ,  $n$ , and  $u$  represent the projection, intersection, negation, and union operations respectively. The query types on the left are trained and evaluated under supervised settings. There are not training queries for the four types of queries on the right, and they are directly evaluated at the test time to measure the generalization capability of the models to unseen query types.
FB15K	FB15K-237	NELL
MODEL	1P	2P	3P	2I	3I	PI	IP	2U	UP	AVG	Avg
BETAE	65.1	25.7	24.7	55.8	66.5	43.9	28.1	40.1	25.4	41.6	20.9
Q2B	68.0	21.0	14.2	55.1	66.5	39.4	26.1	35.1	16.7	38.0	20.1
GQE	54.6	15.3	10.8	39.7	51.4	27.6	19.1	22.1	11.6	28.0	16.3
Q2P (OURS)	82.6	30.8	25.5	65.1	74.7	49.5	34.9	32.1	26.2	46.8	21.9
Table 1: The MRR results for existential positive first-order (EPFO) queries used by Ren and Leskovec (2020). The full results are shown in the supplementary materials.