コード例 #1
0
    def test_eval_mode(self):
        lik = GaussianLikelihood()
        lik.noise = self.noise_var
        model = OnlineWoodburyGP(self.xs, self.labels, lik, self.kernel,
                                 self.mean_module)
        model.covar_module.base_kernel.lengthscale = self.lengthscale
        model.eval()
        pred_nn = model(self.test_points)
        pred = model.likelihood(pred_nn)

        r_lik = GaussianLikelihood()
        r_lik.noise = self.noise_var
        r_kernel = GridInterpolationKernelWithFantasy(RBFKernel(),
                                                      grid_size=self.grid_size,
                                                      grid_bounds=[(-4.0, 14.0)
                                                                   ]).double()
        r_model = RegularExactGP(self.xs, self.labels, r_lik, r_kernel,
                                 deepcopy(self.mean_module))
        r_model.covar_module.base_kernel.lengthscale = self.lengthscale
        r_model.eval()
        r_pred_nn = r_model(self.test_points)
        r_pred = r_model.likelihood(r_pred_nn)

        np.testing.assert_allclose(r_pred.mean.detach().numpy(),
                                   pred.mean.detach().numpy())

        np.testing.assert_allclose(
            r_pred.covariance_matrix.detach().numpy(),
            pred.covariance_matrix.detach().numpy(),
        )
コード例 #2
0
    def test_online_eval_mode(self):

        r_lik = GaussianLikelihood()
        r_lik.noise = 1.0
        # r_kernel = RBFKernel().double()
        r_kernel = GridInterpolationKernelWithFantasy(RBFKernel(),
                                                      grid_size=self.grid_size,
                                                      grid_bounds=[(-4.0, 14.0)
                                                                   ]).double()
        r_model = RegularExactGP(self.xs, self.labels, r_lik, r_kernel,
                                 deepcopy(self.mean_module))
        r_model.eval()
        r_model(self.new_points).mean
        r_model = r_model.get_fantasy_model(self.new_points, self.new_targets)

        r_test_output = r_lik(r_model(self.test_points))

        lik = GaussianLikelihood()
        lik.noise = 1.0
        model = OnlineWoodburyGP(self.xs, self.labels, lik, self.kernel,
                                 self.mean_module)
        model.eval()
        model(self.new_points).mean
        model = model.get_fantasy_model(self.new_points, self.new_targets)

        test_output = lik(model(self.test_points))

        np.testing.assert_allclose(r_test_output.mean.detach().numpy(),
                                   test_output.mean.detach().numpy())

        np.testing.assert_allclose(
            r_test_output.covariance_matrix.detach().numpy(),
            test_output.covariance_matrix.detach().numpy(),
        )
コード例 #3
0
    def setUp(self):
        self.xs = torch.tensor([0.20, 0.30, 0.40, 0.10, 0.70],
                               dtype=torch.double)
        self.kernel = GridInterpolationKernel(RBFKernel(),
                                              grid_size=4,
                                              grid_bounds=[(-0.4, 1.4)
                                                           ]).double()
        self.mean_vec = torch.sin(self.xs).double() * 0.0
        self.labels = torch.sin(self.xs) + torch.tensor(
            [0.1, 0.2, -0.1, -0.2, -0.2], dtype=torch.double)
        self.lik = GaussianLikelihood().double()
        self.lik.noise = 0.1
        self.train_train_covar = self.kernel(self.xs).evaluate_kernel()
        self.distr = MultivariateNormal(self.mean_vec, self.train_train_covar)

        e_distr = MultivariateNormal(self.mean_vec, self.train_train_covar)
        e_lik = GaussianLikelihood().double()
        e_lik.noise = 0.1
        self.expected_strategy = InterpolatedPredictionStrategyWithFantasy(
            self.xs, e_distr, self.labels, e_lik)
        self.strategy = ShermanMorrisonOnlineStrategy(self.xs, self.distr,
                                                      self.labels, self.lik)

        self.new_points = torch.tensor([0.5, 0.8], dtype=torch.double)
        self.test_mean = torch.sin(self.new_points) * 0.0
        self.test_train_covar = self.kernel(self.new_points,
                                            self.xs).evaluate_kernel()
        self.test_test_covar = self.kernel(self.new_points,
                                           self.new_points).evaluate_kernel()
コード例 #4
0
 def test_init(self):
     # mostly make sure it doesn't crash
     lik = GaussianLikelihood()
     lik.noise = 1.0
     model = OnlineWoodburyGP(self.xs, self.labels, lik, self.kernel,
                              self.mean_module)