PiShield: A PyTorch Package for Learning with Requirements

Mihaela C. Stoian, Alex Tatomir, Thomas Lukasiewicz, Eleonora Giunchiglia

Abstract

Deep learning models have shown their strengths in various application domains, however, they often struggle to meet safety requirements for their outputs. In this paper, we introduce PiShield, the first package ever allowing for the integration of the requirements into the neural networks' topology. PiShield guarantees compliance with these requirements, regardless of input. Additionally, it allows for integrating requirements both at inference and/or training time, depending on the practitioners' needs. Given the widespread application of deep learning, there is a growing need for frameworks allowing for the integration of the requirements across various domains. Here, we explore three application scenarios: functional genomics, autonomous driving, and tabular data generation.

Introduction

Deep Neural Networks (DNNs) are powerful "black boxes" that occasionally produce nonsensical or unsafe outputs. For instance, an autonomous driving model might predict that an object is simultaneously a "pedestrian" and a "cyclist," or a medical model might generate synthetic patient data where the "minimum temperature" is higher than the "maximum temperature."

Existing solutions usually fall into two categories:

  1. Loss-based methods: These add a penalty to the loss function when a model violates a rule. While this encourages the model to behave, it provides no guarantee that the final output will be valid.
  2. Topology-based methods: These bake constraints directly into the network's structure. While effective, these have historically been difficult to implement, lacked a unified framework, or struggled with scalability.

PiShield bridges this gap by providing an easy-to-use, standardized library for PyTorch that enforces "hard" constraints. It introduces "Shield Layers" to ensure neural networks strictly adhere to domain-specific requirements or safety constraints, guaranteeing 100% compliance at both training and inference time.

Methodology: The Shield Layer

The core innovation of PiShield is the Shield Layer. This is a custom PyTorch layer designed to be appended to the end of any standard neural network.

Architecture of a Shielded DNN. The Shield Layer integrates domain requirements directly into the network topology to guarantee output satisfaction.

Key technical features include:

  • Seamless Integration: It functions like any other torch.nn layer. Practitioners only need to provide the input dimensions and a path to a file containing the requirements.
  • Flexible Requirements: It supports two main types of domain knowledge:
  • Propositional Logic: Expressed in Conjunctive Normal Form (CNF) for discrete classification tasks (e.g., "If it's a traffic light, it must have a color").
  • Linear Inequalities: For continuous data (e.g., "Max Hemoglobin ≥ Min Hemoglobin").
  • Dual-Mode Utility: Shield Layers can be used during training to guide the model toward valid regions of the search space, or strictly at inference as a post-processing "safety filter" for pre-trained models.

Experimental Evaluation

The authors evaluated PiShield across three distinct domains: functional genomics (hierarchical classification), autonomous driving (ROAD-R dataset), and tabular data generation (synthetic clinical trials).

Key Results

The addition of Shield Layers consistently outperformed baseline "unconstrained" models across all metrics:

Scenario Metric Baseline PiShield
Functional Genomics AU(PRC) 0.225 0.241
Autonomous Driving f-mAP 0.288 0.303
Tabular Data Generation Utility-F1 0.430 0.458
Comparison of hemoglobin levels in synthetic tabular data. Unlike the baseline, PiShield ensures all generated records satisfy the biological constraint that maximum hemoglobin must exceed minimum hemoglobin.

Qualitatively, the results are striking. In autonomous driving tests, the baseline model frequently predicted impossible overlapping labels (e.g., a person being both a pedestrian and a cyclist), whereas PiShield-enabled models produced logically consistent scenes. In tabular data generation, PiShield ensured that synthetic patient records followed basic biological laws that the baseline model ignored, as shown in the hemoglobin distribution comparison.

Discussion and Conclusion

One of the most important takeaways is that constraints aren't just "handcuffs" that limit a model; they act as a guide. By pruning away impossible output spaces, the model can converge faster and more accurately. PiShield moves the needle from probabilistic compliance to a mathematical guarantee.

While PiShield simplifies the integration of requirements, its effectiveness depends entirely on the quality of the requirements provided. If the domain knowledge is incorrect, the shield will enforce those errors. Furthermore, very large sets of complex, interlocking constraints may introduce computational overhead during the forward pass. Despite these considerations, PiShield offers a robust framework for fields where "black box" uncertainty is currently a barrier to deployment, such as healthcare, law, and robotics.

Source Code: https://github.com/mihaela-stoian/PiShield

Summary updated

BibTeX
@inproceedings{stoian2024pishield,
  author = {Mihaela C. Stoian and Alex Tatomir and Thomas Lukasiewicz and Eleonora Giunchiglia},
  title = {{PiShield: A PyTorch Package for Learning with Requirements}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2024},
  url = {https://openreview.net/forum?id=RM97NoQgzq},
}
Figure