PYLON: A PyTorch Framework for Learning with Constraints

Kareem Ahmed, Tao Li, Thy Ton, Quan Guo, Kai-Wei Chang, Parisa Kordjamshidi, Vivek Srikumar, Guy Van den Broeck, Sameer Singh

Introduction

Deep learning models have established themselves as "universal function approximators," demonstrating remarkable performance when trained on massive, labeled datasets. However, these models often lack the ability to incorporate high-level, declarative knowledge—the "common sense" or domain-specific rules that humans use to reason. For instance, a human knows that a person cannot be in two places at once, or that in a game of Sudoku, numbers must be unique in every row. Expressing these constraints in traditional deep learning often requires complex, custom-engineered loss functions or specialized architectures.

PYLON is a neuro-symbolic framework designed to bridge this gap. It allows developers to augment PyTorch models with high-level domain knowledge using standard Python functions. By compiling these declarative constraints into differentiable loss functions, PYLON enables models to learn from rules as well as data, which is particularly beneficial in low-data regimes.

Addressing the Integration Gap

Existing neuro-symbolic systems often face significant hurdles that prevent widespread adoption:

  1. Specialized Syntax: Many systems require users to learn logic-based languages (like Prolog or Datalog) or proprietary syntaxes.
  2. Integration Friction: Incorporating these systems often necessitates a major overhaul of existing PyTorch or TensorFlow codebases.
  3. Scalability Issues: Calculating the probability that a complex constraint is satisfied is often an NP-hard problem, leading to computational bottlenecks.

PYLON addresses these issues by making constraint-based learning "Pythonic." It requires only a few lines of code to integrate into a standard deep learning pipeline, removing the need for specialized logic languages.

The PYLON Framework

The centerpiece of PYLON is the Constraint Function. Instead of defining a complex mathematical loss, users write a standard Python function. This function takes the model's outputs (decodings) and returns a Boolean value indicating whether the constraint is satisfied.

To make these Boolean constraints compatible with gradient-based optimization, PYLON "compiles" them into differentiable loss functions using three main strategies:

1. Exact Compilers (Logical Circuits)

For constraints that can be represented as logical statements, PYLON can compile them into arithmetic circuits. This approach allows for the efficient and exact computation of the probability that a constraint is satisfied, providing a precise signal for the model to follow.

2. Fuzzy Logic (T-norms)

In cases where exact computation is computationally prohibitive, PYLON utilizes "fuzzy" approximations, such as Product or Łukasiewicz T-norms. These map logical operators (AND, OR, NOT) to continuous mathematical operations. This creates a smooth gradient, allowing the model to gradually learn to satisfy the constraints.

3. Black-box Optimization (Sampling)

Some constraints involve non-differentiable components, such as calls to external APIs or databases. For these "black-box" scenarios, PYLON employs the REINFORCE gradient estimator. By treating constraint satisfaction as a reward signal, the framework uses Monte Carlo sampling to guide the model's learning process.

Case Studies and Applications

The versatility of PYLON is demonstrated across a variety of domains:

  • MNIST Addition: In this task, the model is shown images of two digits and provided with their sum rather than individual labels. PYLON uses the addition rule as a constraint to guide the digit recognition.
  • NLI Transitivity: In Natural Language Inference (NLI), PYLON enforces logical transitivity: if sentence A entails B, and B entails C, then A must entail C.
  • Semantic Role Labeling (SRL): PYLON ensures that specific core arguments, such as the "agent" of an action, appear no more than once per predicate, enforcing structural consistency.
  • Sudoku: The framework is used to teach a model the fundamental rules of Sudoku—that every row, column, and $3 \times 3$ subgrid must contain unique digits.

Key Insights

The primary innovation of PYLON is the use of Python as a specification language. By leveraging native Python syntax (loops, all(), any(), sum()), PYLON makes defining complex logic intuitive for developers. It shifts the focus from procedural engineering (how to enforce a rule) to declarative specification (what the rule is). Furthermore, PYLON serves as a unified interface, allowing researchers to easily swap between different neuro-symbolic techniques (e.g., switching from a fuzzy loss to a circuit-based loss) with a single line of code.

Results and Impact

PYLON emphasizes ease of use and efficiency. Implementation of complex constraints typically requires only 5-10 lines of Python code. More importantly, PYLON-augmented models exhibit significantly higher data efficiency. By using constraints as a form of "supervision," these models can achieve high accuracy with far fewer labeled examples, as the rules prevent the model from making logically impossible predictions.

Limitations and Future Directions

While PYLON simplifies the process, users still face the "selection problem"—deciding which compiler (fuzzy, exact, or sampling) is best suited for their specific constraint and scale. Additionally, the framework remains bound by the inherent computational complexity of the logic being enforced; some constraints can lead to an exponential increase in circuit size. Future work aims to expand the library of compilers and automate the selection process based on the structure of the user's Python constraint function.

Summary updated

BibTeX
@inproceedings{ahmed2022pylon,
  author = {Kareem Ahmed and Tao Li and Thy Ton and Quan Guo and Kai-Wei Chang and Parisa Kordjamshidi and Vivek Srikumar and Guy Van den Broeck and Sameer Singh},
  title = {{PYLON: A PyTorch Framework for Learning with Constraints}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2022},
  doi = {10.1609/aaai.v36i11.21711},
}
Figure