GLS
Gaussian loss smoothing with existing auto_LiRPA certified losses.
GLSModelWrapper
Bases: CTRAINWrapper
Wrap a classifier for certified training with Gaussian loss smoothing.
Gaussian smoothing makes objectives based on tight relaxations suitable for training. RGS averages gradients at sampled parameters, while PGPE estimates updates for both the parameter centre and the Gaussian scale. The selected bound_method supplies the underlying certified classification loss.
Following Balauca et al. (2025), PGPE experiments may start from adversarially pretrained weights: load them into model and set shi_init=False. Samples are evaluated sequentially. See https://arxiv.org/abs/2403.07095.
Source code in CTRAIN/model_wrappers/certified/gls_model_wrapper.py
11 12 13 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 | |
__init__(model, input_shape, eps, num_epochs, train_eps_factor=1, optimizer_func=torch.optim.Adam, lr=5e-05, warm_up_epochs=1, ramp_up_epochs=80, lr_scheduler_func=torch.optim.lr_scheduler.MultiStepLR, lr_decay_kwargs=None, checkpoint_save_path=None, checkpoint_save_interval=10, bound_opts=None, device='cuda', population_bn=False, estimator='rgs', samples=2, sigma=0.001, sigma_lr=0.001, sigma_decay_milestones=(), sigma_decay_factor=0.4, bound_method='CROWN', shi_init=True, shi_reg_weight=0.0, shi_reg_decay=True, l1_reg_weight=0.0, gradient_clip=None, *, lr_decay_schedule_unit='epoch')
Configure Gaussian smoothing, the certified loss and the 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
|
optimizer_func
|
callable
|
Optimiser constructor accepting the model parameters and a learning rate. |
Adam
|
lr
|
float
|
Learning rate passed to the optimiser. |
5e-05
|
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. |
80
|
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
|
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
|
estimator
|
str
|
Gaussian smoothing estimator: 'rgs' or 'pgpe'. |
'rgs'
|
samples
|
int
|
Number of sampled loss evaluations per update; PGPE requires an even count. |
2
|
sigma
|
float
|
Initial Gaussian perturbation scale in parameter space. |
0.001
|
sigma_lr
|
float
|
Learning rate for the scale optimiser used by PGPE. |
0.001
|
sigma_decay_milestones
|
Sequence[int]
|
Completed epochs at which RGS reduces its fixed perturbation scale. |
()
|
sigma_decay_factor
|
float
|
Multiplier for the RGS scale at each decay milestone. |
0.4
|
bound_method
|
str
|
Bound-propagation method used to construct the training loss. |
'CROWN'
|
shi_init
|
bool
|
Whether to apply the initialisation proposed by Shi et al. at the start of training. |
True
|
shi_reg_weight
|
float
|
Weight of the interval-stability regulariser proposed by Shi et al. (2021). |
0.0
|
shi_reg_decay
|
bool
|
Whether to reduce the Shi regulariser as the perturbation radius increases. |
True
|
l1_reg_weight
|
float
|
Weight of the L1 parameter regulariser. |
0.0
|
gradient_clip
|
float
|
Maximum parameter-gradient norm. None disables clipping where supported. |
None
|
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/gls_model_wrapper.py
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 | |
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 |
|---|---|
BoundedModule
|
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/gls_model_wrapper.py
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 | |