def test_local_scheduler(): param_space = dict(x=range(-10, 10), y=range(-10, 10)) @scheduler.serial def obj(x, y): return x - y results = Tuner(param_space, obj).maximize() assert abs(results['best_params']['x'] - 10) <= 3 assert abs(results['best_params']['y'] + 10) <= 3 @scheduler.parallel(n_jobs=-1) def obj(x, y): return x - y results = Tuner(param_space, obj).maximize() assert abs(results['best_params']['x'] - 10) <= 3 assert abs(results['best_params']['y'] + 10) <= 3 @scheduler.parallel(n_jobs=2) def obj(x, y): return x - y results = Tuner(param_space, obj).maximize() assert abs(results['best_params']['x'] - 10) <= 3 assert abs(results['best_params']['y'] + 10) <= 3
def test_six_hump(): def camel(x, y): x2 = math.pow(x, 2) x4 = math.pow(x, 4) y2 = math.pow(y, 2) return (4.0 - 2.1 * x2 + (x4 / 3.0)) * x2 + x * y + (-4.0 + 4.0 * y2) * y2 param_dict = { 'x': uniform(-3, 3), 'y': uniform(-2, 2), } x_opt = 0.0898 # or -0;0898 y_opt = -0.7126 # or 0.7126 def objfunc(args_list): results = [] for hyper_par in args_list: x = hyper_par['x'] y = hyper_par['y'] result = -camel(x, y) results.append(result) return results tuner = Tuner(param_dict, objfunc) results = tuner.run() print('best hyper parameters:', results['best_params']) print('best objective:', results['best_objective']) assert abs(results['best_params']['x']) - abs(x_opt) <= 0.1 assert abs(results['best_params']['y']) - abs(y_opt) <= 0.2
def test_config(): param_dict = { 'x': range(-10, 10), 'y': range(-10, 10), } x_opt = 0 y_opt = 0 def objfunc(args_list): results = [] for hyper_par in args_list: x = hyper_par['x'] y = hyper_par['y'] result = (x**2 + y**2) / 1e4 results.append(result) return results def check(results, error_msg): assert abs(results['best_params']['x'] - x_opt) <= 3, error_msg assert abs(results['best_params']['y'] - y_opt) <= 3, error_msg tuner = Tuner(param_dict, objfunc, conf_dict=dict(optimizer='Random')) results = tuner.minimize() check(results, 'error while minimizing random')
def test_convex(): param_dict = { 'x': range(-100, 10), 'y': range(-10, 20), } x_opt = 0 y_opt = 0 def objfunc(args_list): results = [] for hyper_par in args_list: x = hyper_par['x'] y = hyper_par['y'] result = (x**2 + y**2) / 1e4 results.append(result) return results tuner = Tuner(param_dict, objfunc) results = tuner.minimize() print('best hyper parameters:', results['best_params']) print('best Accuracy:', results['best_objective']) assert abs(results['best_params']['x'] - x_opt) <= 3 assert abs(results['best_params']['y'] - y_opt) <= 3
def test_rosenbrock(): param_dict = { 'x': range(-10, 10), 'y': range(-10, 10), } a = 1 b = 100 x_opt = a y_opt = a**2 def objfunc(args_list): results = [] for hyper_par in args_list: x = hyper_par['x'] y = hyper_par['y'] result = -(b * ((y - x**2)**2) + ((a - x)**2)) results.append(result) return results tuner = Tuner(param_dict, objfunc, conf_dict=dict(domain_size=100000)) results = tuner.run() print('best hyper parameters:', results['best_params']) print('best Accuracy:', results['best_objective']) assert abs(results['best_params']['x'] - x_opt) <= 2 assert abs(results['best_params']['x'] - y_opt) <= 2
def main(): tuner = Tuner(param_space, objective, {'num_iteration': 30}) results = tuner.maximize() print('best parameters:', results['best_params']) print('best accuracy:', results['best_objective']) assert results['best_objective'] > 0.93
def main(): tuner = Tuner(param_space, objective) # Initialize Tuner results = tuner.minimize() # Run Tuner best_params = results["best_params"] best_objective = results["best_objective"] print(f'Optimal value of parameters: {best_params} and objective: {best_objective}') assert abs(best_params['x'] - 0) < 1 assert abs(best_objective - 0) < 1
def main(): tuner = Tuner(param_space, objective) results = tuner.minimize() print('best parameters:', results['best_params']) print('best accuracy:', results['best_objective']) assert abs(results['best_objective'] - 0) < 1
def test_custom_scheduler(): # search space param_space = dict(x=range(-10, 10)) @scheduler.custom(n_jobs=2) def objective(params): assert len(params) == 2 return [p['x'] * p['x'] for p in params] tuner = Tuner(param_space, objective, dict(initial_random=2)) results = tuner.minimize() assert abs(results['best_params']['x'] - 0) <= 0.1
def test_early_stopping_complex(): '''testing early stop by time since last improvement''' param_dict = dict(x=range(-10, 10)) def objfunc(p_list): time.sleep(2) res = [np.random.uniform()] * len(p_list) return res class Context: previous_best = None previous_best_time = None min_improvement_secs = 1 objective_variation = 1 def early_stop(results): context = Context current_best = results['best_objective'] current_time = time.time() _stop = False if context.previous_best is None: context.previous_best = current_best context.previous_best_time = current_time elif (current_best <= context.previous_best + context.objective_variation) and \ (current_time - context.previous_best_time > context.min_improvement_secs): print("no improvement in %d seconds: stopping early." % context.min_improvement_secs) _stop = True else: context.previous_best = current_best context.previous_best_time = current_time return _stop config = dict(num_iteration=20, initial_random=1, early_stopping=early_stop) tuner = Tuner(param_dict, objfunc, conf_dict=config) results = tuner.minimize() assert (len(results['params_tried']) == 3)
def main(): if (sys.argv[1] != "nextpnr-ice40"): print("ERROR: only nexpnr-ice40 is supported") sys.exit(1) space = { 'alpha': [x * .025 for x in range(1, 21)], # 0.025->0.5 (.25 step) 'beta': [x * .025 for x in range(20, 41)], # 0.500->1.0 (.25 step) 'critexp': range(1, 11, 1), 'tweight': range(1, 35, 5), } cfg = dict(num_iteration=20) tuner = Tuner(space, evalpnr, cfg) results = tuner.maximize() print("Best clock: %s Mhz" % results["best_objective"]) print("Best parameters: [%s]" % results["best_params"])
def test_early_stopping_simple(): param_dict = dict(x=range(-10, 10)) def objfunc(p_list): return [p['x']**2 for p in p_list] def early_stop(results): if len(results['params_tried']) >= 5: return True config = dict(num_iteration=20, initial_random=1, early_stopping=early_stop) tuner = Tuner(param_dict, objfunc, conf_dict=config) results = tuner.minimize() assert (len(results['params_tried']) == 5) config = dict(num_iteration=20, initial_random=1, early_stopping=early_stop, optimizer='Random') tuner = Tuner(param_dict, objfunc, conf_dict=config) results = tuner.minimize() assert (len(results['params_tried']) == 5)
def test_celery_scheduler(): try: import celery except ModuleNotFoundError: return # search space param_space = dict(x=range(-10, 10)) class MockTask: def __init__(self, x): self.x = x def objective(self): return self.x * self.x def get(self, timeout=None): return self.objective() @scheduler.celery(n_jobs=2) def objective(x): return MockTask(x) tuner = Tuner(param_space, objective) assert tuner.config.batch_size == 2 results = tuner.minimize() assert abs(results['best_params']['x']) <= 0.1 class MockTask: def __init__(self, x): self.x = x def objective(self): return (self.x - 5) * (self.x - 5) def get(self, timeout=None): if self.x < -8: raise celery.exceptions.TimeoutError("timeout") return self.objective() @scheduler.celery(n_jobs=1) def objective(x): return MockTask(x) tuner = Tuner(param_space, objective) results = tuner.minimize() assert abs(results['best_params']['x'] - 5) <= 0.1
def test_tuner(): tuner_user = Tuner(param_dict, objectiveFunction) results = tuner_user.run() # max objective is 8, and minimum is 1 assert results['best_objective'] > 1
# x_test = std_scaler.transform(x_test) del x_train, x_dev # freeing up space # tuner = svm_fits.train_mango_skcv(X=x_combined, Y=y_combined, n_splits=10) param_space = { # 'kernel': ['rbf', 'linear'], # 'gamma': uniform(0.1, 4), # 0.1 to 4.1 'C': [1e-6, 1e-5, 1e-4, 1e-3, 0.01, 0.1] # loguniform(-6, 6) # 10^-7 to 10 } # @scheduler.serial def objectiveSVM(args_list): results = [] for hyper_par in args_list: svc = svm.LinearSVC(**hyper_par, max_iter=100000, class_weight='balanced') # svc = thunder(**hyper_par, max_iter=100000, # class_weight='balanced') result = cross_val_score(svc, x_combined, y_combined, scoring=my_scorer, n_jobs=-1, cv=10).mean() results.append(result) return results tuner = Tuner(param_dict=param_space, objective=objectiveSVM) results = tuner.maximize() print('best hyper parameters:', results['best_params']) print('best objective:', results['best_objective'])