@@ -88,7 +88,7 @@ def check_X_shape(self, X: np.ndarray, fun_control: dict) -> np.ndarray:
8888
8989 def fun (self , X : np .ndarray , fun_control : dict = None ) -> np .ndarray :
9090 """
91- Evaluates the function for the given input array X of shape (n,k)
91+ Evaluates the function for the given input array X of shape (n, k)
9292 and control parameters specified as a dict.
9393 Calls the train_model function from spotpython.light.trainmodel
9494 to train the model and evaluate the results.
@@ -102,57 +102,22 @@ def fun(self, X: np.ndarray, fun_control: dict = None) -> np.ndarray:
102102
103103 Returns:
104104 (np.ndarray):
105- (n,) array containing the `n` evaluation results.
106-
107- Examples:
108- >>> from math import inf
109- import numpy as np
110- from spotpython.data.diabetes import Diabetes
111- from spotpython.hyperdict.light_hyper_dict import LightHyperDict
112- from spotpython.fun.mohyperlight import MoHyperLight
113- from spotpython.utils.init import fun_control_init
114- from spotpython.utils.eda import print_exp_table
115- from spotpython.spot import spot
116- from spotpython.hyperparameters.values import get_default_hyperparameters_as_array
117- PREFIX="000"
118- data_set = Diabetes()
119- fun_control = fun_control_init(
120- PREFIX=PREFIX,
121- save_experiment=True,
122- fun_evals=inf,
123- max_time=1,
124- data_set = data_set,
125- core_model_name="light.regression.NNLinearRegressor",
126- hyperdict=LightHyperDict,
127- _L_in=10,
128- _L_out=1,
129- TENSORBOARD_CLEAN=True,
130- tensorboard_log=True,
131- seed=42,)
132- print_exp_table(fun_control)
133- X = get_default_hyperparameters_as_array(fun_control)
134- # set epochs to 2^8:
135- X[0, 1] = 8
136- # set patience to 2^10:
137- X[0, 7] = 10
138- print(f"X: {X}")
139- # combine X and X to a np.array with shape (2, n_hyperparams)
140- # so that two values are returned
141- X = np.vstack((X, X))
142- hyper_light = MoHyperLight(seed=125, log_level=50)
143- hyper_light.fun(X, fun_control)
105+ (2, n) array where the first row contains the evaluation results (z_res)
106+ and the second row contains the extracted "epochs" values (epochs_res).
144107 """
145108 z_res = np .array ([], dtype = float )
109+ epochs_res = np .array ([], dtype = float ) # Array to store "epochs" values
110+
146111 self .check_X_shape (X = X , fun_control = fun_control )
147112 var_dict = assign_values (X , get_var_name (fun_control ))
148- # type information and transformations are considered in generate_one_config_from_var_dict:
113+
114+ # Type information and transformations are considered in generate_one_config_from_var_dict:
149115 for config in generate_one_config_from_var_dict (var_dict , fun_control ):
150116 if fun_control ["show_config" ]:
151117 print ("\n In fun(): config:" )
152118 pprint .pprint (config )
153119 logger .debug (f"\n config: { config } " )
154- # extract parameters like epochs, batch_size, lr, etc. from config
155- # config_id = generate_config_id(config)
120+
156121 try :
157122 logger .debug ("fun: Calling train_model" )
158123 df_eval = train_model (config , fun_control )
@@ -167,11 +132,17 @@ def fun(self, X: np.ndarray, fun_control: dict = None) -> np.ndarray:
167132 logger .error (f"Error in fun(). Call to train_model failed. { err = } , { type (err )= } " )
168133 logger .error ("Setting df_eval to np.nan" )
169134 df_eval = np .nan
135+
170136 # Multiply results by the weights. Positive weights mean that the result is to be minimized.
171137 # Negative weights mean that the result is to be maximized, e.g., accuracy.
172138 z_val = fun_control ["weights" ] * df_eval
173- # Append, since several configurations can be evaluated at once.
174- z_res = np .append (z_res , z_val )
175- # Finally, z_res is a 1-dim array
176- # of shape (n,) where n is the number of configurations evaluated.
177- return z_res
139+ z_res = np .append (z_res , z_val ) # Append evaluation result
140+
141+ # Extract "epochs" from the config and append to epochs_res
142+ epochs_val = config .get ("epochs" , np .nan ) # Default to np.nan if "epochs" is not in config
143+ epochs_res = np .append (epochs_res , epochs_val )
144+
145+ # Stack z_res and epochs_res into a (2, n) array
146+ result = np .vstack ((z_res , epochs_res ))
147+
148+ return result
0 commit comments