SHI IBP
ShiIBPModelWrapper
Bases: CTRAINWrapper
Wrapper class for training models using SHI-IBP method. For details, see Shi et al. (2021) Fast certified robust training with short warmup. https://proceedings.neurips.cc/paper/2021/file/988f9153ac4fd966ea302dd9ab9bae15-Paper.pdf
Source code in CTRAIN/model_wrappers/shi_ibp_model_wrapper.py
10 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 |
|
__init__(model, input_shape, eps, num_epochs, train_eps_factor=1, optimizer_func=torch.optim.Adam, lr=0.0005, warm_up_epochs=1, ramp_up_epochs=70, lr_decay_factor=0.2, lr_decay_milestones=(80, 90), gradient_clip=10, l1_reg_weight=1e-06, shi_reg_weight=0.5, shi_reg_decay=True, start_kappa=1, end_kappa=0, checkpoint_save_path=None, checkpoint_save_interval=10, bound_opts=dict(conv_mode='patches', relu='adaptive'), device=torch.device('cuda'))
Initializes the ShiIBPModelWrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
The model to be trained. |
required |
input_shape
|
tuple
|
Shape of the input data. |
required |
eps
|
float
|
Epsilon value describing the perturbation the network should be certifiably robust against. |
required |
num_epochs
|
int
|
Number of epochs for training. |
required |
train_eps_factor
|
float
|
Factor for training epsilon. |
1
|
optimizer_func
|
Optimizer
|
Optimizer function. |
Adam
|
lr
|
float
|
Learning rate. |
0.0005
|
warm_up_epochs
|
int
|
Number of warm-up epochs, i.e. epochs where the model is trained on clean loss. |
1
|
ramp_up_epochs
|
int
|
Number of ramp-up epochs, i.e. epochs where the epsilon is gradually increased to the target train epsilon. |
70
|
lr_decay_factor
|
float
|
Learning rate decay factor. |
0.2
|
lr_decay_milestones
|
tuple
|
Milestones for learning rate decay. |
(80, 90)
|
gradient_clip
|
float
|
Gradient clipping value. |
10
|
l1_reg_weight
|
float
|
L1 regularization weight. |
1e-06
|
shi_reg_weight
|
float
|
SHI regularization weight. |
0.5
|
shi_reg_decay
|
bool
|
Whether to decay SHI regularization during the ramp up phase. |
True
|
start_kappa
|
float
|
Starting value of kappa that trades-off IBP and clean loss. |
1
|
end_kappa
|
float
|
Ending value of kappa. |
0
|
checkpoint_save_path
|
str
|
Path to save checkpoints. |
None
|
checkpoint_save_interval
|
int
|
Interval for saving checkpoints. |
10
|
bound_opts
|
dict
|
Options for bounding according to the auto_LiRPA documentation. |
dict(conv_mode='patches', relu='adaptive')
|
device
|
device
|
Device to run the training on. |
device('cuda')
|
Source code in CTRAIN/model_wrappers/shi_ibp_model_wrapper.py
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 |
|
_hpo_runner(config, seed, epochs, train_loader, val_loader, output_dir, cert_eval_samples=1000, include_nat_loss=True, include_adv_loss=True, include_cert_loss=True)
Function called during hyperparameter optimization (HPO) using SMAC3, returns the loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
dict
|
Configuration of hyperparameters. |
required |
seed
|
int
|
Seed used. |
required |
epochs
|
int
|
Number of epochs for training. |
required |
train_loader
|
DataLoader
|
DataLoader for training data. |
required |
val_loader
|
DataLoader
|
DataLoader for validation data. |
required |
output_dir
|
str
|
Directory to save output. |
required |
cert_eval_samples
|
int
|
Number of samples for certification evaluation. |
1000
|
include_nat_loss
|
bool
|
Whether to include natural loss into HPO loss. |
True
|
include_adv_loss
|
bool
|
Whether to include adversarial loss into HPO loss. |
True
|
include_cert_loss
|
bool
|
Whether to include certification loss into HPO loss. |
True
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
Loss and dictionary of accuracies that is saved as information to the run by SMAC3. |
Source code in CTRAIN/model_wrappers/shi_ibp_model_wrapper.py
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 |
|
train_model(train_loader, val_loader=None, start_epoch=0, end_epoch=None)
Trains the model using the SHI-IBP method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_loader
|
DataLoader
|
DataLoader for training data. |
required |
val_loader
|
DataLoader
|
DataLoader for validation data. |
None
|
start_epoch
|
int
|
Epoch to start training from. Initialises learning rate and epsilon schedulers accordingly. Defaults to 0. |
0
|
end_epoch
|
int
|
Epoch to prematurely end training at. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
BoundedModule
|
Trained model. |
Source code in CTRAIN/model_wrappers/shi_ibp_model_wrapper.py
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 |
|