CTRAIN
CTRAINWrapper.evaluate(...) returns (standard_accuracy, certified_accuracy,
adversarial_accuracy).
CTRAINWrapper.evaluate_complete(...) also returns the same aggregate tuple,
but additionally persists complete-verification details to
results_path/results_filename and writes abCROWN logs under
results_path/abCROWN_logs. Use warm_start=True to reuse existing results, or
start_idx/end_idx to verify a dataset slice. Complete verification expects
the newer abCROWN-compatible auto_LiRPA installation, such as
auto_LiRPA 0.7.0.
CTRAINWrapper.hpo(...) is the standard HPO entry point. It runs the
novel multi-objective optimisation procedure, maximizes natural and
certified validation accuracy, stores the Optuna study in
output_dir/optuna_study.db, writes trial checkpoints to output_dir/nets, and
returns the whole Pareto front. It does not load one checkpoint implicitly; pick
the Pareto point you want and load its checkpoint_path. If min_nat_acc or
min_cert_acc are set, the returned front is computed over feasible trials when
any feasible trial exists; otherwise, it falls back to the unconstrained front
and marks entries as infeasible.
CTRAINWrapper.hpo_single_objective(...) runs scalar Optuna HPO. By default it
maximizes nat_acc + cert_acc, returns the best trial record, and loads that
checkpoint unless load_best=False. Change the scalar objective with
nat_acc_weight, adv_acc_weight, and cert_acc_weight.
CTRAINWrapper.hpo_smac(...) keeps the older SMAC3 single-objective workflow.
Use sampler="botorch" for the default constrained Bayesian optimization
sampler or sampler="nsgaii" for a lighter Optuna-only smoke test.
CTRAINWrapper
Bases: Module
Wrapper base class for certifiably training models.
Source code in CTRAIN/model_wrappers/model_wrapper.py
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
__init__(model, eps, input_shape, train_eps_factor=1, lr=0.0005, optimizer_func=torch.optim.Adam, lr_scheduler_func=torch.optim.lr_scheduler.MultiStepLR, lr_decay_kwargs=dict(milestones=(80, 90), gamma=0.2), bound_opts=dict(conv_mode='patches', relu='adaptive'), device='cuda', checkpoint_save_path=None, checkpoint_save_interval=10)
Initialize the CTRAINWrapper Base Class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The neural network model to be wrapped. |
required |
eps
|
float
|
The epsilon value for training. |
required |
input_shape
|
tuple
|
The shape of the input tensor. |
required |
train_eps_factor
|
float
|
Factor to scale epsilon during training. Default is 1. |
1
|
lr
|
float
|
Learning rate for the optimizer. Default is 0.0005. |
0.0005
|
optimizer_func
|
Optimizer
|
The optimizer function to use. Default is torch.optim.Adam. |
Adam
|
bound_opts
|
dict
|
Options for bounding the model. Default is {'conv_mode': 'patches', 'relu': 'adaptive'}. |
dict(conv_mode='patches', relu='adaptive')
|
device
|
str or device
|
The device to run the model on. Default is 'cuda'. |
'cuda'
|
checkpoint_save_path
|
str
|
Path to save checkpoints. Default is None. |
None
|
checkpoint_save_interval
|
int
|
Interval to save checkpoints. Default is 10. |
10
|
Attributes:
| Name | Type | Description |
|---|---|---|
original_model |
Module
|
The original neural network model. |
eps |
float
|
The epsilon value for training. |
train_eps |
float
|
The scaled epsilon value for training. |
device |
device
|
The device to run the model on. |
n_classes |
int
|
The number of classes in the model's output. |
bound_opts |
dict
|
Options for bounding the model. |
bounded_model |
BoundedModule
|
The bounded version of the original model. |
input_shape |
tuple
|
The shape of the input tensor. |
optimizer_func |
Optimizer
|
The optimizer function. |
optimizer |
Optimizer
|
The optimizer instance. |
epoch |
int
|
The current epoch number. |
checkpoint_path |
str
|
Path to save checkpoints. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
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 | |
eval()
Sets the model to evaluation mode.
This method sets both the original model and the bounded model to evaluation mode. In evaluation mode, certain layers like dropout and batch normalization behave differently compared to training mode, typically affecting the model's performance and predictions.
Source code in CTRAIN/model_wrappers/model_wrapper.py
109 110 111 112 113 114 115 116 117 118 | |
evaluate(test_loader, test_samples=np.inf, eval_method='ADAPTIVE')
Evaluate the model using the provided test data loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_loader
|
DataLoader
|
DataLoader containing the test dataset. |
required |
test_samples
|
int
|
Number of test samples to evaluate. Defaults to np.inf. |
inf
|
eval_method
|
str or list
|
The certification method to use. Options are 'IBP', 'CROWN', 'CROWN-IBP', 'ADAPTIVE', or a list of methods (which results in an ADAPTIVE evaluation using these methods). Default is 'ADAPTIVE'. |
'ADAPTIVE'
|
Returns:
| Type | Description |
|---|---|
Tuple
|
Evaluation results in terms of std_acc, cert_acc and adv_acc. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
evaluate_complete(test_loader, test_samples=np.inf, timeout=1000, no_cores=4, abcrown_batch_size=512, abcrown_config_dict=None, results_path='./abcrown_results', warm_start=False, start_idx=0, end_idx=None, results_filename='results.json')
Evaluate the model using the complete verification tool abCROWN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
test_loader
|
DataLoader
|
DataLoader for the test set. |
required |
test_samples
|
int
|
Number of test samples to evaluate. Defaults to np.inf. |
inf
|
timeout
|
int
|
Per-instance timeout for the verification process in seconds. Defaults to 1000. |
1000
|
no_cores
|
int
|
Number of CPU cores to use for verification. Only relevant, if MIP refinement is used in abCROWN. Defaults to 4. |
4
|
abcrown_batch_size
|
int
|
Batch size for abCROWN. Defaults to 512. Decrease, if you run out of memory. |
512
|
abcrown_config_dict
|
dict
|
Configuration dictionary for abCROWN according to the tools documentation. Defaults to an empty dictionary. |
None
|
results_path
|
str
|
Path to save abCROWN results and logs. Defaults to './abcrown_results'. |
'./abcrown_results'
|
warm_start
|
bool
|
Reuse existing results from the results file. Defaults to False. |
False
|
start_idx
|
int
|
First dataset index to verify. Defaults to 0. |
0
|
end_idx
|
int
|
Exclusive end dataset index to verify. Defaults to None. |
None
|
results_filename
|
str
|
JSON file name under results_path. Defaults to 'results.json'. |
'results.json'
|
Returns:
| Type | Description |
|---|---|
tuple): A tuple containing: std_acc (float): Standard accuracy of the model on the test set, certified_acc (float): Certified accuracy of the model on the test set and adv_acc (float
|
Adversarial accuracy of the model on the test set. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
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 | |
forward(x)
Perform a forward pass through the LiRPA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor to be passed through the model. |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Output tensor after passing through the bounded model. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
120 121 122 123 124 125 126 127 128 129 130 | |
hpo(train_loader, val_loader, budget_time=5 * 24 * 60 * 60, budget_trials=np.inf, defaults=None, eval_samples=np.inf, output_dir='./optuna_hpo', min_nat_acc=0.0, min_cert_acc=0.0, seed=0, sampler='botorch', complete_verify=False, study_name='moctrain')
Perform multi-objective HPO with Optuna and return the Pareto front.
The objectives are natural and certified validation accuracy. The full
Pareto front is stored in the Optuna study database under output_dir.
Trial checkpoints are written to output_dir/nets. This method does
not load one checkpoint implicitly, because selecting a final model is a
downstream decision on the returned Pareto front.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_loader
|
DataLoader
|
DataLoader for the training dataset. |
required |
val_loader
|
DataLoader
|
DataLoader for validation. |
required |
budget_time
|
int
|
Wall-clock budget in seconds. |
5 * 24 * 60 * 60
|
budget_trials
|
int or float
|
Maximum number of trials. |
inf
|
defaults
|
dict
|
Default hyperparameter values for the ConfigSpace. |
None
|
eval_samples
|
int
|
Number of validation samples used per trial. |
inf
|
output_dir
|
str
|
Directory for Optuna DBs and trial checkpoints. |
'./optuna_hpo'
|
min_nat_acc
|
float
|
Feasibility threshold for natural accuracy. |
0.0
|
min_cert_acc
|
float
|
Feasibility threshold for certified accuracy. |
0.0
|
seed
|
int
|
Random seed. |
0
|
sampler
|
str or BaseSampler
|
|
'botorch'
|
complete_verify
|
bool
|
Use complete verification for the certified objective. |
False
|
study_name
|
str
|
Name of the persisted Optuna study. |
'moctrain'
|
Returns:
| Type | Description |
|---|---|
|
list[dict]: Pareto-optimal trials with their configs, metrics, feasibility status, and checkpoint paths. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
hpo_single_objective(train_loader, val_loader, budget_time=5 * 24 * 60 * 60, budget_trials=np.inf, defaults=None, eval_samples=np.inf, output_dir='./optuna_hpo_single_objective', nat_acc_weight=1.0, adv_acc_weight=0.0, cert_acc_weight=1.0, seed=0, sampler='botorch', complete_verify=False, study_name='ctrain_single_objective', load_best=True)
Perform scalar Optuna HPO and optionally load the best checkpoint.
By default, the optimized objective is nat_acc + cert_acc. The
objective can be changed with nat_acc_weight, adv_acc_weight,
and cert_acc_weight.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Best trial with its scalar objective value, config, metrics, and checkpoint path. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
hpo_smac(train_loader, val_loader, budget=5 * 24 * 60 * 60, defaults=dict(), eval_samples=1000, output_dir='./smac_hpo', deterministic=False, seed=42, nat_loss_weight=1.0, adv_loss_weight=1.0, cert_loss_weight=1.0)
Perform single-objective hyperparameter optimization using SMAC3.
After the method returns, the model will have loaded the best
hyperparameters found during the optimization and the corresponding
trained weights. New code should prefer :meth:hpo for multi-objective
Optuna HPO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_loader
|
DataLoader
|
DataLoader for the training dataset. |
required |
val_loader
|
DataLoader
|
DataLoader for the validation dataset. |
required |
budget
|
int
|
Time budget for the HPO process in seconds. Default is 5 days (52460*60). |
5 * 24 * 60 * 60
|
defaults
|
dict
|
Default hyperparameter values. Default is an empty dictionary. |
dict()
|
eval_samples
|
int
|
Number of samples to use for loss computation. Default is 1000. |
1000
|
output_dir
|
str
|
Directory to store HPO results. Default is './smac_hpo'. |
'./smac_hpo'
|
deterministic
|
bool
|
Whether SMAC3 should treat the objective function as deterministic. Speeds up the optimisation. Default is False. |
False
|
seed
|
int
|
Random seed for reproducibility of the HPO. Default is 42. |
42
|
nat_loss_weight
|
float
|
Weight for the natural accuracy in the loss function. |
1.0
|
adv_loss_weight
|
float
|
Weight for the adversarial accuracy in the loss function. |
1.0
|
cert_loss_weight
|
float
|
Weight for the certified accuracy in the loss function. |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Configuration |
The best hyperparameter configuration found during the optimization. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
load_state_dict(state_dict, strict=True)
Load the state dictionary into the bounded LiRPA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
dict
|
A dictionary containing model state parameters. |
required |
strict
|
bool
|
Whether to strictly enforce that the keys
in |
True
|
Returns:
| Type | Description |
|---|---|
NamedTuple
|
A named tuple with fields |
Source code in CTRAIN/model_wrappers/model_wrapper.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
resume_from_checkpoint(checkpoint_path, train_loader, val_loader=None, end_epoch=None)
Resume training from a given checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint_path
|
str
|
Path to the checkpoint file. |
required |
train_loader
|
DataLoader
|
DataLoader for the training dataset. |
required |
val_loader
|
DataLoader
|
DataLoader for the validation dataset. Defaults to None. |
None
|
end_epoch
|
int
|
Epoch to prematurely end training at. Defaults to None. |
None
|
Loads the model and optimizer state from the checkpoint, sets the starting epoch, and resumes training from that epoch.
Source code in CTRAIN/model_wrappers/model_wrapper.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
state_dict(destination=None, prefix='', keep_vars=False)
Returns the state dictionary of the LiRPA model.
The state dictionary contains the model parameters and persistent buffers.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A dictionary containing the model's state. |
Source code in CTRAIN/model_wrappers/model_wrapper.py
204 205 206 207 208 209 210 211 212 213 | |
train(mode=True)
Sets wrapper into training mode.
This method calls the train method on both the original_model and
the bounded_model to set them into training mode
Source code in CTRAIN/model_wrappers/model_wrapper.py
99 100 101 102 103 104 105 106 107 | |