def test_randomize_hyperparameters_samples_different_values_for_multi_dimensional_params( ) -> None: kernel = gpflow.kernels.RBF(variance=1.0, lengthscales=[0.2, 0.2]) upper = tf.cast([10.0] * 2, dtype=tf.float64) lower = upper / 100 kernel.lengthscales = gpflow.Parameter(kernel.lengthscales, transform=tfp.bijectors.Sigmoid( low=lower, high=upper)) randomize_hyperparameters(kernel) npt.assert_raises(AssertionError, npt.assert_allclose, kernel.lengthscales[0], kernel.lengthscales[1])
def test_randomize_hyperparameters_randomizes_kernel_parameters_with_priors( dim: int) -> None: kernel = gpflow.kernels.RBF(variance=1.0, lengthscales=[0.2] * dim) kernel.lengthscales.prior = tfp.distributions.LogNormal(loc=tf.math.log( kernel.lengthscales), scale=1.0) randomize_hyperparameters(kernel) npt.assert_allclose(1.0, kernel.variance) npt.assert_array_equal(dim, kernel.lengthscales.shape) npt.assert_raises(AssertionError, npt.assert_allclose, [0.2] * dim, kernel.lengthscales)
def test_randomize_hyperparameters_randomizes_constrained_kernel_parameters( dim: int) -> None: kernel = gpflow.kernels.RBF(variance=1.0, lengthscales=[0.2] * dim) upper = tf.cast([10.0] * dim, dtype=tf.float64) lower = upper / 100 kernel.lengthscales = gpflow.Parameter(kernel.lengthscales, transform=tfp.bijectors.Sigmoid( low=lower, high=upper)) randomize_hyperparameters(kernel) npt.assert_allclose(1.0, kernel.variance) npt.assert_array_equal(dim, kernel.lengthscales.shape) npt.assert_raises(AssertionError, npt.assert_allclose, [0.2] * dim, kernel.lengthscales)
def test_randomize_hyperparameters_randomizes_kernel_parameters_with_constraints_or_priors( dim: int, ) -> None: kernel = gpflow.kernels.RBF(variance=1.0, lengthscales=[0.2] * dim) upper = tf.cast([10.0] * dim, dtype=tf.float64) lower = upper / 100 kernel.lengthscales = gpflow.Parameter(kernel.lengthscales, transform=tfp.bijectors.Sigmoid( low=lower, high=upper)) kernel.variance.prior = tfp.distributions.LogNormal(loc=np.float64(-2.0), scale=np.float64(1.0)) randomize_hyperparameters(kernel) npt.assert_raises(AssertionError, npt.assert_allclose, 1.0, kernel.variance) npt.assert_array_equal(dim, kernel.lengthscales.shape) npt.assert_raises(AssertionError, npt.assert_allclose, [0.2] * dim, kernel.lengthscales)
def test_randomize_hyperparameters_samples_from_constraints_when_given_prior_and_constraint( dim: int, ) -> None: kernel = gpflow.kernels.RBF(variance=1.0, lengthscales=[0.2] * dim) upper = tf.cast([0.5] * dim, dtype=tf.float64) lower = upper / 100 kernel.lengthscales = gpflow.Parameter(kernel.lengthscales, transform=tfp.bijectors.Sigmoid( low=lower, high=upper)) kernel.lengthscales.prior = tfp.distributions.Uniform(low=10.0, high=100.0) kernel.variance.prior = tfp.distributions.LogNormal(loc=np.float64(-2.0), scale=np.float64(1.0)) randomize_hyperparameters(kernel) npt.assert_array_less(kernel.lengthscales, [0.5] * dim) npt.assert_raises(AssertionError, npt.assert_allclose, [0.2] * dim, kernel.lengthscales)