init
X_reshape(X)
¶
Reshape X to 2D array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
array
|
The input array. |
required |
Returns:
Name | Type | Description |
---|---|---|
X |
array
|
The reshaped input array. |
Examples:
>>> from spotPy.utils.init import X_reshape
>>> X = np.array([1,2,3])
>>> X_reshape(X)
array([[1, 2, 3]])
Source code in spotpython/utils/init.py
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
|
check_and_create_dir(path)
¶
Check if the path exists and create it if it does not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path to the directory. |
required |
Returns:
Type | Description |
---|---|
noneType
|
None |
Examples:
>>> fromspotPy.utils.init import check_and_create_dir
>>> check_and_create_dir("data/")
Source code in spotpython/utils/init.py
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
|
create_spot_tensorboard_path(tensorboard_log, prefix)
¶
Creates the spot_tensorboard_path and returns it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensorboard_log |
bool
|
If True, the path to the folder where the tensorboard files are saved is created. |
required |
prefix |
str
|
The prefix for the experiment name. |
required |
Returns:
Name | Type | Description |
---|---|---|
spot_tensorboard_path |
str
|
The path to the folder where the tensorboard files are saved. |
Source code in spotpython/utils/init.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
|
design_control_init(init_size=10, repeats=1)
¶
Initialize design_control dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
init_size |
int
|
The initial size of the experimental design. |
10
|
repeats |
int
|
The number of repeats of the design. |
1
|
Returns:
Name | Type | Description |
---|---|---|
design_control |
dict
|
A dictionary containing the information about the design of experiments. |
Source code in spotpython/utils/init.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
fun_control_init(_L_in=None, _L_out=None, _L_cond=None, _torchmetric=None, PREFIX='00', TENSORBOARD_CLEAN=False, accelerator='auto', converters=None, core_model=None, core_model_name=None, data=None, data_dir='./data', data_module=None, data_set=None, data_set_name=None, db_dict_name=None, design=None, device=None, devices='auto', enable_progress_bar=False, EXPERIMENT_NAME=None, eval=None, fun_evals=15, fun_repeats=1, horizon=None, hyperdict=None, infill_criterion='y', log_every_n_steps=50, log_level=50, lower=None, max_time=1, max_surrogate_points=30, metric_sklearn=None, metric_sklearn_name=None, noise=False, n_points=1, n_samples=None, num_sanity_val_steps=2, n_total=None, num_workers=0, num_nodes=1, ocba_delta=0, oml_grace_period=None, optimizer=None, precision='32', prep_model=None, prep_model_name=None, progress_file=None, save_experiment=False, scaler=None, scaler_name=None, scenario=None, seed=123, show_config=False, show_models=False, show_progress=True, shuffle=None, sigma=0.0, strategy='auto', surrogate=None, target_column=None, target_type=None, task=None, tensorboard_log=False, tensorboard_start=False, tensorboard_stop=False, test=None, test_seed=1234, test_size=0.4, tkagg=False, train=None, tolerance_x=0, upper=None, var_name=None, var_type=['num'], verbosity=0, weights=1.0, weight_coeff=0.0, weights_entry=None)
¶
Initialize fun_control dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_L_in |
int
|
The number of input features. |
None
|
_L_out |
int
|
The number of output features. |
None
|
_L_cond |
int
|
The number of conditional features. |
None
|
_torchmetric |
str
|
The metric to be used by the Lighting Trainer. For example “mean_squared_error”, see https://lightning.ai/docs/torchmetrics/stable/regression/mean_squared_error.html |
None
|
accelerator |
str
|
The accelerator to be used by the Lighting Trainer. It can be either “auto”, “dp”, “ddp”, “ddp2”, “ddp_spawn”, “ddp_cpu”, “gpu”, “tpu”. Default is “auto”. |
'auto'
|
converters |
dict
|
A dictionary containing the converters. Default is None. |
None
|
core_model |
object
|
The core model object. Default is None. |
None
|
core_model_name |
str
|
The name of the core model. Default is None. |
None
|
data |
object
|
The data object. Default is None. |
None
|
data_dir |
str
|
The directory to save the data. Default is “./data”. |
'./data'
|
data_module |
object
|
The data module object. Default is None. |
None
|
data_set |
object
|
The data set object. Default is None. |
None
|
data_set_name |
str
|
The name of the data set. Default is None. |
None
|
db_dict_name |
str
|
The name of the database dictionary. Default is None. |
None
|
device |
str
|
The device to use for the training. It can be either “cpu”, “mps”, or “cuda”. |
None
|
devices |
str or int
|
The number of devices to use for the training/validation/testing. Default is 1. Can be “auto” or an integer. |
'auto'
|
design |
object
|
The experimental design object. Default is None. |
None
|
enable_progress_bar |
bool
|
Whether to enable the progress bar or not. |
False
|
eval |
str
|
evaluation method used in sklearn taintest.py. Can be “eval_test”, “eval_oon_score”, “train_cv” or None. Default is None. |
None
|
EXPERIMENT_NAME |
str
|
The name of the experiment. Default is None. If None, the experiment name is generated based on the current date and time. |
None
|
fun_evals |
int
|
The number of function evaluations. |
15
|
fun_repeats |
int
|
The number of function repeats during the optimization. this value does not affect the number of the repeats in the initial design (this value can be set in the design_control). Default is 1. |
1
|
horizon |
int
|
The horizon of the time series data. Default is None. |
None
|
hyperdict |
dict
|
A dictionary containing the hyperparameters. Default is None.
For example: |
None
|
infill_criterion |
str
|
Can be |
'y'
|
log_every_n_steps |
int
|
Lightning: How often to log within steps. Default: 50. |
50
|
log_level |
int
|
log level with the following settings:
|
50
|
lower |
array
|
lower bound |
None
|
max_time |
int
|
The maximum time in minutes. |
1
|
max_surrogate_points |
int
|
The maximum number of points in the surrogate model. Default is inf. |
30
|
metric_sklearn |
object
|
The metric object from the scikit-learn library. Default is None. |
None
|
metric_sklearn_name |
str
|
The name of the metric object from the scikit-learn library. Default is None. |
None
|
noise |
bool
|
Whether the objective function is noiy or not. Default is False. Affects the repeat of the function evaluations. |
False
|
n_points |
int
|
The number of infill points to be generated by the surrogate in each iteration. |
1
|
num_sanity_val_steps |
int
|
|
2
|
n_samples |
int
|
The number of samples in the dataset. Default is None. |
None
|
n_total |
int
|
The total number of samples in the dataset. Default is None. |
None
|
num_nodes |
int
|
The number of GPU nodes to use for the training/validation/testing. Default is 1. |
1
|
num_workers |
int
|
The number of workers to use for the data loading. Default is 0. |
0
|
ocba_delta |
int
|
The number of additional, new points (only used if noise==True) generated by the OCBA infill criterion. Default is 0. |
0
|
oml_grace_period |
int
|
The grace period for the OML algorithm. Default is None. |
None
|
optimizer |
object
|
The optimizer object used for the search on surrogate. Default is None. |
None
|
precision |
str
|
The precision of the data. Default is “32”. Can be e.g., “16-mixed” or “16-true”. |
'32'
|
PREFIX |
str
|
The prefix of the experiment name. If the PREFIX is not None, a spotWriter that us an instance of a SummaryWriter(), is created. Default is “00”. |
'00'
|
prep_model |
object
|
The preprocessing model object. Used for river. Default is None. |
None
|
prep_model_name |
str
|
The name of the preprocessing model. Default is None. |
None
|
progress_file |
str
|
The name of the progress file. Default is None. |
None
|
save_experiment |
bool
|
Whether to save the experiment or not. Default is False. |
False
|
scaler |
object
|
The scaler object, e.g., the TorchStandard scaler from spot.utils.scaler.py. Default is None. |
None
|
scaler_name |
str
|
The name of the scaler object. Default is None. |
None
|
scenario |
str
|
The scenario to use. Default is None. Can be “river”, “sklearn”, or “lightning”. |
None
|
seed |
int
|
The seed to use for the random number generator. Default is 123. |
123
|
sigma |
float
|
The standard deviation of the noise of the objective function. |
0.0
|
show_progress |
bool
|
Whether to show the progress or not. Default is |
True
|
show_models |
bool
|
Plot model each generation.
Currently only 1-dim functions are supported. Default is |
False
|
show_config |
bool
|
Whether to show the configuration or not. Default is |
False
|
shuffle |
bool
|
Whether the data were shuffled or not. Default is None. |
None
|
surrogate |
object
|
The surrogate model object. Default is None. |
None
|
strategy |
str
|
The strategy to use. Default is “auto”. |
'auto'
|
target_column |
str
|
The name of the target column. Default is None. |
None
|
target_type |
str
|
The type of the target column. Default is None. |
None
|
task |
str
|
The task to perform. It can be either “classification” or “regression”. Default is None. |
None
|
TENSORBOARD_CLEAN |
bool
|
Whether to clean (delete) the tensorboard folder or not. Default is False. |
False
|
tensorboard_log |
bool
|
Whether to log the tensorboard or not. Starts the SummaryWriter. Default is False. |
False
|
tensorboard_start |
bool
|
Whether to start the tensorboard or not. Default is False. |
False
|
tensorboard_stop |
bool
|
Whether to stop the tensorboard or not. Default is False. |
False
|
test |
object
|
The test data set for spotriver. Default is None. |
None
|
test_seed |
int
|
The seed to use for the test set. Default is 1234. |
1234
|
test_size |
float
|
The size of the test set. Default is 0.4, i.e., 60% of the data is used for training and 40% for testing. |
0.4
|
tkagg |
bool
|
Whether to use matplotlib TkAgg or not. Default is False. |
False
|
tolerance_x |
float
|
tolerance for new x solutions. Minimum distance of new solutions,
generated by |
0
|
train |
object
|
The training data set for spotriver. Default is None. |
None
|
upper |
array
|
upper bound |
None
|
var_name |
list
|
A list containing the name of the variables, e.g., [“x1”, “x2”]. Default is None. |
None
|
var_type |
List[str]
|
list of type information, can be either “int”, “num” or “factor”. Default is [“num”]. |
['num']
|
verbosity |
int
|
The verbosity level. Determines print output to console. Higher values result in more output. Default is 0. |
0
|
weights |
float
|
The weight coefficient of the objective function. Positive values mean minimization. If set to -1, scores that are better when maximized will be minimized, e.g, accuracy. Can be an array, so that different weights can be used for different (multiple) objectives. Default is 1.0. |
1.0
|
weight_coeff |
float
|
Determines how to weight older measures. Default is 1.0. Used in the OML algorithm eval_oml.py. Default is 0.0. |
0.0
|
weights_entry |
str
|
The weights entry used in the GUI. Default is None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
fun_control |
dict
|
A dictionary containing the information about the core model, loss function, metrics, and the hyperparameters. |
Examples:
>>> from spotpython.utils.init import fun_control_init
fun_control = fun_control_init(_L_in=64, _L_out=11, num_workers=0, device=None)
fun_control
{'CHECKPOINT_PATH': 'saved_models/',
'DATASET_PATH': 'data/',
'RESULTS_PATH': 'results/',
'TENSORBOARD_PATH': 'runs/',
'_L_in': 64,
'_L_out': 11,
'_L_cond': None,
'accelerator': "auto",
'core_model': None,
'core_model_name': None,
'data': None,
'data_dir': './data',
'db_dict_name': None,
'device': None,
'devices': "auto",
'enable_progress_bar': False,
'eval': None,
'horizon': 7,
'infill_criterion': 'y',
'k_folds': None,
'loss_function': None,
'lower': None,
'max_surrogate_points': 100,
'metric_river': None,
'metric_sklearn': None,
'metric_sklearn_name': None,
'metric_torch': None,
'metric_params': {},
'model_dict': {},
'noise': False,
'n_points': 1,
'n_samples': None,
'num_workers': 0,
'ocba_delta': 0,
'oml_grace_period': None,
'optimizer': None,
'path': None,
'prep_model': None,
'prep_model_name': None,
'save_model': False,
'scenario': "lightning",
'seed': 1234,
'show_batch_interval': 1000000,
'shuffle': None,
'sigma': 0.0,
'target_column': None,
'target_type': None,
'train': None,
'test': None,
'task': 'classification',
'tensorboard_path': None,
'upper': None,
'weights': 1.0,
'writer': None}
Source code in spotpython/utils/init.py
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 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 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
|
get_experiment_name(prefix='00')
¶
Returns a unique experiment name with a given prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix |
str
|
Prefix for the experiment name. Defaults to “00”. |
'00'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Unique experiment name. |
Examples:
>>> from spotpython.utils.init import get_experiment_name
>>> get_experiment_name(prefix="00")
00_ubuntu_2021-08-31_14-30-00
Source code in spotpython/utils/init.py
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 |
|
get_feature_names(fun_control)
¶
Get the feature names from the fun_control dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun_control |
dict
|
The function control dictionary. Must contain a “data_set” key. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: List of feature names. |
Raises:
Type | Description |
---|---|
ValueError
|
If “data_set” is not in fun_control. |
ValueError
|
If “data_set” is None. |
Examples:
>>> from spotpython.utils.init import get_feature_names
get_feature_names(fun_control)
Source code in spotpython/utils/init.py
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
|
get_spot_tensorboard_path(experiment_name)
¶
Get the path to the spot tensorboard files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
experiment_name |
str
|
The name of the experiment. |
required |
Returns:
Name | Type | Description |
---|---|---|
spot_tensorboard_path |
str
|
The path to the folder where the spot tensorboard files are saved. |
Examples:
>>> from spotpython.utils.init import get_spot_tensorboard_path
>>> get_spot_tensorboard_path("00_ubuntu_2021-08-31_14-30-00")
runs/spot_logs/00_ubuntu_2021-08-31_14-30-00
Source code in spotpython/utils/init.py
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 |
|
get_tensorboard_path(fun_control)
¶
Get the path to the tensorboard files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun_control |
dict
|
The function control dictionary. |
required |
Returns:
Name | Type | Description |
---|---|---|
tensorboard_path |
str
|
The path to the folder where the tensorboard files are saved. |
Examples:
>>> from spotpython.utils.init import get_tensorboard_path
>>> get_tensorboard_path(fun_control)
runs/
Source code in spotpython/utils/init.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 |
|
optimizer_control_init(max_iter=1000, seed=125)
¶
Initialize optimizer_control dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_iter |
int
|
The maximum number of iterations. This will be used for the optimization of the surrogate model. Default is 1000. |
1000
|
seed |
int
|
The seed to use for the random number generator. Default is 125. |
125
|
Notes
- Differential evaluation uses
maxiter = 1000
and sets the number of function evaluations to (maxiter + 1) * popsize * N, which results in 1000 * 15 * k, because the default popsize is 15 and N is the number of parameters. This is already sufficient for many situations. For example, for k=2 these are 30 000 iterations. Therefore we set this value to 1000. - This value will be passed to the surrogate model in the
Spot
class.
Returns:
Name | Type | Description |
---|---|---|
optimizer_control |
dict
|
A dictionary containing the information about the optimizer. |
Source code in spotpython/utils/init.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 |
|
setup_paths(tensorboard_clean)
¶
Setup paths for checkpoints, datasets, results, and tensorboard files. This function also handles cleaning the tensorboard path if specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensorboard_clean |
bool
|
If True, move the existing tensorboard folder to a timestamped backup folder to avoid overwriting old tensorboard files. |
required |
Returns:
Name | Type | Description |
---|---|---|
CHECKPOINT_PATH |
str
|
The path to the folder where the pretrained models are saved. |
DATASET_PATH |
str
|
The path to the folder where the datasets are/should be downloaded. |
RESULTS_PATH |
str
|
The path to the folder where the results (plots, csv, etc.) are saved. |
TENSORBOARD_PATH |
str
|
The path to the folder where the tensorboard files are saved. |
Examples:
>>> from spotpython.utils.init import setup_paths
>>> setup_paths(tensorboard_clean=True)
('runs/saved_models/', 'data/', 'results/', 'runs/')
Source code in spotpython/utils/init.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
|
surrogate_control_init(log_level=50, noise=False, model_optimizer=differential_evolution, model_fun_evals=10000, min_theta=-3.0, max_theta=2.0, n_theta='anisotropic', p_val=2.0, n_p=1, optim_p=False, min_Lambda=1e-09, max_Lambda=1, seed=124, theta_init_zero=True, var_type=None, metric_factorial='canberra')
¶
Initialize surrogate_control dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_optimizer |
object
|
The optimizer object used for the search on surrogate. Default is differential_evolution. |
differential_evolution
|
model_fun_evals |
int
|
The number of function evaluations. This will be used for the optimization of the surrogate model. Default is 1000. |
10000
|
min_theta |
float
|
The minimum value of theta. Note that the base10-logarithm is used. Default is -3. |
-3.0
|
max_theta |
float
|
The maximum value of theta. Note that the base10-logarithm is used. Default is 3. |
2.0
|
noise |
bool
|
Whether the objective function is noisy or not. If Kriging, then a nugget is added. Default is False. Note: Will be set in the Spot class. |
False
|
n_theta |
int
|
The number of theta values. If larger than 1 or set to the string “anisotropic”, then the k theta values are used, where k is the problem dimension. This is handled in spot.py. Default is “anisotropic”. |
'anisotropic'
|
p_val |
float
|
|
2.0
|
n_p |
int
|
The number of p values. Number of p values to be used. Default is 1. |
1
|
optim_p |
bool
|
Whether to optimize p or not. |
False
|
min_Lambda |
float
|
The minimum value of lambda. Default is 1e-9. |
1e-09
|
max_Lambda |
float
|
The maximum value of lambda. Default is 1. |
1
|
seed |
int
|
The seed to use for the random number generator. |
124
|
theta_init_zero |
bool
|
Whether to initialize theta with zero or not. Default is True. |
True
|
var_type |
list
|
A list containing the type of the variables. Default is None. Note: Will be set in the Spot class. |
None
|
metric_factorial |
str
|
The metric to be used for the factorial design. Default is “canberra”. |
'canberra'
|
Returns:
Name | Type | Description |
---|---|---|
surrogate_control |
dict
|
A dictionary containing the information about the surrogate model. |
Note
- The surrogate_control dictionary is used in the Spot class. The following values
are updated in the Spot class if they are None in the surrogate_control dictionary:
noise
: If the surrogate model dictionary is passed to the Spot class, and thenoise
value isNone
, then the noise value is set in the Spot class based on the value ofnoise
in the Spot class fun_control dictionary.var_type
: Thevar_type
value is set in the Spot class based on the value ofvar_type
in the Spot class fun_control dictionary and the dimension of the problem. If the Kriging model is used as a surrogate in the Spot class, the setting from surrogate_control_init() is overwritten.n_theta
: If self.surrogate_control[“n_theta”] > 1, use k theta values, where k is the problem dimension specified in the Spot class. The problem dimension is set in the Spot class based on the length of the lower bounds.
- This value
model_fun_evals
will used for the optimization of the surrogate model, e.g., theta values. Differential evaluation usesmaxiter = 1000
and sets the number of function evaluations to (maxiter + 1) * popsize * N, which results in 1000 * 15 * k, because the default popsize is 15 and N is the number of parameters. This is already sufficient for many situations. For example, for k=2 these are 30 000 iterations. Therefore we set this value to 1000.
Source code in spotpython/utils/init.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
|