Bridge symbolic predicate logic (SymPy) and PyTorch for neuro-symbolic AI
Project description
pysignet: Logic-as-Loss for Neural Networks
pysignet is a PyTorch library that converts symbolic predicate logic expressions (written in SymPy notation) into differentiable loss functions, enabling you to train neural networks with logical constraints using First-Order Logic (FOL). It bridges symbolic reasoning and gradient-based optimization so that logical rules like implication, mutual exclusion, or quantified constraints become training signals.
Documentation: pysignet.github.io
Quick Start
Define a logical constraint in SymPy notation, map each predicate symbol to a neural network, and compile it into a differentiable loss whose gradients flow back through the logic to every model involved.
import torch
import torch.nn as nn
from pysignet import Symbol, Variable, Implies, logic_to_loss
# Define predicate symbols and FOL variables
P, Q = Symbol("P Q")
X = Variable("X")
# "For all inputs X: if P(X) then Q(X)"
expr = Implies(P(X), Q(X))
# Map symbols to neural network models
model_p = nn.Sequential(nn.Linear(10, 1), nn.Sigmoid())
model_q = nn.Sequential(nn.Linear(10, 1), nn.Sigmoid())
predicates = {
"P": model_p,
"Q": model_q,
}
# Compile to a loss function
logic_loss = logic_to_loss(expr, predicates)
# Training loop
x = torch.randn(32, 10)
loss = logic_loss.loss(X=x) # Bind X to x to get a scalar loss
loss.backward() # Gradients flow to both models
Installation
pip install pysignet
Or with Poetry:
poetry add pysignet
Key Features
- First-Order Logic: Variables, predicates with arguments, quantifiers
- Domain Quantifiers:
ForAllandExistsover finite domains - Flexible Predicates: Neural networks or deterministic functions
- Multiple T-Norms: Product, Lukasiewicz, Godel, and Mixed t-norms
- Multiple Compilers: T-norm (
mode='tnorm') and LinearThresholdUnit (mode='ltu') built-in; extend viaLogicCompiler - Full PyTorch Integration: Gradients flow through all operations
Learn More
- Core Concepts: Symbols, Variables, Predicates, T-Norms, and Quantifiers
- API Reference: Full API documentation
- Custom Compilers: Implement your own logic compilation strategy
- Notebooks: Interactive examples
Development Setup
git clone https://github.com/pysignet/pysignet.git
cd pysignet
poetry install
poetry run pytest tests/
The pre-commit hook (already configured) runs tests, type checking, and linting before each commit. See CONTRIBUTING.md for the full workflow.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysignet-1.0.0.tar.gz.
File metadata
- Download URL: pysignet-1.0.0.tar.gz
- Upload date:
- Size: 53.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f125ed0b1f7c2e089ab4e1ef43d03470bfeae08c35e9b759bb0a5c136e97ed73
|
|
| MD5 |
05df2a3e99ad635609dba09d5ff4163e
|
|
| BLAKE2b-256 |
a4b56cce7e5dfd2739275a3b9e879f9139a8599062b123ceec6b2bf7faaecedd
|
Provenance
The following attestation bundles were made for pysignet-1.0.0.tar.gz:
Publisher:
publish.yml on pysignet/pysignet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pysignet-1.0.0.tar.gz -
Subject digest:
f125ed0b1f7c2e089ab4e1ef43d03470bfeae08c35e9b759bb0a5c136e97ed73 - Sigstore transparency entry: 2164363054
- Sigstore integration time:
-
Permalink:
pysignet/pysignet@a690c725cac5c885c9204f51b68d676c25fc57a5 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/pysignet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a690c725cac5c885c9204f51b68d676c25fc57a5 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pysignet-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pysignet-1.0.0-py3-none-any.whl
- Upload date:
- Size: 68.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fddbccf48d0f22e2a494045519cdc7879246caa53a88010716571d7481afaa32
|
|
| MD5 |
ce457b8265ae5e4dd69558fcae9ddb76
|
|
| BLAKE2b-256 |
0c4c284f9d660b8e5d3feb61f28bee2af9f305471fbc9802a7bd387599cd84b2
|
Provenance
The following attestation bundles were made for pysignet-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on pysignet/pysignet
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pysignet-1.0.0-py3-none-any.whl -
Subject digest:
fddbccf48d0f22e2a494045519cdc7879246caa53a88010716571d7481afaa32 - Sigstore transparency entry: 2164363094
- Sigstore integration time:
-
Permalink:
pysignet/pysignet@a690c725cac5c885c9204f51b68d676c25fc57a5 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/pysignet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a690c725cac5c885c9204f51b68d676c25fc57a5 -
Trigger Event:
push
-
Statement type: