MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / SchedulerInitializer

Class SchedulerInitializer

numpy_ml/neural_nets/initializers/initializers.py:106–173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104
105
106class SchedulerInitializer(object):
107 def __init__(self, param=None, lr=None):
108 """
109 A class for initializing learning rate schedulers. Valid `param` values
110 are:
111 (a) __str__ representations of `SchedulerBase` instances
112 (b) `SchedulerBase` instances
113 (c) Parameter dicts (e.g., as produced via the `summary` method in
114 `LayerBase` instances)
115
116 If `param` is `None`, return the ConstantScheduler with learning rate
117 equal to `lr`.
118 """
119 if all([lr is None, param is None]):
120 raise ValueError("lr and param cannot both be `None`")
121
122 self.lr = lr
123 self.param = param
124
125 def __call__(self):
126 """Initialize scheduler"""
127 param = self.param
128 if param is None:
129 scheduler = ConstantScheduler(self.lr)
130 elif isinstance(param, SchedulerBase):
131 scheduler = param
132 elif isinstance(param, str):
133 scheduler = self.init_from_str()
134 elif isinstance(param, dict):
135 scheduler = self.init_from_dict()
136 return scheduler
137
138 def init_from_str(self):
139 """Initialize scheduler from the param string"""
140 r = r"([a-zA-Z]*)=([^,)]*)"
141 sch_str = self.param.lower()
142 kwargs = {i: _eval(j) for i, j in re.findall(r, sch_str)}
143
144 if "constant" in sch_str:
145 scheduler = ConstantScheduler(**kwargs)
146 elif "exponential" in sch_str:
147 scheduler = ExponentialScheduler(**kwargs)
148 elif "noam" in sch_str:
149 scheduler = NoamScheduler(**kwargs)
150 elif "king" in sch_str:
151 scheduler = KingScheduler(**kwargs)
152 else:
153 raise NotImplementedError("{}".format(sch_str))
154 return scheduler
155
156 def init_from_dict(self):
157 """Initialize scheduler from the param dictionary"""
158 S = self.param
159 sc = S["hyperparameters"] if "hyperparameters" in S else None
160
161 if sc is None:
162 raise ValueError("Must have `hyperparameters` key: {}".format(S))
163

Callers 2

__init__Method · 0.85
set_paramsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected