-
Notifications
You must be signed in to change notification settings - Fork 32
/
hyper_random.py
109 lines (77 loc) · 2.63 KB
/
hyper_random.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Fake hyper optimization using random sampling.
"""
import math
import functools
from .hyper import register_hyper_optlib
from ..utils import get_rng
def sample_bool(rng):
return rng.choice([False, True])
def sample_int(rng, low, high):
return rng.randint(low, high)
def sample_option(rng, options):
return rng.choice(options)
def sample_uniform(rng, low, high):
return rng.uniform(low, high)
def sample_loguniform(rng, low, high):
return 2 ** rng.uniform(math.log2(low), math.log2(high))
class RandomSpace:
def __init__(self, space, seed=None):
self.rng = get_rng(seed)
self._samplers = {}
for k, param in space.items():
if param["type"] == "BOOL":
self._samplers[k] = sample_bool
elif param["type"] == "INT":
self._samplers[k] = functools.partial(
sample_int, low=param["min"], high=param["max"]
)
elif param["type"] == "STRING":
self._samplers[k] = functools.partial(
sample_option, options=param["options"]
)
elif param["type"] == "FLOAT":
self._samplers[k] = functools.partial(
sample_uniform, low=param["min"], high=param["max"]
)
elif param["type"] == "FLOAT_EXP":
self._samplers[k] = functools.partial(
sample_loguniform, low=param["min"], high=param["max"]
)
else:
raise ValueError("Didn't understand space {}.".format(param))
def sample(self):
return {k: fn(self.rng) for k, fn in self._samplers.items()}
class RandomSampler:
def __init__(self, methods, spaces, seed=None):
self.rng = get_rng(seed)
self._rmethods = tuple(methods)
self._rspaces = {m: RandomSpace(spaces[m], self.rng) for m in methods}
def ask(self):
method = self.rng.choice(self._rmethods)
rspace = self._rspaces[method]
params = rspace.sample()
return method, params
def random_init_optimizers(
self,
methods,
space,
seed=None,
):
"""Initialize a completely random sampling optimizer.
Parameters
----------
space : dict[str, dict[str, dict]]
The search space.
"""
self.sampler = RandomSampler(methods, space, seed=seed)
def random_get_setting(self):
method, params = self.sampler.ask()
return {"method": method, "params": params}
def random_report_result(*_, **__):
pass
register_hyper_optlib(
"random",
random_init_optimizers,
random_get_setting,
random_report_result,
)