Sensitivity Analysis
Overview
Not all layers in a neural network are equally important. Some are fragile — compressing them even slightly degrades performance — while others are robust and can be heavily compressed with minimal impact. Sensitivity analysis measures this per-layer fragility, enabling smarter compression strategies.
The SensitivityAnalyzer works by compressing one layer at a time, measuring the impact on a user-provided evaluation metric, and ranking layers by their sensitivity (delta from baseline).
Key Features:
- Supports three compression types: sparsity (weight zeroing), pruning (structural filter removal), and quantization (precision reduction)
- Generates non-uniform per-layer targets via
to_layer_targets()— fragile layers get less compression, robust layers get more - Uses fasterai’s
SparsifierandPrunerinternally for consistent results - Visualization with
plot()and export to pandas withto_dataframe()
When to Use
| Compression Type | level parameter |
What it tests | Best for |
|---|---|---|---|
"sparsity" |
% of weights zeroed (e.g., 50) | Unstructured weight removal | Generating per-layer sparsity targets |
"pruning" |
% of filters removed (e.g., 30) | Structural filter pruning | Identifying layers to protect via ignored_layers |
"quantization" |
Bit width (e.g., 8) | Precision reduction | Finding layers that need higher precision |
Data Classes
Sensitivity results are returned as structured dataclasses for easy inspection, sorting, and export.
LayerSensitivity holds the result for a single layer: the metric before and after compression, the delta (positive = degradation), and layer metadata. Use as_dict() to serialize.
SensitivityResult aggregates all per-layer results. It provides methods to inspect, rank, visualize, and convert sensitivity data into actionable compression targets.
Key Methods
The to_layer_targets() method converts sensitivity scores into a dict[str, float] mapping layer names to compression percentages. This dict can be passed directly to Sparsifier.sparsify_model() or SparsifyCallback(sparsity=...) for non-uniform compression.
The gamma parameter controls differentiation: higher values protect fragile layers more aggressively.
targets = result.to_layer_targets(model, target_pct=50, min_pct=10, max_pct=80, gamma=1.5)
# {'conv1': 62.5, 'layer1.0.conv1': 65.3, 'layer1.1.conv2': 10.0, ...}SensitivityAnalyzer
The SensitivityAnalyzer class provides full control over the analysis process. For quick one-off analysis, see analyze_sensitivity() below.
The eval_fn should take a nn.Module and return a scalar metric (e.g., accuracy, loss). The analyzer will call it once per layer, so it should be reasonably fast — a forward pass on a small validation batch is typical.
Key Methods
For quantization analysis, additional parameters control the behavior:
quant_per_channel=True— per-channel quantization (more accurate, standard for weights)quant_activations=False— set toTrueto also quantize activations (slower but more realistic)levelis interpreted as bit width (e.g., 8 for INT8) instead of percentage
Convenience Function
For quick one-off analysis without creating an analyzer instance:
Usage Example
from fasterai.analyze.sensitivity import analyze_sensitivity
result = analyze_sensitivity(model, sample, eval_fn, compression="sparsity", level=50)
# Inspect results
result.summary() # formatted console output
fragile = result.top(5, most_sensitive=True) # most sensitive layers
# Generate per-layer targets for non-uniform compression
targets = result.to_layer_targets(model, target_pct=50, min_pct=10, max_pct=80)
# Pass directly to Sparsifier: sparsifier.sparsify_model(sparsity=targets)See Also
- Sensitivity Tutorial - Step-by-step guide with real examples on ResNet18
- Sparsifier - Apply non-uniform sparsity using
to_layer_targets()output - Pruner - Structural pruning (use
top()to find layers to protect) - Criteria - Importance scoring methods used during analysis
- Schedules - Control compression progression during training