コード例 #1
0
    def test_neighbor_erasing_dataset_2(self):
        x = np.linspace(0, 1, 100, dtype=np.float32).reshape((-1, 1))
        y = np.exp(-x**2).reshape(-1)

        r = 0.0125

        gp = MaternGP(x,
                      y,
                      noise_constraint=(0, 1e-3),
                      dataset_type='neighborerasing',
                      dataset_params={'radius': r})
        x_ = np.linspace(0, 1, 20, dtype=np.float32).reshape((-1, 1))
        y_ = np.exp(-x_**2).reshape(-1)
        forgettable = [False] * len(y_)

        gp.append_data(x_, y_, forgettable=forgettable)

        x__ = np.linspace(0, 1, 21, dtype=np.float32).reshape((-1, 1))
        y__ = np.exp(-x_**2).reshape(-1)

        gp.append_data(x__, y__)

        all_present_x_ = all(
            x_ex.tolist() in gp.train_x.cpu().numpy().tolist() for x_ex in x_)
        all_present_x__ = all(
            x__ex.tolist() in gp.train_x.cpu().numpy().tolist()
            for x__ex in x__)

        self.assertTrue(all_present_x_ and all_present_x__)
コード例 #2
0
    def test_neighbor_erasing_dataset_1(self):
        x = np.linspace(0, 1, 100, dtype=np.float32).reshape((-1, 1))
        y = np.exp(-x**2).reshape(-1)

        r = 0.0125

        gp = MaternGP(x,
                      y,
                      noise_constraint=(0, 1e-3),
                      dataset_type='neighborerasing',
                      dataset_params={'radius': r})
        x_ = np.linspace(0, 1, 20, dtype=np.float32).reshape((-1, 1))
        y_ = np.exp(-x_**2).reshape(-1)

        gp.append_data(x_, y_)
        distances = np.abs(gp.train_x.cpu().numpy().reshape((-1, 1)) -
                           x_.squeeze())

        self.assertTrue(np.all(distances[:-len(x_), :] >= r))
        self.assertTrue((gp.train_x.cpu().numpy()[-len(x_):, :] == x_).all())
        # Set to True to plot
        if False:
            import matplotlib.pyplot as plt
            plt.figure()
            plt.scatter(
                gp.train_x.cpu().numpy()[-len(x_):],
                gp.train_x.cpu().numpy()[-len(x_):],
                color='r',
            )
            plt.scatter(gp.train_x.cpu().numpy()[:-len(x_)],
                        gp.train_x.cpu().numpy()[:-len(x_)],
                        color='b')
コード例 #3
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))
コード例 #4
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())