Linear requirements¶
The backend for linear requirements — linear arithmetic inequalities over the
variables.
Shield Layer¶
shield_layer ¶
The Shield Layer for linear requirements.
Defines :class:ShieldLayer, a differentiable PyTorch module that corrects a
batch of predictions so they satisfy a set of linear inequality requirements,
by clipping each variable into the feasible interval implied by its requirements
following a fixed variable ordering.
ShieldLayer ¶
Bases: Module
Differentiable layer that corrects predictions so they satisfy a set of linear inequality requirements.
The correction works variable by variable, following a fixed ordering. For each variable x,
the requirements that bound x are rewritten as a lower bound and an upper bound, each expressed
as a linear function of the other variables. At inference time the variables are corrected one
at a time in this order: x is clipped into the [lower bound, upper bound] interval implied by the
requirements, using the already-corrected values of the variables that precede it. Because every
variable is only ever clipped using values that are already feasible, correcting one variable can
never break a requirement that was already satisfied, so all requirements hold after the pass.
Attributes:
| Name | Type | Description |
|---|---|---|
num_variables |
Number of variables (labels or features) in a prediction. |
|
ordering |
The ordering of variables used to correct predictions. |
|
constraints |
The full list of parsed linear requirements. |
|
sets_of_constr |
Mapping from each variable to the requirements bounding it. |
|
pos_matrices |
Per-variable coefficient matrices encoding lower bounds. |
|
neg_matrices |
Per-variable coefficient matrices encoding upper bounds. |
|
dense_ordering |
The ordering restricted to variables that appear in some requirement. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_variables
|
int
|
Number of variables in each prediction vector. |
required |
requirements_filepath
|
str
|
Path to the requirements file (containing the variable ordering and the linear requirements). |
required |
ordering_choice
|
str
|
How to pick the correction ordering: |
'given'
|
Example
layer = ShieldLayer(num_variables=4, requirements_filepath='reqs.txt') corrected = layer(predictions) # corrected satisfies all requirements
Build the layer: parse requirements and precompute per-variable bounds.
Source code in pishield/linear_requirements/shield_layer.py
create_matrices ¶
Precompute the lower- and upper-bound matrices for every variable.
For each variable x, builds a matrix C+ encoding the requirements
that lower-bound x and a matrix C- encoding those that upper-bound
it.
Returns:
| Type | Description |
|---|---|
|
A tuple |
|
|
class: |
|
|
(a tensor, or |
Source code in pishield/linear_requirements/shield_layer.py
get_dense_ordering ¶
Restrict the ordering to variables that have requirements.
Returns:
| Type | Description |
|---|---|
List[Variable]
|
The variables of |
List[Variable]
|
requirement, in order. Requires |
Source code in pishield/linear_requirements/shield_layer.py
create_matrix ¶
create_matrix(x: Variable, x_constr: List[Constraint], positive_x: bool) -> Union[torch.Tensor, float]
Build the coefficient matrix encoding one side of x's bounds.
Each requirement contributes a row that expresses the bound it places on
x as a linear function of the other variables, plus a bias column for
the requirement's constant. Evaluating a row against a prediction vector
yields the value x must be >= (when positive_x) or <=
(when not positive_x).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Variable
|
The variable whose bound matrix is built. |
required |
x_constr
|
List[Constraint]
|
The requirements that bound |
required |
positive_x
|
bool
|
|
required |
Returns:
| Type | Description |
|---|---|
Union[Tensor, float]
|
A tensor of shape |
Union[Tensor, float]
|
|
Source code in pishield/linear_requirements/shield_layer.py
apply_matrix ¶
Evaluate a bound matrix against predictions and reduce over requirements.
Computes, for every requirement row, the bound value as a dot product with the prediction vector, then optionally reduces across rows to obtain the tightest bound.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
Prediction tensor of shape |
required |
matrix
|
Union[Tensor, float]
|
The bound matrix for the variable, or a float when the variable is unbounded on that side (returned unchanged). |
required |
reduction
|
|
'none'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
The per-sample bound value(s); shape |
Tensor
|
or the float |
Source code in pishield/linear_requirements/shield_layer.py
Parser¶
parser ¶
Parser for linear requirements files.
Reads a requirements file into the constraint data model: the variable ordering
and a list of :class:Constraint objects built from the textual linear
inequalities (with support for or disjunctions and neg negation).
neg_postprocess_ineq ¶
Negate an inequality (logical negation of the linear constraint).
Negates every atom and flips the inequality sign between '>' and
'>=', e.g. neg (x1 + x2 - x3 >= 0) becomes -x1 - x2 + x3 > 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ineq
|
Inequality
|
The inequality to negate. |
required |
Returns:
| Type | Description |
|---|---|
Inequality
|
A new :class: |
Source code in pishield/linear_requirements/parser.py
parse_atom ¶
Parse a single textual atom into an :class:Atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
The atom string, e.g. |
required |
Returns:
| Type | Description |
|---|---|
|
The parsed :class: |
Source code in pishield/linear_requirements/parser.py
parse_inequality ¶
Parse a textual linear inequality into an :class:Inequality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inequality
|
The inequality string, e.g. |
required |
Returns:
| Type | Description |
|---|---|
|
The parsed :class: |
Source code in pishield/linear_requirements/parser.py
parse_constraint ¶
Parse one textual requirement line into a :class:Constraint.
Splits the line on or into disjunct inequalities, applying neg
negation where present, and wraps the resulting inequalities in a constraint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constr
|
The requirement line, e.g. |
required |
Returns:
| Type | Description |
|---|---|
Constraint
|
The parsed :class: |
Source code in pishield/linear_requirements/parser.py
parse_constraints_file ¶
Parse a requirements file into a variable ordering and requirements.
The file must contain an ordering line listing the variables, followed by
one requirement per line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
Path to the requirements file. |
required |
Returns:
| Type | Description |
|---|---|
(List[Variable], List[Constraint])
|
A tuple |
(List[Variable], List[Constraint])
|
the list of :class: |
Raises:
| Type | Description |
|---|---|
Exception
|
If the file does not provide a variable ordering. |
Source code in pishield/linear_requirements/parser.py
split_constraints ¶
Cluster requirements into variable-disjoint groups.
Splits a list of requirements into groups Gi of requirements such that
Vars(Gi) \intersect Vars(Gj) = null for distinct groups, i.e. requirements
are grouped together whenever they share any variable (transitively).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordering
|
List[Variable]
|
The variable ordering, used to order each group's variables. |
required |
constraints
|
List[Constraint]
|
The requirements to cluster. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple |
|
|
each entry is, respectively, the variable ordering and the requirement |
|
|
list of one variable-disjoint cluster. |
Source code in pishield/linear_requirements/parser.py
Classes¶
classes ¶
Data model for linear requirements.
Defines the building blocks of a linear requirement: a :class:Variable, a
signed scaled :class:Atom (a coefficient times a variable), an
:class:Inequality (a body of atoms compared against a constant), and a
:class:Constraint wrapping one inequality. These classes also provide
satisfaction checks against batches of predictions.
Variable ¶
A variable (a label or feature) identified by a string name.
Attributes:
| Name | Type | Description |
|---|---|---|
variable |
The variable's string name (e.g. |
|
id |
The integer id parsed from the trailing part of the name. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable
|
str
|
The variable's string name, whose trailing token after the final underscore is parsed as the integer id. |
required |
Initialize the variable and parse its integer id from the name.
Source code in pishield/linear_requirements/classes.py
Atom ¶
A signed, scaled occurrence of a variable in a linear requirement.
An atom represents (+/-) coefficient * variable.
Attributes:
| Name | Type | Description |
|---|---|---|
variable |
The :class: |
|
coefficient |
The (unsigned) magnitude of the coefficient. |
|
positive_sign |
|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable
|
Variable
|
The :class: |
required |
coefficient
|
float
|
The unsigned coefficient magnitude. |
required |
positive_sign
|
bool
|
Whether the term carries a positive sign. |
required |
Initialize the atom from a variable, coefficient and sign.
Source code in pishield/linear_requirements/classes.py
get_variable_id ¶
Return the integer id of the atom's variable.
Returns:
| Type | Description |
|---|---|
|
The variable's integer id. |
eval ¶
Evaluate the atom for a given value of its variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_value
|
The value(s) of the atom's variable. |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in pishield/linear_requirements/classes.py
get_signed_coefficient ¶
Return the coefficient with its sign applied.
Returns:
| Type | Description |
|---|---|
|
|
Source code in pishield/linear_requirements/classes.py
readable ¶
Return a human-readable string for the atom, e.g. ' + 2.00y_3'.
Returns:
| Type | Description |
|---|---|
|
A string with the sign, coefficient (omitted when 1) and variable. |
Source code in pishield/linear_requirements/classes.py
get_atom_attributes ¶
Return the atom's defining attributes.
Returns:
| Type | Description |
|---|---|
|
A tuple |
Inequality ¶
A linear inequality: a body of atoms compared against a constant.
Represents sum(body) ineq_sign constant (e.g. 2y_1 - y_2 >= 0).
Attributes:
| Name | Type | Description |
|---|---|---|
ineq_sign |
The inequality operator, |
|
constant |
The right-hand-side constant. |
|
body |
The list of :class: |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
List[Atom]
|
The atoms forming the left-hand side. |
required |
ineq_sign
|
str
|
The inequality operator ( |
required |
constant
|
float
|
The right-hand-side constant. |
required |
Initialize the inequality from its body, sign and constant.
Source code in pishield/linear_requirements/classes.py
readable ¶
Return a human-readable string for the inequality.
Returns:
| Type | Description |
|---|---|
|
A string combining the readable body atoms, the sign and the |
|
|
constant. |
Source code in pishield/linear_requirements/classes.py
get_body_variables ¶
Return the variables appearing in the inequality body.
Returns:
| Type | Description |
|---|---|
|
A list of :class: |
Source code in pishield/linear_requirements/classes.py
get_body_atoms ¶
Return the atoms forming the inequality body.
Returns:
| Type | Description |
|---|---|
|
A list of the body :class: |
Source code in pishield/linear_requirements/classes.py
get_x_complement_body_atoms ¶
Split the body into x's atom and the remaining atoms.
Given an inequality in which x appears, separate the single atom over
x from the rest of the body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Variable
|
The variable to isolate. |
required |
Returns:
| Type | Description |
|---|---|
(List[Atom], Atom, bool)
|
A tuple ``(complementary_atom_list, x_atom_occurrences, constant, |
(List[Atom], Atom, bool)
|
is_strict_inequality) |
(List[Atom], Atom, bool)
|
atoms other than |
(List[Atom], Atom, bool)
|
class: |
(List[Atom], Atom, bool)
|
|
(List[Atom], Atom, bool)
|
is |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in pishield/linear_requirements/classes.py
get_ineq_attributes ¶
Return the inequality's defining attributes.
Returns:
| Type | Description |
|---|---|
|
A tuple |
contains_variable ¶
Check whether a variable appears in the inequality body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Variable
|
The variable to look for. |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in pishield/linear_requirements/classes.py
check_satisfaction ¶
Check, per sample, whether predictions satisfy the inequality.
The body is evaluated against the predictions and compared to the
constant up to TOLERANCE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
Prediction tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A boolean tensor of shape |
Source code in pishield/linear_requirements/classes.py
detailed_sat_check ¶
Check satisfaction and also return the intermediate evaluation values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
Prediction tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tuple |
Tensor
|
|
Tensor
|
|
Tensor
|
|
Source code in pishield/linear_requirements/classes.py
Constraint ¶
A linear requirement, wrapping its inequality (or disjunction thereof).
Most methods delegate to the first inequality in the list (single_inequality).
Attributes:
| Name | Type | Description |
|---|---|---|
inequality_list |
The list of :class: |
|
single_inequality |
The first inequality, used as the primary inequality. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inequality_list
|
List[Inequality]
|
The list of inequalities forming the requirement; the
first element becomes |
required |
Initialize the requirement and select its primary inequality.
Source code in pishield/linear_requirements/classes.py
readable ¶
Return a human-readable string for the requirement.
Returns:
| Type | Description |
|---|---|
|
The readable form of the primary inequality. |
verbose_readable ¶
Return a human-readable string for the requirement's first inequality.
Returns:
| Type | Description |
|---|---|
|
The readable form of the first inequality. |
Source code in pishield/linear_requirements/classes.py
contains_variable ¶
Check whether the requirement involves a given variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Variable
|
The variable to look for. |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in pishield/linear_requirements/classes.py
get_body_atoms ¶
Return the atoms of the requirement's primary inequality body.
Returns:
| Type | Description |
|---|---|
|
A list of :class: |
check_satisfaction ¶
Check whether the whole batch satisfies the requirement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Prediction tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
|
Source code in pishield/linear_requirements/classes.py
check_satisfaction_per_sample ¶
Check requirement satisfaction for each sample individually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Prediction tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
A boolean tensor of shape |
Source code in pishield/linear_requirements/classes.py
detailed_sample_sat_check ¶
Return per-sample satisfaction along with intermediate values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Prediction tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
|
The tuple produced by :meth: |
Source code in pishield/linear_requirements/classes.py
Computing sets of constraints¶
compute_sets_of_constraints ¶
Derive, per variable, the set of linear requirements that bound it.
This module performs Fourier-Motzkin-style variable elimination over the linear requirements: following the (reversed) variable ordering, it repeatedly eliminates the highest-ranking remaining variable by combining the requirements that contain it, producing for each variable the set of requirements that bound it in terms of lower-ranked variables only.
collapse_atoms ¶
Merge atoms that refer to the same variable into a single atom.
Atoms over the same variable are combined by summing their signed coefficients; any atom whose resulting coefficient is zero is dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atom_list
|
List of :class: |
required |
Returns:
| Type | Description |
|---|---|
|
A list of :class: |
|
|
no zero-coefficient atoms. |
Source code in pishield/linear_requirements/compute_sets_of_constraints.py
split_constr_atoms ¶
Separate the coefficient of y from the rest of a constraint's body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
Variable
|
The variable to extract. |
required |
constr
|
Constraint
|
The constraint whose body is split. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple |
|
|
|
|
|
|
Source code in pishield/linear_requirements/compute_sets_of_constraints.py
multiply_coefficients_of_atoms ¶
Scale every atom's coefficient by a constant factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
List[Atom]
|
The atoms to scale. |
required |
coeff
|
float
|
The multiplicative factor applied to each coefficient. |
required |
Returns:
| Type | Description |
|---|---|
|
A new list of :class: |
|
|
variables and signs are preserved. |
Source code in pishield/linear_requirements/compute_sets_of_constraints.py
create_constr_by_reduction ¶
Eliminate a variable by pairwise combination of its requirements.
Splits the requirements containing y by the sign of y and, for every
pair of a positive and a negative occurrence, derives a new requirement in
which y has been cancelled out (a Fourier-Motzkin elimination step).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
Variable
|
The variable to eliminate. |
required |
constraints_with_y
|
List[Constraint]
|
The requirements that contain |
required |
Returns:
| Type | Description |
|---|---|
|
A list of new :class: |
Source code in pishield/linear_requirements/compute_sets_of_constraints.py
get_pos_neg_x_constr ¶
Partition requirements by the sign of a variable's occurrence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
The variable whose sign is inspected. |
required | |
constraints_with_y
|
Requirements that contain |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple |
|
|
occurs with a positive and a negative coefficient, respectively. |
Source code in pishield/linear_requirements/compute_sets_of_constraints.py
compute_set_of_constraints_for_variable ¶
compute_set_of_constraints_for_variable(x: Variable, prev_x: Variable, constraints_at_previous_level: List[Constraint], verbose)
Derive the requirement set for one variable by eliminating the previous one.
Takes the requirements available at the previous level (those that bound
prev_x), eliminates prev_x from the requirements that contain it, and
returns the union of the requirements that did not contain prev_x with
the newly reduced requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Variable
|
The variable whose level is being computed. |
required |
prev_x
|
Variable
|
The previously processed (higher-ranked) variable to eliminate. |
required |
constraints_at_previous_level
|
List[Constraint]
|
Requirements available at |
required |
verbose
|
Whether to print diagnostic information. |
required |
Returns:
| Type | Description |
|---|---|
|
The list of :class: |
|
|
|
Source code in pishield/linear_requirements/compute_sets_of_constraints.py
compute_sets_of_constraints ¶
compute_sets_of_constraints(ordering: List[Variable], constraints: List[Constraint], verbose) -> {Variable: List[Constraint]}
Compute the requirement set bounding each variable along the ordering.
Processes the variables from the highest-ranked to the lowest-ranked (the reversed ordering), at each step eliminating the previously processed variable, so that each variable is associated with the requirements that bound it in terms of lower-ranked variables only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordering
|
List[Variable]
|
The ordering of the variables. |
required |
constraints
|
List[Constraint]
|
All linear requirements. |
required |
verbose
|
Whether to print diagnostic information. |
required |
Returns:
| Type | Description |
|---|---|
{Variable: List[Constraint]}
|
A dict mapping each :class: |
{Variable: List[Constraint]}
|
objects that bound it at its level. |
Source code in pishield/linear_requirements/compute_sets_of_constraints.py
Correcting predictions¶
correct_predictions ¶
Helpers for clipping predictions into the interval allowed by requirements.
Provides utilities to look up the requirement set bounding a given variable, to clip a variable's predicted value into the feasible interval implied by its lower and upper bounds, and to check that corrected predictions satisfy all requirements.
get_constr_at_level_x ¶
Look up the requirement set associated with a given variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
The :class: |
required | |
sets_of_constr
|
Mapping from variables to their requirement sets, as
produced by :func: |
required |
Returns:
| Type | Description |
|---|---|
|
The list of requirements bounding |
|
|
|
Source code in pishield/linear_requirements/correct_predictions.py
get_final_x_correction ¶
get_final_x_correction(initial_x_val: Tensor, pos_x_corrected: Tensor, neg_x_corrected: Tensor) -> torch.Tensor
Clip a variable's value into the interval allowed by its constraints.
initial_x_val is the model's (per-sample) prediction for x; pos_x_corrected is the tightest
lower bound implied by x's positive constraints, and neg_x_corrected the tightest upper bound
from its negative ones. The corrected value is therefore min(max(x, lower bound), upper bound),
i.e. x is only moved if it falls outside the feasible interval. A non-tensor bound (or an inf
entry) means "unbounded on that side", in which case the initial value is kept.
Source code in pishield/linear_requirements/correct_predictions.py
example_predictions_heloc ¶
Load a pickled tensor of example HELOC predictions for debugging.
Returns:
| Type | Description |
|---|---|
|
The unpickled predictions object loaded from a hard-coded local path. |
Source code in pishield/linear_requirements/correct_predictions.py
check_all_constraints_are_sat ¶
Check whether corrected predictions satisfy all requirements (batch-wise).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraints
|
The requirements to check. |
required | |
preds
|
The original (uncorrected) predictions. |
required | |
corrected_preds
|
The predictions after correction. |
required | |
verbose
|
Whether to print which requirements are violated/satisfied. |
False
|
Returns:
| Type | Description |
|---|---|
|
|
|
|
correction, |
Source code in pishield/linear_requirements/correct_predictions.py
check_all_constraints_sat ¶
Assert that corrected predictions satisfy all requirements per sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
corrected_preds
|
The predictions after correction. |
required | |
constraints
|
The requirements to check. |
required | |
error_raise
|
Retained for backward compatibility; the function raises on violation regardless of this flag. |
True
|
Returns:
| Type | Description |
|---|---|
|
|
Raises:
| Type | Description |
|---|---|
Exception
|
If any requirement is violated by any sample. |
Source code in pishield/linear_requirements/correct_predictions.py
Manual correction¶
manual_correct ¶
Reference (loop-based) implementation of the prediction correction.
Provides an explicit, per-variable implementation of the correction that the
:class:~pishield.linear_requirements.shield_layer.ShieldLayer performs with
precomputed matrices. It clips each variable into the interval implied by its
linear requirements, following the variable ordering.
get_partial_x_correction ¶
get_partial_x_correction(x: Variable, x_positive: bool, x_constraints: List[Constraint], preds: Tensor, epsilon=1e-12) -> torch.Tensor
Compute one side of the corrected value for a variable from its requirements.
For each requirement bounding x, eliminates x from the body, weights
the evaluated remainder by -1/coeff (positive x) or 1/coeff
(negative x), adds the weighted constant and an epsilon margin for
strict inequalities, then reduces across requirements: the tightest lower
bound (max) when x_positive, the tightest upper bound (min) otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Variable
|
The variable being corrected. |
required |
x_positive
|
bool
|
|
required |
x_constraints
|
List[Constraint]
|
The requirements bounding |
required |
preds
|
Tensor
|
Prediction tensor of shape |
required |
epsilon
|
Margin added for strict ( |
1e-12
|
Returns:
| Type | Description |
|---|---|
Tensor
|
A per-sample tensor with the partial bound on |
Tensor
|
|
Source code in pishield/linear_requirements/manual_correct.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
correct_preds ¶
correct_preds(preds: Tensor, ordering: List[Variable], sets_of_constr: {Variable: List[Constraint]})
Correct predictions variable by variable so they satisfy all requirements.
Iterates over the variables in ordering and, for each, clips its
predicted value into the [lower bound, upper bound] interval implied by
its requirements, using the already-corrected values of earlier variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
Prediction tensor of shape |
required |
ordering
|
List[Variable]
|
The order in which variables are corrected. |
required |
sets_of_constr
|
{Variable: List[Constraint]}
|
Mapping from each variable to the requirements bounding
it, as produced by :func: |
required |
Returns:
| Type | Description |
|---|---|
|
A tensor of the same shape as |
|
|
requirements. |
Source code in pishield/linear_requirements/manual_correct.py
Feature orderings¶
feature_orderings ¶
Selection of the variable ordering used to correct predictions.
Provides helpers to choose the ordering in which variables are corrected by the Shield Layer: either the ordering given in the requirements file or a random permutation of it.
set_random_ordering ¶
Shuffle a variable ordering in place into a random permutation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordering
|
List[Variable]
|
The list of variables to shuffle (modified in place). |
required |
Returns:
| Type | Description |
|---|---|
|
The same list, randomly permuted. |
Source code in pishield/linear_requirements/feature_orderings.py
set_ordering ¶
Select the variable ordering according to the chosen strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordering
|
List[Variable]
|
The variable ordering parsed from the requirements file. |
required |
label_ordering_choice
|
str
|
Either |
required |
Returns:
| Type | Description |
|---|---|
|
The chosen ordering of variables, or |
|
|
is neither |
Source code in pishield/linear_requirements/feature_orderings.py
Utilities¶
utils ¶
Utility functions for evaluating atoms and checking requirement satisfaction.
eval_atoms_list ¶
Evaluate a list of atoms against a batch of predictions.
Each atom is evaluated by multiplying its variable's predicted value by the atom's signed coefficient; the per-atom values are then reduced across atoms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms_list
|
List
|
The :class: |
required |
preds
|
Tensor
|
Prediction tensor of shape |
required |
reduction
|
How to combine the per-atom values. Only |
'sum'
|
Returns:
| Type | Description |
|---|---|
|
A tensor of shape |
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Source code in pishield/linear_requirements/utils.py
check_constraint_satisfaction ¶
Check whether predictions satisfy all requirements, printing the outcome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
Prediction tensor of shape |
required |
constraints
|
List
|
The requirements to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
otherwise. |