36  Hyperparameter Tuning with spotpython and PyTorch Lightning Using a CondNet Model

from spotpython.data.diabetes import Diabetes
from spotpython.hyperdict.light_hyper_dict import LightHyperDict
from spotpython.fun.hyperlight import HyperLight
from spotpython.utils.init import (fun_control_init, surrogate_control_init, design_control_init)
from spotpython.utils.eda import gen_design_table
from spotpython.spot import spot
from spotpython.utils.file import get_experiment_filename
from math import inf
from spotpython.hyperparameters.values import set_hyperparameter

PREFIX="CondNet_01"

data_set = Diabetes()
input_dim = 10
output_dim = 1
cond_dim = 2

fun_control = fun_control_init(
    PREFIX=PREFIX,
    fun_evals=inf,
    max_time=1,
    data_set = data_set,
    core_model_name="light.regression.NNCondNetRegressor",
    hyperdict=LightHyperDict,
    _L_in=input_dim - cond_dim,
    _L_out=1,
    _L_cond=cond_dim,)

fun = HyperLight().fun


set_hyperparameter(fun_control, "optimizer", [ "Adadelta", "Adam", "Adamax"])
set_hyperparameter(fun_control, "l1", [3,4])
set_hyperparameter(fun_control, "epochs", [3,7])
set_hyperparameter(fun_control, "batch_size", [4,5])
set_hyperparameter(fun_control, "dropout_prob", [0.0, 0.025])
set_hyperparameter(fun_control, "patience", [2,3])
set_hyperparameter(fun_control, "lr_mult", [0.1, 20.0])

design_control = design_control_init(init_size=10)

print(gen_design_table(fun_control))
module_name: light
submodule_name: regression
model_name: NNCondNetRegressor
| name           | type   | default   |   lower |   upper | transform             |
|----------------|--------|-----------|---------|---------|-----------------------|
| l1             | int    | 3         |     3   |   4     | transform_power_2_int |
| epochs         | int    | 4         |     3   |   7     | transform_power_2_int |
| batch_size     | int    | 4         |     4   |   5     | transform_power_2_int |
| act_fn         | factor | ReLU      |     0   |   5     | None                  |
| optimizer      | factor | SGD       |     0   |   2     | None                  |
| dropout_prob   | float  | 0.01      |     0   |   0.025 | None                  |
| lr_mult        | float  | 1.0       |     0.1 |  20     | None                  |
| patience       | int    | 2         |     2   |   3     | transform_power_2_int |
| batch_norm     | factor | 0         |     0   |   1     | None                  |
| initialization | factor | Default   |     0   |   4     | None                  |
spot_tuner = spot.Spot(fun=fun,fun_control=fun_control, design_control=design_control)
res = spot_tuner.run()
train_model result: {'val_loss': 24158.83203125, 'hp_metric': 24158.83203125}
train_model result: {'val_loss': 23447.546875, 'hp_metric': 23447.546875}
train_model result: {'val_loss': 5128.1162109375, 'hp_metric': 5128.1162109375}
train_model result: {'val_loss': 24131.58984375, 'hp_metric': 24131.58984375}
train_model result: {'val_loss': 22622.26953125, 'hp_metric': 22622.26953125}
train_model result: {'val_loss': 23945.794921875, 'hp_metric': 23945.794921875}
train_model result: {'val_loss': 23592.5, 'hp_metric': 23592.5}
train_model result: {'val_loss': 3970.03271484375, 'hp_metric': 3970.03271484375}
train_model result: {'val_loss': 22140.10546875, 'hp_metric': 22140.10546875}
train_model result: {'val_loss': 22977.068359375, 'hp_metric': 22977.068359375}
train_model result: {'val_loss': 9490.4794921875, 'hp_metric': 9490.4794921875}
spotpython tuning: 3970.03271484375 [----------] 3.93% 
train_model result: {'val_loss': 4811.8203125, 'hp_metric': 4811.8203125}
spotpython tuning: 3970.03271484375 [#---------] 7.73% 
train_model result: {'val_loss': 16126.7421875, 'hp_metric': 16126.7421875}
spotpython tuning: 3970.03271484375 [#---------] 11.08% 
train_model result: {'val_loss': 5215.19384765625, 'hp_metric': 5215.19384765625}
spotpython tuning: 3970.03271484375 [##--------] 16.15% 
train_model result: {'val_loss': 23775.759765625, 'hp_metric': 23775.759765625}
spotpython tuning: 3970.03271484375 [#####-----] 46.19% 
train_model result: {'val_loss': 4698.92138671875, 'hp_metric': 4698.92138671875}
spotpython tuning: 3970.03271484375 [#####-----] 52.12% 
train_model result: {'val_loss': 4691.24072265625, 'hp_metric': 4691.24072265625}
spotpython tuning: 3970.03271484375 [######----] 57.69% 
train_model result: {'val_loss': 20540.259765625, 'hp_metric': 20540.259765625}
spotpython tuning: 3970.03271484375 [#########-] 88.69% 
train_model result: {'val_loss': 5512.07861328125, 'hp_metric': 5512.07861328125}
spotpython tuning: 3970.03271484375 [#########-] 94.12% 
train_model result: {'val_loss': 5099.7158203125, 'hp_metric': 5099.7158203125}
spotpython tuning: 3970.03271484375 [##########] 99.47% 
train_model result: {'val_loss': 5745.54296875, 'hp_metric': 5745.54296875}
spotpython tuning: 3970.03271484375 [##########] 100.00% Done...

36.1 Looking at the Results

36.1.1 Tuning Progress

After the hyperparameter tuning run is finished, the progress of the hyperparameter tuning can be visualized with spotpython’s method plot_progress. The black points represent the performace values (score or metric) of hyperparameter configurations from the initial design, whereas the red points represents the hyperparameter configurations found by the surrogate model based optimization.

spot_tuner.plot_progress()

36.1.2 Tuned Hyperparameters and Their Importance

Results can be printed in tabular form.

from spotpython.utils.eda import gen_design_table
print(gen_design_table(fun_control=fun_control, spot=spot_tuner))
| name           | type   | default   |   lower |   upper | tuned                 | transform             |   importance | stars   |
|----------------|--------|-----------|---------|---------|-----------------------|-----------------------|--------------|---------|
| l1             | int    | 3         |     3.0 |     4.0 | 3.0                   | transform_power_2_int |         0.00 |         |
| epochs         | int    | 4         |     3.0 |     7.0 | 7.0                   | transform_power_2_int |         0.46 | .       |
| batch_size     | int    | 4         |     4.0 |     5.0 | 4.0                   | transform_power_2_int |         2.39 | *       |
| act_fn         | factor | ReLU      |     0.0 |     5.0 | Swish                 | None                  |       100.00 | ***     |
| optimizer      | factor | SGD       |     0.0 |     2.0 | Adadelta              | None                  |         0.16 | .       |
| dropout_prob   | float  | 0.01      |     0.0 |   0.025 | 0.0012790404219919403 | None                  |         0.62 | .       |
| lr_mult        | float  | 1.0       |     0.1 |    20.0 | 4.855811791679552     | None                  |        33.39 | *       |
| patience       | int    | 2         |     2.0 |     3.0 | 2.0                   | transform_power_2_int |         0.01 |         |
| batch_norm     | factor | 0         |     0.0 |     1.0 | 1                     | None                  |        83.27 | **      |
| initialization | factor | Default   |     0.0 |     4.0 | kaiming_uniform       | None                  |         0.03 |         |

A histogram can be used to visualize the most important hyperparameters.

spot_tuner.plot_importance(threshold=1.0)

spot_tuner.plot_important_hyperparameter_contour(max_imp=3)
l1:  0.002630582887976033
epochs:  0.4606891518840429
batch_size:  2.394261291219359
act_fn:  100.0
optimizer:  0.16089983428810503
dropout_prob:  0.6164605053903585
lr_mult:  33.39161542714438
patience:  0.008298255583675019
batch_norm:  83.26784727834846
initialization:  0.03237326772226264

36.1.3 Get the Tuned Architecture

import pprint
from spotpython.hyperparameters.values import get_tuned_architecture
config = get_tuned_architecture(spot_tuner, fun_control)
pprint.pprint(config)
{'act_fn': Swish(),
 'batch_norm': True,
 'batch_size': 16,
 'dropout_prob': 0.0012790404219919403,
 'epochs': 128,
 'initialization': 'kaiming_uniform',
 'l1': 8,
 'lr_mult': 4.855811791679552,
 'optimizer': 'Adadelta',
 'patience': 4}