Skip to main content

Bridge symbolic predicate logic (SymPy) and PyTorch for neuro-symbolic AI

Project description

pysignet: Logic-as-Loss for Neural Networks

CI

Pysignet logo

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: ForAll and Exists over 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 via LogicCompiler
  • Full PyTorch Integration: Gradients flow through all operations

Learn More

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


Download files

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

Source Distribution

pysignet-1.0.0.tar.gz (53.6 kB view details)

Uploaded Source

Built Distribution

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

pysignet-1.0.0-py3-none-any.whl (68.0 kB view details)

Uploaded Python 3

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

Hashes for pysignet-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f125ed0b1f7c2e089ab4e1ef43d03470bfeae08c35e9b759bb0a5c136e97ed73
MD5 05df2a3e99ad635609dba09d5ff4163e
BLAKE2b-256 a4b56cce7e5dfd2739275a3b9e879f9139a8599062b123ceec6b2bf7faaecedd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysignet-1.0.0.tar.gz:

Publisher: publish.yml on pysignet/pysignet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for pysignet-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fddbccf48d0f22e2a494045519cdc7879246caa53a88010716571d7481afaa32
MD5 ce457b8265ae5e4dd69558fcae9ddb76
BLAKE2b-256 0c4c284f9d660b8e5d3feb61f28bee2af9f305471fbc9802a7bd387599cd84b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pysignet-1.0.0-py3-none-any.whl:

Publisher: publish.yml on pysignet/pysignet

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page