46  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 print_exp_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_exp_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(fun=fun,fun_control=fun_control, design_control=design_control)
res = spot_tuner.run()
train_model result: {'val_loss': 24159.23828125, 'hp_metric': 24159.23828125}
train_model result: {'val_loss': 23455.20703125, 'hp_metric': 23455.20703125}
train_model result: {'val_loss': 33373.7109375, 'hp_metric': 33373.7109375}
train_model result: {'val_loss': 24069.580078125, 'hp_metric': 24069.580078125}
train_model result: {'val_loss': 22378.9453125, 'hp_metric': 22378.9453125}
train_model result: {'val_loss': 23910.111328125, 'hp_metric': 23910.111328125}
train_model result: {'val_loss': 23796.3828125, 'hp_metric': 23796.3828125}
train_model result: {'val_loss': 5523.42138671875, 'hp_metric': 5523.42138671875}
train_model result: {'val_loss': 22435.35546875, 'hp_metric': 22435.35546875}
train_model result: {'val_loss': 22645.388671875, 'hp_metric': 22645.388671875}
train_model result: {'val_loss': 3290.885498046875, 'hp_metric': 3290.885498046875}
spotpython tuning: 3290.885498046875 [#---------] 5.65% 
train_model result: {'val_loss': 3131.23828125, 'hp_metric': 3131.23828125}
spotpython tuning: 3131.23828125 [#---------] 10.46% 
train_model result: {'val_loss': 4305.9658203125, 'hp_metric': 4305.9658203125}
spotpython tuning: 3131.23828125 [##--------] 16.17% 
train_model result: {'val_loss': 21271.921875, 'hp_metric': 21271.921875}
spotpython tuning: 3131.23828125 [###-------] 25.56% 
train_model result: {'val_loss': 23266.283203125, 'hp_metric': 23266.283203125}
spotpython tuning: 3131.23828125 [###-------] 27.70% 
train_model result: {'val_loss': 3650.49609375, 'hp_metric': 3650.49609375}
spotpython tuning: 3131.23828125 [###-------] 32.06% 
train_model result: {'val_loss': 3656.44091796875, 'hp_metric': 3656.44091796875}
spotpython tuning: 3131.23828125 [####------] 38.86% 
train_model result: {'val_loss': 24079.833984375, 'hp_metric': 24079.833984375}
spotpython tuning: 3131.23828125 [####------] 43.51% 
train_model result: {'val_loss': 23844.15625, 'hp_metric': 23844.15625}
spotpython tuning: 3131.23828125 [#####-----] 46.71% 
train_model result: {'val_loss': 23948.400390625, 'hp_metric': 23948.400390625}
spotpython tuning: 3131.23828125 [######----] 57.08% 
train_model result: {'val_loss': 23921.521484375, 'hp_metric': 23921.521484375}
spotpython tuning: 3131.23828125 [#######---] 65.60% 
train_model result: {'val_loss': 10811.8681640625, 'hp_metric': 10811.8681640625}
spotpython tuning: 3131.23828125 [########--] 75.59% 
train_model result: {'val_loss': 22712.64453125, 'hp_metric': 22712.64453125}
spotpython tuning: 3131.23828125 [########--] 81.18% 
train_model result: {'val_loss': 20829.63671875, 'hp_metric': 20829.63671875}
spotpython tuning: 3131.23828125 [#########-] 88.02% 
train_model result: {'val_loss': 15038.802734375, 'hp_metric': 15038.802734375}
spotpython tuning: 3131.23828125 [##########] 100.00% Done...

Experiment saved to CondNet_01_res.pkl

46.1 Looking at the Results

46.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()

46.1.2 Tuned Hyperparameters and Their Importance

Results can be printed in tabular form.

from spotpython.utils.eda import print_res_table
print_res_table(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 | 5.0                  | transform_power_2_int |         0.00 |         |
| batch_size     | int    | 4         |     4.0 |     5.0 | 4.0                  | transform_power_2_int |         0.00 |         |
| act_fn         | factor | ReLU      |     0.0 |     5.0 | Swish                | None                  |         0.00 |         |
| optimizer      | factor | SGD       |     0.0 |     2.0 | Adadelta             | None                  |         0.00 |         |
| dropout_prob   | float  | 0.01      |     0.0 |   0.025 | 0.021718274234589927 | None                  |         0.00 |         |
| lr_mult        | float  | 1.0       |     0.1 |    20.0 | 15.991195956185367   | None                  |         0.00 |         |
| patience       | int    | 2         |     2.0 |     3.0 | 3.0                  | transform_power_2_int |       100.00 | ***     |
| batch_norm     | factor | 0         |     0.0 |     1.0 | 1                    | None                  |         0.00 |         |
| initialization | factor | Default   |     0.0 |     4.0 | xavier_uniform       | None                  |         0.00 |         |

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.004489039545218923
epochs:  0.004489039545218923
batch_size:  0.004489039545218923
act_fn:  0.004489039545218923
optimizer:  0.004489039545218923
dropout_prob:  0.004489039702010138
lr_mult:  0.004489039545218923
patience:  99.99999999999999
batch_norm:  0.004489039545218923
initialization:  0.004489039545218923

46.1.3 Get the Tuned Architecture

import pprint
from spotpython.hyperparameters.values import get_tuned_architecture
config = get_tuned_architecture(spot_tuner)
pprint.pprint(config)
{'act_fn': Swish(),
 'batch_norm': True,
 'batch_size': 16,
 'dropout_prob': 0.021718274234589927,
 'epochs': 32,
 'initialization': 'xavier_uniform',
 'l1': 8,
 'lr_mult': 15.991195956185367,
 'optimizer': 'Adadelta',
 'patience': 8}