|
11 | 11 | BetaIntegerHyperparameter, |
12 | 12 | CategoricalHyperparameter, |
13 | 13 | Constant, |
| 14 | + FloatHyperparameter, |
14 | 15 | IntegerHyperparameter, |
15 | 16 | NormalFloatHyperparameter, |
16 | 17 | NormalIntegerHyperparameter, |
@@ -206,12 +207,13 @@ def transform_continuous_designs( |
206 | 207 | configs : list[Configuration] |
207 | 208 | Continuous transformed configs. |
208 | 209 | """ |
209 | | - params = configspace.get_hyperparameters() |
| 210 | + params = list(configspace.values()) |
210 | 211 | for idx, param in enumerate(params): |
211 | | - if isinstance(param, IntegerHyperparameter): |
212 | | - design[:, idx] = param._inverse_transform(param._transform(design[:, idx])) |
213 | | - elif isinstance(param, NumericalHyperparameter): |
214 | | - continue |
| 212 | + if isinstance(param, NumericalHyperparameter): |
| 213 | + if isinstance(param, IntegerHyperparameter): |
| 214 | + design[:, idx] = param.to_vector(param.to_value(design[:, idx])) |
| 215 | + elif isinstance(param, NumericalHyperparameter) and not isinstance(param, IntegerHyperparameter): |
| 216 | + continue |
215 | 217 | elif isinstance(param, Constant): |
216 | 218 | design_ = np.zeros(np.array(design.shape) + np.array((0, 1))) |
217 | 219 | design_[:, :idx] = design[:, :idx] |
@@ -243,73 +245,37 @@ def transform_continuous_designs( |
243 | 245 | return configs |
244 | 246 |
|
245 | 247 |
|
246 | | -# def check_subspace_points( |
247 | | -# X: np.ndarray, |
248 | | -# cont_dims: np.ndarray | list = [], |
249 | | -# cat_dims: np.ndarray | list = [], |
250 | | -# bounds_cont: np.ndarray | None = None, |
251 | | -# bounds_cat: list[tuple] | None = None, |
252 | | -# expand_bound: bool = False, |
253 | | -# ) -> np.ndarray: |
254 | | -# """Check which points are place inside a given subspace. |
255 | | - |
256 | | -# Parameters |
257 | | -# ---------- |
258 | | -# X: Optional[np.ndarray(N,D)], |
259 | | -# points to be checked, where D = D_cont + D_cat |
260 | | -# cont_dims: Union[np.ndarray(D_cont), List] |
261 | | -# which dimensions represent continuous hyperparameters |
262 | | -# cat_dims: Union[np.ndarray(D_cat), List] |
263 | | -# which dimensions represent categorical hyperparameters |
264 | | -# bounds_cont: optional[List[Tuple]] |
265 | | -# subspaces bounds of categorical hyperparameters, its length is the number of continuous hyperparameters |
266 | | -# bounds_cat: Optional[List[Tuple]] |
267 | | -# subspaces bounds of continuous hyperparameters, its length is the number of categorical hyperparameters |
268 | | -# expand_bound: bool |
269 | | -# if the bound needs to be expanded to contain more points rather than the points inside the subregion |
270 | | -# Return |
271 | | -# ---------- |
272 | | -# indices_in_ss:np.ndarray(N) |
273 | | -# indices of data that included in subspaces |
274 | | -# """ |
275 | | -# if len(X.shape) == 1: |
276 | | -# X = X[np.newaxis, :] |
277 | | -# if len(cont_dims) == 0 and len(cat_dims) == 0: |
278 | | -# return np.ones(X.shape[0], dtype=bool) |
279 | | - |
280 | | -# if len(cont_dims) > 0: |
281 | | -# if bounds_cont is None: |
282 | | -# raise ValueError("bounds_cont must be given if cont_dims provided") |
283 | | - |
284 | | -# if len(bounds_cont.shape) != 2 or bounds_cont.shape[1] != 2 or bounds_cont.shape[0] != len(cont_dims): |
285 | | -# raise ValueError( |
286 | | -# f"bounds_cont (with shape {bounds_cont.shape}) should be an array with shape of" |
287 | | -# f"({len(cont_dims)}, 2)" |
288 | | -# ) |
289 | | - |
290 | | -# data_in_ss = np.all(X[:, cont_dims] <= bounds_cont[:, 1], axis=1) & np.all( |
291 | | -# X[:, cont_dims] >= bounds_cont[:, 0], axis=1 |
292 | | -# ) |
293 | | - |
294 | | -# if expand_bound: |
295 | | -# bound_left = bounds_cont[:, 0] - np.min(X[data_in_ss][:, cont_dims] - bounds_cont[:, 0], axis=0) |
296 | | -# bound_right = bounds_cont[:, 1] + np.min(bounds_cont[:, 1] - X[data_in_ss][:, cont_dims], axis=0) |
297 | | -# data_in_ss = np.all(X[:, cont_dims] <= bound_right, axis=1) & np.all(X[:, cont_dims] >= bound_left, |
298 | | -# axis=1) |
299 | | -# else: |
300 | | -# data_in_ss = np.ones(X.shape[0], dtype=bool) |
301 | | - |
302 | | -# if len(cat_dims) == 0: |
303 | | -# return data_in_ss |
304 | | -# if bounds_cat is None: |
305 | | -# raise ValueError("bounds_cat must be given if cat_dims provided") |
306 | | - |
307 | | -# if len(bounds_cat) != len(cat_dims): |
308 | | -# raise ValueError( |
309 | | -# f"bounds_cat ({len(bounds_cat)}) and cat_dims ({len(cat_dims)}) must have " f"the same number of elements" |
310 | | -# ) |
311 | | - |
312 | | -# for bound_cat, cat_dim in zip(bounds_cat, cat_dims): |
313 | | -# data_in_ss &= np.in1d(X[:, cat_dim], bound_cat) |
314 | | - |
315 | | -# return data_in_ss |
| 248 | +def create_uniform_configspace_copy( |
| 249 | + configspace: ConfigurationSpace, |
| 250 | +) -> ConfigurationSpace: |
| 251 | + """Creates a copy of the given configuration space with uniform transformed hyperparameters. |
| 252 | +
|
| 253 | + Parameters |
| 254 | + ---------- |
| 255 | + configspace : ConfigurationSpace |
| 256 | + The configuration space to be transformed. |
| 257 | +
|
| 258 | + Returns |
| 259 | + ------- |
| 260 | + ConfigurationSpace |
| 261 | + A new configuration space with uniform transformed hyperparameters. |
| 262 | + """ |
| 263 | + configspace_name = configspace.name |
| 264 | + configspace_meta = configspace.meta |
| 265 | + random_state = configspace.random.get_state() |
| 266 | + new_configuration_space = ConfigurationSpace(name=configspace_name, meta=configspace_meta) |
| 267 | + new_configuration_space.random.set_state(random_state) |
| 268 | + |
| 269 | + for hyperparameter in configspace.values(): |
| 270 | + if isinstance(hyperparameter, CategoricalHyperparameter): |
| 271 | + new_hyperparameter = hyperparameter.to_uniform() |
| 272 | + elif isinstance(hyperparameter, IntegerHyperparameter): |
| 273 | + new_hyperparameter = hyperparameter.to_uniform() |
| 274 | + elif isinstance(hyperparameter, FloatHyperparameter): |
| 275 | + new_hyperparameter = hyperparameter.to_uniform() |
| 276 | + else: |
| 277 | + new_hyperparameter = hyperparameter |
| 278 | + new_configuration_space.add(new_hyperparameter) |
| 279 | + conditions = configspace.conditions |
| 280 | + new_configuration_space.add(conditions) |
| 281 | + return new_configuration_space |
0 commit comments