Skip to content

Commit 6acd07c

Browse files
0.23.5
1 parent 120ed78 commit 6acd07c

8 files changed

Lines changed: 785 additions & 196 deletions

notebooks/00_spotPython_tests.ipynb

Lines changed: 249 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7630,33 +7630,33 @@
76307630
"import os\n",
76317631
"import pickle\n",
76327632
"import numpy as np\n",
7633-
"from spotpython.spot import Spot\n",
7634-
"from spotpython.fun.objectivefunctions import Analytical\n",
7635-
"from spotpython.utils.init import fun_control_init, design_control_init\n",
7636-
"from spotpython.utils.file import load_result\n",
7633+
"from spotpythonspot import Spot\n",
7634+
"from spotpythonfunobjectivefunctions import Analytical\n",
7635+
"from spotpythonutilsinit import fun_control_init, design_control_init\n",
7636+
"from spotpythonutilsfile import load_result\n",
76377637
"import pprint\n",
76387638
"\n",
76397639
"def _compare_dicts(dict1, dict2, ignore_keys=None):\n",
76407640
" \"\"\"\n",
7641-
" Compare two dictionaries, including element-wise comparison for numpy arrays.\n",
7642-
" Print missing elements (keys) if the dictionaries do not match.\n",
7641+
" Compare two dictionaries, including element-wise comparison for numpy arrays\n",
7642+
" Print missing elements (keys) if the dictionaries do not match\n",
76437643
"\n",
76447644
" Args:\n",
7645-
" dict1 (dict): First dictionary to compare.\n",
7646-
" dict2 (dict): Second dictionary to compare.\n",
7647-
" ignore_keys (list, optional): List of keys to ignore during comparison. Default is None.\n",
7645+
" dict1 (dict): First dictionary to compare\n",
7646+
" dict2 (dict): Second dictionary to compare\n",
7647+
" ignore_keys (list, optional): List of keys to ignore during comparison Default is None\n",
76487648
"\n",
76497649
" Returns:\n",
7650-
" bool: True if the dictionaries match, False otherwise.\n",
7650+
" bool: True if the dictionaries match, False otherwise\n",
76517651
" \"\"\"\n",
76527652
" if ignore_keys is None:\n",
76537653
" ignore_keys = []\n",
76547654
" # ensure that ignore_keys is a list\n",
76557655
" if not isinstance(ignore_keys, list):\n",
76567656
" ignore_keys = [ignore_keys]\n",
76577657
"\n",
7658-
" keys1 = set(dict1.keys()) - set(ignore_keys)\n",
7659-
" keys2 = set(dict2.keys()) - set(ignore_keys)\n",
7658+
" keys1 = set(dict1keys()) - set(ignore_keys)\n",
7659+
" keys2 = set(dict2keys()) - set(ignore_keys)\n",
76607660
"\n",
76617661
" if keys1 != keys2:\n",
76627662
" missing_in_dict1 = keys2 - keys1\n",
@@ -7666,8 +7666,8 @@
76667666
" return False\n",
76677667
"\n",
76687668
" for key in keys1:\n",
7669-
" if isinstance(dict1[key], np.ndarray) and isinstance(dict2[key], np.ndarray):\n",
7670-
" if not np.array_equal(dict1[key], dict2[key]):\n",
7669+
" if isinstance(dict1[key], npndarray) and isinstance(dict2[key], npndarray):\n",
7670+
" if not nparray_equal(dict1[key], dict2[key]):\n",
76717671
" print(f\"Mismatch in key '{key}': {dict1[key]} != {dict2[key]}\")\n",
76727672
" return False\n",
76737673
" else:\n",
@@ -7682,22 +7682,22 @@
76827682
" # Initialize function control\n",
76837683
" fun_control = fun_control_init(\n",
76847684
" PREFIX=PREFIX,\n",
7685-
" lower=np.array([-1, -1]),\n",
7686-
" upper=np.array([1, 1]),\n",
7685+
" lower=nparray([-1, -1]),\n",
7686+
" upper=nparray([1, 1]),\n",
76877687
" verbosity=1\n",
76887688
" )\n",
76897689
" \n",
76907690
" design_control = design_control_init(init_size=7)\n",
76917691
"\n",
7692-
" fun = Analytical().fun_sphere\n",
7692+
" fun = Analytical()fun_sphere\n",
76937693
" \n",
76947694
" S = Spot(\n",
76957695
" fun=fun,\n",
76967696
" fun_control=fun_control,\n",
76977697
" design_control=design_control,\n",
76987698
" )\n",
76997699
" \n",
7700-
" X_start = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])\n",
7700+
" X_start = nparray([[0, 0], [0, 1], [1, 0], [1, 1]])\n",
77017701
" S.run(X_start=X_start)\n",
77027702
"\n",
77037703
" # Load the experiment\n",
@@ -7954,25 +7954,249 @@
79547954
},
79557955
{
79567956
"cell_type": "code",
7957-
"execution_count": 6,
7957+
"execution_count": null,
7958+
"metadata": {},
7959+
"outputs": [],
7960+
"source": [
7961+
"from spotpython.hyperparameters.values import convert_keys\n",
7962+
"d = {'a': 1, 'b': 2.1, 'c': 3}\n",
7963+
"var_type = [\"int\", \"num\", \"int\"]\n",
7964+
"convert_keys(d, var_type)\n"
7965+
]
7966+
},
7967+
{
7968+
"cell_type": "markdown",
7969+
"metadata": {},
7970+
"source": [
7971+
"## get_dict_with_levels_and_types()"
7972+
]
7973+
},
7974+
{
7975+
"cell_type": "code",
7976+
"execution_count": null,
7977+
"metadata": {},
7978+
"outputs": [],
7979+
"source": [
7980+
"from spotpython.hyperdict.light_hyper_dict import LightHyperDict\n",
7981+
"from spotpython.hyperparameters.values import get_default_hyperparameters_as_array\n",
7982+
"from spotpython.hyperparameters.values import assign_values, get_var_name, iterate_dict_values, convert_keys, get_dict_with_levels_and_types, get_core_model_from_name, add_core_model_to_fun_control\n",
7983+
"import pprint\n",
7984+
"core_model_name=\"light.regression.NNLinearRegressor\"\n",
7985+
"hyperdict=LightHyperDict\n",
7986+
"fun_control = {}\n",
7987+
"coremodel, core_model_instance = get_core_model_from_name(core_model_name)\n",
7988+
"add_core_model_to_fun_control(\n",
7989+
" core_model=core_model_instance,\n",
7990+
" fun_control=fun_control,\n",
7991+
" hyper_dict=hyperdict,\n",
7992+
" filename=None,\n",
7993+
")\n",
7994+
"X = get_default_hyperparameters_as_array(fun_control)\n",
7995+
"var_dict = assign_values(X, get_var_name(fun_control))\n",
7996+
"for values in iterate_dict_values(var_dict):\n",
7997+
" values = convert_keys(values, fun_control[\"var_type\"])\n",
7998+
" pprint.pprint(values)\n",
7999+
" # pprint.pprint(fun_control)\n",
8000+
" values = get_dict_with_levels_and_types(fun_control=fun_control, v=values)\n",
8001+
" pprint.pprint(values)"
8002+
]
8003+
},
8004+
{
8005+
"cell_type": "markdown",
8006+
"metadata": {},
8007+
"source": [
8008+
"## generate_one_config_from_var_dict()"
8009+
]
8010+
},
8011+
{
8012+
"cell_type": "code",
8013+
"execution_count": null,
8014+
"metadata": {},
8015+
"outputs": [],
8016+
"source": [
8017+
"import numpy as np\n",
8018+
"from spotpython.hyperdict.light_hyper_dict import LightHyperDict\n",
8019+
"from spotpython.hyperparameters.values import get_core_model_from_name, add_core_model_to_fun_control, generate_one_config_from_var_dict\n",
8020+
"import pprint\n",
8021+
"core_model_name=\"light.regression.NNLinearRegressor\"\n",
8022+
"hyperdict=LightHyperDict\n",
8023+
"fun_control = {}\n",
8024+
"_ , core_model_instance = get_core_model_from_name(core_model_name)\n",
8025+
"add_core_model_to_fun_control(\n",
8026+
" core_model=core_model_instance,\n",
8027+
" fun_control=fun_control,\n",
8028+
" hyper_dict=hyperdict,\n",
8029+
" filename=None,\n",
8030+
")\n",
8031+
"var_dict = {'l1': np.array([3.]),\n",
8032+
" 'epochs': np.array([4.]),\n",
8033+
" 'batch_size': np.array([4.]),\n",
8034+
" 'act_fn': np.array([2.]),\n",
8035+
" 'optimizer': np.array([11.]),\n",
8036+
" 'dropout_prob': np.array([0.01]),\n",
8037+
" 'lr_mult': np.array([1.]),\n",
8038+
" 'patience': np.array([2.]),\n",
8039+
" 'batch_norm': np.array([0.]),\n",
8040+
" 'initialization': np.array([0.])}\n",
8041+
"g = generate_one_config_from_var_dict(var_dict=var_dict, fun_control=fun_control)\n",
8042+
"# Since g is an iterator, we need to call next to get the values\n",
8043+
"values = next(g)\n",
8044+
"pprint.pprint(values)"
8045+
]
8046+
},
8047+
{
8048+
"cell_type": "markdown",
8049+
"metadata": {},
8050+
"source": [
8051+
"## return_conf_list_from_var_dict()"
8052+
]
8053+
},
8054+
{
8055+
"cell_type": "code",
8056+
"execution_count": null,
8057+
"metadata": {},
8058+
"outputs": [],
8059+
"source": [
8060+
"import numpy as np\n",
8061+
"from spotpython.hyperdict.light_hyper_dict import LightHyperDict\n",
8062+
"from spotpython.hyperparameters.values import get_core_model_from_name, add_core_model_to_fun_control, return_conf_list_from_var_dict\n",
8063+
"import pprint\n",
8064+
"core_model_name=\"light.regression.NNLinearRegressor\"\n",
8065+
"hyperdict=LightHyperDict\n",
8066+
"fun_control = {}\n",
8067+
"_ , core_model_instance = get_core_model_from_name(core_model_name)\n",
8068+
"add_core_model_to_fun_control(\n",
8069+
" core_model=core_model_instance,\n",
8070+
" fun_control=fun_control,\n",
8071+
" hyper_dict=hyperdict,\n",
8072+
" filename=None,\n",
8073+
")\n",
8074+
"var_dict = {'l1': np.array([3., 4.]),\n",
8075+
" 'epochs': np.array([4., 3.]),\n",
8076+
" 'batch_size': np.array([4., 4.]),\n",
8077+
" 'act_fn': np.array([2., 1.]),\n",
8078+
" 'optimizer': np.array([11., 10.]),\n",
8079+
" 'dropout_prob': np.array([0.01, 0.]),\n",
8080+
" 'lr_mult': np.array([1., 1.1]),\n",
8081+
" 'patience': np.array([2., 3.]),\n",
8082+
" 'batch_norm': np.array([0., 1.]),\n",
8083+
" 'initialization': np.array([0., 1.])}\n",
8084+
"return_conf_list_from_var_dict(var_dict=var_dict, fun_control=fun_control)\n"
8085+
]
8086+
},
8087+
{
8088+
"cell_type": "markdown",
8089+
"metadata": {},
8090+
"source": [
8091+
"## get_one_config_from_X()"
8092+
]
8093+
},
8094+
{
8095+
"cell_type": "code",
8096+
"execution_count": null,
8097+
"metadata": {},
8098+
"outputs": [],
8099+
"source": [
8100+
"import numpy as np\n",
8101+
"from spotpython.hyperdict.light_hyper_dict import LightHyperDict\n",
8102+
"from spotpython.hyperparameters.values import get_core_model_from_name, add_core_model_to_fun_control, get_one_config_from_X\n",
8103+
"core_model_name=\"light.regression.NNLinearRegressor\"\n",
8104+
"hyperdict=LightHyperDict\n",
8105+
"fun_control = {}\n",
8106+
"coremodel, core_model_instance = get_core_model_from_name(core_model_name)\n",
8107+
"add_core_model_to_fun_control(\n",
8108+
" core_model=core_model_instance,\n",
8109+
" fun_control=fun_control,\n",
8110+
" hyper_dict=hyperdict,\n",
8111+
" filename=None,\n",
8112+
")\n",
8113+
"X = np.array([[3.0e+00, 4.0e+00, 4.0e+00, 2.0e+00, 1.1e+01, 1.0e-02, 1.0e+00, 2.0e+00, 0.0e+00,\n",
8114+
" 0.0e+00]])\n",
8115+
"print(X)\n",
8116+
"get_one_config_from_X(X, fun_control)"
8117+
]
8118+
},
8119+
{
8120+
"cell_type": "markdown",
8121+
"metadata": {},
8122+
"source": [
8123+
"## get_tuned_architecture()"
8124+
]
8125+
},
8126+
{
8127+
"cell_type": "code",
8128+
"execution_count": 1,
79588129
"metadata": {},
79598130
"outputs": [
8131+
{
8132+
"name": "stderr",
8133+
"output_type": "stream",
8134+
"text": [
8135+
"Seed set to 123\n",
8136+
"Seed set to 123\n"
8137+
]
8138+
},
8139+
{
8140+
"name": "stdout",
8141+
"output_type": "stream",
8142+
"text": [
8143+
"module_name: light\n",
8144+
"submodule_name: regression\n",
8145+
"model_name: NNLinearRegressor\n"
8146+
]
8147+
},
79608148
{
79618149
"data": {
79628150
"text/plain": [
7963-
"{'a': 1, 'b': 2.1, 'c': 3}"
8151+
"{'l1': 16,\n",
8152+
" 'epochs': 4,\n",
8153+
" 'batch_size': 2,\n",
8154+
" 'act_fn': LeakyReLU(),\n",
8155+
" 'optimizer': 'SGD',\n",
8156+
" 'dropout_prob': 0.03480567,\n",
8157+
" 'lr_mult': 7.98104224,\n",
8158+
" 'patience': 2,\n",
8159+
" 'batch_norm': True,\n",
8160+
" 'initialization': 'Default'}"
79648161
]
79658162
},
7966-
"execution_count": 6,
8163+
"execution_count": 1,
79678164
"metadata": {},
79688165
"output_type": "execute_result"
79698166
}
79708167
],
79718168
"source": [
7972-
"from spotpython.hyperparameters.values import convert_keys\n",
7973-
"d = {'a': 1, 'b': 2.1, 'c': 3}\n",
7974-
"var_type = [\"int\", \"num\", \"int\"]\n",
7975-
"convert_keys(d, var_type)\n"
8169+
"from spotpython.data.diabetes import Diabetes\n",
8170+
"from spotpython.hyperdict.light_hyper_dict import LightHyperDict\n",
8171+
"from spotpython.fun.hyperlight import HyperLight\n",
8172+
"from spotpython.utils.init import fun_control_init, design_control_init\n",
8173+
"from spotpython.spot import Spot\n",
8174+
"import numpy as np\n",
8175+
"from spotpython.hyperparameters.values import set_hyperparameter, get_one_config_from_X\n",
8176+
"\n",
8177+
"\n",
8178+
"fun_control = fun_control_init(\n",
8179+
" force_run=False,\n",
8180+
" PREFIX=\"get_one_config_from_X\",\n",
8181+
" save_experiment=True,\n",
8182+
" fun_evals=10,\n",
8183+
" max_time=1,\n",
8184+
" data_set = Diabetes(),\n",
8185+
" core_model_name=\"light.regression.NNLinearRegressor\",\n",
8186+
" hyperdict=LightHyperDict,\n",
8187+
" _L_in=10,\n",
8188+
" _L_out=1)\n",
8189+
"\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",
8193+
"\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)"
79768200
]
79778201
},
79788202
{

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

0 commit comments

Comments
 (0)