Neural Query Language: A Knowledge Base Query Language for Tensorflow

William W. Cohen, Matthew Siegler, R. Alex HoferarXiv 2019

Abstract

Large knowledge bases (KBs) are useful for many AI tasks, but are difficult to integrate into modern gradient-based learning systems. Here we describe a framework for accessing soft symbolic database using only differentiable operators. For example, this framework makes it easy to conveniently write neural models that adjust confidences associated with facts in a soft KB; incorporate prior knowledge in the form of hand-coded KB access rules; or learn to instantiate query templates using information extracted from text. NQL can work well with KBs with millions of tuples and hundreds of thousands of entities on a single GPU.

Introduction

In modern AI, a significant divide exists between symbolic reasoning—characterized by structured databases and logic—and connectionist learning, which relies on neural networks. While Knowledge Bases (KBs) like Freebase or Wikidata contain vast repositories of structured facts, their discrete and non-differentiable nature makes them difficult to integrate into gradient-based deep learning models.

Traditionally, querying a KB within a neural pipeline requires multi-step processes: entity linking, relation identification, and a "hard" database lookup. Because the lookup step is non-differentiable, end-to-end training is impossible. Neural Query Language (NQL) addresses this by treating database queries as differentiable dataflow operations, allowing for seamless integration into TensorFlow models.

Methodology: Differentiable KB Operations

NQL reinterprets the KB not as a logical system, but as a series of linear algebra operations. This shift allows queries to be represented as standard matrix-vector multiplications, which are natively differentiable.

  • Entities as Vectors: A set of entities is represented as a sparse vector, where each index corresponds to an entity and the value represents a "weight" or confidence score.
  • Relations as Matrices: A relationship (e.g., "father_of") is represented as a sparse adjacency matrix.
  • Querying as Multiplication: Following a relation from a set of entities is performed via matrix-vector multiplication.

NQL provides a suite of operators for union, intersection, and conditional filtering, all implemented as differentiable TensorFlow operations.

Table of NQL expressions and their corresponding vector-matrix specifications.

The language is embedded in Python and TensorFlow, converting high-level query code into a TensorFlow compute graph. This allows models to learn "Rule Templates," approximating complex relationships (like "uncle") by learning weights over simpler relations (like parent().brother()).

Practical Implementation

NQL's syntax is designed to be intuitive for programmers, resembling standard object-oriented code while executing complex tensor operations under the hood.

An example of NQL code querying a royal family dataset, showing the concise syntax and the resulting weighted entity sets.

As shown in the example above, NQL handles uncertainty through its "soft" KB approach. If an entity linker provides multiple possible matches with varying confidence, NQL propagates these weights through the query, allowing the model to maintain a distribution over possible answers.

Scalability and Performance

A key advantage of NQL is its scalability. By leveraging sparse matrix operations in TensorFlow, NQL can handle KBs with millions of tuples and hundreds of thousands of entities on a single commodity GPU. This is a significant improvement over previous differentiable logic systems, which often struggled with toy-scale datasets.

The framework's efficiency stems from its dataflow architecture. Unlike logic-based predecessors such as TensorLog, NQL's dataflow approach is more intuitive for integration with modern architectures like LSTMs or Transformers. This allows for end-to-end backpropagation from a final answer back to initial components like entity-linking or relation-extraction networks.

Discussion and Future Work

While NQL offers a powerful bridge between symbolic and neural systems, it has some limitations. Currently, it only returns sets of entities rather than tuples (e.g., parent-child pairs), which limits its application in certain traditional database tasks. Furthermore, its performance is tied to TensorFlow's sparse-sparse matrix multiplication capabilities.

Future work may involve automating the indexing process for complex relational joins, which currently requires manual schema engineering. Despite these challenges, NQL represents a significant step toward unifying structured knowledge with deep learning, providing a concise and flexible tool for building knowledge-aware neural models.

Summary updated

BibTeX
@misc{cohen2019neural,
  author = {William W. Cohen and Matthew Siegler and R. Alex Hofer},
  title = {{Neural Query Language: A Knowledge Base Query Language for Tensorflow}},
  year = {2019},
  eprint = {1905.06209},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1905.06209},
}
Figure