Skip to content

Commit

Permalink
add hp
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisChen1992 committed Feb 1, 2019
1 parent de47c31 commit 66b9214
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
54 changes: 54 additions & 0 deletions tool/HyperParameterSpace.py
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])
2 changes: 1 addition & 1 deletion tool/create_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import json

from src.hyper_parameter.HyperParameterSpace import HyperParameterSpace
from HyperParameterSpace import HyperParameterSpace

parser = argparse.ArgumentParser(description='Create configurations')
parser.add_argument('--src', help='Source config file')
Expand Down

0 comments on commit 66b9214

Please sign in to comment.