PGD Teachers
Bases: Module
Wrap a native PyTorch classifier for PGD adversarial training.
This wrapper can train adversarial teachers for CC-Dist without constructing a bound graph. SGD with momentum 0.9 follows a triangular learning-rate schedule, peaking at lr halfway through training. Evaluation converts a copy of the trained network through the common CTRAIN interfaces.
The optimiser is SGD(momentum=.9), peaking at lr halfway through training.
Training accepts native PyTorch classifiers, including residual networks.
Evaluation converts a copy using the existing CTRAIN evaluation interfaces.
Checkpoints store native weights and batch LR state, not RNG/loader state.
Source code in CTRAIN/model_wrappers/adversarial/pgd_model_wrapper.py
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 | |
__init__(model, input_shape, eps, num_epochs, lr=0.2, pgd_steps=10, pgd_alpha=0.25, pgd_restarts=1, gradient_clip=10, l1_reg_weight=0.0, shi_init=True, population_bn=False, checkpoint_save_path=None, checkpoint_save_interval=10, device='cuda')
Configure native PGD training with the triangular SGD 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 |
lr
|
float
|
Learning rate passed to the optimiser. |
0.2
|
pgd_steps
|
int
|
Number of projected gradient updates in the training attack. |
10
|
pgd_alpha
|
float
|
PGD step-size factor relative to the search-region radius. |
0.25
|
pgd_restarts
|
int
|
Number of random starts for the training attack. |
1
|
gradient_clip
|
float
|
Maximum parameter-gradient norm. None disables clipping where supported. |
10
|
l1_reg_weight
|
float
|
Weight of the L1 parameter regulariser. |
0.0
|
shi_init
|
bool
|
Whether to apply the initialisation proposed by Shi et al. at the start of training. |
True
|
population_bn
|
bool
|
Whether to recalibrate BatchNorm on training data after each epoch, before model selection and saving. |
False
|
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
|
device
|
str or device
|
Device on which to run the model and tensor computations. |
'cuda'
|
Source code in CTRAIN/model_wrappers/adversarial/pgd_model_wrapper.py
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 | |
evaluate(*args, **kwargs)
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/adversarial/pgd_model_wrapper.py
150 151 152 153 154 155 156 | |
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/adversarial/pgd_model_wrapper.py
158 159 160 161 162 163 | |
forward(data)
Evaluate the native network on the supplied input batch.
Source code in CTRAIN/model_wrappers/adversarial/pgd_model_wrapper.py
75 76 77 | |
load_state_dict(state_dict, strict=True)
Load native network parameters and buffers, checking keys when strict=True.
Source code in CTRAIN/model_wrappers/adversarial/pgd_model_wrapper.py
83 84 85 | |
resume_from_checkpoint(path, train_loader, end_epoch=None, val_loader=None)
Restore native model, SGD and scheduler state, then continue PGD training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Checkpoint containing the completed epoch and triangular scheduler state. |
required |
train_loader
|
DataLoader
|
Training loader using the original batch protocol. |
required |
end_epoch
|
int
|
Exclusive ending epoch; None completes the schedule. |
None
|
val_loader
|
DataLoader
|
Validation loader used for model selection. |
None
|
Returns:
| Type | Description |
|---|---|
Module
|
Trained native classifier. |
RNG and data-loader state are not restored by these checkpoints.
Source code in CTRAIN/model_wrappers/adversarial/pgd_model_wrapper.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
state_dict(*args, **kwargs)
Return the native network parameters and persistent buffers for inference.
Source code in CTRAIN/model_wrappers/adversarial/pgd_model_wrapper.py
79 80 81 | |
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/adversarial/pgd_model_wrapper.py
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 | |