losses
CosineLoss
¶
Bases: Module
Source code in orthogonium\losses.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
__init__()
¶
A class that implements the Cosine Loss for measuring the cosine similarity between predictions and targets. Designed for use in scenarios involving angle-based loss calculations or similarity measurements.
Source code in orthogonium\losses.py
158 159 160 161 162 163 164 165 166 167 168 |
|
LossXent
¶
Bases: Module
Source code in orthogonium\losses.py
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 |
|
__init__(n_classes, offset=2.12132, temperature=0.25)
¶
A custom loss function class for cross-entropy calculation.
This class initializes a cross-entropy loss criterion along with additional parameters, such as an offset and a temperature factor, to allow a finer control over the accuracy/robustness tradeoff during training.
Attributes:
Name | Type | Description |
---|---|---|
criterion |
CrossEntropyLoss
|
The PyTorch cross-entropy loss criterion. |
n_classes |
int
|
The number of classes present in the dataset. |
offset |
float
|
An offset value for customizing the loss computation. |
temperature |
float
|
A temperature factor for scaling logits during loss calculation. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_classes
|
int
|
The number of classes in the dataset. |
required |
offset
|
float
|
The offset value for loss computation. Default is 2.12132. |
2.12132
|
temperature
|
float
|
The temperature scaling factor. Default is 0.25. |
0.25
|
Source code in orthogonium\losses.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
SoftHKRMulticlassLoss
¶
Bases: Module
Source code in orthogonium\losses.py
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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
current_mean = torch.tensor((self.min_margin_v), dtype=torch.float32)
instance-attribute
¶
constraint=lambda x: torch.clamp(x, 0.005, 1000), name="current_mean", )
__init__(alpha=10.0, min_margin=1.0, alpha_mean=0.99, temperature=1.0)
¶
The multiclass version of HKR with softmax. This is done by computing the HKR term over each class and averaging the results.
Note that y_true
could be either one-hot encoded, +/-1 values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
float
|
regularization factor (0 <= alpha <= 1), 0 for KR only, 1 for hinge only |
10.0
|
min_margin
|
float
|
margin to enforce. |
1.0
|
temperature
|
float
|
factor for softmax temperature (higher value increases the weight of the highest non y_true logits) |
1.0
|
alpha_mean
|
float
|
geometric mean factor |
0.99
|
one_hot_ytrue
|
bool
|
set to True when y_true are one hot encoded (0 or 1), and False when y_true already signed bases (for instance +/-1) |
required |
reduction
|
passed to tf.keras.Loss constructor |
required | |
name
|
str
|
passed to tf.keras.Loss constructor |
required |
Source code in orthogonium\losses.py
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 |
|
multiclass_hinge_preproc(signed_y_pred, min_margin)
¶
From multiclass_hinge(y_true, y_pred, min_margin) simplified to use precalculated signed_y_pred
Source code in orthogonium\losses.py
305 306 307 308 309 310 |
|
signed_y_pred(y_true, y_pred)
¶
Return for each item sign(y_true)*y_pred.
Source code in orthogonium\losses.py
299 300 301 302 303 |
|
VRA(output, class_indices, last_layer_type='classwise', L=1.0, eps=36 / 255, return_certs=False)
¶
Compute the verified robust accuracy (VRA) of a model's output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
torch.Tensor The output of the model. |
required | |
class_indices
|
torch.Tensor The indices of the correct classes. Should not be one-hot encoded. |
required | |
last_layer_type
|
str The type of the last layer of the model. Should be either "classwise" (L-lip per class) or "global" (L-lip globally). |
'classwise'
|
|
L
|
float The Lipschitz constant of the model. |
1.0
|
|
eps
|
float The perturbation size. |
36 / 255
|
|
return_certs
|
bool Whether to return the certificates instead of the VRA. |
False
|
Returns:
Name | Type | Description |
---|---|---|
vra |
torch.Tensor The VRA of the model. |
Source code in orthogonium\losses.py
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 |
|
check_last_linear_layer_type(model)
¶
Determines the type of the last linear layer in a given model.
This function inspects the architecture of the model and identifies the last linear layer of specific types (nn.Linear, OrthoLinear, UnitNormLinear). It then returns a string indicating the type of the last linear layer based on its class. This allows to determine the parameter to use for computing the VRA of a model's output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
The model containing layers to be inspected. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
A string indicating the type of the last linear layer. The possible values are: - "global" if the layer is of type OrthoLinear. - "classwise" if the layer is of type UnitNormLinear. - "unknown" if the layer is of any other type or if no linear layer is found. |
Source code in orthogonium\losses.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 |
|