Пример #1
0
def build_gp_model(data, x_std=1.0, y_std=0.1):

    dim = data.query_points.shape[-1]
    empirical_variance = tf.math.reduce_variance(data.observations)

    prior_lengthscales = [0.2 * x_std * np.sqrt(dim)] * dim
    prior_scale = tf.cast(1.0, dtype=tf.float64)

    x_std = tf.cast(x_std, dtype=tf.float64)
    y_std = tf.cast(y_std, dtype=tf.float64)

    kernel = gpflow.kernels.Matern52(
        variance=empirical_variance,
        lengthscales=prior_lengthscales,
    )
    kernel.variance.prior = tfp.distributions.LogNormal(
        tf.math.log(y_std), prior_scale)
    kernel.lengthscales.prior = tfp.distributions.LogNormal(
        tf.math.log(kernel.lengthscales), prior_scale)
    gpr = gpflow.models.GPR(
        data.astuple(),
        kernel,
        mean_function=gpflow.mean_functions.Constant(),
        noise_variance=1e-5,
    )
    gpflow.set_trainable(gpr.likelihood, False)

    return GaussianProcessRegression(
        model=gpr,
        optimizer=Optimizer(gpflow.optimizers.Scipy(),
                            minimize_args={"options": dict(maxiter=100)}),
        num_kernel_samples=100,
    )
Пример #2
0
def test_gpr_raises_for_invalid_num_kernel_samples() -> None:
    x_np = np.arange(5, dtype=np.float64).reshape(-1, 1)
    x = tf.convert_to_tensor(x_np, x_np.dtype)
    y = fnc_3x_plus_10(x)

    with pytest.raises(ValueError):
        GaussianProcessRegression(gpr_model(x, y), num_kernel_samples=-1)
 def build_model(data: Dataset) -> GaussianProcessRegression:
     variance = tf.math.reduce_variance(data.observations)
     kernel = gpflow.kernels.Matern52(variance,
                                      tf.constant([0.2, 0.2], tf.float64))
     gpr = gpflow.models.GPR((data.query_points, data.observations),
                             kernel,
                             noise_variance=1e-5)
     gpflow.utilities.set_trainable(gpr.likelihood, False)
     return GaussianProcessRegression(gpr)
Пример #4
0
 def build_model(data: Dataset) -> GaussianProcessRegression:
     variance = tf.math.reduce_variance(data.observations)
     kernel = gpflow.kernels.Matern52(variance, tf.constant([0.2, 0.2], tf.float64))
     scale = tf.constant(1.0, dtype=tf.float64)
     kernel.variance.prior = tfp.distributions.LogNormal(
         tf.constant(-2.0, dtype=tf.float64), scale
     )
     kernel.lengthscales.prior = tfp.distributions.LogNormal(
         tf.math.log(kernel.lengthscales), scale
     )
     gpr = gpflow.models.GPR((data.query_points, data.observations), kernel, noise_variance=1e-5)
     gpflow.utilities.set_trainable(gpr.likelihood, False)
     return GaussianProcessRegression(gpr)
Пример #5
0
    def build_stacked_independent_objectives_model(
            data: Dataset) -> ModelStack:
        gprs = []
        for idx in range(2):
            single_obj_data = Dataset(
                data.query_points, tf.gather(data.observations, [idx], axis=1))
            variance = tf.math.reduce_variance(single_obj_data.observations)
            kernel = gpflow.kernels.Matern52(
                variance, tf.constant([0.2, 0.2], tf.float64))
            gpr = gpflow.models.GPR(single_obj_data.astuple(),
                                    kernel,
                                    noise_variance=1e-5)
            gpflow.utilities.set_trainable(gpr.likelihood, False)
            gprs.append((GaussianProcessRegression(gpr), 1))

        return ModelStack(*gprs)
def build_model(data):
    variance = tf.math.reduce_variance(data.observations)
    kernel = gpflow.kernels.RBF(variance=variance)
    gpr = gpflow.models.GPR(data.astuple(), kernel, noise_variance=1e-5)
    gpflow.set_trainable(gpr.likelihood, False)
    return GaussianProcessRegression(gpr)
Пример #7
0
def test_sgpr_raises_for_covariance_between_points() -> None:
    data = mock_data()
    model = GaussianProcessRegression(sgpr_model(*data))

    with pytest.raises(NotImplementedError):
        model.covariance_between_points(data[0], data[0])