Ejemplo n.º 1
0
    def test_data_manipulation(self):
        tol = 1e-1
        x = np.linspace(0, 1, 101).reshape((-1, 1))
        y = np.exp(-x**2).reshape(-1)
        x_ = np.linspace(1.5, 2, 51).reshape((-1, 1))
        y_ = 1 + np.exp(-x_**2).reshape(-1)

        gp = MaternGP(x, y, noise_prior=(0.1, 0.1))

        tmp = gp.empty_data()
        self.assertEqual(tmp, gp)
        self.assertTrue(tuple(gp.train_x.shape), (0, 1))
        # GPyTorch fails when predicting with an empty dataset, so the following line fails if uncommented
        # gp.predict(x)

        gp.set_data(x, y)
        self.assertEqual(tuple(gp.train_x.shape), (len(x), 1))

        gp.optimize_hyperparameters(epochs=10)
        gp_pred = gp.predict(x_).mean.cpu().numpy()
        self.assertFalse(np.all(np.abs(gp_pred - y_) < tol))

        tmp = gp.append_data(x_, y_)
        # self.assertTrue(gp != tmp)
        # self.assertEqual(tuple(gp.train_x.shape), (len(x), 1))
        self.assertEqual(tuple(tmp.train_x.shape), (len(x) + len(x_), 1))

        tmp.optimize_hyperparameters(epochs=10)
        tmp_pred = tmp.predict(x_).mean.cpu().numpy()
        self.assertTrue(np.all(np.abs(tmp_pred - y_) < tol))
Ejemplo n.º 2
0
    def test_timeforgetting_dataset(self):
        x = np.linspace(0, 1, 100, dtype=np.float32).reshape((-1, 1))
        y = np.exp(-x**2).reshape(-1)

        gp = MaternGP(x,
                      y,
                      noise_constraint=(0, 1e-3),
                      dataset_type='timeforgetting',
                      dataset_params={'keep': 50})
        self.assertTrue((gp.train_x.cpu().numpy() == x[-50:]).all())
        self.assertTrue((gp.train_y.cpu().numpy() == y[-50:]).all())
        gp.append_data(x[:10], y[:10])
        self.assertTrue((gp.train_x.cpu().numpy() == np.vstack(
            (x[-40:], x[:10]))).all())
        self.assertTrue((gp.train_y.cpu().numpy() == np.hstack(
            (y[-40:], y[:10]))).all())
        gp.set_data(x[:75], y[:75])
        self.assertTrue((gp.train_x.cpu().numpy() == x[25:75]).all())
        self.assertTrue((gp.train_y.cpu().numpy() == y[25:75]).all())