API Documentation: Utils
Utility functions
EarlyStopping
Early stopping class to stop training the flow model when the validation loss does not improve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patience
|
int
|
Number of epochs to wait before stopping training. Default is 50. |
50
|
delta
|
float
|
Minimum change in the monitored quantity to qualify as an improvement. Default is 1e-6. |
0.0001
|
Methods:
| Name | Description |
|---|---|
__call__ |
Checks if the validation loss has improved. |
Source code in flowevidence/utils.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
__call__(val_loss)
Checks if the validation loss has improved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val_loss
|
float
|
The validation loss to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
stop |
bool
|
True if the validation loss has not improved for the specified number of epochs, False otherwise. |
Source code in flowevidence/utils.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
cornerplot_training(samples, target_distribution=None, epoch=0, plot_dir='./', savename='corner')
Generates a corner plot for the given samples and optionally overlays it with a target distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
ndarray
|
The samples to be plotted. |
required |
target_distribution
|
ndarray
|
The target distribution to overlay on the plot. Defaults to None. |
None
|
epoch
|
int
|
The current epoch number, used for labeling. Defaults to 0. |
0
|
plot_dir
|
str
|
The directory where the plot will be saved. Defaults to './'. |
'./'
|
savename
|
str
|
The name of the saved plot file. Defaults to 'corner'. |
'corner'
|
Source code in flowevidence/utils.py
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 | |
create_data_loaders(train_samples, val_samples, batch_size=256, num_workers=0, pin_memory=True)
Creates data loaders for training and validation datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_samples
|
Tensor
|
The training samples. |
required |
val_samples
|
Tensor
|
The validation samples. |
required |
batch_size
|
int
|
Number of samples per batch to load. Default is 256. |
256
|
num_workers
|
int
|
How many subprocesses to use for data loading. Default is 0. |
0
|
pin_memory
|
bool
|
If True, the data loader will copy Tensors into CUDA pinned memory. Default is True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
train_loader |
DataLoader
|
The training data loader. |
val_loader |
DataLoader
|
The validation data loader. |
Source code in flowevidence/utils.py
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 | |
denormalize_gaussian(samples, mean, std)
Denormalizes the given samples by adding the mean and scaling by the standard deviation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
A tensor containing the samples to be destandardized. |
required |
mean
|
Tensor
|
The mean of the original samples. |
required |
std
|
Tensor
|
The standard deviation of the original samples. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
destandardized_samples |
Tensor
|
The destandardized samples. |
Source code in flowevidence/utils.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
denormalize_minmax(samples, minimum, range)
Denormalizes the given samples by scaling by the range and adding the minimum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
A tensor containing the samples to be denormalized. |
required |
minimum
|
Tensor
|
The minimum value of the original samples. |
required |
range
|
Tensor
|
The range of the original samples. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
denormalized_samples |
Tensor
|
The denormalized samples. |
Source code in flowevidence/utils.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
denormalize_sigmoid(samples, *args, **kwargs)
Denormalizes the given samples using the inverse sigmoid function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
A tensor containing the samples to be denormalized. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
denormalized_samples |
Tensor
|
The denormalized samples. |
Source code in flowevidence/utils.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
l2_regularization(model, lambdaL2)
Add L2 regularization to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The model to which L2 regularization will be added. |
required |
lambdaL2
|
float
|
The regularization strength. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: The L2 regularization |
Source code in flowevidence/utils.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
loss_fn(model, x)
Computes the negative log likelihood of the given samples under the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The model to evaluate. |
required |
x
|
Tensor
|
The samples to evaluate. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: The negative log likelihood of the samples under the model. |
Source code in flowevidence/utils.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
lossplot(epochs, train_losses, val_losses, plot_dir='./', savename='losses')
Plots the training and validation losses over epochs and saves the plot as an image file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epochs
|
list or array - like
|
List or array of epoch numbers. |
required |
train_losses
|
list or array - like
|
List or array of training losses for each epoch. |
required |
val_losses
|
list or array - like
|
List or array of validation losses for each epoch. |
required |
plot_dir
|
str
|
Directory where the plot image will be saved. Default is './'. |
'./'
|
savename
|
str
|
Name of the saved plot image file. Default is 'losses'. |
'losses'
|
Source code in flowevidence/utils.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
normalize_gaussian(samples)
Standardizes the given samples by removing the mean and scaling to unit variance. Add masking operations to deal with NaN values, for example introduced by RJMCMC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
A tensor containing the samples to be standardized. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
normalized_samples |
Tensor
|
The standardized samples. |
mean |
Tensor
|
The mean of the original samples. |
std |
Tensor
|
The standard deviation of the original samples. |
Source code in flowevidence/utils.py
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 | |
normalize_minmax(samples)
Normalizes the given samples by scaling to the range [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
A tensor containing the samples to be normalized. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
normalized_samples |
Tensor
|
The normalized samples. |
minimum |
Tensor
|
The minimum value of the original samples. |
range |
Tensor
|
The range of the original samples. |
Source code in flowevidence/utils.py
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 | |
normalize_sigmoid(samples)
Normalizes the given samples using the sigmoid function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
A tensor containing the samples to be normalized. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
normalized_samples |
Tensor
|
The normalized samples. |
Source code in flowevidence/utils.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
setup_logging(verbose=False)
Configures the logging settings for the application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
If True, sets the logging level to INFO. Otherwise, sets it to WARNING. |
False
|
Source code in flowevidence/utils.py
15 16 17 18 19 20 21 22 23 24 25 26 | |
shuffle(samples)
Shuffles the given tensor of samples along the first dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
A tensor containing the samples to be shuffled. |
required |
Returns:
| Type | Description |
|---|---|
tensor
|
torch.Tensor: A tensor with the samples shuffled along the first dimension. |
Source code in flowevidence/utils.py
173 174 175 176 177 178 179 180 181 182 183 184 185 | |
split(samples, train_ratio=0.8)
Splits the given samples into training and validation sets based on the specified training ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
samples
|
Tensor
|
The input samples to be split. |
required |
train_ratio
|
float
|
The ratio of samples to be used for training. Defaults to 0.8. |
0.8
|
Returns:
| Name | Type | Description |
|---|---|---|
train_samples |
Tensor
|
The training samples. |
val_samples |
Tensor
|
The validation samples. |
Source code in flowevidence/utils.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |