def get_param_grids(): lrs = [10**i for i in range(-4, -2)] gd_params = {'learning_rate': [0.0001], 'max_iters': [2000]} rhc_params = { 'learning_rate': [0.1], 'restarts': [100], #[10] 'max_iters': [2000] } sa_params = { 'learning_rate': [0.0001, 0.001, 0.01, 0.1], 'schedule': [ ExpDecay(), ExpDecay(exp_const=0.002), ExpDecay(exp_const=1 / 2000), GeomDecay(), GeomDecay(decay=.999), ArithDecay(), ArithDecay(decay=1 / 2000), ], 'max_iters': [2000] } ga_params = { 'learning_rate': [0.0001, 0.001, 0.01, 0.1], 'pop_size': [400], #[200, 400], #[200] 'mutation_prob': [0.1, .2, .5, .9], 'max_iters': [500] } params = { 'gradient_descent': gd_params, 'random_hill_climb': rhc_params, 'simulated_annealing': sa_params, 'genetic_alg': ga_params } return params
def test_exp_below_min(): """Test exponential decay evaluation function for case where result is below the minimum""" schedule = ExpDecay(init_temp=10, exp_const=0.05, min_temp=1) x = schedule.evaluate(50) assert x == 1
def test_exp_above_min(): """Test exponential decay evaluation function for case where result is above the minimum""" schedule = ExpDecay(init_temp=10, exp_const=0.05, min_temp=1) x = schedule.evaluate(5) assert round(x, 5) == 7.78801
def extract_best_hyperparameters(gridsearch_results): """extract_best_hyperparameters Parameters ---------- gridsearch_results : dataframe of gridsearch results Returns the best hyperparameters as a dictionary ------- """ params = [col for col in gridsearch_results.columns if col not in METRICS] best_row = gridsearch_results["Best Fitness"].idxmax() best_params = gridsearch_results[params].iloc[best_row].to_dict() for key, value in best_params.items(): if key == "schedule": # Special case of conversion best_params[key] = ExpDecay(exp_const=value) if value >= 1: best_params[key] = int( value ) # This might not be very safe but it works for these cases return best_params
def get_param_grids(): lrs = [10**i for i in range(-4, -2)] gd_params = {'learning_rate': lrs, 'max_iters': [2000]} rhc_params = { 'learning_rate': lrs, 'restarts': [10, 30], #[10] 'max_iters': [2000] } sa_params = { 'learning_rate': lrs, 'schedule': [GeomDecay(), ArithDecay(), ExpDecay()], 'max_iters': [2000] } ga_params = { 'learning_rate': lrs, 'pop_size': [200], #[200] 'mutation_prob': [0.05, 0.1, 0.2], #[0.1] 'max_iters': [500] } params = { 'gradient_descent': gd_params, 'random_hill_climb': rhc_params, 'simulated_annealing': sa_params, 'genetic_alg': ga_params } return params
def extract_best_hyperparameters(algo, output_dir): if algo == "gs": return best_nn_params filename = os.path.join(output_dir, f"{algo}_nn_gsresults.csv") df = pd.read_csv(filename, index_col=0) params = [col for col in df.columns if col not in METRICS + ["Fold"]] grouped = df.groupby(params) mean_groups = grouped.mean().reset_index() max_ix = mean_groups["Val_F1"].argmax() best_params = mean_groups.loc[max_ix, params].to_dict() for key, value in best_params.items(): if key == "schedule": best_params[key] = ExpDecay(exp_const=value) elif value.is_integer(): best_params[key] = int(value) return best_params
def get_param_grids(): lrs = [10**i for i in range(-4, -2)] gd_params = {'learning_rate': [0.0001], 'max_iters': [40]} rhc_params = { 'learning_rate': [0.1], 'restarts': [100], #[10] 'max_iters': [4000] } sa_params = { 'learning_rate': [0.1], 'schedule': [ #AirithDecay(), ExpDecay(), #ExpDecay(exp_const=0.01), #ExpDecay(exp_const=0.001), #ExpDecay(exp_const=0.002), #ExpDecay(exp_const=0.005), #ExpDecay(exp_const=0.002, init_temp=.9), #ExpDecay(exp_const=0.002, init_temp=.8), #ExpDecay(exp_const=0.002, init_temp=.7), ], 'max_iters': [4000] } ga_params = { 'learning_rate': [0.1], #, 0.01], 'pop_size': [400], #[200, 400], #[200] 'mutation_prob': [0.9], #[0.1] 'clip_max': [100], 'max_iters': [1000] } params = { 'gradient_descent': gd_params, 'random_hill_climb': rhc_params, 'simulated_annealing': sa_params, 'genetic_alg': ga_params } return params
"Time Elapsed", "Best Fitness", "Fitness Evaluations", "Global Optimum", "Found Global Optimum", ] OUTPUT_DIR = "output" MAX_RESTARTS = 10 GRID = { "rhc": { "restarts": [10] }, "sa": { "schedule": [ ExpDecay(exp_const=0.001), ExpDecay(exp_const=0.005), ExpDecay(exp_const=0.01), ExpDecay(exp_const=0.05), ExpDecay(exp_const=0.1), ], }, "ga": { "pop_size": [200], "mutation_prob": [0.05, 0.1, 0.2, 0.4] }, "mimic": { "pop_size": [200], "keep_pct": [0.1, 0.3, 0.5, 0.7, 0.9] }, }
def get_optimizers(): return { 'RHC': { 'function': random_hill_climb, 'kwargs': [{}], }, 'SA': { 'function': simulated_annealing, 'kwargs': [ { 'schedule': ExpDecay() }, { 'schedule': ExpDecay(exp_const=0.01) }, { 'schedule': ExpDecay(exp_const=0.002) }, { 'schedule': GeomDecay() }, { 'schedule': GeomDecay(decay=.98) }, { 'schedule': GeomDecay(decay=.96) }, { 'schedule': ArithDecay() }, { 'schedule': ArithDecay(decay=0.001) }, { 'schedule': ArithDecay(decay=1 / 5000) }, { 'schedule': ExpDecay(exp_const=0.002) }, ] }, 'GA': { 'function': genetic_alg, 'kwargs': [ { 'pop_size': 200, 'mutation_prob': 0.1 }, { 'pop_size': 200, 'mutation_prob': 0.2 }, { 'pop_size': 200, 'mutation_prob': 0.05 }, { 'pop_size': 800, 'mutation_prob': 0.1 }, ] }, 'MIMIC': { 'function': mimic, 'kwargs': [ { 'pop_size': 200, 'keep_pct': 0.2, #'fast_mimic': True }, { 'pop_size': 200, 'keep_pct': 0.4, #'fast_mimic': True }, { 'pop_size': 400, 'keep_pct': 0.2, }, ] } }
def get_optimizers(): return { #'RHC': { # 'function': random_hill_climb, # 'kwargs': [ # {} # ], #}, 'SA': { 'function': simulated_annealing, 'kwargs': [ { 'schedule': ExpDecay() }, #{'schedule': ExpDecay(exp_const=0.01)}, { 'schedule': ExpDecay(exp_const=0.002) }, { 'schedule': ExpDecay(exp_const=1 / 5000) }, { 'schedule': GeomDecay() }, #{'schedule': GeomDecay(decay=.98)}, { 'schedule': GeomDecay(decay=.9993) }, { 'schedule': ArithDecay() }, #{'schedule': ArithDecay(decay=0.001)}, { 'schedule': ArithDecay(decay=1 / 5000) }, ] }, #'GA': { # 'function': genetic_alg, # 'kwargs': [ # #{'pop_size': 200, # # 'mutation_prob': 0.1 # #}, # #{'pop_size': 200, # # 'mutation_prob': 0.2 # #}, # #{'pop_size': 200, # # 'mutation_prob': 0.05 # #}, # #{'pop_size': 400, # # 'mutation_prob': 0.4 # #}, # #{'pop_size': 400, # # 'mutation_prob': 0.2 # #}, # #{'pop_size': 400, # # 'mutation_prob': 0.1 # #}, # {'pop_size': 800, # 'mutation_prob': 0.1 # }, # ] #}, #'MIMIC': { # 'function': mimic, # 'kwargs': [ # {'pop_size': 400, # 'keep_pct': 0.2, # }, # {'pop_size': 400, # 'keep_pct': 0.1, # }, # {'pop_size': 400, # 'keep_pct': 0.05, # }, # #{'pop_size': 800, # # 'keep_pct': 0.2, # #}, # #{'pop_size': 800, # # 'keep_pct': 0.1, # #}, # ] #} }