Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_minimum_observed_value_metric():
x_observations = np.random.rand(50, 2)
y_observations = np.random.rand(50, 2)
mock_model = mock.create_autospec(IModel)
model_updater_mock = mock.create_autospec(ModelUpdater)
model_updater_mock.model = mock_model
mock_loop = mock.create_autospec(OuterLoop)
mock_loop.model_updaters = [model_updater_mock]
loop_state = create_loop_state(x_observations, y_observations)
loop_state.metrics = dict()
metric = MinimumObservedValueMetric()
metric_value = metric.evaluate(mock_loop, loop_state)
assert metric_value.shape == (2,)
x_test = np.linspace(0, 1)[:, None]
y_test = user_function(x_test)
x_init = np.linspace(0, 1, 5)[:, None]
y_init = user_function(x_init)
gpy_model = GPy.models.GPRegression(x_init, y_init)
model = GPyModelWrapper(gpy_model)
mse = []
def compute_mse(self, loop_state):
mse.append(np.mean(np.square(model.predict(x_test)[0] - y_test)))
loop_state = create_loop_state(x_init, y_init)
acquisition = ModelVariance(model)
acquisition_optimizer = AcquisitionOptimizer(space)
candidate_point_calculator = SequentialPointCalculator(acquisition, acquisition_optimizer)
model_updater = FixedIntervalUpdater(model)
loop = OuterLoop(candidate_point_calculator, model_updater, loop_state)
loop.iteration_end_event.append(compute_mse)
loop.run_loop(user_function, 5)
assert len(mse) == 5
def test_loop_state_update_error():
x = np.array([[1], [2], [3], [4]])
y = np.array([[4], [5], [6], [7]])
loop_state = create_loop_state(x[:3, :], y[:3, :])
with pytest.raises(ValueError):
loop_state.update(None)
with pytest.raises(ValueError):
loop_state.update([])
def test_cost_returns_none():
x = np.array([[1], [2], [3], [4]])
y = np.array([[4], [5], [6], [7]])
loop_state = create_loop_state(x[:3, :], y[:3, :])
assert np.array_equiv(loop_state.cost, np.array([None, None, None]))
def test_create_loop_state_with_cost():
x_init = np.array([[1], [2], [3]])
y_init = np.array([[4], [5], [6]])
cost = np.array([[5], [2], [0]])
loop_state = create_loop_state(x_init, y_init, cost)
assert_array_equal(loop_state.X, x_init)
assert_array_equal(loop_state.Y, y_init)
assert_array_equal(loop_state.cost, cost)
assert loop_state.iteration == 0
def test_time_metric():
x_observations = np.random.rand(50, 2)
y_observations = np.random.rand(50, 2)
mock_model = mock.create_autospec(IModel)
model_updater_mock = mock.create_autospec(ModelUpdater)
model_updater_mock.model = mock_model
mock_loop = mock.create_autospec(OuterLoop)
mock_loop.model_updater = model_updater_mock
loop_state = create_loop_state(x_observations, y_observations)
loop_state.metrics = dict()
name = 'time'
metric = TimeMetric(name)
metric.reset()
metric_value = metric.evaluate(mock_loop, loop_state)
assert metric_value.shape == (1,)
:param acquisition: The acquisition function that is be used to collect new points.
default, IntegralVarianceReduction
:param model_updater: Defines how and when the quadrature model is updated if new data arrives.
Defaults to updating hyper-parameters every iteration.
"""
if acquisition is None:
acquisition = IntegralVarianceReduction(model)
if model_updater is None:
model_updater = FixedIntervalUpdater(model, 1)
space = ParameterSpace(model.integral_bounds.convert_to_list_of_continuous_parameters())
acquisition_optimizer = AcquisitionOptimizer(space)
candidate_point_calculator = SequentialPointCalculator(acquisition, acquisition_optimizer)
loop_state = create_loop_state(model.X, model.Y)
super().__init__(candidate_point_calculator, model_updater, loop_state)
self.model = model
acquisition = ModelVariance(model)
# This AcquisitionOptimizer object deals with optimizing the acquisition to find the next point to collect
acquisition_optimizer = AcquisitionOptimizer(space)
# Construct emukit classes
if batch_size == 1:
candidate_point_calculator = SequentialPointCalculator(acquisition, acquisition_optimizer)
elif batch_size > 1:
candidate_point_calculator = \
GreedyBatchPointCalculator(model, acquisition, acquisition_optimizer, batch_size)
else:
raise ValueError('Batch size value of ' + str(batch_size) + ' is invalid.')
model_updater = FixedIntervalUpdater(model, update_interval)
loop_state = create_loop_state(model.X, model.Y)
super().__init__(candidate_point_calculator, model_updater, loop_state)
self.model = model
acquisition = ExpectedImprovement(model)
model_updaters = FixedIntervalUpdater(model, update_interval)
acquisition_optimizer = AcquisitionOptimizer(space)
if batch_size == 1:
candidate_point_calculator = SequentialPointCalculator(acquisition, acquisition_optimizer)
else:
if not isinstance(model, IDifferentiable):
raise ValueError('Model must implement ' + str(IDifferentiable) +
' for use with Local Penalization batch method.')
log_acquisition = LogAcquisition(acquisition)
candidate_point_calculator = LocalPenalizationPointCalculator(log_acquisition, acquisition_optimizer, model,
space, batch_size)
loop_state = create_loop_state(model.X, model.Y)
super().__init__(candidate_point_calculator, model_updaters, loop_state)
target_fidelity_index=len(
extended_space.parameters) - 1)
entropy_search = IntegratedHyperParameterAcquisition(model_objective, acquisition_generator)
else:
entropy_search = ContinuousFidelityEntropySearch(model_objective, space=extended_space,
target_fidelity_index=len(extended_space.parameters) - 1)
acquisition = acquisition_per_expected_cost(entropy_search, model_cost)
model_updater_objective = FixedIntervalUpdater(model_objective, update_interval)
model_updater_cost = FixedIntervalUpdater(model_cost, update_interval, lambda state: state.cost)
acquisition_optimizer = RandomSearchAcquisitionOptimizer(extended_space, num_eval_points=num_eval_points)
candidate_point_calculator = SequentialPointCalculator(acquisition, acquisition_optimizer)
loop_state = create_loop_state(model_objective.X, model_objective.Y, model_cost.Y)
super(CostSensitiveBayesianOptimizationLoop, self).__init__(candidate_point_calculator,
[model_updater_objective, model_updater_cost],
loop_state)