Exemplo n.º 1
0
def test_rff_sampler_returns_same_posterior_from_each_calculation_method(
) -> None:
    model = QuadraticMeanAndRBFKernel(
        noise_variance=tf.constant(1.0, dtype=tf.float64))
    model.kernel = (
        gpflow.kernels.RBF()
    )  # need a gpflow kernel object for random feature decompositions
    x_range = tf.linspace(0.0, 1.0, 5)
    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))
    ys = quadratic(xs)
    dataset = Dataset(xs, ys)

    sampler = RandomFourierFeatureThompsonSampler(dataset, model, 100)
    sampler.get_trajectory()

    posterior_1 = sampler._prepare_theta_posterior_in_design_space()
    posterior_2 = sampler._prepare_theta_posterior_in_gram_space()

    npt.assert_allclose(posterior_1.loc, posterior_2.loc, rtol=0.02)
    npt.assert_allclose(posterior_1.scale_tril,
                        posterior_2.scale_tril,
                        rtol=0.02)
Exemplo n.º 2
0
def test_rff_sampler_raises_for_a_non_gpflow_kernel() -> None:
    model = QuadraticMeanAndRBFKernel()
    dataset = Dataset(tf.constant([[-2.0]]), tf.constant([[4.1]]))
    sampler = RandomFourierFeatureThompsonSampler(dataset, model, 100)

    with pytest.raises(AssertionError):
        sampler.get_trajectory()
Exemplo n.º 3
0
def test_rff_sampler_does_pre_calc_during_first_trajectory_call() -> None:
    model = QuadraticMeanAndRBFKernel(
        noise_variance=tf.constant(1.0, dtype=tf.float64))
    model.kernel = gpflow.kernels.RBF()
    dataset = Dataset(tf.constant([[-2.0]], dtype=tf.float64),
                      tf.constant([[4.1]], dtype=tf.float64))
    sampler = RandomFourierFeatureThompsonSampler(dataset, model, 100)
    assert sampler._pre_calc is False

    sampler.get_trajectory()
    assert sampler._pre_calc is True
Exemplo n.º 4
0
def test_rff_sampler_returns_trajectory_function_with_correct_shaped_output(num_evals: int) -> None:
    model = QuadraticMeanAndRBFKernel(noise_variance=tf.constant(1.0, dtype=tf.float64))
    model.kernel = (
        gpflow.kernels.RBF()
    )  # need a gpflow kernel object for random feature decompositions
    dataset = Dataset(
        tf.constant([[-2.0]], dtype=tf.float64), tf.constant([[4.1]], dtype=tf.float64)
    )
    sampler = RandomFourierFeatureThompsonSampler(1, model, dataset, num_features=100)

    trajectory = sampler.get_trajectory()
    xs = tf.linspace([-10.0], [10.0], num_evals)

    tf.debugging.assert_shapes([(trajectory(xs), [num_evals, 1])])
Exemplo n.º 5
0
def test_rff_sampler_returns_deterministic_trajectory() -> None:
    model = QuadraticMeanAndRBFKernel(noise_variance=tf.constant(1.0, dtype=tf.float64))
    model.kernel = (
        gpflow.kernels.RBF()
    )  # need a gpflow kernel object for random feature decompositions
    x_range = tf.linspace(0.0, 1.0, 5)
    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))
    ys = quadratic(xs)
    dataset = Dataset(xs, ys)

    sampler = RandomFourierFeatureThompsonSampler(1, model, dataset, num_features=100)
    trajectory = sampler.get_trajectory()

    trajectory_eval_1 = trajectory(xs)
    trajectory_eval_2 = trajectory(xs)

    npt.assert_allclose(trajectory_eval_1, trajectory_eval_2)