-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de47c31
commit 66b9214
Showing
2 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class HyperParameterSpace: | ||
def __init__(self, hp): | ||
"""Define Hyper-Parameters. | ||
""" | ||
self.hp = hp | ||
self.params = sorted(list(self.hp.keys())) | ||
|
||
def iterateAllCombinations(self): | ||
"""Go through the whole combinations of hyper-parameters. | ||
""" | ||
idx = [0] * len(self.params) | ||
maxIdx = [len(self.hp[param]) for param in self.params] | ||
while True: | ||
yield self.idx2Str(idx), [(param, self.hp[param][i]) for param, i in zip(self.params, idx)] | ||
self.addOneIdx(idx, maxIdx) | ||
if self.isZeroIdx(idx): | ||
break | ||
|
||
def isZeroIdx(self, idx): | ||
"""Whether idx is zero. | ||
""" | ||
for i in range(len(idx)): | ||
if idx[i] > 0: | ||
return False | ||
return True | ||
|
||
def addOneIdx(self, idx, maxIdx): | ||
"""Add idx by one. | ||
""" | ||
assert(len(idx)==len(maxIdx)) | ||
i = len(idx) - 1 | ||
while i >= 0: | ||
idx[i] += 1 | ||
if idx[i] == maxIdx[i]: | ||
idx[i] = 0 | ||
i -= 1 | ||
else: | ||
return | ||
|
||
def idx2Str(self, idx): | ||
"""Convert idx to string. | ||
""" | ||
return '_'.join([str(num) for num in idx]) | ||
|
||
def idx2Val(self, idx): | ||
"""Convert idx to values. | ||
""" | ||
return [self.hp[param][i] for param, i in zip(self.params, idx)] | ||
|
||
def getParamsName(self): | ||
return self.params | ||
|
||
def getParamsType(self): | ||
return list([type(self.hp[param][0]) for param in self.params]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters