GLS
Gaussian loss smoothing (Balauca et al., arXiv:2403.07095).
The released PGPE implementation uses preconditioned score gradients: d(L+-L-) for the mean and (d²-s²)/s(mean(L+,L-)-baseline) for the deviation. RGS averages ordinary gradients at independent Gaussian parameter samples.
GLSOptimizer
Bases: Optimizer
Optimise a Gaussian-smoothed loss using RGS or PGPE.
RGS averages ordinary gradients at independent Gaussian parameter samples. PGPE uses antithetic samples and the unperturbed loss as a baseline to estimate centre and scale updates, following the released implementation. The sample count includes both sides of each PGPE pair and must be even.
Buffers and centre weights are restored between samples. RGS keeps a fixed scale, optionally decayed by the trainer; PGPE learns its scale with a maximum 2% change per update. Its optimiser states and scales can be checkpointed.
Source code in CTRAIN/train/certified/gls.py
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 | |
__init__(model, optimizer_func=torch.optim.Adam, lr=5e-05, estimator='rgs', samples=2, sigma=0.001, sigma_lr=0.001, gradient_clip=None)
Configure the smoothing estimator, parameter optimiser and Gaussian scales.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch model to train or evaluate. |
required |
optimizer_func
|
callable
|
Optimiser constructor accepting the model parameters and a learning rate. |
Adam
|
lr
|
float
|
Learning rate passed to the optimiser. |
5e-05
|
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
|
gradient_clip
|
float
|
Maximum parameter-gradient norm. None disables clipping where supported. |
None
|
Source code in CTRAIN/train/certified/gls.py
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 | |
load_state_dict(state)
Restore smoothing and optimiser state, requiring a matching estimator and sample count.
Source code in CTRAIN/train/certified/gls.py
75 76 77 78 79 80 81 82 83 84 | |
state_dict()
Return centre-optimiser, scale-optimiser and Gaussian-scale state for checkpointing.
Source code in CTRAIN/train/certified/gls.py
68 69 70 71 72 73 | |
step(closure)
Evaluate the closure at sampled parameters and update the Gaussian centre.
The closure returns a scalar loss without applying an optimiser step. RGS computes and averages sample gradients; PGPE evaluates antithetic losses and also updates scale parameters. Sampling restores model parameters, buffers and pre-existing gradients if loss or gradient computation fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
closure
|
callable
|
Zero-argument scalar objective on the current sampled model. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Mean sampled loss detached from the autograd graph. |
Source code in CTRAIN/train/certified/gls.py
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 | |
gls_train_model(original_model, hardened_model, train_loader, optimizer, lr_scheduler, num_epochs, eps, n_classes, start_epoch=0, end_epoch=None, warm_up_epochs=1, ramp_up_epochs=80, bound_method='CROWN', shi_init=True, shi_reg_weight=0.0, shi_reg_decay=True, l1_reg_weight=0.0, population_bn=False, sigma_decay_milestones=(), sigma_decay_factor=0.4, results_path=None, checkpoint_save_interval=10, device='cuda', val_loader=None, eval_eps=None, selection_state=None, *, validation_eps=None, lr_decay_schedule_unit='epoch', history=None)
Train a classifier using a Gaussian-smoothed certified objective.
After natural-loss warm-up, a linear perturbation ramp introduces the selected bound-based loss. GLSOptimizer evaluates this objective at sampled parameter values. Validation selects the centre model using sound bounds after ramp-up, while checkpoints retain the optimiser trajectory.
No clean/robust loss mixing is used after warm-up. Without validation, the final model is returned. Validation selects by sound centre-model bounds after ramp-up; optimiser checkpoints retain the training trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_model
|
Module
|
Original PyTorch model sharing the trainable network parameters. |
required |
hardened_model
|
BoundedModule
|
Bounded auto_LiRPA graph used for training and bound propagation. |
required |
train_loader
|
DataLoader
|
Training loader providing input and target batches and any required normalisation metadata. |
required |
optimizer
|
Optimizer
|
Optimiser holding the model parameters and current update state. |
required |
lr_scheduler
|
LRScheduler
|
Learning-rate scheduler associated with the supplied optimiser. |
required |
num_epochs
|
int
|
Total training budget, including warm-up and ramp-up. |
required |
eps
|
float or Tensor
|
Target perturbation radius in raw input units, before loader normalisation. |
required |
n_classes
|
int
|
Number of output classes. |
required |
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
|
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
|
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
|
population_bn
|
bool
|
Whether to recalibrate BatchNorm on training data after each epoch, before model selection and saving. |
False
|
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
|
results_path
|
str or Path
|
Directory for the results or checkpoints written by this function. |
None
|
checkpoint_save_interval
|
int
|
Save an epoch checkpoint at this interval and at the end of the training call. |
10
|
device
|
str or device
|
Device on which to run the model and tensor computations. |
'cuda'
|
val_loader
|
DataLoader
|
Optional validation loader used for model selection. Without it, training returns the final weights. |
None
|
eval_eps
|
float or Tensor
|
Legacy name for validation_eps. Supplying conflicting values is rejected. |
None
|
selection_state
|
dict
|
Mutable dictionary retaining the best validation score, inference weights and epoch across training calls. |
None
|
validation_eps
|
float or Tensor
|
Fixed raw-input radius used for validation; None uses the training target. |
None
|
lr_decay_schedule_unit
|
str
|
Whether to advance the learning-rate scheduler per 'epoch' or per 'batch'. |
'epoch'
|
history
|
list
|
Optional list to which the trainer appends per-epoch losses, radii and learning rates. |
None
|
Source code in CTRAIN/train/certified/gls.py
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 | |