Skip to main content

PyNeuraLogic

PyPI version License Tests Status Code Quality Status Documentation Status Tweet

Documentation · Examples · Papers · Report Bug · Request Feature

PyNeuraLogic lets you use Python to write Differentiable Logic Programs


About

Logic programming is a declarative coding paradigm in which you declare your logical variables and relations between them. These can be further composed into so-called rules that drive the computation. Such a rule set then forms a logic program, and its execution is equivalent to performing logic inference with the rules.

PyNeuralogic, through its NeuraLogic backend, then makes this inference process differentiable which, in turn, makes it equivalent to forward propagation in deep learning. This lets you learn numeric parameters that can be associated with the rules, just like you learn weights in neural networks.

SQL tutorial

What is this good for?

Many things! For instance - ever heard of Graph Neural Networks (GNNs)? Well, a graph happens to be a special case of a logical relation - a binary one to be more exact. Now, at the heart of any GNN model there is a so-called propagation rule for passing 'messages' between the neighboring nodes. Particularly, the representation ('message') of a node X is calculated by aggregating the previous representations of adjacent nodes Y, i.e. those with an edge between X and Y.

Or, a bit more 'formally':

R.msg2(Var.X) <= (R.msg1(V.Y), R.edge(V.Y, V.X))

...and that's the actual code! Now for a classic learnable GNN layer, you'll want to add some weights, such as

R.msg2(Var.X)[5,10] <= (R.msg1(V.Y)[10,20], R.edge(V.Y, V.X))

to project your [20,1] input node embeddings ('message1') through a learnable [10,20] layer before the aggregation, and subsequently a [5,10] layer after the aggregation.

If you don't like the default settings, you can of course specify various additional details, such as the particular aggregation and activation functions

(R.msg2(V.X)[5,10] <= (R.msg1(V.Y)[10,20], R.edge(V.Y, V.X))) | [Transformation.RELU, Aggregation.AVG]

to instantiate the classic GCN layer specification, which you can directly train now!

graph TD;
    edge10[/"edge(1, 0)"\]-->RuleNeuron1("msg2(0) <= msg1(1), edge(1, 0).");
    msg1[/"msg1(1)"\]-- w_1 -->RuleNeuron1;

    edge00[/"edge(0, 0)"\]-->RuleNeuron2("msg2(0) <= msg1(0), edge(0, 0).");
    msg0[/"msg1(0)"\]-- w_1 -->RuleNeuron2;

    edge30[/"edge(3, 0)"\]-->RuleNeuron3("msg2(0) <= msg1(3), edge(3, 0).");
    msg3[/"msg1(3)"\]-- w_1 -->RuleNeuron3;

    RuleNeuron1-- ReLU -->AggregationNeuron[["Rules Aggregation (Average)"]]
    RuleNeuron2-- ReLU -->AggregationNeuron[["Rules Aggregation (Average)"]]
    RuleNeuron3-- ReLU -->AggregationNeuron[["Rules Aggregation (Average)"]]

    AggregationNeuron-- w_2 -->OutputNeuron[\"Output Neuron (Tanh)"/]

How is it different from other GNN frameworks?

Naturally, PyNeuralogic is by no means limited to GNN models, as the expressiveness of relational logic goes much further beyond graphs. Hence, nothing stops you from playing directly with:

  • multiple relations and object types
  • hypergraphs, nested graphs, relational databases
  • relational pattern matching, various subgraph GNNs
  • alternative propagation schemes
  • inclusion of logical background knowledge
  • and more...

In PyNeuraLogic, all these ideas take the same form of simple small logic programs. These are commonly highly transparent and easy to understand, thanks to their declarative nature. Consequently, there is no need to design a zoo of blackbox class names for each small modification of the GNN rule - you code directly at the level of the logical principles here!

The backend engine then creates the underlying differentiable computation (inference) graphs in a fully automated and dynamic fashion, hence you don't have to care about aligning everything into some static (tensor) operations.

How does it perform?

While PyNeuraLogic allows you to easily declare highly expressive models with capabilities far beyond the common GNNs, it does not come at the cost of performance for the basic GNNs either. On the contrary, for a range of common GNN models and applications, such as learning with molecules, PyNeuraLogic is actually considerably faster than the popular GNN frameworks, as demonstrated in our benchmarks.

Benchmark of PyNeuraLogic


We hope you'll find the framework useful in designing your own deep relational learning ideas beyond the GNNs! Please let us know if you need some guidance or would like to cooperate!

🚀 Getting started

Installation

To install PyNeuraLogic's latest release from the PyPI repository, use the following command:

$ pip install neuralogic
Prerequisites

To use PyNeuraLogic, you need to install the following prerequisites:

Python >= 3.10
Java >= 1.8

[!TIP]

In case you want to use visualization provided in the library, it is required to have Graphviz installed.


📦 Predefined Modules

PyNeuraLogic has a set of predefined modules to get you quickly started with your experimenting! It contains, for example, predefined modules for:

  • Graph Neural Networks (GCNConv, SAGEConv, GINConv, RGCNConv, ...)
  • Meta graphs and meta paths (MetaConv, MAGNN, ...)
  • Transformer, LSTM, GRU, RNN, ...and more!

🔬 Examples

Open In Colab Simple XOR example
Open In Colab Molecular GNNs
Open In Colab Recursive XOR generalization
Open In Colab Visualization

Open In Colab Subgraph Patterns
Open In Colab Distinguishing k-regular graphs
Open In Colab Distinguishing non-regular graphs

🤝 Community & Support

📝 Papers

📘 Articles

🎥 Videos

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neuralogic-0.9.1.tar.gz (8.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neuralogic-0.9.1-py3-none-any.whl (8.4 MB view details)

Uploaded Python 3

File details

Details for the file neuralogic-0.9.1.tar.gz.

File metadata

  • Download URL: neuralogic-0.9.1.tar.gz
  • Upload date:
  • Size: 8.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for neuralogic-0.9.1.tar.gz
Algorithm Hash digest
SHA256 44590527460977ab8d9e56f8b8448b9d4bf2e6d5da64ade019b1fa43dc394692
MD5 623f4df73c170e5629285768d7a602cb
BLAKE2b-256 b4ed1d586503dccd494591f15d64a60691402f68bcad681e1462ea6546d1099c

See more details on using hashes here.

File details

Details for the file neuralogic-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: neuralogic-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 8.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for neuralogic-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 af7653e83f1bb6fb5cff0ff2e76a396cef076bc791c7bee83b5d38903c3f1dc3
MD5 ccd00aedb25da2be591013f9780c61e5
BLAKE2b-256 44e14de980951c90216872b4d2530cc7c6473924c5a01e88f7e4a45a18c1532d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.9.2

2 files

This release

0.9.1 This release

2 files

0.9.0

2 files

0.8.0

2 files

0.7.21

2 files

0.7.20

2 files

0.7.19

2 files

0.7.18

2 files

0.7.16

2 files

0.7.15

2 files

0.7.14

2 files

0.7.13

2 files

0.7.11

2 files

0.7.10

2 files

0.7.9

2 files

0.7.8

2 files

0.7.7

2 files

0.7.6

2 files

0.7.5

2 files

0.7.4

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page