Set-based wrapper API
See set-based training for graph restrictions, objectives, BatchNorm statistics and sound full-box evaluation.
Bases: CTRAINWrapper
Wrap a sequential classifier for set-based certified training.
The objective combines classification at the output centre with a penalty on the propagated output enclosure. Training retains correlated generators through auto_LiRPA forward operators and uses clean-batch BatchNorm statistics. Supported layers are checked against the model's forward graph. Evaluation converts current native weights and running buffers to a fresh bounded graph; full-box certification is independent of attack-generated training sets.
See Koller et al. (2025), Set-Based Training for Neural Network Verification.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
14 15 16 17 18 19 20 21 22 23 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
__init__(model, input_shape, eps, num_epochs, train_eps_factor=1.0, optimizer_func=torch.optim.Adam, lr=0.0005, warm_up_epochs=1, ramp_up_epochs=20, lr_scheduler_func=torch.optim.lr_scheduler.MultiStepLR, lr_decay_kwargs=None, gradient_clip=10.0, checkpoint_save_path=None, checkpoint_save_interval=10, bound_opts=None, device='cuda', population_bn=False, tau=0.1, input_set='box', num_generators=8, error_propagation='interval', propagation_batch_size=1, pgd_eps_factor=1.0, *, lr_decay_schedule_unit='epoch')
Configure the input enclosure, output penalty and set-based training schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch model to train or evaluate. |
required |
input_shape
|
tuple[int, ...]
|
Shape of a model input, excluding the batch dimension unless supplied explicitly. |
required |
eps
|
float or Tensor
|
Target perturbation radius in raw input units, before loader normalisation. |
required |
num_epochs
|
int
|
Total training budget, including warm-up and ramp-up. |
required |
train_eps_factor
|
float
|
Factor applied to the training radius; the nominal evaluation radius remains eps. |
1.0
|
optimizer_func
|
callable
|
Optimiser constructor accepting the model parameters and a learning rate. |
Adam
|
lr
|
float
|
Learning rate passed to the optimiser. |
0.0005
|
warm_up_epochs
|
int
|
Initial epochs using only the natural task loss. |
1
|
ramp_up_epochs
|
int
|
Epochs over which the perturbation radius increases to its training target. |
20
|
lr_scheduler_func
|
callable
|
Constructor for the learning-rate scheduler. |
MultiStepLR
|
lr_decay_kwargs
|
dict
|
Scheduler arguments, such as milestones and gamma for MultiStepLR. |
None
|
gradient_clip
|
float
|
Maximum parameter-gradient norm. None disables clipping where supported. |
10.0
|
checkpoint_save_path
|
str or Path
|
Optional directory for epoch checkpoints containing the training trajectory. |
None
|
checkpoint_save_interval
|
int
|
Save an epoch checkpoint at this interval and at the end of the training call. |
10
|
bound_opts
|
dict
|
Bound-propagation options passed to auto_LiRPA. |
None
|
device
|
str or device
|
Device on which to run the model and tensor computations. |
'cuda'
|
population_bn
|
bool
|
Whether to recalibrate BatchNorm on training data after each epoch, before model selection and saving. |
False
|
tau
|
float
|
Weight of the output-enclosure penalty, with 1 - tau weighting centre cross-entropy. |
0.1
|
input_set
|
str
|
Training input representation: 'box' or an attack-generated 'zonotope'. |
'box'
|
num_generators
|
int
|
Number of attack directions used to construct an input zonotope. |
8
|
error_propagation
|
str
|
Whether nonlinear interval errors remain an 'interval' or become explicit 'generators'. |
'interval'
|
propagation_batch_size
|
int
|
Maximum number of inputs propagated together to limit bound-computation memory. |
1
|
pgd_eps_factor
|
float
|
Positive factor scaling the PGD search range, independently of the nominal bound region. |
1.0
|
lr_decay_schedule_unit
|
str
|
Whether to advance the learning-rate scheduler per 'epoch' or per 'batch'. |
'epoch'
|
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
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 | |
eval()
Set the wrapper and native network to evaluation mode.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
120 121 122 | |
evaluate(test_loader, test_samples=float('inf'), eval_method='FORWARD', adversarial_method='PGD', alpha_iterations=20, autoattack_kwargs=None, propagation_batch_size=None)
Evaluate current native weights through a fresh bounded network copy.
Arguments and returned natural, certified and adversarial accuracies follow CTRAINWrapper.evaluate.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
178 179 180 181 182 183 184 185 186 187 188 189 | |
evaluate_complete(*args, **kwargs)
Evaluate current native weights through the complete-verification interface.
Arguments and returned accuracy tuple follow CTRAINWrapper.evaluate_complete.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
191 192 193 194 195 196 | |
forward(x)
Evaluate the native network on the supplied input batch.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
124 125 126 | |
load_state_dict(state_dict, strict=True)
Load native network parameters and buffers, checking keys when strict=True.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
132 133 134 | |
parameters(recurse=True)
Iterate over the native network parameters exposed to the optimiser.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
136 137 138 | |
resume_from_checkpoint(checkpoint_path, train_loader, val_loader=None, end_epoch=None)
Restore the training trajectory and selection state, without RNG replay.
Loads model, optimiser and LR scheduler state; caller-managed RNG and data-loader state are not restored, as in the other training wrappers.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
state_dict(destination=None, prefix='', keep_vars=False)
Return the native network parameters and persistent buffers for inference.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
128 129 130 | |
train(mode=True)
Set the wrapper and native network to the requested training mode.
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
115 116 117 118 | |
train_model(train_loader, val_loader=None, start_epoch=0, end_epoch=None)
Train the wrapped model using the configured method and schedule.
The wrapper forwards its configuration to the corresponding library trainer. start_epoch and end_epoch select a segment of the full schedule, retaining its original warm-up, ramp-up and learning-rate milestones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_loader
|
DataLoader
|
Training loader providing input and target batches and any required normalisation metadata. |
required |
val_loader
|
DataLoader
|
Optional validation loader used for model selection. Without it, training returns the final weights. |
None
|
start_epoch
|
int
|
Zero-based starting epoch in the full training schedule. |
0
|
end_epoch
|
int
|
Exclusive ending epoch. None runs to the end of the configured schedule. |
None
|
Returns:
| Type | Description |
|---|---|
Module
|
Trained model. Validation-selected inference weights are restored when a validation loader is supplied; otherwise final weights are returned. Optimiser and scheduler state remain at the final epoch. |
Source code in CTRAIN/model_wrappers/certified/set_based_model_wrapper.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |