Skip to content

Commit 638d216

Browse files
committed
Implemented passing strategy to hyperparametertune by CLI argument
1 parent 1433930 commit 638d216

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

kernel_tuner/hyper.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pathlib import Path
55
from random import randint
6+
from argparse import ArgumentParser
67

78
import kernel_tuner
89

@@ -87,28 +88,36 @@ def put_if_not_present(target_dict, key, value):
8788
result_unique[config_id] = r
8889
return list(result_unique.values()), env
8990

90-
if __name__ == "__main__": # TODO remove in production
91-
hyperparams = {
92-
'popsize': [10, 20, 30],
93-
'maxiter': [50, 100, 150],
94-
'w': [0.25, 0.5, 0.75],
95-
'c1': [1.0, 2.0, 3.0],
96-
'c2': [0.5, 1.0, 1.5]
97-
}
98-
result, env = tune_hyper_params('pso', hyperparams)
99-
100-
# hyperparams = {
101-
# 'neighbor': ['Hamming', 'adjacent'],
102-
# 'restart': [True, False],
103-
# 'no_improvement': [1, 10, 25, 33, 50, 66, 75, 100, 200],
104-
# 'random_walk': [0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.99]
105-
# }
106-
# result, env = tune_hyper_params('greedy_ils', hyperparams)
107-
108-
# hyperparams = {
109-
# 'method': ['COBYLA', 'L-BFGS-B', 'SLSQP', 'CG', 'Powell', 'Nelder-Mead', 'BFGS', 'trust-constr'],
110-
# }
111-
# result, env = tune_hyper_params('dual_annealing', hyperparams)
112-
91+
if __name__ == "__main__":
92+
parser = ArgumentParser()
93+
parser.add_argument("strategy_to_tune")
94+
args = parser.parse_args()
95+
strategy_to_tune = args.strategy_to_tune
96+
97+
# select the hyperparameter parameters for the selected optimization algorithm
98+
if strategy_to_tune.lower() == "pso":
99+
hyperparams = {
100+
'popsize': [10, 20, 30],
101+
'maxiter': [50, 100, 150],
102+
'w': [0.25, 0.5, 0.75],
103+
'c1': [1.0, 2.0, 3.0],
104+
'c2': [0.5, 1.0, 1.5]
105+
}
106+
elif strategy_to_tune.lower() == "greedy_ils":
107+
hyperparams = {
108+
'neighbor': ['Hamming', 'adjacent'],
109+
'restart': [True, False],
110+
'no_improvement': [1, 10, 25, 33, 50, 66, 75, 100, 200],
111+
'random_walk': [0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.99]
112+
}
113+
elif strategy_to_tune.lower() == "dual_annealing":
114+
hyperparams = {
115+
'method': ['COBYLA', 'L-BFGS-B', 'SLSQP', 'CG', 'Powell', 'Nelder-Mead', 'BFGS', 'trust-constr'],
116+
}
117+
else:
118+
raise ValueError(f"Invalid argument {strategy_to_tune=}")
119+
120+
# run the hyperparameter tuning
121+
result, env = tune_hyper_params(strategy_to_tune.lower(), hyperparams)
113122
print(result)
114123
print(env['best_config'])

0 commit comments

Comments
 (0)