コード例 #1
0
ファイル: test_function.py プロジェクト: hstojic/trieste
def test_independent_reparametrization_sampler_sample_is_repeatable() -> None:
    sampler = IndependentReparametrizationSampler(100, _dim_two_gp())
    xs = tf.random.uniform([100, 2],
                           minval=-10.0,
                           maxval=10.0,
                           dtype=tf.float64)
    npt.assert_allclose(sampler.sample(xs), sampler.sample(xs))
コード例 #2
0
ファイル: test_function.py プロジェクト: hstojic/trieste
def test_independent_reparametrization_sampler_sample_raises_for_invalid_at_shape(
) -> None:
    sampler = IndependentReparametrizationSampler(1,
                                                  QuadraticMeanAndRBFKernel())

    with pytest.raises(TF_DEBUGGING_ERROR_TYPES):
        sampler.sample(tf.constant(0))
コード例 #3
0
ファイル: test_function.py プロジェクト: hstojic/trieste
def test_independent_reparametrization_sampler_sample_is_continuous() -> None:
    sampler = IndependentReparametrizationSampler(100, _dim_two_gp())
    xs = tf.random.uniform([100, 2],
                           minval=-10.0,
                           maxval=10.0,
                           dtype=tf.float64)
    npt.assert_array_less(
        tf.abs(sampler.sample(xs + 1e-20) - sampler.sample(xs)), 1e-20)
コード例 #4
0
def test_independent_reparametrization_sampler_samples_approximate_expected_distribution() -> None:
    class ArbitraryButNonTrivialModel(GaussianMarginal):
        def predict(self, query_points: TensorType) -> Tuple[TensorType, TensorType]:
            latent1 = tf.sin(query_points ** 2) + tf.cos(query_points)
            latent2 = tf.sin(query_points) - tf.cos(query_points ** 2)
            mean_ = tf.concat([latent1, latent2], axis=-1)
            return mean_, tf.sin(mean_)

    sample_size = 100
    x = tf.linspace([-10.0], [10.0], 20)

    model = ArbitraryButNonTrivialModel()
    samples = IndependentReparametrizationSampler(sample_size, model).sample(x)

    assert samples.shape == [len(x), sample_size, 2]

    samples_sorted = tf.sort(samples, axis=-2)
    edf = tf.range(1.0, sample_size + 1)[:, None, None] / sample_size

    mean, var = model.predict(x)
    expected_dist = tfp.distributions.Normal(mean, tf.sqrt(var))
    expected_cdf = expected_dist.cdf(tf.transpose(samples_sorted, [1, 0, 2]))

    _95_percent_bound = 1.36 / math.sqrt(sample_size)
    assert tf.reduce_max(tf.abs(edf - expected_cdf)) < _95_percent_bound
コード例 #5
0
 def _build_with_sampler(
     self,
     dataset: Dataset,
     model: ProbabilisticModel,
     sampler: IndependentReparametrizationSampler,
 ) -> AcquisitionFunction:
     x = tf.random.uniform([100, 2], minval=-10.0, maxval=10.0, dtype=tf.float64)
     samples = sampler.sample(x)
     mean, var = model.predict(x)
     _assert_kolmogorov_smirnov_95(
         tf.linalg.matrix_transpose(samples),
         tfp.distributions.Normal(mean[..., None], tf.sqrt(var)[..., None]),
     )
     return raise_
コード例 #6
0
def test_independent_reparametrization_sampler_samples_approximate_expected_distribution() -> None:
    sample_size = 1000
    x = tf.random.uniform([100, 2], minval=-10.0, maxval=10.0, dtype=tf.float64)

    model = _dim_two_gp()
    samples = IndependentReparametrizationSampler(sample_size, model).sample(x)  # [N, S, L]

    assert samples.shape == [len(x), sample_size, 2]

    mean, var = model.predict(x)  # [N, L], [N, L]
    _assert_kolmogorov_smirnov_95(
        tf.linalg.matrix_transpose(samples),
        tfp.distributions.Normal(mean[..., None], tf.sqrt(var[..., None])),
    )
コード例 #7
0
ファイル: test_function.py プロジェクト: hstojic/trieste
def test_independent_reparametrization_sampler_samples_are_distinct_for_new_instances(
) -> None:
    sampler1 = IndependentReparametrizationSampler(100, _dim_two_gp())
    sampler2 = IndependentReparametrizationSampler(100, _dim_two_gp())
    xs = tf.random.uniform([100, 2],
                           minval=-10.0,
                           maxval=10.0,
                           dtype=tf.float64)
    npt.assert_array_less(1e-9,
                          tf.abs(sampler2.sample(xs) - sampler1.sample(xs)))
コード例 #8
0
def test_independent_reparametrization_sampler_raises_for_negative_sample_size(
    sample_size: int,
) -> None:
    with pytest.raises(TF_DEBUGGING_ERROR_TYPES):
        IndependentReparametrizationSampler(sample_size, QuadraticMeanAndRBFKernel())
コード例 #9
0
def test_independent_reparametrization_sampler_samples_are_distinct_for_new_instances() -> None:
    sampler1 = IndependentReparametrizationSampler(100, QuadraticWithUnitVariance())
    sampler2 = IndependentReparametrizationSampler(100, QuadraticWithUnitVariance())
    xs = tf.linspace([-10.0], [10.0], 100)
    npt.assert_array_less(1e-9, tf.abs(sampler2.sample(xs) - sampler1.sample(xs)))
コード例 #10
0
def test_independent_reparametrization_sampler_sample_is_repeatable() -> None:
    sampler = IndependentReparametrizationSampler(100, QuadraticWithUnitVariance())
    xs = tf.linspace([-10.0], [10.0], 100)
    npt.assert_allclose(sampler.sample(xs), sampler.sample(xs))
コード例 #11
0
def test_independent_reparametrization_sampler_sample_is_continuous() -> None:
    sampler = IndependentReparametrizationSampler(100, QuadraticWithUnitVariance())
    xs = tf.linspace([-10.0], [10.0], 100)
    diff = tf.abs(sampler.sample(xs + 1e-9) - sampler.sample(xs))
    npt.assert_array_less(diff, 1e-9)
コード例 #12
0
def test_independent_reparametrization_sampler_sample_raises_for_invalid_at_shape() -> None:
    sampler = IndependentReparametrizationSampler(1, QuadraticWithUnitVariance())
    with pytest.raises((ValueError, tf.errors.InvalidArgumentError)):
        sampler.sample(tf.constant(0))
コード例 #13
0
def test_independent_reparametrization_sampler_raises_for_negative_sample_size(
    sample_size: int,
) -> None:
    with pytest.raises((ValueError, tf.errors.InvalidArgumentError)):
        IndependentReparametrizationSampler(sample_size, QuadraticWithUnitVariance())