Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
[0., 1., 0.],
[0., 1., 1.],
[1., 0., 0.],
[1., 0., 1.],
[1., 1., 0.],
[1., 1., 1.]], dtype=np.float64)
y = np.array([
[.1],
[.2],
[9],
[9.2],
[100.],
[100.2],
[109.],
[109.2]], dtype=np.float64)
model = RandomForestWithInstances(
configspace=self._get_cs(3),
types=np.array([0, 0, 0], dtype=np.uint),
bounds=[(0, np.nan), (0, np.nan), (0, np.nan)],
instance_features=None,
seed=12345,
ratio_features=1.0,
)
model.train(np.vstack((X, X, X, X, X, X, X, X)), np.vstack((y, y, y, y, y, y, y, y)))
y_hat, _ = model.predict(X)
for y_i, y_hat_i in zip(y.reshape((1, -1)).flatten(), y_hat.reshape((1, -1)).flatten()):
self.assertAlmostEqual(y_i, y_hat_i, delta=0.1)
def test_comp_builder(self):
conf = {"model": "RF", "acq_func": "EI", "y_transform": "y"}
model, acqf, rh2epm = self.acs._component_builder(conf)
self.assertIsInstance(acqf, EI)
self.assertIsInstance(model, RandomForestWithInstances)
self.assertIsInstance(rh2epm, AbstractRunHistory2EPM)
conf = {"model": "GP", "acq_func": "EI", "y_transform": "y"}
model, acqf, rh2epm = self.acs._component_builder(conf)
self.assertIsInstance(acqf, EI)
self.assertIsInstance(model, GaussianProcessMCMC)
self.assertIsInstance(rh2epm, AbstractRunHistory2EPM)
def test_construct_epm(self):
rng = np.random.RandomState(42)
smbo = SMAC4AC(self.scenario)
self.assertIsInstance(smbo.solver.model, RandomForestWithInstances)
smbo = SMAC4AC(self.scenario, rng=rng)
self.assertIsInstance(smbo.solver.model, RandomForestWithInstances)
self.assertEqual(smbo.solver.model.seed, 1935803228)
smbo = SMAC4AC(self.scenario, model_kwargs={'seed': 2})
self.assertIsInstance(smbo.solver.model, RandomForestWithInstances)
self.assertEqual(smbo.solver.model.seed, 2)
smbo = SMAC4AC(self.scenario, model_kwargs={'num_trees': 20})
self.assertIsInstance(smbo.solver.model, RandomForestWithInstances)
self.assertEqual(smbo.solver.model.rf_opts.num_trees, 20)
smbo = SMAC4AC(self.scenario, model=RandomEPM, model_kwargs={'seed': 2})
self.assertIsInstance(smbo.solver.model, RandomEPM)
self.assertEqual(smbo.solver.model.seed, 2)
# Check for construction failure on wrong argument
with self.assertRaisesRegex(Exception, 'got an unexpected keyword argument'):
SMAC4AC(self.scenario, model_kwargs={'dummy': 0.1})
a = cs.add_hyperparameter(CategoricalHyperparameter('a', [0, 1]))
b = cs.add_hyperparameter(CategoricalHyperparameter('b', [0, 1]))
c = cs.add_hyperparameter(UniformFloatHyperparameter('c', 0, 1))
cs.add_condition(EqualsCondition(b, a, 1))
cs.add_condition(EqualsCondition(c, a, 0))
cs.seed(1)
configs = cs.sample_configuration(size=100)
config_array = smac.configspace.convert_configurations_to_array(configs)
for line in config_array:
if line[0] == 0:
self.assertTrue(np.isnan(line[1]))
elif line[0] == 1:
self.assertTrue(np.isnan(line[2]))
model = RandomForestWithInstances(
configspace=cs,
types=np.zeros((3,), dtype=np.uint),
bounds=list(map(lambda x: (0, 1), range(10))),
seed=1,
)
config_array = model._impute_inactive(config_array)
for line in config_array:
if line[0] == 0:
self.assertEqual(line[1], 2)
elif line[0] == 1:
self.assertEqual(line[2], -1)
def test_predict(self):
rs = np.random.RandomState(1)
X = rs.rand(20, 10)
Y = rs.rand(10, 1)
model = RandomForestWithInstances(
configspace=self._get_cs(10),
types=np.zeros((10,), dtype=np.uint),
bounds=list(map(lambda x: (0, 10), range(10))),
seed=1,
)
model.train(X[:10], Y[:10])
m_hat, v_hat = model.predict(X[10:])
self.assertEqual(m_hat.shape, (10, 1))
self.assertEqual(v_hat.shape, (10, 1))
def test_init_only_scenario_quality(self):
epils = EPILS(self.scenario).solver
self.assertIsInstance(epils.model, RandomForestWithInstances)
self.assertIsInstance(epils.rh2EPM, RunHistory2EPM4Cost)
self.assertIsInstance(epils.acquisition_func, EI)
def test_predict_marginalized_over_instances(self):
rs = np.random.RandomState(1)
X = rs.rand(20, 10)
F = rs.rand(10, 5)
Y = rs.rand(len(X) * len(F), 1)
X_ = rs.rand(200, 15)
model = RandomForestWithInstances(
configspace=self._get_cs(10),
types=np.zeros((15,), dtype=np.uint),
instance_features=F,
bounds=list(map(lambda x: (0, 10), range(10))),
seed=1,
)
model.train(X_, Y)
means, vars = model.predict_marginalized_over_instances(X)
self.assertEqual(means.shape, (20, 1))
self.assertEqual(vars.shape, (20, 1))
optional, RunHistory-object to reuse runs
reuse_epm: bool
if true (and if `self.epm`), reuse epm to validate runs
Returns
-------
runhistory: RunHistory
runhistory with predicted runs
"""
if not isinstance(runhistory, RunHistory) and (self.epm is None or
reuse_epm is False):
raise ValueError("No runhistory specified for validating with EPM!")
elif reuse_epm is False or self.epm is None:
# Create RandomForest
types, bounds = get_types(self.scen.cs, self.scen.feature_array)
self.epm = RandomForestWithInstances(
configspace=self.scen.cs,
types=types,
bounds=bounds,
instance_features=self.scen.feature_array,
seed=self.rng.randint(MAXINT),
ratio_features=1.0,
)
# Use imputor if objective is runtime
imputor = None
impute_state = None
impute_censored_data = False
if self.scen.run_obj == 'runtime':
threshold = self.scen.cutoff * self.scen.par_factor
imputor = RFRImputator(rng=self.rng,
cutoff=self.scen.cutoff,
threshold=threshold,
def _refit_model(self, types, bounds, X, y):
"""
Easily allows for refitting of the model.
Parameters
----------
types: list
SMAC EPM types
X:ndarray
X matrix
y:ndarray
corresponding y vector
"""
# take at most 80% of the data per split to ensure enough data for oob error
self.model = RandomForestWithInstances(types=types, bounds=bounds, do_bootstrapping=True,
n_points_per_tree=int(X.shape[1]*0.8))
self.model.rf_opts.compute_oob_error = True
self.model.train(X, y)
len(time), len(set(configs)))
if validator.epm:
epm = validator.epm
else:
self.logger.debug("No EPM passed! Training new one from runhistory.")
# Train random forest and transform training data (from given rh)
# Not using validator because we want to plot uncertainties
rh2epm = RunHistory2EPM4Cost(num_params=len(self.scenario.cs.get_hyperparameters()),
scenario=self.scenario)
X, y = rh2epm.transform(rh)
self.logger.debug("Training model with data of shape X: %s, y:%s",
str(X.shape), str(y.shape))
types, bounds = get_types(self.scenario.cs, self.scenario.feature_array)
epm = RandomForestWithInstances(types=types,
bounds=bounds,
instance_features=self.scenario.feature_array,
#seed=self.rng.randint(MAXINT),
ratio_features=1.0)
epm.train(X, y)
## not necessary right now since the EPM only knows the features
## of the training instances
# use only training instances
#=======================================================================
# if self.scenario.feature_dict:
# feat_array = []
# for inst in self.scenario.train_insts:
# feat_array.append(self.scenario.feature_dict[inst])
# backup_features_epm = epm.instance_features
# epm.instance_features = np.array(feat_array)