generated from nabenabe0928/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_metalearn_tpe.py
36 lines (27 loc) · 1.02 KB
/
example_metalearn_tpe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from __future__ import annotations
import time
import ConfigSpace as CS
import ConfigSpace.hyperparameters as CSH
import numpy as np
from optimizers import TPEOptimizer
def sphere(eval_config: dict[str, float], shift: float = 0) -> tuple[dict[str, float], float]:
start = time.time()
vals = np.array(list(eval_config.values()))
return {"loss": np.sum((vals - shift) ** 2)}, time.time() - start
if __name__ == "__main__":
dim = 10
cs = CS.ConfigurationSpace()
for d in range(dim):
cs.add_hyperparameter(CSH.UniformFloatHyperparameter(f"x{d}", lower=-5, upper=5))
meta_learner = TPEOptimizer(
obj_func=lambda eval_config: sphere(eval_config, shift=1),
config_space=cs,
n_init=50,
max_evals=50,
)
meta_learner.optimize()
metadata = {"shift=1": meta_learner.fetch_observations()}
opt = TPEOptimizer(
obj_func=sphere, config_space=cs, min_bandwidth_factor=1e-2, metadata=metadata, resultfile="sphere"
)
opt.optimize(logger_name="sphere")