def test_random_feature_posterior_approximation(self):
        """Tests random feature GP's ability in approximating exact GP posterior."""
        # Set momentum = 0.5 so posterior precision matrix is 0.5 * (I + K).
        gp_cov_momentum = 0.5
        gp_cov_ridge_penalty = 1.
        num_inducing = 1024

        rfgp_model = gaussian_process.RandomFeatureGaussianProcess(
            units=1,
            num_inducing=num_inducing,
            normalize_input=False,
            gp_kernel_type='gaussian',
            gp_cov_momentum=gp_cov_momentum,
            gp_cov_ridge_penalty=gp_cov_ridge_penalty)

        # Computes posterior covariance on test data.
        _, _ = rfgp_model(self.x_tr, training=True)
        _, gp_cov_ts = rfgp_model(self.x_ts, training=False)

        # Scale up covariance estimate since prec matrix is down-scaled by momentum.
        post_kernel_computed = gp_cov_ts * gp_cov_momentum
        post_kernel_expected = _compute_posterior_kernel(
            self.x_tr, self.x_ts, self.rbf_kern_func, gp_cov_ridge_penalty)
        np.testing.assert_allclose(post_kernel_computed, post_kernel_expected,
                                   **self.cov_tolerance)
Beispiel #2
0
  def test_no_matrix_update_during_test(self):
    """Tests if the precision matrix is not updated during testing."""
    rfgp_model = gaussian_process.RandomFeatureGaussianProcess(units=1)

    # Training.
    _, gp_covmat_null = rfgp_model(self.x_tr, training=True)
    precision_mat_before_test = rfgp_model._gp_cov_layer.precision_matrix

    # Testing.
    _ = rfgp_model(self.x_ts, training=False)
    precision_mat_after_test = rfgp_model._gp_cov_layer.precision_matrix

    self.assertAllClose(
        gp_covmat_null, tf.eye(self.num_train_sample), atol=1e-4)
    self.assertAllClose(
        precision_mat_before_test, precision_mat_after_test, atol=1e-4)
    def test_random_feature_prior_approximation(self):
        """Tests random feature GP's ability in approximating exact GP prior."""
        num_inducing = 10240
        rfgp_model = gaussian_process.RandomFeatureGaussianProcess(
            units=1,
            num_inducing=num_inducing,
            normalize_input=False,
            gp_kernel_type='gaussian',
            return_random_features=True)

        # Extract random features.
        _, _, gp_feature = rfgp_model(self.x_tr, training=True)
        gp_feature_np = gp_feature.numpy()

        prior_kernel_computed = gp_feature_np.dot(gp_feature_np.T)
        prior_kernel_expected = self.rbf_kern_func(self.x_tr, self.x_tr)
        np.testing.assert_allclose(prior_kernel_computed,
                                   prior_kernel_expected, **self.cov_tolerance)
    def test_state_saving_and_loading(self):
        """Tests if the loaded model returns same results."""
        input_data = np.random.random((1, 2))
        rfgp_model = gaussian_process.RandomFeatureGaussianProcess(units=1)

        inputs = tf.keras.Input((2, ), batch_size=1)
        outputs = rfgp_model(inputs)
        model = tf.keras.Model(inputs, outputs)
        gp_output, gp_covmat = model.predict(input_data)

        # Save and then load the model.
        temp_dir = self.get_temp_dir()
        self.addCleanup(shutil.rmtree, temp_dir)
        saved_model_dir = os.path.join(temp_dir, 'rfgp_model')
        model.save(saved_model_dir)
        new_model = tf.keras.models.load_model(saved_model_dir)

        gp_output_new, gp_covmat_new = new_model.predict(input_data)
        self.assertAllClose(gp_output, gp_output_new, atol=1e-4)
        self.assertAllClose(gp_covmat, gp_covmat_new, atol=1e-4)