Skip to content

Commit f70433f

Browse files
0.23.6
get_tuned_architecture() simplified
1 parent 6acd07c commit f70433f

4 files changed

Lines changed: 54 additions & 52 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8142,7 +8142,18 @@
81428142
"text": [
81438143
"module_name: light\n",
81448144
"submodule_name: regression\n",
8145-
"model_name: NNLinearRegressor\n"
8145+
"model_name: NNLinearRegressor\n",
8146+
"Experiment saved to get_one_config_from_X_exp.pkl\n",
8147+
"Result file get_one_config_from_X_res.pkl exists. Loading the result.\n",
8148+
"Loaded experiment from get_one_config_from_X_res.pkl\n"
8149+
]
8150+
},
8151+
{
8152+
"name": "stderr",
8153+
"output_type": "stream",
8154+
"text": [
8155+
"/Users/bartz/miniforge3/envs/spot312/lib/python3.12/site-packages/torch/storage.py:414: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
8156+
" return torch.load(io.BytesIO(b))\n"
81468157
]
81478158
},
81488159
{
@@ -8153,8 +8164,8 @@
81538164
" 'batch_size': 2,\n",
81548165
" 'act_fn': LeakyReLU(),\n",
81558166
" 'optimizer': 'SGD',\n",
8156-
" 'dropout_prob': 0.03480567,\n",
8157-
" 'lr_mult': 7.98104224,\n",
8167+
" 'dropout_prob': 0.034805674424520705,\n",
8168+
" 'lr_mult': 7.981042243396318,\n",
81588169
" 'patience': 2,\n",
81598170
" 'batch_norm': True,\n",
81608171
" 'initialization': 'Default'}"
@@ -8172,7 +8183,7 @@
81728183
"from spotpython.utils.init import fun_control_init, design_control_init\n",
81738184
"from spotpython.spot import Spot\n",
81748185
"import numpy as np\n",
8175-
"from spotpython.hyperparameters.values import set_hyperparameter, get_one_config_from_X\n",
8186+
"from spotpython.hyperparameters.values import set_hyperparameter, get_tuned_architecture\n",
81768187
"\n",
81778188
"\n",
81788189
"fun_control = fun_control_init(\n",
@@ -8187,16 +8198,14 @@
81878198
" _L_in=10,\n",
81888199
" _L_out=1)\n",
81898200
"\n",
8190-
"# set_hyperparameter(fun_control, \"epochs\", [2,3])\n",
8191-
"# set_hyperparameter(fun_control, \"patience\", [1,2])\n",
8192-
"# design_control = design_control_init(init_size=5)\n",
8201+
"set_hyperparameter(fun_control, \"epochs\", [2,2])\n",
8202+
"set_hyperparameter(fun_control, \"patience\", [1,2])\n",
8203+
"design_control = design_control_init(init_size=5)\n",
81938204
"\n",
8194-
"# fun = HyperLight().fun\n",
8195-
"# S = Spot(fun=fun,fun_control=fun_control, design_control=design_control)\n",
8196-
"# S.run()\n",
8197-
"# X = S.to_all_dim(S.min_X.reshape(1, -1))\n",
8198-
"X = np.array([[ 4., 2., 1., 3., 11., 0.03480567, 7.98104224, 1., 1., 0.]])\n",
8199-
"get_one_config_from_X(X, fun_control)"
8205+
"fun = HyperLight().fun\n",
8206+
"S = Spot(fun=fun,fun_control=fun_control, design_control=design_control)\n",
8207+
"S.run()\n",
8208+
"get_tuned_architecture(S)"
82008209
]
82018210
},
82028211
{

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.23.5"
10+
version = "0.23.6"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/hyperparameters/values.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,9 @@ def get_tuned_architecture(spot_tuner, force_minX=False) -> dict:
464464
from spotpython.utils.init import fun_control_init, design_control_init
465465
from spotpython.spot import Spot
466466
import numpy as np
467-
from spotpython.hyperparameters.values import set_hyperparameter, get_one_config_from_X
467+
from spotpython.hyperparameters.values import set_hyperparameter, get_tuned_architecture
468+
469+
468470
fun_control = fun_control_init(
469471
force_run=False,
470472
PREFIX="get_one_config_from_X",
@@ -476,24 +478,22 @@ def get_tuned_architecture(spot_tuner, force_minX=False) -> dict:
476478
hyperdict=LightHyperDict,
477479
_L_in=10,
478480
_L_out=1)
479-
# Uncomment the following lines to perform the hyperparameter optimization:
480-
# set_hyperparameter(fun_control, "epochs", [2,3])
481-
# set_hyperparameter(fun_control, "patience", [1,2])
482-
# design_control = design_control_init(init_size=5)
483-
# fun = HyperLight().fun
484-
# S = Spot(fun=fun,fun_control=fun_control, design_control=design_control)
485-
# S.run()
486-
# X = S.to_all_dim(S.min_X.reshape(1, -1))
487-
# Otherwise, only take the result:
488-
X = np.array([[ 4., 2., 1., 3., 11., 0.03480567, 7.98104224, 1., 1., 0.]])
489-
get_one_config_from_X(X, fun_control)
490-
{'l1': 16,
481+
482+
set_hyperparameter(fun_control, "epochs", [2,3])
483+
set_hyperparameter(fun_control, "patience", [1,2])
484+
design_control = design_control_init(init_size=5)
485+
486+
fun = HyperLight().fun
487+
S = Spot(fun=fun,fun_control=fun_control, design_control=design_control)
488+
S.run()
489+
get_tuned_architecture(S)
490+
{'l1': 16,
491491
'epochs': 4,
492492
'batch_size': 2,
493493
'act_fn': LeakyReLU(),
494494
'optimizer': 'SGD',
495-
'dropout_prob': 0.03480567,
496-
'lr_mult': 7.98104224,
495+
'dropout_prob': 0.034805674424520705,
496+
'lr_mult': 7.981042243396318,
497497
'patience': 2,
498498
'batch_norm': True,
499499
'initialization': 'Default'}
@@ -503,7 +503,7 @@ def get_tuned_architecture(spot_tuner, force_minX=False) -> dict:
503503
else:
504504
# noise or force_minX is False:
505505
X = spot_tuner.to_all_dim(spot_tuner.min_mean_X.reshape(1, -1))
506-
fun_control = copy(spot_tuner.fun_control)
506+
fun_control = copy.copy(spot_tuner.fun_control)
507507
config = get_one_config_from_X(X, fun_control)
508508
return config
509509

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
from spotpython.data.diabetes import Diabetes
22
from spotpython.hyperdict.light_hyper_dict import LightHyperDict
33
from spotpython.fun.hyperlight import HyperLight
4-
from spotpython.utils.init import fun_control_init
4+
from spotpython.utils.init import fun_control_init, design_control_init
55
from spotpython.spot import Spot
66
import numpy as np
7-
from spotpython.hyperparameters.values import get_one_config_from_X
7+
from spotpython.hyperparameters.values import set_hyperparameter, get_tuned_architecture
88

99

1010
def test_get_tuned_architecture():
1111
fun_control = fun_control_init(
12-
force_run=False,
12+
force_run=True,
1313
PREFIX="get_one_config_from_X",
14-
save_experiment=True,
15-
fun_evals=10,
14+
fun_evals=5,
1615
max_time=1,
1716
data_set = Diabetes(),
1817
core_model_name="light.regression.NNLinearRegressor",
1918
hyperdict=LightHyperDict,
2019
_L_in=10,
2120
_L_out=1)
22-
X = np.array([[ 4., 2., 1., 3., 11., 0.03480567, 7.98104224, 1., 1., 0.]])
23-
values = get_one_config_from_X(X, fun_control)
24-
25-
# Replace values['act_fn'] with the class name, i.e. .__class__.__name__ to get the class name
26-
values['act_fn'] = values['act_fn'].__class__.__name__
27-
# This is necessary for this comparison, because the act_fn is a class object and not a string
21+
22+
set_hyperparameter(fun_control, "epochs", [2,2])
23+
set_hyperparameter(fun_control, "patience", [1,1])
24+
design_control = design_control_init(init_size=4)
25+
26+
fun = HyperLight().fun
27+
S = Spot(fun=fun,fun_control=fun_control, design_control=design_control)
28+
S.run()
29+
conf = get_tuned_architecture(S)
2830

29-
expected = {'l1': 16,
30-
'epochs': 4,
31-
'batch_size': 2,
32-
'act_fn': 'LeakyReLU',
33-
'optimizer': 'SGD',
34-
'dropout_prob': 0.03480567,
35-
'lr_mult': 7.98104224,
36-
'patience': 2,
37-
'batch_norm': True,
38-
'initialization': 'Default'}
39-
assert values == expected
31+
# Consider transformation of the values
32+
assert conf['epochs'] == 4

0 commit comments

Comments
 (0)