PGD trainer API
The native PGD wrapper calls this trainer. Its underlying loss is documented in the PGD loss API.
Train a native PyTorch classifier using adversarial cross-entropy.
PGD searches run in evaluation mode, and adversarial loss evaluation uses training mode. The supplied triangular learning-rate schedule advances once per optimiser update. Validation selects by adversarial cross-entropy; without validation the trainer returns final weights. LiRPA conversion is unnecessary for this empirical training objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch model to train or evaluate. |
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 |
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 |
lr_scheduler
|
LRScheduler
|
Learning-rate scheduler associated with the supplied optimiser. |
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
|
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
|
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'
|
n_classes
|
int
|
Number of output classes. |
10
|
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
|
Source code in CTRAIN/train/adversarial/pgd.py
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 | |