示例#1
0
    def test_kernel_learning_NN(self):

        for learning_mode in ['learn_kernel', 'both']:

            gpr_model_vanilla = GPRegressionLearned(
                self.x_train,
                self.y_train_sin,
                learning_mode='learn_kernel',
                num_iter_fit=1,
                mean_module='zero',
                covar_module='NN')
            gpr_model_vanilla.fit()

            gpr_model_learn_kernel = GPRegressionLearned(
                self.x_train,
                self.y_train_sin,
                learning_mode=learning_mode,
                num_iter_fit=500,
                mean_module='constant',
                covar_module='NN',
                kernel_nn_layers=(16, 16),
                mean_nn_layers=(16, 16))
            gpr_model_learn_kernel.fit(valid_x=self.x_train,
                                       valid_t=self.y_train_sin)

            ll_vanilla, rmse_vanilla, _ = gpr_model_vanilla.eval(
                self.x_train, self.y_train_sin)
            ll_kernel, rmse_kernel, _ = gpr_model_learn_kernel.eval(
                self.x_train, self.y_train_sin)

            print('learning_mode', learning_mode)
            print(ll_kernel, ll_vanilla)
            print(rmse_kernel, rmse_vanilla)
            self.assertGreater(ll_kernel, ll_vanilla)
            self.assertLess(rmse_kernel, rmse_vanilla)
示例#2
0
    def test_mean_learning(self):
        for mean_module in ['NN']:

            gpr_model_vanilla = GPRegressionLearned(self.x_train,
                                                    self.y_train_sin,
                                                    learning_mode='vanilla',
                                                    num_iter_fit=20,
                                                    mean_module='constant',
                                                    covar_module='SE')
            gpr_model_vanilla.fit()

            gpr_model_learn_mean = GPRegressionLearned(
                self.x_train,
                self.y_train_sin,
                learning_mode='learn_mean',
                num_iter_fit=100,
                mean_module=mean_module,
                covar_module='SE',
                mean_nn_layers=(16, 16))
            gpr_model_learn_mean.fit()

            ll_vanilla, rmse_vanilla, _ = gpr_model_vanilla.eval(
                self.x_train, self.y_train_two)
            ll_mean, rmse_mean, _ = gpr_model_learn_mean.eval(
                self.x_train, self.y_train_sin)

            print(ll_mean, ll_vanilla)
            print(rmse_mean, rmse_vanilla)
            self.assertGreater(ll_mean, ll_vanilla)
            self.assertLess(rmse_mean, rmse_vanilla)
示例#3
0
 def fit_eval(x_context, y_context, x_test, y_test, params):
     from meta_learn.GPR_mll import GPRegressionLearned
     torch.set_num_threads(1)
     model = GPRegressionLearned(x_context,
                                 y_context,
                                 **params,
                                 random_seed=seed)
     model.fit(verbose=False)
     return model.eval(x_test, y_test)
示例#4
0
        def fit_eval_gpr(x_context, t_context, x_test, t_test):
            gpr = GPRegressionLearned(x_context,
                                      t_context,
                                      learning_mode='both',
                                      mean_nn_layers=(64, 64),
                                      covar_module='SE',
                                      mean_module='NN',
                                      weight_decay=0.0,
                                      num_iter_fit=num_iter_fit)
            gpr.fit(valid_x=x_test, valid_t=t_test)

            return gpr.eval(x_test, t_test)[0]
示例#5
0
    def test_random_seed_consistency(self):
        gpr_model_1 = GPRegressionLearned(self.x_train,
                                          self.y_train_two,
                                          learning_mode='both',
                                          num_iter_fit=5,
                                          mean_module='NN',
                                          covar_module='NN',
                                          random_seed=22)

        gpr_model_2 = GPRegressionLearned(self.x_train,
                                          self.y_train_two,
                                          learning_mode='both',
                                          num_iter_fit=5,
                                          mean_module='NN',
                                          covar_module='NN',
                                          random_seed=22)

        gpr_model_1.fit()
        t_predict_1 = gpr_model_1.predict(self.x_test)

        gpr_model_2.fit()
        t_predict_2 = gpr_model_2.predict(self.x_test)

        self.assertTrue(np.array_equal(t_predict_1, t_predict_2))
示例#6
0
    def test_kernel_learning_COS(self):

        for learning_mode in ['learn_kernel', 'both']:

            gpr_model_vanilla = GPRegressionLearned(
                self.x_train,
                self.y_train_sin,
                learning_mode='vanilla',
                num_iter_fit=1,
                mean_module='constant',
                covar_module=CosineKernel())
            gpr_model_vanilla.fit()

            gpr_model_learn_kernel = GPRegressionLearned(
                self.x_train,
                self.y_train_sin,
                learning_mode='learn_kernel',
                num_iter_fit=500,
                mean_module='constant',
                covar_module=CosineKernel())

            print(gpr_model_learn_kernel.model.covar_module.lengthscale)
            gpr_model_learn_kernel.fit(valid_x=self.x_train,
                                       valid_t=self.y_train_sin)
            print(gpr_model_learn_kernel.model.covar_module.lengthscale)

            ll_vanilla, rmse_vanilla, _ = gpr_model_vanilla.eval(
                self.x_train, self.y_train_sin)
            ll_kernel, rmse_kernel, _ = gpr_model_learn_kernel.eval(
                self.x_train, self.y_train_sin)

            print('learning_mode', learning_mode)
            print(ll_kernel, ll_vanilla)
            print(rmse_kernel, rmse_vanilla)
            self.assertGreater(ll_kernel, ll_vanilla)
            self.assertLess(rmse_kernel, rmse_vanilla)
示例#7
0
    def test_serializable(self):
        torch.manual_seed(40)
        np.random.seed(22)
        import itertools
        # check that more datasets improve performance
        for mean_module, covar_module in itertools.product(['constant', 'NN'],
                                                           ['SE', 'NN']):

            gpr_model = GPRegressionLearned(self.x_train,
                                            self.y_train_two,
                                            learning_mode='both',
                                            num_iter_fit=10,
                                            mean_module=mean_module,
                                            covar_module='NN',
                                            random_seed=22)
            gpr_model.fit()
            pred_1 = gpr_model.predict(self.x_train)

            gpr_model2 = GPRegressionLearned(self.x_train,
                                             self.y_train_two,
                                             learning_mode='both',
                                             num_iter_fit=1,
                                             mean_module=mean_module,
                                             covar_module='NN',
                                             random_seed=345)
            gpr_model2.fit()
            pred_2 = gpr_model2.predict(self.x_train)

            file = ('/tmp/test_torch_serialization.pkl')
            torch.save(gpr_model.state_dict(), file)
            gpr_model2.load_state_dict(torch.load(file))
            pred_3 = gpr_model2.predict(self.x_train)
            assert not np.array_equal(pred_1, pred_2)
            assert np.array_equal(pred_1, pred_3)