DeepStochLog is a neural-symbolic framework for neural stochastic logic programming. This framework allows users to express background knowledge using logic and probabilities to train neural networks more effectively. It achieved state-of-the-art results on a wide variety of tasks while scaling much better than alternative neural-symbolic frameworks.
For more depth, see the paper and the code on GitHub.
Capabilities
Sum of handwritten numbers
For example, take adding up two handwritten numbers, when you have never seen a handwritten digit with its label: only pairs of handwritten numbers together with their sum.

DeepStochLog encodes this information as follows:
digit_dom(Y) :- member(Y,[0,1,2,3,4,5,6,7,8,9]).
nn(number, [X], Y, digit_dom) :: digit(Y) --> [X].
1 :: addition(N) --> digit(N1),
digit(N2),
{N is N1 + N2}.
General tasks
More generally, DeepStochLog can easily parse and classify sequences of subsymbolic information like images that follow a known grammar structure. It can even encode general problems like word algebra problems, labeling papers in a citation graph, or any other task that could be expressed using neural stochastic logic.
In the paper’s experiments, DeepStochLog reached state-of-the-art accuracy on all tasks, and it scaled better than similar neural-symbolic frameworks. On the sum of handwritten numbers, for example, it works out the probability of a query in a few milliseconds, where DeepProbLog and NeurASP need up to two hundred, and can’t finish once the numbers have four digits:
Understanding neural definite clause grammars
DeepStochLog extends definite clause grammars with neural probabilities to create “Neural Definite Clause Grammars”. To understand this, let’s look at the basic building blocks, namely context-free grammars and probabilistic context-free grammars, as well as its logic-based extensions, definite clause grammars, and stochastic definite clause grammars. The figure below parses the same sum with each of them, in the order of the sections after it.
CFG: Context-free grammar Rules that rewrite a non-terminal into non-terminals and terminals.
E --> NE --> E, P, NP --> ["+"]N --> ["0"]N --> ["1"]…N --> ["9"] E E E N P N P N 2 + 3 + 8 PCFG: Probabilistic context-free grammar A probability for every rule; the rules of a non-terminal add up to 1.
0.5 :: E --> N0.5 :: E --> E, P, N1.0 :: P --> ["+"]0.1 :: N --> ["0"]0.1 :: N --> ["1"]…0.1 :: N --> ["9"] E 0.5 E 0.5 E 0.5 N 0.1 P 1 N 0.1 P 1 N 0.1 2 + 3 + 8 Probability of this parse: 0.5 × 0.5 × 0.5 × 0.1 × 1 × 0.1 × 1 × 0.1 = 0.000125
DCG: Definite clause grammar Arguments and Prolog goals between braces: here, the value of the sum.
e(N) --> n(N).e(N) --> e(N1), p, n(N2), {N is N1 + N2}.p --> ["+"].n(0) --> ["0"].n(1) --> ["1"].…n(9) --> ["9"]. e(13) e(5) e(2) n(2) p n(3) p n(8) 2 + 3 + 8 SDCG: Stochastic definite clause grammar Probabilities on the rules again. A derivation that fails loses its probability.
0.5 :: e(N) --> n(N).0.5 :: e(N) --> e(N1), p, n(N2), {N is N1 + N2}.1.0 :: p --> ["+"].0.1 :: n(0) --> ["0"].0.1 :: n(1) --> ["1"].…0.1 :: n(9) --> ["9"]. e(13) 0.5 e(5) 0.5 e(2) 0.5 n(2) 0.1 p 1 n(3) 0.1 p 1 n(8) 0.1 2 + 3 + 8 Probability of this parse: 0.5 × 0.5 × 0.5 × 0.1 × 1 × 0.1 × 1 × 0.1 = 0.000125
NDCG: Neural definite clause grammar (DeepStochLog) A neural network gives a rule its probabilities, so the terminals can be images.
0.5 :: e(N) --> n(N).0.5 :: e(N) --> e(N1), p, n(N2), {N is N1 + N2}.1.0 :: p --> ["+"].nn(number_nn, [X], [Y], [digit]) :: n(Y) --> [X].digit(Y) :- member(Y, [0,1,…,9]). e(13) 0.5 e(5) 0.5 e(2) 0.5 n(2) P(2) p 1 n(3) P(3) p 1 n(8) P(8)
+
+
Probability of this parse: 0.5 × 0.5 × 0.5 × P(2) × 1 × P(3) × 1 × P(8), where P(2) is how sure number_nn is that its image shows a 2
Each grammar parses 2 + 3 + 8 (from our talk at AAAI 2022). What a grammar adds to the one before is on the sun, in its rules and in its tree. The number under a node is the probability of the rule that expands it.
Context-free grammars
Context-free grammars model rules as just mappings from non-terminals to sequences of other non-terminals and terminals. They are useful for many language tasks, such as:
- Finding out if a sequence is an element of the specified language
- Labeling the “part of speech”-tag of a terminal
- Generating every sentence of the language
Probabilistic context-free grammars
Probabilistic context-free grammars extend context-free grammars by adding probabilities to every rule. The probabilities of all used rules in the expansion are then used to calculate the probability of a particular parse. This framework helps find answers to the following questions:
- What is the most likely parse for this sequence of terminals? (useful for ambiguous grammars)
- What is the probability of generating this string?
Definite clause grammars
Definite clause grammars extend context-free grammars with logic programming concepts. They allow adding arguments to the non-terminals, and extra logic between curly brackets {}. The addition of logic programming makes this type of grammar Turing-complete.
Definite clause grammars are helpful for:
- Modeling more complex languages (e.g. context-sensitive)
- Adding constraints between non-terminals thanks to Prolog power (e.g., through unification)
- Extra inputs & outputs aside from terminal sequence (through unification of input variables)
Stochastic definite clause grammars
Stochastic definite clause grammars add the same benefit to definite clause grammars as probabilistic context-free grammars add to context-free grammars, namely adding probabilities to rules. These grammars thus give rise to the same benefits, like finding the most likely parse. However, since expansions can fail in a definite clause grammar, probability mass can get lost in such a failing derivation, meaning that the sum of the probabilities of all possible elements is not necessarily 1.
Neural definite clause grammars
Neural definite clause grammars add neural probabilities to stochastic definite clause grammars, which swap rule probabilities with neural networks that output a probability. This is the same trick as DeepProbLog does to probabilistic logic programming, but then to stochastic definite clause grammars.
This framework is helpful for:
- Subsymbolic processing: e.g. tensors as terminals
- Learning rule probabilities using neural networks
Defining neural definite clause grammars
Rules in DeepStochLog are modeled as grammar rules that are allowed to contain arguments, logic, and (neural) probabilities.
nn(m,[I1,…,Im],[O1,…,OL],[D1,…,DL]) :: nt --> g1, …, gn.
Where:
ntis an atomg1,…,gnare goals (goal = atom or list of terminals & variables)I1,…,ImandO1,…,OLare variables occurring ing1,…,gnand are the inputs and outputs ofmD1,…,DLare the predicates specifying the domains ofO1,…,OLmis a neural network mappingI1,…,Imto probability distribution overO1,…,OL(= over cross product ofD1,…,DL)
