Skip to content

Commit e628775

Browse files
0.9.12
add_core_model_to_fun_control: consistent argument order spot.print_result() shows level names, not numbers
1 parent 6e84fe5 commit e628775

7 files changed

Lines changed: 302 additions & 33 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,4 @@ runs/lightning_logs/*
303303
lightning_logs/*
304304
runs/*
305305
src/spotPython/data/pkldataset_intern.py
306+
runs_OLD/*

notebooks/00_spotPython_tests.ipynb

Lines changed: 177 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,151 @@
17231723
},
17241724
{
17251725
"cell_type": "code",
1726-
"execution_count": 2,
1726+
"execution_count": null,
1727+
"metadata": {},
1728+
"outputs": [],
1729+
"source": [
1730+
"from spotPython.utils.init import fun_control_init\n",
1731+
"from spotPython.hyperparameters.values import get_control_key_value\n",
1732+
"from spotPython.light.regression.netlightregression import NetLightRegression\n",
1733+
"from spotPython.hyperdict.light_hyper_dict import LightHyperDict\n",
1734+
"from spotPython.hyperparameters.values import add_core_model_to_fun_control\n",
1735+
"from spotPython.hyperparameters.values import set_control_hyperparameter_value\n",
1736+
"\n",
1737+
"fun_control = fun_control_init()\n",
1738+
"add_core_model_to_fun_control(core_model=NetLightRegression,\n",
1739+
" fun_control=fun_control,\n",
1740+
" hyper_dict=LightHyperDict)\n",
1741+
"set_control_hyperparameter_value(control_dict=fun_control, hyperparameter=\"l1\", value=[1,7])\n",
1742+
"set_control_hyperparameter_value(control_dict=fun_control, hyperparameter=\"initialization\", value=[\"xavier2\", \"kaiming2\"])\n",
1743+
"print(fun_control)\n"
1744+
]
1745+
},
1746+
{
1747+
"cell_type": "code",
1748+
"execution_count": null,
1749+
"metadata": {},
1750+
"outputs": [],
1751+
"source": []
1752+
},
1753+
{
1754+
"cell_type": "markdown",
1755+
"metadata": {},
1756+
"source": [
1757+
"## get names"
1758+
]
1759+
},
1760+
{
1761+
"cell_type": "code",
1762+
"execution_count": null,
1763+
"metadata": {},
1764+
"outputs": [],
1765+
"source": [
1766+
"def get_entry(dictionary, key, i):\n",
1767+
" if key in dictionary:\n",
1768+
" if 'levels' in dictionary[key]:\n",
1769+
" if i < len(dictionary[key]['levels']):\n",
1770+
" return dictionary[key]['levels'][i]\n",
1771+
" return None"
1772+
]
1773+
},
1774+
{
1775+
"cell_type": "code",
1776+
"execution_count": null,
1777+
"metadata": {},
1778+
"outputs": [],
1779+
"source": [
1780+
"import torch\n",
1781+
"from spotPython.data.pkldataset_intern import PKLDataset\n",
1782+
"from spotPython.utils.device import getDevice\n",
1783+
"from spotPython.utils.init import fun_control_init\n",
1784+
"from spotPython.utils.file import get_experiment_name, get_spot_tensorboard_path\n",
1785+
"import numpy as np\n",
1786+
"from spotPython.hyperparameters.values import set_control_key_value\n",
1787+
"from spotPython.data.diabetes import Diabetes\n",
1788+
"from spotPython.light.regression.netlightregression import NetLightRegression\n",
1789+
"from spotPython.hyperdict.light_hyper_dict import LightHyperDict\n",
1790+
"from spotPython.hyperparameters.values import add_core_model_to_fun_control\n",
1791+
"from spotPython.utils.eda import gen_design_table\n",
1792+
"from math import inf\n",
1793+
"\n",
1794+
"MAX_TIME = 60\n",
1795+
"FUN_EVALS = inf\n",
1796+
"INIT_SIZE = 25\n",
1797+
"WORKERS = 0\n",
1798+
"PREFIX=\"031\"\n",
1799+
"DEVICE = getDevice()\n",
1800+
"\n",
1801+
"\n",
1802+
"experiment_name = get_experiment_name(prefix=PREFIX)\n",
1803+
"fun_control = fun_control_init(\n",
1804+
" spot_tensorboard_path=get_spot_tensorboard_path(experiment_name),\n",
1805+
" _L_in=10,\n",
1806+
" _L_out=1,\n",
1807+
" TENSORBOARD_CLEAN=True,\n",
1808+
" device=DEVICE,\n",
1809+
" enable_progress_bar=False,\n",
1810+
" fun_evals=FUN_EVALS,\n",
1811+
" log_level=10,\n",
1812+
" max_time=MAX_TIME,\n",
1813+
" num_workers=WORKERS,\n",
1814+
" show_progress=True,\n",
1815+
" tolerance_x=np.sqrt(np.spacing(1)),\n",
1816+
" )\n",
1817+
"\n",
1818+
"dataset = Diabetes()\n",
1819+
"dataset = PKLDataset(directory=\"/Users/bartz/workspace/spotPython/notebooks/data/spotPython/\", filename=\"data_sensitive.pkl\", target_column='N', feature_type=torch.float32, target_type=torch.float32, rmNA=True, rmMF=True)\n",
1820+
"set_control_key_value(control_dict=fun_control,\n",
1821+
" key=\"data_set\",\n",
1822+
" value=dataset,\n",
1823+
" replace=True)\n",
1824+
"\n",
1825+
"set_control_key_value(control_dict=fun_control,\n",
1826+
" key=\"_L_in\",\n",
1827+
" value=133,\n",
1828+
" replace=True)\n",
1829+
"\n",
1830+
"\n",
1831+
"add_core_model_to_fun_control(core_model=NetLightRegression,\n",
1832+
" fun_control=fun_control,\n",
1833+
" hyper_dict=LightHyperDict)\n",
1834+
"# from spotPython.hyperparameters.values import modify_hyper_parameter_bounds\n",
1835+
"\n",
1836+
"from spotPython.hyperparameters.values import set_control_hyperparameter_value\n",
1837+
"set_control_hyperparameter_value(fun_control, \"l1\", [3,8])\n",
1838+
"set_control_hyperparameter_value(fun_control, \"epochs\", [4,9])\n",
1839+
"set_control_hyperparameter_value(fun_control, \"batch_size\", [1, 4])\n",
1840+
"set_control_hyperparameter_value(fun_control, \"optimizer\", [\"Adam\", \"AdamW\", \"Adamax\", \"NAdam\"])"
1841+
]
1842+
},
1843+
{
1844+
"cell_type": "code",
1845+
"execution_count": null,
1846+
"metadata": {},
1847+
"outputs": [],
1848+
"source": [
1849+
"fun_control"
1850+
]
1851+
},
1852+
{
1853+
"cell_type": "code",
1854+
"execution_count": null,
1855+
"metadata": {},
1856+
"outputs": [],
1857+
"source": [
1858+
"def get_entry(dictionary, key, i):\n",
1859+
" if 'core_model_hyper_dict' in dictionary:\n",
1860+
" if key in dictionary['core_model_hyper_dict']:\n",
1861+
" if 'levels' in dictionary['core_model_hyper_dict'][key]:\n",
1862+
" if i < len(dictionary['core_model_hyper_dict'][key]['levels']):\n",
1863+
" return dictionary['core_model_hyper_dict'][key]['levels'][i]\n",
1864+
" return None\n",
1865+
"print(get_entry(fun_control, \"optimizer\", 0)) "
1866+
]
1867+
},
1868+
{
1869+
"cell_type": "code",
1870+
"execution_count": 3,
17271871
"metadata": {},
17281872
"outputs": [
17291873
{
@@ -1732,30 +1876,47 @@
17321876
"text": [
17331877
"Seed set to 1234\n"
17341878
]
1735-
},
1736-
{
1737-
"name": "stdout",
1738-
"output_type": "stream",
1739-
"text": [
1740-
"{'CHECKPOINT_PATH': 'runs/saved_models/', 'DATASET_PATH': 'data/', 'RESULTS_PATH': 'results/', 'TENSORBOARD_PATH': 'runs/', '_L_in': None, '_L_out': None, 'data': None, 'data_dir': './data', 'data_module': None, 'data_set': None, 'device': None, 'enable_progress_bar': False, 'eval': None, 'fun_evals': 15, 'k_folds': 3, 'log_level': 10, 'loss_function': None, 'max_time': 1, 'metric_river': None, 'metric_sklearn': None, 'metric_torch': None, 'metric_params': {}, 'model_dict': {}, 'n_samples': None, 'num_workers': 0, 'optimizer': None, 'path': None, 'prep_model': None, 'save_model': False, 'seed': 1234, 'show_batch_interval': 1000000, 'show_progress': False, 'shuffle': None, 'sigma': 0.0, 'target_column': None, 'tolerance_x': 0, 'train': None, 'test': None, 'task': 'classification', 'spot_tensorboard_path': None, 'var_name': ['l1', 'epochs', 'batch_size', 'act_fn', 'optimizer', 'dropout_prob', 'lr_mult', 'patience', 'initialization'], 'var_type': ['int', 'int', 'int', 'factor', 'factor', 'float', 'float', 'int', 'factor'], 'weights': 1.0, 'spot_writer': None, 'core_model': <class 'spotPython.light.regression.netlightregression.NetLightRegression'>, 'core_model_hyper_dict': {'l1': {'type': 'int', 'default': 3, 'transform': 'transform_power_2_int', 'lower': 1, 'upper': 7}, 'epochs': {'type': 'int', 'default': 4, 'transform': 'transform_power_2_int', 'lower': 4, 'upper': 9}, 'batch_size': {'type': 'int', 'default': 4, 'transform': 'transform_power_2_int', 'lower': 1, 'upper': 4}, 'act_fn': {'levels': ['Sigmoid', 'Tanh', 'ReLU', 'LeakyReLU', 'ELU', 'Swish'], 'type': 'factor', 'default': 'ReLU', 'transform': 'None', 'class_name': 'spotPython.torch.activation', 'core_model_parameter_type': 'instance()', 'lower': 0, 'upper': 5}, 'optimizer': {'levels': ['Adadelta', 'Adagrad', 'Adam', 'AdamW', 'SparseAdam', 'Adamax', 'ASGD', 'NAdam', 'RAdam', 'RMSprop', 'Rprop', 'SGD'], 'type': 'factor', 'default': 'SGD', 'transform': 'None', 'class_name': 'torch.optim', 'core_model_parameter_type': 'str', 'lower': 0, 'upper': 11}, 'dropout_prob': {'type': 'float', 'default': 0.01, 'transform': 'None', 'lower': 0.0, 'upper': 0.25}, 'lr_mult': {'type': 'float', 'default': 1.0, 'transform': 'None', 'lower': 0.1, 'upper': 10.0}, 'patience': {'type': 'int', 'default': 2, 'transform': 'transform_power_2_int', 'lower': 2, 'upper': 6}, 'initialization': {'levels': ['xavier2', 'kaiming2'], 'type': 'factor', 'default': 'Default', 'transform': 'None', 'core_model_parameter_type': 'str', 'lower': 0, 'upper': 1}}}\n"
1741-
]
17421879
}
17431880
],
17441881
"source": [
1882+
"from spotPython.utils.device import getDevice\n",
17451883
"from spotPython.utils.init import fun_control_init\n",
1746-
"from spotPython.hyperparameters.values import get_control_key_value\n",
1884+
"from spotPython.utils.file import get_experiment_name, get_spot_tensorboard_path\n",
1885+
"import numpy as np\n",
1886+
"from spotPython.data.diabetes import Diabetes\n",
17471887
"from spotPython.light.regression.netlightregression import NetLightRegression\n",
17481888
"from spotPython.hyperdict.light_hyper_dict import LightHyperDict\n",
17491889
"from spotPython.hyperparameters.values import add_core_model_to_fun_control\n",
1890+
"from spotPython.hyperparameters.values import get_ith_hyperparameter_name_from_fun_control\n",
1891+
"from spotPython.hyperparameters.values import set_control_key_value\n",
17501892
"from spotPython.hyperparameters.values import set_control_hyperparameter_value\n",
1751-
"\n",
1752-
"fun_control = fun_control_init()\n",
1893+
"experiment_name = get_experiment_name(prefix=\"000\")\n",
1894+
"fun_control = fun_control_init(\n",
1895+
" spot_tensorboard_path=get_spot_tensorboard_path(experiment_name),\n",
1896+
" _L_in=10,\n",
1897+
" _L_out=1,\n",
1898+
" TENSORBOARD_CLEAN=True,\n",
1899+
" device=getDevice(),\n",
1900+
" enable_progress_bar=False,\n",
1901+
" fun_evals=15,\n",
1902+
" log_level=10,\n",
1903+
" max_time=1,\n",
1904+
" num_workers=0,\n",
1905+
" show_progress=True,\n",
1906+
" tolerance_x=np.sqrt(np.spacing(1)),\n",
1907+
" )\n",
1908+
"dataset = Diabetes()\n",
1909+
"set_control_key_value(control_dict=fun_control,\n",
1910+
" key=\"data_set\",\n",
1911+
" value=dataset,\n",
1912+
" replace=True)\n",
17531913
"add_core_model_to_fun_control(core_model=NetLightRegression,\n",
1754-
" fun_control=fun_control,\n",
1755-
" hyper_dict=LightHyperDict)\n",
1756-
"set_control_hyperparameter_value(control_dict=fun_control, hyperparameter=\"l1\", value=[1,7])\n",
1757-
"set_control_hyperparameter_value(control_dict=fun_control, hyperparameter=\"initialization\", value=[\"xavier2\", \"kaiming2\"])\n",
1758-
"print(fun_control)\n"
1914+
" fun_control=fun_control,\n",
1915+
" hyper_dict=LightHyperDict)\n",
1916+
"\n",
1917+
"set_control_hyperparameter_value(fun_control, \"l1\", [3,8])\n",
1918+
"set_control_hyperparameter_value(fun_control, \"optimizer\", [\"Adam\", \"AdamW\", \"Adamax\", \"NAdam\"])\n",
1919+
"assert get_ith_hyperparameter_name_from_fun_control(fun_control, key=\"optimizer\", i=0) == \"Adam\""
17591920
]
17601921
},
17611922
{

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.9.11"
10+
version = "0.9.12"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/hyperparameters/values.py

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -684,15 +684,15 @@ def get_values_from_dict(dictionary) -> np.array:
684684
return np.array(list(dictionary.values()))
685685

686686

687-
def add_core_model_to_fun_control(core_model, fun_control, hyper_dict=None, filename=None) -> dict:
687+
def add_core_model_to_fun_control(fun_control, core_model, hyper_dict=None, filename=None) -> dict:
688688
"""Add the core model to the function control dictionary. It updates the keys "core_model",
689689
"core_model_hyper_dict", "var_type", "var_name" in the fun_control dictionary.
690690
691691
Args:
692-
core_model (class):
693-
The core model.
694692
fun_control (dict):
695693
The fun_control dictionary.
694+
core_model (class):
695+
The core model.
696696
hyper_dict (dict):
697697
The hyper parameter dictionary. Optional. Default is None. If no hyper_dict is provided,
698698
the function will try to load the hyper_dict from the file specified by filename.
@@ -717,14 +717,14 @@ def add_core_model_to_fun_control(core_model, fun_control, hyper_dict=None, file
717717
>>> from spotPython.light.regression.netlightregression import NetLightRegression
718718
from spotPython.hyperdict.light_hyper_dict import LightHyperDict
719719
from spotPython.hyperparameters.values import add_core_model_to_fun_control
720-
add_core_model_to_fun_control(core_model=NetLightRegression,
721-
fun_control=fun_control,
722-
hyper_dict=LightHyperDict)
720+
add_core_model_to_fun_control(fun_control=fun_control,
721+
core_model=NetLightRegression,
722+
hyper_dict=LightHyperDict)
723723
# or, if a user wants to use a custom hyper_dict:
724724
>>> from spotPython.light.regression.netlightregression import NetLightRegression
725725
from spotPython.hyperparameters.values import add_core_model_to_fun_control
726-
add_core_model_to_fun_control(core_model=NetLightRegression,
727-
fun_control=fun_control,
726+
add_core_model_to_fun_control(fun_control=fun_control,
727+
core_model=NetLightRegression,
728728
filename="./hyperdict/user_hyper_dict.json")
729729
730730
"""
@@ -737,7 +737,9 @@ def add_core_model_to_fun_control(core_model, fun_control, hyper_dict=None, file
737737
fun_control.update({"core_model_hyper_dict": new_hyper_dict[core_model.__name__]})
738738
var_type = get_var_type(fun_control)
739739
var_name = get_var_name(fun_control)
740-
fun_control.update({"var_type": var_type, "var_name": var_name})
740+
lower = get_bound_values(fun_control, "lower", as_list=False)
741+
upper = get_bound_values(fun_control, "upper", as_list=False)
742+
fun_control.update({"var_type": var_type, "var_name": var_name, "lower": lower, "upper": upper})
741743

742744

743745
def get_one_core_model_from_X(X, fun_control=None):
@@ -1098,3 +1100,65 @@ def get_var_type_from_var_name(fun_control, var_name) -> str:
10981100
var_type_list = get_control_key_value(control_dict=fun_control, key="var_type")
10991101
var_name_list = get_control_key_value(control_dict=fun_control, key="var_name")
11001102
return var_type_list[var_name_list.index(var_name)]
1103+
1104+
1105+
def get_ith_hyperparameter_name_from_fun_control(fun_control, key, i):
1106+
"""
1107+
Get the ith hyperparameter name from the fun_control dictionary.
1108+
1109+
Args:
1110+
fun_control (dict): fun_control dictionary
1111+
key (str): key
1112+
i (int): index
1113+
1114+
Returns:
1115+
(str): hyperparameter name
1116+
1117+
Examples:
1118+
>>> from spotPython.utils.device import getDevice
1119+
from spotPython.utils.init import fun_control_init
1120+
from spotPython.utils.file import get_experiment_name, get_spot_tensorboard_path
1121+
import numpy as np
1122+
from spotPython.data.diabetes import Diabetes
1123+
from spotPython.light.regression.netlightregression import NetLightRegression
1124+
from spotPython.hyperdict.light_hyper_dict import LightHyperDict
1125+
from spotPython.hyperparameters.values import add_core_model_to_fun_control
1126+
from spotPython.hyperparameters.values import get_ith_hyperparameter_name_from_fun_control
1127+
from spotPython.hyperparameters.values import set_control_key_value
1128+
from spotPython.hyperparameters.values import set_control_hyperparameter_value
1129+
experiment_name = get_experiment_name(prefix="000")
1130+
fun_control = fun_control_init(
1131+
spot_tensorboard_path=get_spot_tensorboard_path(experiment_name),
1132+
_L_in=10,
1133+
_L_out=1,
1134+
TENSORBOARD_CLEAN=True,
1135+
device=getDevice(),
1136+
enable_progress_bar=False,
1137+
fun_evals=15,
1138+
log_level=10,
1139+
max_time=1,
1140+
num_workers=0,
1141+
show_progress=True,
1142+
tolerance_x=np.sqrt(np.spacing(1)),
1143+
)
1144+
dataset = Diabetes()
1145+
set_control_key_value(control_dict=fun_control,
1146+
key="data_set",
1147+
value=dataset,
1148+
replace=True)
1149+
add_core_model_to_fun_control(core_model=NetLightRegression,
1150+
fun_control=fun_control,
1151+
hyper_dict=LightHyperDict)
1152+
1153+
set_control_hyperparameter_value(fun_control, "l1", [3,8])
1154+
set_control_hyperparameter_value(fun_control, "optimizer", ["Adam", "AdamW", "Adamax", "NAdam"])
1155+
get_ith_hyperparameter_name_from_fun_control(fun_control, key="optimizer", i=0)
1156+
Adam
1157+
1158+
"""
1159+
if "core_model_hyper_dict" in fun_control:
1160+
if key in fun_control["core_model_hyper_dict"]:
1161+
if "levels" in fun_control["core_model_hyper_dict"][key]:
1162+
if i < len(fun_control["core_model_hyper_dict"][key]["levels"]):
1163+
return fun_control["core_model_hyper_dict"][key]["levels"][i]
1164+
return None

0 commit comments

Comments
 (0)