|
7630 | 7630 | "import os\n", |
7631 | 7631 | "import pickle\n", |
7632 | 7632 | "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", |
7637 | 7637 | "import pprint\n", |
7638 | 7638 | "\n", |
7639 | 7639 | "def _compare_dicts(dict1, dict2, ignore_keys=None):\n", |
7640 | 7640 | " \"\"\"\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", |
7643 | 7643 | "\n", |
7644 | 7644 | " 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", |
7648 | 7648 | "\n", |
7649 | 7649 | " Returns:\n", |
7650 | | - " bool: True if the dictionaries match, False otherwise.\n", |
| 7650 | + " bool: True if the dictionaries match, False otherwise\n", |
7651 | 7651 | " \"\"\"\n", |
7652 | 7652 | " if ignore_keys is None:\n", |
7653 | 7653 | " ignore_keys = []\n", |
7654 | 7654 | " # ensure that ignore_keys is a list\n", |
7655 | 7655 | " if not isinstance(ignore_keys, list):\n", |
7656 | 7656 | " ignore_keys = [ignore_keys]\n", |
7657 | 7657 | "\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", |
7660 | 7660 | "\n", |
7661 | 7661 | " if keys1 != keys2:\n", |
7662 | 7662 | " missing_in_dict1 = keys2 - keys1\n", |
|
7666 | 7666 | " return False\n", |
7667 | 7667 | "\n", |
7668 | 7668 | " 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", |
7671 | 7671 | " print(f\"Mismatch in key '{key}': {dict1[key]} != {dict2[key]}\")\n", |
7672 | 7672 | " return False\n", |
7673 | 7673 | " else:\n", |
|
7682 | 7682 | " # Initialize function control\n", |
7683 | 7683 | " fun_control = fun_control_init(\n", |
7684 | 7684 | " 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", |
7687 | 7687 | " verbosity=1\n", |
7688 | 7688 | " )\n", |
7689 | 7689 | " \n", |
7690 | 7690 | " design_control = design_control_init(init_size=7)\n", |
7691 | 7691 | "\n", |
7692 | | - " fun = Analytical().fun_sphere\n", |
| 7692 | + " fun = Analytical()fun_sphere\n", |
7693 | 7693 | " \n", |
7694 | 7694 | " S = Spot(\n", |
7695 | 7695 | " fun=fun,\n", |
7696 | 7696 | " fun_control=fun_control,\n", |
7697 | 7697 | " design_control=design_control,\n", |
7698 | 7698 | " )\n", |
7699 | 7699 | " \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", |
7701 | 7701 | " S.run(X_start=X_start)\n", |
7702 | 7702 | "\n", |
7703 | 7703 | " # Load the experiment\n", |
|
7954 | 7954 | }, |
7955 | 7955 | { |
7956 | 7956 | "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, |
7958 | 8129 | "metadata": {}, |
7959 | 8130 | "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 | + }, |
7960 | 8148 | { |
7961 | 8149 | "data": { |
7962 | 8150 | "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'}" |
7964 | 8161 | ] |
7965 | 8162 | }, |
7966 | | - "execution_count": 6, |
| 8163 | + "execution_count": 1, |
7967 | 8164 | "metadata": {}, |
7968 | 8165 | "output_type": "execute_result" |
7969 | 8166 | } |
7970 | 8167 | ], |
7971 | 8168 | "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)" |
7976 | 8200 | ] |
7977 | 8201 | }, |
7978 | 8202 | { |
|
0 commit comments