Skip to main content

predicateneuron

A differentiable predicate neuron and ready-made predicate layers for Keras, TensorFlow and PyTorch.

The neuron combines two inputs in [0, 1] with a fuzzy-logic operation (intersection, union or symmetric difference). A single learnable parameter, theta, decides which operation is used, so the network learns which logical relation to apply to each pair of features.

Code accompanying the paper: Predicate Neuron: Differentiable Quantization of Predicate Relations in Gradient-based Learning (link).

Installation

pip install "predicateneuron[keras]"        # Keras 3 (TensorFlow, PyTorch or JAX backend)
pip install "predicateneuron[tensorflow]"   # TensorFlow >= 2.16
pip install "predicateneuron[torch]"        # PyTorch
pip install "predicateneuron[all]"          # everything

Only the framework you need has to be installed. Importing predicateneuron itself pulls in none of them.

Quick start

Keras / TensorFlow

import keras
from predicateneuron.keras import PredicateLayer  # or: from predicateneuron.tf import ...

model = keras.Sequential([
    keras.Input((8,)),
    PredicateLayer(
        temperature=0.06, theta_initializer=keras.initializers.Constant(0.5),
        hard_forward=False, hard_backward=False
    ),
    keras.layers.Dense(1, activation="sigmoid"),
])

PyTorch

import torch
from predicateneuron.torch import PredicateLayer

model = torch.nn.Sequential(
    PredicateLayer(in_features=8, temperature=0.06, theta_init=0.5,
                   hard_forward=False, hard_backward=False),
    torch.nn.Linear(28, 1),  # 8 inputs -> 8*7/2 = 28 pairs
    torch.nn.Sigmoid(),
)

Inputs are expected to lie in [0, 1], for example sigmoid outputs or normalized features.

The neuron

For inputs x1, x2 and a learnable theta:

theta operation formula
< 0.25 intersection x1·x2
0.25 … 0.75 union x1 + x2 − x1·x2
≥ 0.75 symmetric difference x1 + x2 − 2·x1·x2

The function is also available directly: from predicateneuron.keras import predicate_neuron (or .torch).

Forward and backward modes

Two independent switches control how the neuron behaves.

Flag True False (default)
hard_forward output uses the single operation selected by theta output is a softmax mixture of the three operations (temperature)
hard_backward input gradients from the selected operation only input gradients from the exact derivative of the soft mixture

The gradient with respect to theta always comes from the softmax surrogate, in every mode. All four combinations are valid. The defaults are (hard_forward=False, hard_backward=False).

Layers

Layer Pairs Output size
PredicateLayer every unique pair (i, j), i < j n(n−1)/2
MinimalPredicateLayer disjoint pairs (0,1), (2,3), …; odd last input is passed through ⌈n/2⌉
MinimalJointPredicateLayer overlapping circular pairs (0,1), (1,2), …, (n−1,0) n

Every layer owns one theta per pair and accepts inputs of any rank, with the features on the last axis.

Switching modes

Each layer keeps its own flags, so layers in one model can run in different modes.

from predicateneuron.keras import set_model_hard_forward, set_model_hard_backward

# a single layer
layer.set_hard_forward(False)
layer.set_hard_backward(False)

# every predicate layer in a model (including nested ones)
set_model_hard_forward(model, False)  # e.g. soft warm-up
set_model_hard_backward(model, False)
set_model_hard_forward(model, True)  # switch back to hard gates

# mixed: all soft except one layer
set_model_hard_forward(model, False)
model.get_layer("last_predicate").set_hard_forward(True)

The flags are stored as variables (Keras) or buffers (PyTorch), so they can be changed during training without rebuilding the model. In Keras they are also part of get_config, so saved models restore their mode.

Notes

  • Keras support targets Keras 3. TensorFlow 2.15 and earlier ship Keras 2 as tf.keras, which is not supported.
  • Both theta and the three candidate operations are differentiated separately by design (see Forward and backward modes), so soft mode is not plain backpropagation through the mixture unless hard_backward=False.

Citation

If you use this code, please cite the paper (see CITATION.cff).

License

Apache 2.0

Metadata

Release files for predicateneuron 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for predicateneuron 0.1.3
File Size Uploaded Provenance
predicateneuron-0.1.3.tar.gz 15.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for predicateneuron 0.1.3
File Interpreter ABI Platform
predicateneuron-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 29.2 kB

Release files / predicateneuron-0.1.3.tar.gz

Download URL predicateneuron-0.1.3.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3f0feafdc43c0146b6c71ca5c862bbb1960acde444a9636f3a7490b1a0691aed
BLAKE2b-256 checksum
How to use checksums
b5897d0b4a9a1e32744097f19030f88fef408725146aa5bf756ebb4d0e849c47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / predicateneuron-0.1.3-py3-none-any.whl

Download URL predicateneuron-0.1.3-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a525203c5b3820b1c1cabb2176eb7ea6995603c8ee5f43050da83104ebe23741
BLAKE2b-256 checksum
How to use checksums
b53ac2f22924aa0d3868ab83dbef11c145bc23037c289c495561aa5b7d422702
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Security for predicateneuron 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files No provenance

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