def test_concatenate_datasets() -> None: qp_this = [[1.2, 3.4], [5.6, 7.8]] qp_that = [[5., 6.], [7., 8.]] obs_this = [[1.1, 2.2], [3.3, 4.4]] obs_that = [[-1., -2.], [-3., -4.]] this = Dataset(tf.constant(qp_this), tf.constant(obs_this)) that = Dataset(tf.constant(qp_that), tf.constant(obs_that)) merged = this + that assert tf.reduce_all(merged.query_points == tf.constant(qp_this + qp_that)) assert tf.reduce_all(merged.observations == tf.constant(obs_this + obs_that))
class ReducerTestData: type_class: Type[Union[Sum, Product]] raw_reduce_op: Callable[[Sequence], float] dataset: Dataset = Dataset( np.arange(5, dtype=np.float64).reshape(-1, 1), np.zeros(5).reshape(-1, 1) ) query_point: tf.Tensor = tf.convert_to_tensor(np.array([[0.1], [0.2]]))
def test_trust_region_successful_global_to_global_trust_region_unchanged( ) -> None: tr = TrustRegion(NegativeLowerConfidenceBound(0).using(OBJECTIVE)) dataset = Dataset(tf.constant([[0.1, 0.2], [-0.1, -0.2]]), tf.constant([[0.4], [0.3]])) lower_bound = tf.constant([-2.2, -1.0]) upper_bound = tf.constant([1.3, 3.3]) search_space = Box(lower_bound, upper_bound) eps = 0.5 * (search_space.upper - search_space.lower) / 10 previous_y_min = dataset.observations[0] is_global = True previous_state = TrustRegion.State(search_space, eps, previous_y_min, is_global) query_point, current_state = tr.acquire( search_space, {OBJECTIVE: dataset}, {OBJECTIVE: QuadraticWithUnitVariance()}, previous_state) npt.assert_array_almost_equal(current_state.eps, previous_state.eps) assert current_state.is_global npt.assert_array_almost_equal(query_point, tf.constant([[0.0, 0.0]]), 5) npt.assert_array_almost_equal(current_state.acquisition_space.lower, lower_bound) npt.assert_array_almost_equal(current_state.acquisition_space.upper, upper_bound)
def test_ego(search_space: SearchSpace, expected_minimum: tf.Tensor) -> None: ego = EfficientGlobalOptimization( NegativeLowerConfidenceBound(0).using(OBJECTIVE)) dataset = Dataset(tf.constant([[]]), tf.constant([[]])) query_point, _ = ego.acquire(search_space, {OBJECTIVE: dataset}, {OBJECTIVE: QuadraticWithUnitVariance()}) npt.assert_array_almost_equal(query_point, expected_minimum, decimal=5)
def test_dataset_raises_on_initialisation_for_invalid_ranks( query_points_shape: Tuple[int, ...], observations_shape: Tuple[int, ...]) -> None: query_points = tf.zeros(query_points_shape) observations = tf.ones(observations_shape) with pytest.raises(ValueError): Dataset(query_points, observations)
def test_dataset_raises_on_initialisation_for_different_leading_shapes( query_points_leading_shape: Tuple[int, ...], observations_leading_shape: Tuple[int, ...], last_dim_size: int) -> None: query_points = tf.zeros(query_points_leading_shape + (last_dim_size, )) observations = tf.ones(observations_leading_shape + (last_dim_size, )) with pytest.raises(ValueError, match='(L|l)eading'): Dataset(query_points, observations)
def test_expected_improvement_builder_builds_expected_improvement( query_at: tf.Tensor) -> None: dataset = Dataset(tf.constant([[-2.], [-1.], [0.], [1.], [2.]]), tf.zeros([5, 1])) model = QuadraticWithUnitVariance() builder = ExpectedImprovement() acq_fn = builder.prepare_acquisition_function(dataset, model) expected = expected_improvement(model, tf.constant([0.]), query_at) npt.assert_array_almost_equal(acq_fn(query_at), expected)
def test_negative_lower_confidence_bound_builder_builds_negative_lower_confidence_bound( ) -> None: model = QuadraticWithUnitVariance() beta = 1.96 acq_fn = NegativeLowerConfidenceBound(beta).prepare_acquisition_function( Dataset(tf.constant([[]]), tf.constant([[]])), model) query_at = tf.constant([[-3.], [-2.], [-1.], [0.], [1.], [2.], [3.]]) expected = -lower_confidence_bound(model, beta, query_at) npt.assert_array_almost_equal(acq_fn(query_at), expected)
def test_product_reducer_multiplies_tensors(combination, inputs): combination_builder, expected_fn = combination inputs = [np.array(i) for i in inputs] expected = expected_fn(inputs) builders = [_InputIdentity(i) for i in inputs] reducer = combination_builder(*builders) data = Dataset(tf.zeros((1, 1)), tf.zeros((1, 1))) prepared_fn = reducer.prepare_acquisition_function(data, QuadraticWithUnitVariance()) result = prepared_fn(tf.zeros(1)) np.testing.assert_allclose(result, expected)
def test_trust_region_for_default_state() -> None: tr = TrustRegion(NegativeLowerConfidenceBound(0).using(OBJECTIVE)) dataset = Dataset(tf.constant([[0.1, 0.2]]), tf.constant([[0.012]])) lower_bound = tf.constant([-2.2, -1.0]) upper_bound = tf.constant([1.3, 3.3]) search_space = Box(lower_bound, upper_bound) query_point, state = tr.acquire(search_space, {OBJECTIVE: dataset}, {OBJECTIVE: QuadraticWithUnitVariance()}, None) npt.assert_array_almost_equal(query_point, tf.constant([[0.0, 0.0]]), 5) npt.assert_array_almost_equal(state.acquisition_space.lower, lower_bound) npt.assert_array_almost_equal(state.acquisition_space.upper, upper_bound) npt.assert_array_almost_equal(state.y_min, [0.012]) assert state.is_global
def test_dataset_length() -> None: assert len(Dataset(tf.ones((7, 8, 10)), tf.ones((7, 8, 13)))) == 7
def test_dataset_getters() -> None: query_points, observations = tf.zeros((3, 3)), tf.zeros((3, 3)) dataset = Dataset(query_points, observations) assert tf.reduce_all(dataset.query_points == query_points) assert tf.reduce_all(dataset.observations == observations)
def zero_dataset() -> Dataset: """ :return: A 1D input, 1D output dataset with a single entry of zeroes. """ return Dataset(tf.constant([[0.]]), tf.constant([[0.]]))