-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameters.py
More file actions
74 lines (60 loc) · 2.27 KB
/
Copy pathparameters.py
File metadata and controls
74 lines (60 loc) · 2.27 KB
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
import numpy as np
import tensorflow as tf
import types
class ParamsDict(dict):
def __init__(self, *args, **kwargs):
super(ParamsDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def define_flags(self):
for k, v in self.items():
param = v.get() if isinstance(v, HParam) else v
if isinstance(param, bool):
tf.app.flags.DEFINE_bool(k, None, k)
elif isinstance(param, float):
tf.app.flags.DEFINE_float(k, None, k)
elif isinstance(param, int):
tf.app.flags.DEFINE_integer(k, None, k)
elif isinstance(param, str):
tf.app.flags.DEFINE_string(k, None, k)
else:
print('Could not create a flag for: %s with type: %s' % (k, type(v)))
def initialized(self):
params_values = dict()
for k, v in self.items():
if isinstance(v, HParam):
params_values[k] = v.get()
elif isinstance(v, types.FunctionType):
params_values[k] = v()
else:
params_values[k] = v
return params_values
def describe(self):
for k, v in self.items():
print(k, '=', v)
class HParam(object):
def __init__(self):
super(HParam, self).__init__()
def get(self):
raise NotImplementedError()
class HParamSelect(HParam):
def __init__(self, array):
super(HParamSelect, self).__init__()
self.array = array
def get(self):
return self.array[np.random.randint(0, len(self.array))]
class HParamRange(HParam):
def __init__(self, lowerbound, upperbound, size=1, method='uniform', fn=None):
super(HParamRange, self).__init__()
if lowerbound >= upperbound:
raise ValueError('Lowerbound >= Upperbound')
self.method = method
self.lowerbound = lowerbound
self.upperbound = upperbound
self.size = None if size == 1 else size
self.fn = (lambda x: x) if fn is None else fn
def get(self):
if self.method == 'uniform':
value = np.random.uniform(self.lowerbound, self.upperbound, self.size)
return value
else:
raise ValueError('Unknown HParamRange method: %s' % self.method)