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: TODO: title (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.2

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.2
File Size Uploaded Provenance
predicateneuron-0.1.2.tar.gz 15.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 29.2 kB

Release files / predicateneuron-0.1.2.tar.gz

Download URL predicateneuron-0.1.2.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5fee9e6686c821e1c3cc83f22f495cb70b74f9d5fd1335c59665d62191aed99c
BLAKE2b-256 checksum
How to use checksums
ce2cc6bbce25f4c01618734ddba25926aa2c6343d235a68d30f52b42fa1267bd
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.2-py3-none-any.whl

Download URL predicateneuron-0.1.2-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1e06a0b2f138eed8388ea288206ddede539e379eaea17b5af364bf2b80f2b7db
BLAKE2b-256 checksum
How to use checksums
02fe94c2c562a0f8179fc4002096e6ea826af1c78994056288c89653e98ed5af
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.2

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.2 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