Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tfm = PCA() | Nystroem() | NoOp()
planned_pipeline1 = (OneHotEncoder(handle_unknown = 'ignore', sparse = False) | NoOp()) >> tfm >> (LogisticRegression() | KNeighborsClassifier())
cs:ConfigurationSpace = get_smac_space(planned_pipeline1, lale_num_grids=1)
# Scenario object
scenario = Scenario({"run_obj": "quality", # we optimize quality (alternatively runtime)
"runcount-limit": 1, # maximum function evaluations
"cs": cs, # configuration space
"deterministic": "true"
})
# Optimize, using a SMAC-object
tae = iris_fmin_tae(planned_pipeline1, num_folds=2)
print("Optimizing! Depending on your machine, this might take a few minutes.")
smac = orig_SMAC(scenario=scenario, rng=np.random.RandomState(42),
tae_runner=tae)
incumbent = smac.optimize()
inc_value = tae(incumbent)
print("Optimized Value: %.2f" % (inc_value))
lr = LogisticRegression()
cs:ConfigurationSpace = get_smac_space(lr)
# Scenario object
scenario = Scenario({"run_obj": "quality", # we optimize quality (alternatively runtime)
"runcount-limit": 1, # maximum function evaluations
"cs": cs, # configuration space
"deterministic": "true",
"abort_on_first_run_crash": False
})
# Optimize, using a SMAC-object
tae = iris_fmin_tae(lr, num_folds=2)
print("Optimizing! Depending on your machine, this might take a few minutes.")
smac = orig_SMAC(scenario=scenario, rng=np.random.RandomState(42),
tae_runner=tae)
incumbent = smac.optimize()
inc_value = tae(incumbent)
print("Optimized Value: %.2f" % (inc_value))
# Test exception handling on scoring
mbo_wrapper.scoring = 'sklearn'
assert_raises(ValueError, mbo_wrapper.fit, X, y)
mbo_params = mbo_wrapper.best_estimator_.get_params()
# SMAC scenario oject
scenario = Scenario({"run_obj": "quality",
"runcount-limit": n_iter,
"cs": config_space,
"deterministic": "true",
"memory_limit": 3072,
"intensification_percentage": intensification})
# To optimize, we pass the function to the SMAC-object
smac = SMAC(scenario=scenario, rng=random_seed,
tae_runner=obj_function)
smac_incumbent = smac.optimize()
smac_inc_value = np.mean(np.array(smac.get_tae_runner().run(smac_incumbent, 1)[3]['test_scores']))
# === COMPARE INCUMBENTS ===
# note that multiple configurations could have led to the same
# SMAC always takes the most recently found (in case of 1 instance problems)
# so we find all the maximized indices in mbo results
scores = mbo_wrapper.cv_results_['mean_test_score']
mbo_max_indices = [i for i, j in enumerate(scores) if j == max(scores)]
self.assertAlmostEqual(mbo_wrapper.best_score_, smac_inc_value, 2) # apparently, scikit-learn search returns only 2 digits
foundEqual = False
for mbo_idx in mbo_max_indices:
for param_name, param_value in smac_incumbent.get_dictionary().items():
self.assertIn(param_name, mbo_params)
def __init__(self, config_space, seed=None):
if seed is None:
self.seed = np.random.randint(0, 10000)
else:
self.seed = seed
self.rng = np.random.RandomState(self.seed)
super(SMAC, self).__init__(sacred_space_to_configspace(config_space))
self.scenario = Scenario({"run_obj": "quality",
"cs": self.config_space,
"deterministic": "true"})
self.solver = smac_facade.SMAC(scenario=self.scenario,
rng=self.rng)
loss = self.evaluate_and_log(config, budget=self.max_budget)
with open(tmp_fn, 'wb') as fh:
pickle.dump(self.run_data, fh)
return loss, []
scenario = Scenario({ "run_obj": "quality",
"runcount-limit": num_iterations,
"cs": self.configspace,
"deterministic": deterministic,
"initial_incumbent": "RANDOM",
"output_dir": working_directory})
smac = SMAC(scenario=scenario, tae_runner=smac_objective)
smac.optimize()
with open(tmp_fn, 'rb') as fh:
self.run_data = pickle.load(fh)
os.remove(tmp_fn)
return(self.get_result())
"deterministic": "true",
"output_dir": None
})
# string translations
acq = {"LCB": LCB, "EI": EI, "PI": PI}
r2e = {"InvScaled(y)": RunHistory2EPM4InvScaledCost,
"LogScaled(y)": RunHistory2EPM4LogScaledCost,
"y": RunHistory2EPM4Cost}
init_design = {"LHD": LHDesign,
"Sobol": SobolDesign,
"Default": DefaultConfiguration}
# Optimize, using a SMAC-object
print("Optimizing! Depending on your machine, this might take a few minutes.")
smac = SMAC(scenario=scenario,
rng=np.random.RandomState(seed),
tae_runner=wrapper,
initial_design=init_design[smac_config['init_design']],
initial_design_kwargs={'n_configs_x_params':smac_config['n_configs_x_params'],
'max_config_fracs':1.0},
random_configuration_chooser_kwargs={'prob': smac_config['rand_prob']},
runhistory2epm=r2e[smac_config['y_trans']],
runhistory2epm_kwargs={'scale_perc': smac_config['scale_perc']},
model_kwargs={'num_trees': smac_config['num_trees'],
'log_y': smac_config['log_y'],
'do_bootstrapping': smac_config['do_bootstrapping'],
'ratio_features': smac_config['ratio_features'],
'min_samples_split': smac_config['min_samples_split'],
'min_samples_leaf': smac_config['min_samples_leaf']},
acquisition_function=acq[smac_config['acq_func']],
acquisition_function_kwargs={'par':smac_config['par'] if smac_config['par'] is not None else smac_config['lcb_par']}
from smac.utils.io.input_reader import InputReader
from smac.runhistory.runhistory import RunKey, RunValue, RunHistory
from smac.scenario.scenario import Scenario
from smac.utils.io.traj_logging import TrajLogger
from smac.utils.validate import Validator
@contextmanager
def changedir(newdir):
olddir = os.getcwd()
os.chdir(os.path.expanduser(newdir))
try:
yield
finally:
os.chdir(olddir)
class SMACrun(SMAC):
"""
SMACrun keeps all information on a specific SMAC run. Extends the standard
SMAC-facade.
"""
def __init__(self, folder: str, ta_exec_dir: Union[str, None]=None):
"""Initialize scenario, runhistory and incumbent from folder, execute
init-method of SMAC facade (so you could simply use SMAC-instances instead)
Parameters
----------
folder: string
output-dir of this run
ta_exec_dir: string
if the execution directory for the SMAC-run differs from the cwd,
there might be problems loading instance-, feature- or PCS-files
in the scenario-object. since instance- and PCS-files are necessary,
root_logger.addHandler(logger_handler)
# remove default handler
root_logger.removeHandler(root_logger.handlers[0])
# Create defaults
rh = None
initial_configs = None
stats = None
incumbent = None
# Create scenario-object
scen = Scenario(args.scenario_file, [])
self.cs = scen.cs
if args.mode == "SMAC":
optimizer = SMAC(
scenario=scen,
rng=np.random.RandomState(args.seed),
runhistory=rh,
initial_configurations=initial_configs,
stats=stats,
restore_incumbent=incumbent,
run_id=args.seed)
elif args.mode == "ROAR":
optimizer = ROAR(
scenario=scen,
rng=np.random.RandomState(args.seed),
runhistory=rh,
initial_configurations=initial_configs,
run_id=args.seed)
elif args.mode == "EPILS":
optimizer = EPILS(
name=k, choices=v.categories
)
)
scenario = Scenario({
"run_obj": "quality", # we optimize quality (alternative runtime)
"runcount-limit": n_calls, # at most 200 function evaluations
"cs": cs, # configuration space
"deterministic": "true"
})
with tempfile.NamedTemporaryFile() as save_to:
tape_recorder = SMACBenchWrapper(func, save_to.name)
taf = ExecuteTAFuncDict(tape_recorder)
smac = SMAC(scenario=scenario, tae_runner=taf)
incumbent = smac.optimize()
Xi, yi = pickle.load(open(save_to.name, 'rb'))
#x = point_aslist(search_space, parameters)
#y = value
return create_result(Xi, yi, dims)