Towards a Practical Neural-Symbolic Framework.

Alexey Potapov
SingularityNET
Published in
9 min readNov 25, 2019

The Need for Neural-Symbolic Integration

Deep learning is still bearing fruits. However, the standard types of networks are exhausting their possibilities, and researchers seek out such extensions to the basic neural network models, which will weaken their inherent limitations. Some extensions such as self-attention layers have enjoyed great practical success.

Remarkably, many shortcomings of neural networks mirror the advantages of symbolic systems (and vice versa). Indeed, one can note that both self-attention layers and capsule networks are attempts to work around the notorious variable binding problem described in the Fodor and Pylyshyn’s paper, which is easily solved in symbolic systems but is very inconvenient for neural networks.

Neural networks are bad at solving compositional problems and require non-standard extensions to combat them (e.g. neural module networks and their variations), while compositionality is the main feature of symbolic systems.

Explicit knowledge constitutes the main content of symbolic systems, but its integration into neural networks is problematic. And yet even limited use of prior knowledge can yield important improvements in neural language understanding, visual scene understanding (and even image classification), etc.

Although we can see that neural networks can be extended to compensate for one missing aspect or another, this is done for each aspect independently and in a task-specific way. The very idea of the neural-symbolic approach is to utilize the strengths of both neural and symbolic paradigms to compensate for all the drawbacks of each of them at once, basically, to combine flexible learning with powerful reasoning.

An interesting approach to neural-symbolic integration is to use the same computational model to represent both neural and symbolic operations, or, more concretely, to map logic-based knowledge representation and inference into neural networks. On the one hand, one can claim that limitations of the neural networks are the very reason to resort to symbolic systems, and such mapping is not useful. On the other hand, the brain does consist of neurons, which are the computational substrate for symbolic reasoning also, and although artificial neurons differ from natural neurons, they can be developed further to capture the necessary properties making this mapping efficient. In particular, the works on Neural Symbolic Cognitive Agent show the possibility to map First-Order Logic into Restricted Boltzmann Machines.

This approach is very interesting and promising, but it restricts us to the models of a certain type, leaving outside its scope a huge body of elaborated models and inference techniques from both neural and symbolic domains. Indeed, the success of deep learning is not due to one particular model, but due to the frameworks that make developing new models simple and efficient. Thus, what can be really impactful is a hybrid neural-symbolic framework that extends an existing deep learning framework with a symbolic knowledge representation and reasoning system.

Here, we present a prototype of such a framework that integrates the PyTorch deep learning library with the OpenCog cognitive architecture and discuss its utility on an example of visual reasoning.

Problem

As we discussed earlier, the most basic (yet frequently used) approach to hybrid neural-symbolic systems, which consists simply in running deep neural networks (DNNs) to produce symbols for subsequent symbolic reasoning, is far from enough, and cognitive feedback to control DNNs processing subsymbolic data (e.g. image) is necessary, which is lacking in the naïve hybrid systems.

This situation can be understood on an example of visual question answering (VQA). Indeed, reliable and exhaustive symbolic descriptions, which would be enough for answering questions via reasoning, cannot be extracted in a bottom-up data-driven way. Even humans analyze images in detail depending on their goals (e.g. questions). For example, are all boats on the same side of the river as bicycles and cars in the following image?

And now let us try to answer a few more questions. How many trees with green leaves were there? How many boats with humans in them were there? Most of us will need to look at the image once again. The rest will be able to recall the image itself without looking at it. But none of us will be able to answer arbitrary questions without processing visual information once again.

Pure DNN-based VQA models use attention mechanisms that alter the way how the images are processed. However, they are criticized for overfitting to the biases in the datasets and for incapability to process more complex compositional questions.

At the same time, visually poor synthetic images with compositional questions can be dealt with quite well by naïve hybrid systems with the complete disentanglement of vision from reasoning and language processing (which is claimed to be a feature, although we consider it as a limitation).

Apparently, both symbolic reasoning and cognitive feedback are needed to deal with both rich ambiguous natural images and compositional questions (although the study can be conducted on an example of synthetic datasets also).

As we described in our previous posts (this and this), a deeper neural-symbolic integration in OpenCog can readily be achieved with the use of grounded predicates in Atomese (OpenCog’s knowledge representation language), which are evaluated in the course of reasoning. This can be considered either as a hard attention imposed by the symbolic knowledge-based system onto the subsymbolic/neural vision models, or as a way to assemble neural module networks on fly with the use of general declarative reasoning engine instead of the use of specialized imperative program execution engine, which is typically used in this class of VQA models.

Unfortunately, the original version of OpenCog lacks some functionality to conveniently run and train assembled networks. In particular, it has no means to pass tensors from one grounded predicate (neural network module) to another directly, and thus no means to assemble neural modules into an unbroken computational graph allowing for the gradient descent training. Also, grounded predicates in Atomese are bound to static functions in another language (e.g. Python), while we need to bound them to methods of concrete dynamically created objects (i.e. forward and backward steps of neural modules).

We filled this lack of functionality in the SingularityNET fork of OpenCog. With this extended functionality, a tight neural-symbolic integration can be achieved. Below, we describe one experimental design of a neural-symbolic framework without going into the implementation details.

The Idea

Let us imagine we want to re-use some existing neural modules implemented within a certain popular deep learning framework. We consider PyTorch here due to its native support for dynamic computational graphs, but other choices are quite possible.

Let us consider the Transparency by Design (TbD) model as an example. It contains a number of classes for different modules, e.g. OrModule, AndModule, AttentionModule, QueryModule, SameModule, etc. One class can be instantiated into many objects (particular neural modules corresponding to certain concepts). For example, there are both attention (filter) and query (classification) networks for each color (red, green, etc.), for each shape (cube, cylinder, etc.), material (metal, wood, etc.).

The TbD model takes text question as input and converts it to a “program” (by a seq2seq model), which is then executed by hard-coded imperative narrow program executor. Our intention is to replace this very specific program executor with general-purpose OpenCog’s reasoning engine without introducing any domain-specific modifications to it. At the same time, we would like to reuse the neural modules themselves.

Module network assembled and executed by the imperative program executor (question: How many blocks are tiny blue objects or big purple metallic objects?)

Our idea is to bind neural modules to Atoms in OpenCog’s knowledge base, which represent corresponding concepts, relations, and logical operations.

Atomese

As described in out previous post, OpenCog’s Atomspace can be populated with such pieces of knowledge as

InheritanceLink
ConceptNode "red"
ConceptNode "color"

and grounded predicates (as Atoms) can be bound to DNNs via external functions in such a way that queries like the following can be handled by OpenCog through grounded reasoning

AndLink
InheritanceLink
VariableNode “$X”
ConceptNode “color”
EvaluationLink
GroundedPredicateNode “py:filter”
ConceptNode “cylinder”
EvaluationLink
GroundedPredicateNode “py:filter”
VariableNode “$X”

In particular, this query can help to find what color is the cylinder. However, as mentioned above, EvaluationLink evaluated in the course of reasoning, should return OpenCog’s TruthValue, which can be combined by Probabilistic Logic Network rules. This breaks computational graphs of deep learning libraries and hinders end-to-end training. We would like to keep and combine both DNN modules with possibility to train them and Atomspace knowledge representation with possibility to execute queries via symbolic reasoning.

CogNets

CogNets is a prototype of neural-symbolic framework with a DNN-centric design. That is, it provides means to conveniently extend DNN models with OpenCog in Python rather than to extend OpenCog with DNNs in Atomese. The core class is CogModule that is inherited from torch.nn.Module. If one wants to use his or her PyTorch neural network in reasoning, it suffices to change its base class, e.g. from class AttentionModule(nn.Module) to class AttentionModule(CogModule). CogModule will do all the necessary wrappings. The only necessary thing is to provide Atom to the constructor, to which the instance being created will be bound. This will be a valid program in python:

class AttentionModule(CogModule):
def __init__(self, atom):
super().__init__(atom)
#initializing DNN parameters...
def forward(self, x):
#processing...
InheritanceLink(ConceptNode("red"), ConceptNode("color")) InheritanceLink(ConceptNode("green"), ConceptNode("color")) net1 = AttentionModule(ConceptNode("red")) net2 = AttentionModule(ConceptNode("green"))

Created networks will keep the same behavior as original PyTorch networks. For example, one can execute net1(image) on some tensor input. However, they will also have another way of execution — through Atomese queries. To do this conveniently, one can create InputModule, which only purpose is to memorize the input tensor and bind it to some Atom in Atomspace to make it accessible by OpenCog. InputModule is defined in CogNets and can readily be used, so one can write down, e.g. InputModule(batch, Concept("batch")), where batch is PyTorch tensor to be processed.

Then, one can form Atomese query in the nearly same form as usual in OpenCog, e.g.

BindLink(
TypedVariableLink(VariableNode("$X"), TypeNode("ConceptNode")),
AndLink(
InheritanceLink(VariableNode("$X"), ConceptNode("color")),
evaluate(VariableNode("$X"), execute(ConceptNode("batch"))),
VariableNode("$X"))

Here, evaluate and execute replace EvaluationLink and ExecutionLink and create necessary wrappers to pass tensors from one neural module to another (they don’t execute these modules directly, but tell OpenCog how to execute them; that’s why we can use VariableNode inside them).

The whole TbD model can be put into this form. There will be a few differences, though. In particular, queries will be represented in terms of variable grounding instead of applying the output classifier.

Tensor Truth Values

Direct execution of BindLink results in treating AndLink in a crisp logic style and in loosing the possibility to define a differentiable loss function. In contrast, PLN deals with continuous truth values. PLN is implemented as a set of rules for the Unified Rule Engine. In particular, these rules can calculate truth values of logical expressions based on truth values of their constituents. Equations for calculating these truth values are implemented as GroundedSchema nodes.

We have introduced tensor truth values into Atomese, and implemented PLN version that operates with them using PyTorch over tensors. As a result, the output of PLN becomes PyTorch tensor connected to the whole computational graph assembled in the course of reasoning. Thus, one can construct a differentiable loss function and run gradient descent optimization. Interestingly, tensor truth values can be assigned to IhneritanceLink or ImplicationLink, which are a part of the knowledge base, and can also be optimized in the course of learning.

Another task that can be solved with the use of the very same reasoning engine is illustrated in the following picture. This task was considered in the paper on DeepProbLog.

Another task that can be solved by CogNets

Conclusion

Thus, our prototype of the neural-symbolic framework can be used to re-implement such things as TbD VQA model and DeepProbLog programs. It brings together a modern deep learning library and a powerful symbolic knowledge representation and reasoning system in a practical way. Not only can rich ontologies be used together with deep neural networks, but the weights of relations in these ontologies can be learned or adjusted by gradient descent.

More details can be found in the repository and our recent papers on differentiable PLN and CogNets (preprint).

--

--