def test_min_value_entropy_search_chooses_same_as_probability_of_improvement( ) -> None: """ When based on a single max-value sample, MES should choose the same point that probability of improvement would when calcualted with the max-value as its threshold (See :cite:`wang2017max`). """ kernel = tfp.math.psd_kernels.MaternFiveHalves() model = GaussianProcess([branin], [kernel]) x_range = tf.linspace(0.0, 1.0, 11) x_range = tf.cast(x_range, dtype=tf.float64) xs = tf.reshape( tf.stack(tf.meshgrid(x_range, x_range, indexing="ij"), axis=-1), (-1, 2)) gumbel_sample = tf.constant([1.0], dtype=tf.float64) mes_evals = min_value_entropy_search(model, gumbel_sample, xs) mean, variance = model.predict(xs) gamma = (tf.cast(gumbel_sample, dtype=mean.dtype) - mean) / tf.sqrt(variance) norm = tfp.distributions.Normal(tf.cast(0, dtype=mean.dtype), tf.cast(1, dtype=mean.dtype)) pi_evals = norm.cdf(gamma) npt.assert_array_equal(tf.argmax(mes_evals), tf.argmax(pi_evals))
def test_expected_improvement(variance_scale: float, num_samples_per_point: int, best: tf.Tensor, rtol: float, atol: float) -> None: variance_scale = tf.constant(variance_scale, tf.float64) best = tf.cast(best, dtype=tf.float64) x_range = tf.linspace(0.0, 1.0, 11) x_range = tf.cast(x_range, dtype=tf.float64) xs = tf.reshape( tf.stack(tf.meshgrid(x_range, x_range, indexing="ij"), axis=-1), (-1, 2)) kernel = tfp.math.psd_kernels.MaternFiveHalves(variance_scale, length_scale=0.25) model = GaussianProcess([branin], [kernel]) mean, variance = model.predict(xs) samples = tfp.distributions.Normal( mean, tf.sqrt(variance)).sample(num_samples_per_point) samples_improvement = tf.where(samples < best, best - samples, 0) ei_approx = tf.reduce_mean(samples_improvement, axis=0) ei = expected_improvement(model, best, xs) npt.assert_allclose(ei, ei_approx, rtol=rtol, atol=atol)
def _dim_two_gp(mean_shift: Tuple[float, float] = (0.0, 0.0)) -> GaussianProcess: matern52 = tfp.math.psd_kernels.MaternFiveHalves( amplitude=tf.cast(2.3, tf.float64), length_scale=tf.cast(0.5, tf.float64) ) return GaussianProcess( [lambda x: mean_shift[0] + branin(x), lambda x: mean_shift[1] + quadratic(x)], [matern52, rbf()], )
def _example_gaussian_process() -> GaussianProcess: return GaussianProcess( [quadratic, lambda x: quadratic(x) / 5.0], [ tfp.math.psd_kernels.ExponentiatedQuadratic(amplitude=1.6, length_scale=1.0), tfp.math.psd_kernels.ExponentiatedQuadratic(amplitude=1.6, length_scale=2.0), ], )
def test_batch_monte_carlo_expected_improvement_raises_for_model_with_wrong_event_shape() -> None: builder = BatchMonteCarloExpectedImprovement(100) data = mk_dataset([(0.0, 0.0)], [(0.0, 0.0)]) matern52 = tfp.math.psd_kernels.MaternFiveHalves( amplitude=tf.cast(2.3, tf.float64), length_scale=tf.cast(0.5, tf.float64) ) model = GaussianProcess([lambda x: branin(x), lambda x: quadratic(x)], [matern52, rbf()]) with pytest.raises(TF_DEBUGGING_ERROR_TYPES): builder.prepare_acquisition_function(data, model)
def _mo_test_model( num_obj: int, *kernel_amplitudes: float | TensorType | None) -> GaussianProcess: means = [ quadratic, lambda x: tf.reduce_sum(x, axis=-1, keepdims=True), quadratic ] kernels = [ tfp.math.psd_kernels.ExponentiatedQuadratic(k_amp) for k_amp in kernel_amplitudes ] return GaussianProcess(means[:num_obj], kernels[:num_obj])
def test_expected_constrained_improvement_is_less_for_constrained_points() -> None: class _Constraint(AcquisitionFunctionBuilder): def prepare_acquisition_function( self, datasets: Mapping[str, Dataset], models: Mapping[str, ProbabilisticModel] ) -> AcquisitionFunction: return lambda x: tf.cast(x >= 0, x.dtype) def two_global_minima(x: tf.Tensor) -> tf.Tensor: return x ** 4 / 4 - x ** 2 / 2 initial_query_points = tf.constant([[-2.0], [0.0], [1.2]]) data = {"foo": Dataset(initial_query_points, two_global_minima(initial_query_points))} models_ = {"foo": GaussianProcess([two_global_minima], [rbf()])} eci = ExpectedConstrainedImprovement("foo", _Constraint()).prepare_acquisition_function( data, models_ ) npt.assert_array_less(eci(tf.constant([-1.0])), eci(tf.constant([1.0])))