PyNeuraLogic
PyNeuraLogic lets you use Python to create Differentiable Logic Programs
Install / Use
/learn @LukasZahradnik/PyNeuraLogicREADME
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.
<p align="center"> <a href="https://pyneuralogic.readthedocs.io/en/latest/advanced/database_deep_learning.html"> <img src="https://github.com/LukasZahradnik/PyNeuraLogic/blob/master/docs/_static/sql_banner.svg" alt="SQL tutorial" title="SQL tutorial"/> </a> </p>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.
<p align="center"> <img src="https://github.com/LukasZahradnik/PyNeuraLogic/blob/master/docs/_static/benchmark.svg" alt="Benchmark of PyNeuraLogic" title="Benchmark of PyNeuraLogic"/> </p> <br>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.8
Java >= 1.8
<br />[!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
Simple XOR example
<br />
Molecular GNNs
<br />
Recursive XOR generalization
<br />
Visualization
<br />
<br />
Subgraph Patterns
<br />
Distinguishing k-regular graphs
<br />
Related Skills
claude-opus-4-5-migration
83.4kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
model-usage
338.0kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
mcp-for-beginners
15.7kThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.
TrendRadar
49.8k⭐AI-driven public opinion & trend monitor with multi-platform aggregation, RSS, and smart alerts.🎯 告别信息过载,你的 AI 舆情监控助手与热点筛选工具!聚合多平台热点 + RSS 订阅,支持关键词精准筛选。AI 智能筛选新闻 + AI 翻译 + AI 分析简报直推手机,也支持接入 MCP 架构,赋能 AI 自然语言对话分析、情感洞察与趋势预测等。支持 Docker ,数据本地/云端自持。集成微信/飞书/钉钉/Telegram/邮件/ntfy/bark/slack 等渠道智能推送。
