Shield Layer & Memory-efficient Loss¶
The two top-level entry points of PiShield.
build_shield_layer¶
shield_layer ¶
Top-level entry point for building Shield Layers.
A Shield Layer is a differentiable layer that corrects a model's outputs so
that they are guaranteed to satisfy a given set of requirements (constraints),
regardless of the input. This module exposes :func:build_shield_layer, which
dispatches to the appropriate backend (linear, QFLRA, or propositional) based on
the requirements, and :func:detect_requirements_type, which infers the
requirement type from a requirements file.
build_shield_layer ¶
build_shield_layer(num_variables: int, requirements_filepath: str, ordering_choice: str = 'given', custom_ordering: List = None, requirements_type='auto')
Build a Shield Layer from the given requirements.
Selects and constructs the appropriate Shield Layer backend (linear, QFLRA, or propositional) for the supplied requirements. The returned layer is differentiable and can be used at both inference and training time to correct a model's outputs so that they satisfy the requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_variables
|
int
|
Total number of variables (e.g. labels or features, depending on the task), matching the dimension of the tensors that are to be corrected by the layer. |
required |
requirements_filepath
|
str
|
Path to a |
required |
ordering_choice
|
str
|
How to order the variables when applying corrections.
One of |
'given'
|
custom_ordering
|
List
|
An explicit ordering of the variables (only used by the propositional backend). Defaults to None. |
None
|
requirements_type
|
One of |
'auto'
|
Returns:
| Type | Description |
|---|---|
|
A Shield Layer instance ( |
|
|
or |
|
|
satisfy the requirements. |
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Example
layer = build_shield_layer( ... num_variables=5, ... requirements_filepath='requirements.txt', ... ) corrected = layer(model_output) # corrected satisfies the requirements
Source code in pishield/shield_layer.py
detect_requirements_type ¶
Infer the requirement type from the contents of a requirements file.
Scans the file and classifies it as 'propositional', 'qflra', or
'linear' based on the tokens it contains (see inline comments for the
exact detection rules).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requirements_filepath
|
str
|
Path to a |
required |
Returns:
| Type | Description |
|---|---|
str
|
The detected requirement type as one of |
str
|
or |
Source code in pishield/shield_layer.py
build_shield_loss¶
shield_loss ¶
Top-level entry point for building the Memory-efficient Loss.
The Memory-efficient Loss is an additional loss term that encourages (but,
unlike a Shield Layer, does not guarantee) requirement satisfaction at training
time, using t-norms. It is a memory-efficient t-norm loss inspired by Logic
Tensor Networks (LTN). This module exposes :func:build_shield_loss, which builds it.
build_shield_loss ¶
build_shield_loss(num_variables: int, requirements_filepath: str, tnorm_choice: str = 'godel', requirements_type='propositional')
Build a Memory-efficient Loss from the given requirements.
Constructs a loss term that penalises violations of the requirements during training, using the chosen t-norm to measure satisfaction. The Memory-efficient Loss is a memory-efficient t-norm loss inspired by Logic Tensor Networks (LTN).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_variables
|
int
|
Total number of variables (e.g. labels or features, depending on the task), matching the dimension of the tensors that are scored by the loss. |
required |
requirements_filepath
|
str
|
Path to a |
required |
tnorm_choice
|
str
|
The t-norm used to compute requirement satisfaction. One of
|
'godel'
|
requirements_type
|
The requirement type. Only |
'propositional'
|
Returns:
| Type | Description |
|---|---|
|
A |
|
|
requirement-satisfaction loss. |
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Example
loss_fn = build_shield_loss( ... num_variables=5, ... requirements_filepath='requirements.txt', ... tnorm_choice='product', ... ) penalty = loss_fn(model_output)