Turn a trained neural network into a sector description you can use in control analysis.
Given a feedforward network, an input box, and an operating point, nnsector
computes linear bounds on how the network output changes throughout that box.
It also provides global bounds, Python and MATLAB implementations, and an
optional importer for PyTorch models.
Quick start · Your own network · MATLAB · How it works · Guarantees · Examples · Citation
- Bounds for control analysis. Obtain sector matrices for use in suitable absolute-stability, robustness, and region-of-attraction conditions. The companion nnstab library uses these bounds for exponential-stability verification.
- Choose the region that matters. Compute a local sector around your operating point, or a global bound valid for all inputs.
- Keep biases and shifted operating points. Bound changes relative to the actual network output at the selected point.
- A lightweight workflow. The Python core needs NumPy and no optimization solver. Use weights directly or import a supported
torch.nn.Sequentialmodel.
A sector bound describes the network. A closed-loop stability certificate also requires the plant dynamics and a stability condition whose assumptions are satisfied.
python -m pip install git+https://github.com/hmon91/nnsector.gitRequires Python 3.10+ and NumPy. PyTorch is needed only for
Network.from_torch; Matplotlib is needed only for plotting examples.
For MATLAB or GNU Octave, see setup below.
This example runs immediately with the network included in the package:
import nnsector as ns
net = ns.demo_network() # 1 → 8 → 8 → 1, tanh, with biases
sb = ns.local_bound(net, -1.0, 1.0, ystar=0.0) # local sector on [-1, 1]
print(f"Local sector: [{sb.gamma1.item():.4f}, {sb.gamma2.item():.4f}]")
print("Sample check passed:", ns.check(net, sb).passed)
gb = ns.global_bound(net) # bound for all inputsLocal sector: [-2.7863, -1.2002]
Sample check passed: True
For this single-input network, the result means that every secant from the
fixed operating point has a slope between sb.gamma1 and sb.gamma2:
The network was trained to approximate the controller
nnsector.
| Calculation | Slope interval, rounded | Scope |
|---|---|---|
global_bound(net) |
[-4.6588, 4.6588] |
Every pair of inputs |
local_bound(net, -1, 1) |
[-2.7863, -1.2002] |
Fixed anchor y* = 0, box [-1, 1] |
| Secants evaluated on a dense grid | [-2.5200, -1.4630] |
Sampled reference, not a proof of the extrema |
check searches for violations numerically. A successful sample check is a
useful diagnostic; it does not prove a bound.
Watch the sector change as the input box shrinks
The shaded sector applies only inside the selected box. A static view is available below.
Provide one weight matrix and one bias vector per affine layer. Weights use
the same (out_features, in_features) layout as torch.nn.Linear.
The final layer is linear; all hidden layers use the same activation.
import numpy as np
import nnsector as ns
net = ns.Network(
weights=[np.array([[1.0], [-0.7]]), np.array([[1.2, -0.8]])],
biases=[np.array([0.2, -0.1]), np.array([0.05])],
activation="tanh",
)
sb = ns.local_bound(net, -1.0, 1.0, ystar=0.25)
print(sb.gamma1, sb.gamma2)Omit biases to use zero biases. For a network with p inputs and m
outputs, net(y) accepts one input of shape (p,), or a batch of shape
(N, p), and returns (m,) or (N, m) respectively.
Install PyTorch if needed (python -m pip install torch). Then import a
supported sequential model:
import torch.nn as nn
import nnsector as ns
model = nn.Sequential(
nn.Linear(1, 8), nn.Tanh(),
nn.Linear(8, 8), nn.Tanh(),
nn.Linear(8, 1),
)
# Train the model, or load your trained weights, before analyzing it.
model.eval()
net = ns.Network.from_torch(model)
sb = ns.local_bound(net, -1.0, 1.0)The importer copies weights to NumPy double precision. It accepts nested
nn.Sequential blocks and merges consecutive nn.Linear layers.
nn.Identity, nn.Dropout, and nn.Flatten are skipped under the vector-input,
evaluation-mode interpretation. Hidden activations must all have the same
type and parameters; nn.Softplus requires beta=1.
Convolutions, residual connections, recurrent layers, and a final activation
are outside this importer's supported model format.
activation |
Function | Global secant-slope range |
|---|---|---|
"tanh" (default) |
||
"sigmoid" |
||
"relu" |
||
"leakyrelu" |
||
"softplus" |
Leaky ReLU uses alpha=0.01 by default and accepts alpha >= 0, for example
ns.Network(weights, biases, activation="leakyrelu", alpha=0.1).
The supported activations are nondecreasing; they need not vanish at zero.
Weights may have either sign.
ystar must lie inside the input box. It defaults to zero, so set it
explicitly when your box does not contain zero.
net = ns.demo_network()
sb = ns.local_bound(net, -1.0, 1.0, ystar=0.5)
print(f"[{sb.gamma1.item():.4f}, {sb.gamma2.item():.4f}]")
# [-2.3799, -0.7512]The local result bounds ystar=0 gives a bound relative to
By default, intervals="intersect" refines each neuron interval using both
interval bound propagation (IBP) and the propagated linear bounds.
Use intervals="ibp" to use only the IBP intervals in this same sector algorithm.
On the demo over [-1, 1], these modes return [-2.7863, -1.2002] and
[-2.8782, 0.2514], respectively: a 49.3% reduction in slope-interval width
for the default mode on this example. This is a comparison of two nnsector
settings, not a comparison with another library.
With several inputs, the sign of each component of nnsector splits the box at the anchor and computes a bound on each piece.
import numpy as np
import nnsector as ns
net = ns.Network(
[np.array([[1.0, 0.4], [-0.5, 1.0]]), np.array([[1.2, -0.7]])],
[np.array([0.1, -0.2]), np.array([0.0])],
activation="tanh",
)
sb = ns.local_bound(net, [0, 0], [2, 1], ystar=[1, 0.5])
print(len(sb.pieces)) # 4
print(sb.gamma1 is None) # True: use the piecewise matrices
for piece in sb.pieces:
print(piece.signs, piece.L, piece.U)An interior anchor can produce up to p inputs. A
lower-corner anchor needs one piece. This sign partitioning is an important
cost consideration for high-dimensional inputs.
Write p inputs, m outputs, q hidden layers, and a linear
output layer. The implementation computes its bounds in double precision.
One input. For every
The boundary lines are
Several inputs, with the anchor at the lower corner. Since
Several inputs, other anchor positions. Use sb.pieces. On piece k,
let piece.signs. For
Here sb.gamma1 and sb.gamma2 are None; piece.L and piece.U
have shape (m, p).
Global bound. For every pair
where gb.Gamma1 and gb.Gamma2 have shape (m, p).
With zero biases and an activation that vanishes at zero (tanh, ReLU, or
leaky ReLU),
On each sign piece, nnsector propagates bounds on deviations from the
operating point through the network:
-
Evaluate the anchor. Compute the nominal output and each neuron's nominal preactivation
$\nu^*$ . -
Propagate through the weights (S1). Use the positive and negative parts of each weight matrix to bound preactivation changes by linear functions of
$z=|y-y^*|$ . -
Bound each activation (S2). Enclose the activation change using secant slopes over its preactivation interval. Refine the interval with the linear bounds when
intervals="intersect". -
Apply the output layer. Return the matrices
$L_k,U_k$ , and combine the pieces into scalar-input sector slopes when applicable.
Biases cancel from the difference at an affine layer, but still affect the nominal preactivations and therefore the activation slopes.
The three activation cases and returned diagnostics
For each neuron, the preactivation interval is above
Each piece.case3 records the number of Case 3 neurons per hidden layer.
This helps explain conservatism: on the demo, intervals="ibp" gives
[0, 8] per piece, while the default refinement gives [0, 0].
The result also contains ystar, pistar, y_lo, y_hi, intervals, and
activation. Each piece contains signs, z_hi, L, U, and case3.
Clone or download the repository and add its matlab folder to your path.
From the repository root:
addpath('matlab')
net = nnsector.demo_network();
sb = nnsector.local_bound(net, -1, 1, 'ystar', 0);
disp([sb.gamma1, sb.gamma2])
gb = nnsector.global_bound(net);
res = nnsector.check(net, sb);
disp(res.passed)Requires MATLAB R2016b+ or GNU Octave. The same local and global
bound forms are available. MATLAB evaluates batches by columns using
nnsector.forward(net, X); Python evaluates batches by rows.
See the MATLAB guide for custom networks, options,
examples, and tests.
| Function | Purpose |
|---|---|
Network(weights, biases=None, activation="tanh", alpha=None) |
Define a network from arrays. |
Network.from_torch(model) |
Import a supported sequential PyTorch model. |
local_bound(net, y_lo, y_hi, ystar=None, intervals="intersect") |
Compute a local sector on an input box. Scalar box endpoints and anchors broadcast to every input. |
global_bound(net) |
Compute a bound valid for every pair of inputs. |
check(net, bound, samples=20000, box=None, ystar=None, seed=0) |
Search sample points for bound violations. For a global bound, supply box=(y_lo, y_hi). |
demo_network() |
Load the bundled trained tanh network. |
| Example | What you will learn |
|---|---|
| Demo network | Local and global bounds, shifted anchors, and plotting. |
| Biases and multiple inputs | Bound biased networks and inspect sign pieces. |
| Activation functions | Apply the supported activations to the demo weights. |
| PyTorch controller | Train a small controller and compute its sector. |
| README figures | Reproduce the figures and their numerical data. |
For development:
git clone https://github.com/hmon91/nnsector.git
cd nnsector
python -m pip install -e ".[test,examples]"
pytest
python examples/example1_demo.pyThe tests check the demo's numerical results, compare activation secant ranges against dense evaluations, and sample bounds for networks with biases and different input dimensions. PyTorch-specific tests require PyTorch.
To regenerate the README figures from a development checkout:
python -m pip install pillow
python docs/make_readme_figures.pyStatic figures are provided as PNG and SVG, together with an animated GIF. See figure notes and data.
auto_LiRPA provides
linear-relaxation bound propagation on general PyTorch computational graphs.
nnsector focuses on sector descriptions of feedforward networks around
operating points, with a small NumPy core and a MATLAB implementation.
nnstab connects these sector bounds to
stability verification of neural-network feedback systems.
If you use nnsector in your research, please cite:
H. Montazeri Hedesh, M. K. Wafi, and M. Siami, “Local Stability and Region of Attraction Analysis for Neural Network Feedback Systems under Positivity Constraints,” 2025 IEEE 64th Conference on Decision and Control (CDC), Rio de Janeiro, Brazil, 2025. DOI: 10.1109/CDC57313.2025.11312171 · Preprint: arXiv:2505.22889
@inproceedings{montazerihedesh2025local,
author = {Montazeri Hedesh, Hamidreza and Wafi, Moh Kamalul and Siami, Milad},
title = {Local Stability and Region of Attraction Analysis for Neural Network
Feedback Systems under Positivity Constraints},
booktitle = {2025 IEEE 64th Conference on Decision and Control (CDC)},
year = {2025},
doi = {10.1109/CDC57313.2025.11312171}
}Questions, reproducible bug reports, and examples are welcome in
GitHub Issues.
nnsector is released under the BSD 3-Clause License.





