def smac(trainer, params, steps): global experimentName, optStart optStart = dt.now() experimentName = experiment_name + " - SMAC" opt = pysmac.SMAC_optimizer() parms, forbidden = parse_params(params) opt.minimize(trainer, steps, parms, forbidden_clauses=forbidden)
def run_replication(index, x0, x1, x2, N_POINTS, func_data): # Setup and run our replication parameters = { "x0": ("categorical", x0, random.randint(0, 9)), "x1": ("categorical", x1, random.randint(0, 2)), "x2": ("categorical", x2, random.choice(x2)) } if os.path.exists("tmp_%d.dat" % index): os.remove("tmp_%d.dat" % index) opt = pysmac.SMAC_optimizer() f_min_obj.func_data = func_data f_min_obj.index = index xmin, fval = opt.minimize(f_min_obj_2, N_POINTS, parameters) best_y = [ float(s) for s in open("tmp_%d.dat" % index).read().strip().split('\n') ] os.remove("tmp_%d.dat" % index) # Assuming there is a possibility of converging sooner than all datapoints, # we will pad the ending of best_y with the best observed so far if len(best_y) != N_POINTS: best_y = best_y + [max(best_y) for _ in range(N_POINTS - len(best_y))] return best_y
def init_optimization(): opt = pysmac.SMAC_optimizer(working_directory= '/home/fabian/tuning_deletion_logic/',persistent_files=True, debug = False) parameter_definition= parameters_to_optimize print parameter_definition value, parameters = opt.minimize(optimize_parameters # the function to be minimized , 500 # the maximum number of function evaluations , parameter_definition # dict of parmaeter definition , forbidden_clauses=forbidden_clauses # list of forbidden clauses , t_limit_function_s=360 # time limit cor one tuning iteration , num_runs=2 # number of independent tuning runs , num_train_instances=len(roslaunch_commands) # number of datasets used for tuning , deterministic=True) # deterministic results print('The minimum value %f was found for the configurations %s'%(value, parameters)) for param_key in parameters.keys(): try: current = next(param for param in parameter_list if param['name']==param_key) current['current'] = parameters[param_key] except: pass print("Writing best parameter configuration to param file(s) {}".format(parameter_files_to_search)) write_parameters() print("Exited sucessfully!")
def init_optimization(): opt = pysmac.SMAC_optimizer(working_directory= '/home/fabian/tuning_aspket_logic',persistent_files=True, debug = False) parameter_definition= parameters_to_optimize print parameter_definition value, parameters = opt.minimize(optimize_parameters # the function to be minimized , 400 # the maximum number of function evaluations , parameter_definition , forbidden_clauses=forbidden_clauses # the parameter dictionary , t_limit_function_s=360 , num_runs=2 , deterministic=True , num_train_instances=len(roslaunch_commands)) # for the mainstation file one evaluation needs 6min print('The minimum value %f was found for the configurations %s'%(value, parameters)) for param_key in parameters.keys(): try: current = next(param for param in parameter_list if param['name']==param_key) current['current'] = parameters[param_key] except: pass print("Writing best parameter configuration to param file(s) {}".format(parameter_files_to_search)) write_parameters() print("Exited sucessfully!")
def __init__(self, dreamBed): self.dreamBed = dreamBed self.optimizer = pysmac.SMAC_optimizer() self.iterationCounter = 0 self.totalNumIterations = 0 self.optimizer_service = rospy.Service('dream_bed_evaluation', DreamBedEvaluation, self.serviceCallBack)
def init_optimization(): opt = pysmac.SMAC_optimizer(working_directory= '/home/fabian/imm_tmp',persistent_files=True, debug = False) parameter_definition= parameters_to_optimize print parameter_definition value, parameters = opt.minimize(optimize_parameters # the function to be minimized , 400 # the maximum number of function evaluations , parameter_definition , t_limit_function_s=360 , num_runs=2 , deterministic=True , num_train_instances=len(roslaunch_commands)) # for the mainstation file one evaluation needs 6min print('The minimum value %f was found for the configurations %s'%(value, parameters))
def optimize(folder_name): space = {} for i in range(D): space['x%i' % (i)] = ('categorical', [0, 1], np.random.randint(0, 2)) parameters = dict(space) opt = pysmac.SMAC_optimizer(working_directory='%s/smac' % folder_name, debug=False) value, parameters = opt.minimize( objective, # the function to be minimized number_of_evaluations, # the number of function calls allowed parameters, num_procs = 16) # the parameter dictionary print ('Lowest function value found: %f'%value) print ('Parameter setting %s'%parameters)
def init_optimization(): opt = pysmac.SMAC_optimizer(deterministic=True, working_directory='/home/fabian/tmp/', persistent_files=True, debug=False) parameter_definition = parameters_to_optimize print parameter_definition value, parameters = opt.minimize( optimize_parameters # the function to be minimized , 300 # the maximum number of function evaluations , parameter_definition # the parameter dictionary , t_limit_function_s=360, num_runs=3) # for the mainstation file one evaluation needs 6min print('The minimum value %f was found for the configurations %s' % (value, parameters))
def runOptimizer(self): # x, cost, _ = fmin_smac(func=get_runtime, # x0=[1, 1, 1], # bounds=[(1, 3), (1, 3), (1, 10)], # maxfun=10, # rng=3) if not os.path.isdir(os.getcwd() + "/temp"): os.mkdir(os.getcwd() + "/temp") opt = pysmac.SMAC_optimizer(working_directory=os.getcwd() + "/temp") value, parameters = opt.minimize( self.getObjectiveValue, self.budget, self.parameter_space, forbidden_clauses=self.forbidden_confgs) opt.__del__() best_parameters = dict() best_parameters['type'], best_parameters['size'], best_parameters['num'] = \ self.convertToConfig([parameters['x1'],parameters['x2'], parameters['x3']]) trials = pickleRead('trials.pickle') print(value, best_parameters) # return {'value': value, 'params': best_parameters} print(trials) return trials
# convenience function from pysmac.utils.pcs_merge import merge_configuration_spaces # returns a parameter config space, the conditionals, forbiddens and two wrapper functions p,c,f,wrapper_str = merge_configuration_spaces(\ (random_forest, parameters_trees, [], []), (knn, parameters_knn, [],[])) # workaround to make the generated functions pickable (needed for pySMAC internals): # they are generated as strings, and instantiated by executing this string exec(wrapper_str) # create optimizer object opt = pysmac.SMAC_optimizer( debug=0, working_directory='/tmp/pySMAC_test/', persistent_files=True, ) # perform actual optimization value, parameters = opt.minimize( pysmac_merged_pcs_wrapper, #wrapper function generated by merge_configuration_spaces 50, # number of function evaluations p, # parameter defintion conditional_clauses=c, forbidden_clauses=f) print('The highest accuracy found: %f' % (-value)) print('Parameter setting %s' % parameters)
def opt(): global temp, tempname, n_folds, c, X_list, n_iter, n_features, X_l, trainc config = ConfigParser.ConfigParser() config.read('./config.ini') n_folds = config.getint('Global', 'n_folds') max_dim = config.getint('Global', 'n_features') X_list, source, sname, n_samples, n_points, noise_scale, source_dir = initialize() test_size = config.getfloat('Global', 'test_size') # Open the config file cfgname = './hp_opt/hp_%s_%.4f.ini' % (sname, noise_scale) hp = ConfigParser.ConfigParser() hp.read(cfgname) start_time = timeit.default_timer() c = 0 for X in X_list: if X.shape[0] < 10: c += 1 continue print '============ Cluster %d ============' % c # Initialize file to store the reconstruction error and the count temp = [np.inf, 0, 0, [0]*2] #[err, count, iteration, optimal parameters] create_dir('./hp_opt/temp/') tempname = './hp_opt/temp/kpca' with open(tempname, 'wb') as f: pickle.dump(temp, f) # Reduce dimensionality X_l, dim_increase = reduce_dim(X, plot=False) n_allc = X.shape[0] if n_allc < 10: continue # Specify training and test set n_trainc = int(math.floor(n_allc * (1-test_size))) print 'Training sample size: ', n_trainc trainc = range(n_trainc) # Estimate intrinsic dimension and nonlinearity print 'Estimating intrinsic dimension ...' intr_dim = mide(X_l[trainc], verbose=0)[0] print 'Intrinsic dimension: ', intr_dim if intr_dim < max_dim: n_features = intr_dim else: n_features = max_dim # Define parameters parameters=dict(\ gamma=('real', [1e-3, 1], 1.0/X_l.shape[1]), lg_alpha=('real', [-10, 0], -5), ) # Create a SMAC_optimizer object opt = pysmac.SMAC_optimizer() n_iter = 100 value, parameters = opt.minimize(wrapper, # the function to be minimized n_iter, # the number of function calls allowed parameters) # the parameter dictionary # Write optimal parameters to the config file section = 'kpca'+str(c) if not hp.has_section(section): # Create the section if it does not exist. hp.add_section(section) hp.set(section,'kernel','rbf') hp.write(open(cfgname,'w')) hp.read(cfgname) hp.set(section,'gamma',parameters['gamma']) hp.set(section,'alpha',10**float(parameters['lg_alpha'])) hp.write(open(cfgname,'w')) print(('Lowest function value found: %f'%value)) print(('Parameter setting %s'%parameters)) c += 1 end_time = timeit.default_timer() training_time = (end_time - start_time) print 'Training time: %.2f min' % (training_time/60.)
def opt(): global temp, tempname, n_folds, c, X_list, n_iter, n_features, X_l, trainc config = ConfigParser.ConfigParser() config.read('./config.ini') n_folds = config.getint('Global', 'n_folds') max_dim = config.getint('Global', 'n_features') X_list, source, sname, n_samples, n_points, noise_scale, source_dir = initialize( ) test_size = config.getfloat('Global', 'test_size') # Open the config file cfgname = './hp_opt/hp_%s_%.4f.ini' % (sname, noise_scale) hp = ConfigParser.ConfigParser() hp.read(cfgname) start_time = timeit.default_timer() c = 0 for X in X_list: if X.shape[0] < 10: c += 1 continue print '============ Cluster %d ============' % c # Initialize file to store the reconstruction error and the count # Initialize file to store the reconstruction error and the count temp = [np.inf, 0, 0, [0] * 2] #[err, count, iteration, optimal parameters] create_dir('./hp_opt/temp/') tempname = './hp_opt/temp/mlae' with open(tempname, 'wb') as f: pickle.dump(temp, f) # Reduce dimensionality X_l, dim_increase = reduce_dim(X, plot=False) n_allc = X.shape[0] if n_allc < 10: continue # Specify training and test set n_trainc = int(math.floor(n_allc * (1 - test_size))) print 'Training sample size: ', n_trainc trainc = range(n_trainc) # Estimate intrinsic dimension and nonlinearity print 'Estimating intrinsic dimension ...' intr_dim = mide(X_l[trainc], verbose=0)[0] print 'Intrinsic dimension: ', intr_dim if intr_dim < max_dim: n_features = intr_dim else: n_features = max_dim # Define parameters hs_min = n_features + 1 hs_max = int(X_l.shape[1] * 2) parameters=dict( hidden_size_l1=('categorical', range(hs_min, hs_max+1)+[0], 0),\ hidden_size_l2=('categorical', range(hs_min, hs_max+1)+[0], 0),\ hidden_size_l3=('categorical', range(hs_min, hs_max+1)+[0], 0),\ hidden_size_l4=('categorical', range(hs_min, hs_max+1)+[0], 0),\ lg_l = ('real', [-10, 0.0], -10),\ lr = ('real', [1e-4, 2.0], 1.0),\ lg_epsilon = ('real', [-10, -4], -8),\ ) # Define constraints forbidden_confs = ["{hidden_size_l1 < hidden_size_l2}",\ "{hidden_size_l2 < hidden_size_l3}",\ "{hidden_size_l3 < hidden_size_l4}"] # Create a SMAC_optimizer object opt = pysmac.SMAC_optimizer() n_iter = 500 value, parameters = opt.minimize( wrapper, # the function to be minimized n_iter, # the number of function calls allowed parameters, # the parameter dictionary forbidden_clauses=forbidden_confs) # constraints # Write optimal parameters to the config file section = 'ML_AE' + str(c) if not hp.has_section(section): # Create the section if it does not exist. hp.add_section(section) hp.write(open(cfgname, 'w')) hp.read(cfgname) hp.set(section, 'hidden_size_l1', parameters['hidden_size_l1']) hp.set(section, 'hidden_size_l2', parameters['hidden_size_l2']) hp.set(section, 'hidden_size_l3', parameters['hidden_size_l3']) hp.set(section, 'hidden_size_l4', parameters['hidden_size_l4']) hp.set(section, 'learning_rate', parameters['lr']) hp.set(section, 'epsilon', 10**float(parameters['lg_epsilon'])) hp.set(section, 'weight_decay', 10**float(parameters['lg_l'])) hp.write(open(cfgname, 'w')) print(('Lowest function value found: %f' % value)) print(('Parameter setting %s' % parameters)) c += 1 end_time = timeit.default_timer() training_time = (end_time - start_time) print 'Training time: %.2f h' % (training_time / 3600.)
import pysmac.piac from branin_cli import branin as a_function from branin_cli import parameter_definition # path = '/tmp/pySMAC_run' os.makedirs(path, exist_ok=True) num_instances = 16 # random features, features = np.random.randint(9, size=(num_instances, 3)) # optimizer object opt = pysmac.SMAC_optimizer(t_limit_total_s=360, mem_limit_smac_mb=300, working_directory=path + '/pysmac_output', persistent_files=True) # call its minimize method value, parameters = opt.minimize( a_function, # the function to be minimized 10, # the maximum number of function evaluations parameter_definition, num_train_instances=num_instances, train_instance_features=features, mem_limit_function_mb=1200, t_limit_function_s=5, num_runs=1) model = pysmac.piac.run_ISMAC("/tmp/pySMAC_run/pysmac_output", './branin_cli.py',
x61, x62, x63, x64, x65, x66, x71, x72, x73, x74, x75, x76) return stats['total dead people'].values.mean() if __name__ == "__main__": parameters = dict( x21=('categorical', [1, 2, 3], 2), x22=('categorical', [1, 2, 3], 2), x23=('categorical', [1, 2, 3], 1), x24=('categorical', [1, 2, 3], 1), x25=('categorical', [1, 2, 3], 3), x26=('categorical', [1, 2, 3], 1), x31=('categorical', [1, 2, 3], 2), x32=('categorical', [1, 2, 3], 1), x33=('categorical', [1, 2, 3], 1), x34=('categorical', [1, 2, 3], 1), x35=('categorical', [1, 2, 3], 2), x36=('categorical', [1, 2, 3], 3), x41=('categorical', [1, 2, 3], 1), x42=('categorical', [1, 2, 3], 2), x43=('categorical', [1, 2, 3], 2), x44=('categorical', [1, 2, 3], 1), x45=('categorical', [1, 2, 3], 1), x46=('categorical', [1, 2, 3], 1), x51=('categorical', [1, 2, 3], 3), x52=('categorical', [1, 2, 3], 3), x53=('categorical', [1, 2, 3], 2), x54=('categorical', [1, 2, 3], 2), x55=('categorical', [1, 2, 3], 3), x56=('categorical', [1, 2, 3], 2), x61=('categorical', [1, 2, 3], 1), x62=('categorical', [1, 2, 3], 1), x63=('categorical', [1, 2, 3], 2), x64=('categorical', [1, 2, 3], 3), x65=('categorical', [1, 2, 3], 2), x66=('categorical', [1, 2, 3], 1), x71=('categorical', [1, 2, 3], 1), x72=('categorical', [1, 2, 3], 2), x73=('categorical', [1, 2, 3], 2), x74=('categorical', [1, 2, 3], 1), x75=('categorical', [1, 2, 3], 1), x76=('categorical', [1, 2, 3], 1), ) opt = pysmac.SMAC_optimizer() value, parameters = opt.minimize(avg_queue_length, 1000, parameters) print(('Lowest function value found: %f' % value)) print(('Parameter setting %s' % parameters)) save_obj(parameters, 'dead_people')
r)**2 + s * (1 - t) * math.cos(x1) + s + x3 return ret # Now we have to define the parameters for the function we like to minimize. # The representation tries to stay close to the SMAC manual, but deviates # if necessary. # Because we use a Python dictionary to represent the # parameters, there are different ways of creating it. The author finds the # following way most intuitive: parameter_definition=dict(\ x1=('real', [-5, 5], 1), # this line means x1 is a float between -5 and 5, with a default of 1 x2=('real', [-5, 5], -1), # same as x1, but the default is -1 x3=('integer', [0, 10], 1), # integer that ranges between 0 and 10, default is 1 ) # Consult the docs for a comprehensive presentation of possibilities. # The next step is to create a SMAC_optimizer object opt = pysmac.SMAC_optimizer(debug=False) # Then, call its minimize method with at least the three mandatory parameters value, parameters = opt.minimize( modified_branin, # the function to be minimized 1000, # the maximum number of function evaluations parameter_definition) # the parameter dictionary # the return value is a tuple of the lowest function value and a dictionary # containing corresponding parameter setting. print(('Lowest function value found: %f' % value)) print(('Parameter setting %s' % parameters))
print('The default accuracy is %f'%true_accuracy()) # We haven't changed anything here. parameter_definition=dict(\ max_depth =("integer", [1, 10], 4), max_features=("integer", [1, 20], 10), n_estimators=("integer", [1,100], 10, 'log'), criterion =("categorical", ['gini', 'entropy'], 'entropy'), bootstrap =("integer", [0,1], 1) ) # Same creation of the SMAC_optimizer object opt = pysmac.SMAC_optimizer( working_directory = '/tmp/pysmac_test/',# the folder where SMAC generates output persistent_files=True, # whether the output will persist beyond the python object's lifetime debug = False # if something goes wrong, enable this for diagnostic output ) # The minimize method also has optional arguments value, parameters = opt.minimize(random_forest, 10, parameter_definition, num_runs = 1, # number of independent SMAC runs seed = 0, # the random seed used. can be an int or a list of ints of length num_runs num_procs = 2, # pysmac can harness multicore architecture. Specify the number of processes to use here. num_train_instances = len(kfold),# This tells SMAC how many different instances there are. train_instance_features = features# use the features defined above to better predict the overall performance ) print('Parameter setting %s'%parameters) print('The highest accuracy estimation: %f'%(-value))
return -median(scores) # Here we define the hyperparameters search space from where we sample parameter_definition=dict(\ number_layers = ("integer", [1,4], 2), number_neurons_1 =("integer", [10,1000], 100, 'log'), number_neurons_2 =("integer", [10,1000], 100, 'log'), number_neurons_3 =("integer", [10,1000], 100, 'log'), number_neurons_4 =("integer", [10,1000], 100, 'log'), dropout_rate =("real", [0,0.5], 0) ) # We create the optimizer object opt = pysmac.SMAC_optimizer(working_directory='./results/dataset8/smac/' % os.environ, persistent_files=True, debug=False) # First we try the a MLP set to a default configuration, so we can see if SMAC can improve its performance scores = [] for i in np.arange(n_validations): X_train, X_test, Y_train, Y_test = sklearn.cross_validation.train_test_split( X, Y, test_size=0.3, random_state=1) predictor = Classifier(layers=[ Layer("Sigmoid", units=100, dropout=0), Layer("Sigmoid", units=100, dropout=0), Layer("Softmax", units=2) ], learning_rate=0.001,
r = 6 s = 10 t = 1 / (8 * math.pi) ret = a * (x2 - b * x1**2 + c * x1 - r)**2 + s * (1 - t) * math.cos(x1) + s + x3 return ret # parameter definition parameter_definition=dict(\ x1=('real', [-5, 5], 1), # this line means x1 is a float between -5 and 5, with a default of 1 x2=('real', [-5, 5], -1), # same as x1, but the default is -1 x3=('integer', [0, 10], 1), # integer that ranges between 0 and 10, default is 1 ) # optimizer object opt = pysmac.SMAC_optimizer(working_directory=path + '/pysmac_output', persistent_files=True) # call its minimize method value, parameters = opt.minimize( modified_branin, # the function to be minimized 100, # 1000 the maximum number of function evaluations parameter_definition, num_runs=3) # the parameter dictionary # fanova object fanova = pysmac_fanova.smac_to_fanova(path + '/pysmac_output/out/scenario', path + "/merged_states") res = fanova.quantify_importance((2, )) print(res) best_margs = fanova.get_most_important_pairwise_marginals(n=3) print('Most important pairwise marginals: %s' % best_margs)