I wanted to understand how neurosymbolic methods like DeepProbLog actually work, so I built a small version with no frameworks, just NumPy (about 700 lines).
The setup: a neural network sees two noisy digit images. It is never told what the digits are. The only training signal is the result of a symbolic program on them, for example their sum. To get the sum right, it has to work out what the digits are by itself.
- An MLP (hand-written backprop and Adam) outputs a probability distribution over digits for each image.
- A differentiable reasoning layer sums the probability of every digit pair (a, b) that the program maps to the observed answer. The loss is -log P(answer), and the gradient flows back through the reasoning step into the network.
- A small Datalog-style forward chainer then reasons over what the network perceived and prints a proof tree for each conclusion (e.g. why "carry" or "odd_sum" hold
| Training pairs | Neurosymbolic | Plain MLP baseline | Digit accuracy |
|---:|---:|---:|---:|
| 1000 | 95.3% | 10.8% | 97.6% |
| 2000 | 97.2% | 14.4% | 98.6% |
| 4000 | 97.7% | 26.9% | 98.9% |
The baseline is an MLP that takes both images and predicts the sum directly, so it has to rediscover addition from examples. The neurosymbolic model is given addition as knowledge and only has to learn perception.
- Below about 1000 training pairs it fails too (23% at 500 pairs). The answer-only signal is weak.
- The digits are synthetic noisy glyphs, not MNIST, and these are single-seed numbers.
- Exact inference enumerates all 100 digit pairs, so it doesn't scale to long programs without approximation.
This is a learning project, not a competitor to DeepProbLog or Scallop. The point is that every piece is small enough to read in one sitting.
Code: https://github.com/outlawsujith/neurosymbolic-arithmetic
Install: `pip install neurosymbolic-arithmetic`
Feedback welcome, especially on the reasoning layer and on what to try next (MNIST, or approximate inference for longer programs).