Mlp #
CROWN/DeepPoly-style propagation for MLPs (vector in/out) using TorchLean tensors.
This file is a compact implementation that sits on top of:
NN.MLTheory.CROWN.Core(Box,AffineVec, andIBP.linear), and- TorchLean’s typed tensor layer (
Spec.Tensor).
What is implemented:
- Per-neuron ReLU linear relaxations derived from pre-activation bounds (
ReLU.relax_scalar*). - A basic IBP forward pass for common activations (ReLU/sigmoid/tanh/etc.).
- A 2-layer ReLU MLP wrapper
MLP2with a simple end-to-end bounding API.
Scope boundaries in this MLP-focused module:
- General computation graphs. Use
NN.MLTheory.CROWN.Graphfor the graph-level certificate checker andNN.MLTheory.CROWN.Proofs.GraphCrownCertSoundnessfor the corresponding end-to-end soundness theorem. - Objective-dependent / backward CROWN slope optimization. The
certificate infrastructure for alpha-CROWN and alpha/beta-CROWN artifacts lives under
NN.MLTheory.CROWN.Cert.AlphaCROWNandNN.MLTheory.CROWN.Cert.AlphaBetaCROWN.
References:
- CROWN: Zhang et al., "Efficient Neural Network Robustness Certification with General Activation Functions", arXiv:1811.00866.
- auto_LiRPA: Xu et al., "Automatic Perturbation Analysis for Scalable Certified Robustness and Beyond", NeurIPS 2020, arXiv:2002.12920.
PyTorch analogues:
torch.nn.Linear: https://pytorch.org/docs/stable/generated/torch.nn.Linear.htmltorch.nn.ReLU: https://pytorch.org/docs/stable/generated/torch.nn.ReLU.html
Parameters of a scalar linear relaxation used for bounding ReLU.
Typical shape (for crossing intervals l <= 0 < u) is an upper line y <= slope * x + bias with
slope ∈ [0, 1] and bias >= 0.
- slope : α
Linear coefficient (often written
αin the CROWN literature). - bias : α
Constant offset (often written
β).
Instances For
Compute a standard (upper) linear relaxation for scalar ReLU on an interval [l, u].
This matches the classic CROWN/DeepPoly choice:
- If
u <= 0, ReLU is identically 0. - If
l >= 0, ReLU is the identity. - If
l < 0 < u, use the upper chord through(l, 0)and(u, u).
Instances For
Compute a basic (lower) linear relaxation for scalar ReLU on [l, u].
For the crossing case l <= 0 < u, we choose either:
y >= 0(slope 0), ory >= x(slope 1), depending on which yields the tighter lower bound for the downstream objective.
Instances For
Apply relax_scalar elementwise to vector bounds (lo, hi).
Instances For
Apply relax_scalar_lower elementwise to vector bounds (lo, hi).
Instances For
Propagate an affine form through ReLU using a fixed per-neuron relaxation.
This is the "forward" DeepPoly-style propagation: given an affine bound on z, we produce an
affine bound on relu(z) by scaling rows and adjusting the constant term.
Instances For
Column-wise scaling of a matrix by a vector: scale each column j by v[j].
Instances For
Elementwise positive part of a matrix: replace negative entries by 0.
Instances For
Elementwise negative part of a matrix: replace positive entries by 0.
Instances For
Extract the slope vector from a tensor of ReLU relaxations.
Instances For
Extract the bias vector from a tensor of ReLU relaxations.
Instances For
Interval Bound Propagation (IBP) utilities for vector-shaped activations.
These are correctness-first helpers: they are intended to be sound for any Context α backend that
implements the scalar operations used by Activation.Math.*_spec.
The linear-layer bound helper IBP.linear lives in NN.MLTheory.CROWN.Core.
Interval bounds for ReLU on a vector.
This is the standard elementwise interval evaluation:
relu([l,u]) = [relu(l), relu(u)].
Instances For
Re-export of the runtime-only monotone-activation IBP helper.
We keep this file compact and Mathlib-friendly for proofs, but we do not want to
maintain two copies of the same computational rule. Canonical implementation lives in:
NN.MLTheory.CROWN.Runtime.Ops.IBP.map_minmax.
Semantics (per component): given an interval [l,u], this returns
[min(f(l), f(u)), max(f(l), f(u))] (intended for monotone f).
Instances For
Interval bounds for sigmoid.
Instances For
Interval bounds for tanh.
Instances For
Interval bounds for leaky-ReLU.
Sound when the negative slope αₗ is nonnegative.
Instances For
Interval bounds for ELU.
Sound for alpha > 0.
Instances For
2-layer MLP payload used by this file.
Semantics: y = W2 * relu(W1 * x + b1) + b2.
PyTorch analogue: torch.nn.Sequential(Linear(inDim,hidDim), ReLU(), Linear(hidDim,outDim)).
- W1 : Spec.Tensor α (Spec.Shape.dim hidDim (Spec.Shape.dim inDim Spec.Shape.scalar))
First layer weight matrix.
- b1 : Spec.Tensor α (Spec.Shape.dim hidDim Spec.Shape.scalar)
First layer bias vector.
- W2 : Spec.Tensor α (Spec.Shape.dim outDim (Spec.Shape.dim hidDim Spec.Shape.scalar))
Second layer weight matrix.
- b2 : Spec.Tensor α (Spec.Shape.dim outDim Spec.Shape.scalar)
Second layer bias vector.
Instances For
Forward semantics for MLP2 (used to state soundness theorems).
Instances For
Build an MLP2 from two LinearSpec records.
Instances For
Compute an output interval box via pure IBP.
This is fast and always sound, but typically looser than CROWN/DeepPoly affine bounds.
Instances For
Single-pass affine (CROWN/DeepPoly-style) bounds for the 2-layer ReLU MLP.
This constructs upper and lower affine forms by combining:
- pre-activation intervals from IBP,
- per-neuron ReLU relaxations, and
- sign-splitting of the second-layer weights.
Note: this is the direct MLP-specialized implementation; the graph-level engine in
NN.MLTheory.CROWN.Graph is the long-term home for fully-general CROWN.
Instances For
End-to-end bound API exposed by this file.
This API returns the IBP bound (sound for all backends); bound_affine_crown is kept as a
reference implementation for the 2-layer ReLU case.
Instances For
Theorems inspired by CROWN (Zhang et al., 2018, arXiv:1811.00866)
We record soundness properties of the relaxations and bound propagation. Proofs below require elementary order reasoning and case splits on signs, plus properties of mat-vec interval arithmetic.
Scalar ReLU relaxation soundness over ℝ (upper bound).
If x ∈ [l, u] and rp := ReLU.relax_scalar l u, then:
relu(x) <= rp.slope * x + rp.bias.
This is the standard CROWN/DeepPoly upper chord construction (arXiv:1811.00866).
Vectorized ReLU relaxation (pointwise upper bound) over ℝ.
If x ∈ [lo, hi] and rp := ReLU.relax_vector lo hi, then for every component i we have
relu(xᵢ) ≤ rpᵢ.slope * xᵢ + rpᵢ.bias.
Soundness of IBP.linear over ℝ.
If x ∈ xB and b ∈ bB, then W*x + b lies in the interval box computed by
IBP.linear W xB bB.
Soundness of pure IBP bounds for a 2-layer MLP over ℝ.
Soundness of the affine-bound entrypoint for a 2-layer MLP over ℝ.
In this module bound_affine delegates to the IBP implementation, so this theorem is a direct
corollary of bound_ibp_sound.
Compute both IBP bounds and affine-CROWN bounds for a 2-layer MLP around an ε-box.
The input set is the axis-aligned box centered at x_center with radius eps in each coordinate.
Instances For
Extract the scalar component t[i] from a vector-shaped tensor.
Instances For
Lower endpoint at index i from a vector box.
Instances For
Upper endpoint at index i from a vector box.
Instances For
Maximum upper bound among competitors k ≠ c.
Instances For
Certified margin lower bound: lowerAt c - maxCompetitorUpper c.
Instances For
Decide whether class c is certified by a positive margin.
Instances For
Return the argmax index of a concrete output vector (when n > 0), otherwise none.
This is a utility for pairing certified bounds with a predicted class.