Beispiel #1
0
    def test_construction(self):
        with self.test_context():
            gpflow.ParamList([])
            gpflow.ParamList([gpflow.Param(1)])
            gpflow.ParamList([1.0, np.array([1, 2]), gpflow.Param(1.0)])
            with self.assertRaises(ValueError):
                gpflow.ParamList([gpflow.Param(1), 'stringsnotallowed'])
            with self.assertRaises(ValueError):
                # tuples not valid in constuctor:
                gpflow.ParamList((gpflow.Param(1), ))
            with self.assertRaises(ValueError):
                # param objects not valid in constructor (must be in list)
                gpflow.ParamList(gpflow.Param(1))

            with gpflow.defer_build():
                p = gpflow.ParamList([0.0])
                p[0] = gpflow.Param(1.0)
                with self.assertRaises(ValueError):
                    p[0] = 1.0
                with self.assertRaises(ValueError):
                    p[0] = "test"

            p = gpflow.ParamList([])
            p.append(gpflow.Param(1.0))
            p.append(gpflow.Param(2.0))
            p.append(2.0)
            self.assertEqual(len(p), 3)
            with self.assertRaises(ValueError):
                p.append("test")
Beispiel #2
0
    def test_setitem(self):
        with self.test_context():
            p1 = gpflow.Param(1.2)
            p2 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
            param_list = gpflow.ParamList([p1, p2],
                                          name='param_list',
                                          autobuild=False)

            self.assertEqual(p1.read_value(), param_list[0].read_value())
            self.assertTrue(
                np.all(param_list[1].read_value() == p2.read_value()))

            param_list[0] = gpflow.Param(2.0)
            self.assertEqual(p1.read_value(), 1.2)
            self.assertEqual(p1.root, p1)
            self.assertEqual(param_list[0].read_value(), 2.0)

            arr = np.array([1.1, 2.2], settings.float_type)
            param_list[1] = gpflow.Param(arr)
            self.assertEqual(p2.root, p2)
            self.assertTrue(np.all(param_list[1].read_value() == arr))

            param_list.compile()
            with self.assertRaises(GPflowError):
                param_list[0] = gpflow.Param(12)
Beispiel #3
0
 def test_naming(self):
     with self.test_context():
         p1 = gpflow.Param(1.2)
         p2 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
         l = gpflow.ParamList([p1, p2])
         assert p1.pathname == l.name + '/0'
         assert p2.pathname == l.name + '/1'
Beispiel #4
0
 def test_naming(self):
     with self.test_context():
         p1 = gpflow.Param(1.2)
         p2 = gpflow.Param(np.array([3.4, 5.6], settings.np_float))
         gpflow.ParamList([p1, p2])
         self.assertEqual(p1.name, 'item0')
         self.assertEqual(p2.name, 'item1')
Beispiel #5
0
    def __init__(self, X, Y, ms, a, b, kerns):
        for kern in kerns:
            assert isinstance(
                kern, (gpflow.kernels.Matern12, gpflow.kernels.Matern32,
                       gpflow.kernels.Matern52))
        likelihood = gpflow.likelihoods.Gaussian()
        mean_function = gpflow.mean_functions.Zero()
        gpflow.models.GPModel.__init__(self, X, Y, None, likelihood,
                                       mean_function)
        self.kerns = gpflow.ParamList(kerns)
        self.num_data = X.shape[0]
        self.num_latent = Y.shape[1]
        self.a = a
        self.b = b
        self.ms = ms

        # count the inducing variables:
        self.Ms = []
        for kern in kerns:
            Ncos_d = self.ms.size
            Nsin_d = self.ms.size - 1
            self.Ms.append(Ncos_d + Nsin_d)

        assert np.all(X > a)
        assert np.all(X < b)

        # pre compute static quantities
        Kuf = [
            make_Kuf_np(X[:, i:i + 1], a, b, self.ms)
            for i, (a, b) in enumerate(zip(self.a, self.b))
        ]
        self.Kuf = make_kvs_np(Kuf)
        self.KufY = np.dot(self.Kuf, Y)
        self.KufKfu = np.dot(self.Kuf, self.Kuf.T)
        self.tr_YTY = np.sum(np.square(Y))
Beispiel #6
0
 def test_len(self):
     with self.test_context():
         p1 = gpflow.Param(1.2)
         p2 = gpflow.Param(np.array([3.4, 5.6], settings.np_float))
         l = gpflow.ParamList([p1])
         l.append(p2)
         self.assertTrue(len(l) == 2)
Beispiel #7
0
 def test_with_parameterized(self):
     with self.test_context():
         pzd = gpflow.params.Parameterized()
         p = gpflow.Param(1.2)
         pzd.p = p
         param_list = gpflow.ParamList([pzd])
         param_list[0].p = 5.
         self.assertEqual(param_list[0].p.read_value(), 5)
Beispiel #8
0
 def test_append(self):
     with self.test_context():
         p1 = gpflow.Param(1.2)
         p2 = gpflow.Param(np.array([3.4, 5.6], settings.np_float))
         param_list = gpflow.ParamList([p1])
         param_list.append(p2)
         self.assertTrue(p2 in param_list.params)
         with self.assertRaises(ValueError):
             param_list.append('foo')
Beispiel #9
0
Datei: vgp.py Projekt: zcmail/VFF
    def __init__(self, X, Y, ms, a, b, kerns, likelihood):
        """
        Here we assume the interval is [a,b]
        """
        assert a.size == b.size == len(kerns) == X.shape[1]
        for kern in kerns:
            assert isinstance(
                kern, (gpflow.kernels.Matern12, gpflow.kernels.Matern32,
                       gpflow.kernels.Matern52))
        mf = gpflow.mean_functions.Zero()
        gpflow.models.GPModel.__init__(self,
                                       X,
                                       Y,
                                       kern=None,
                                       likelihood=likelihood,
                                       mean_function=mf)
        self.num_latent = 1  # multiple columns not supported in this version
        self.a = a
        self.b = b
        self.ms = ms
        self.input_dim = X.shape[1]

        # initialize variational parameters
        Ms = []
        for kern in kerns:
            Ncos_d = self.ms.size
            Nsin_d = self.ms.size - 1
            if isinstance(kern, gpflow.kernels.Matern12):
                Ncos_d += 1
            elif isinstance(kern, gpflow.kernels.Matern32):
                Ncos_d += 1
                Nsin_d += 1
            else:
                raise NotImplementedError
            Ms.append(Ncos_d + Nsin_d)

        self.kerns = gpflow.ParamList(kerns)

        self.q_mu = gpflow.Param(np.zeros((np.sum(Ms), 1)))
        pos = gpflow.transforms.positive
        self.q_sqrt = gpflow.ParamList(
            [gpflow.Param(np.ones(M), pos) for M in Ms])
Beispiel #10
0
Datei: vgp.py Projekt: zcmail/VFF
    def __init__(self, X, Y, ms, a, b, kerns, likelihood):
        """
        Here we assume the interval is [a,b]
        We do *not* assume that the variance of q(u) has a kronecker structure.

        This can get very computationally heavy very quickly, use with caution!.
        """
        assert a.size == b.size == len(kerns) == X.shape[1]
        for kern in kerns:
            assert isinstance(
                kern, (gpflow.kernels.Matern12, gpflow.kernels.Matern32,
                       gpflow.kernels.Matern52))
        mf = gpflow.mean_functions.Zero()
        gpflow.models.GPModel.__init__(self,
                                       X,
                                       Y,
                                       kern=None,
                                       likelihood=likelihood,
                                       mean_function=mf)
        self.num_latent = 1  # multiple columns not supported in this version
        self.a = a
        self.b = b
        self.ms = ms

        # initialize variational parameters
        Ms = []
        for kern in kerns:
            Ncos_d = self.ms.size
            Nsin_d = self.ms.size - 1
            Ms.append(Ncos_d + Nsin_d)
        self.Ms = Ms

        self.kerns = gpflow.ParamList(kerns)

        self.q_mu = gpflow.Param(np.zeros((np.prod(Ms), 1)))

        # The covariance matrix gets very big very quickly
        self.q_sqrt = gpflow.Param(np.eye(np.prod(Ms)))

        # pre-compute the Kuf matrices
        self._Kuf = [
            tf.constant(make_Kuf_np(X[:, i:i + 1], ai, bi, self.ms))
            for i, (ai, bi) in enumerate(zip(self.a, self.b))
        ]
Beispiel #11
0
 def test_construction(self):
     with self.test_context():
         gpflow.ParamList([])
         gpflow.ParamList([gpflow.Param(1)])
         gpflow.ParamList([1.0, np.array([1, 2]), gpflow.Param(1.0)])
         with self.assertRaises(ValueError):
             gpflow.ParamList([gpflow.Param(1), 'stringsnotallowed'])
         with self.assertRaises(ValueError):
             # tuples not valid in constuctor:
             gpflow.ParamList((gpflow.Param(1), ))
         with self.assertRaises(ValueError):
             # param objects not valid in constructor (must be in list)
             gpflow.ParamList(gpflow.Param(1))
Beispiel #12
0
 def test_append(self):
     with self.test_context():
         p1 = gpflow.Param(1.2)
         p4 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
         with gpflow.defer_build():
             p2 = gpflow.Param(1.2)
             param_list = gpflow.ParamList([p1])
             param_list.append(p2)
         p3 = gpflow.Param(1.2)
         param_list.append(p3)
         param_list.compile()
         with self.assertRaises(gpflow.GPflowError):
             param_list.append(p4)
         self.assertTrue(p1 in param_list.params)
         self.assertTrue(p2 in param_list.params)
         self.assertTrue(p3 in param_list.params)
         self.assertFalse(p4 in param_list.params)
         with self.assertRaises(ValueError):
             param_list.append('foo')
Beispiel #13
0
    def __init__(self, X, Y, ms, a, b, kern_list):
        assert X.shape[1] == len(kern_list)
        assert a.size == len(kern_list)
        assert b.size == len(kern_list)
        for kern in kern_list:
            assert isinstance(
                kern, (gpflow.kernels.Matern12, gpflow.kernels.Matern32,
                       gpflow.kernels.Matern52))
        likelihood = gpflow.likelihoods.Gaussian()
        mean_function = gpflow.mean_functions.Zero()
        gpflow.models.GPModel.__init__(self, X, Y, None, likelihood,
                                       mean_function)
        self.num_data = X.shape[0]
        self.num_latent = Y.shape[1]
        self.a = a
        self.b = b
        self.ms = ms

        self.kerns = gpflow.ParamList(kern_list)

        # pre compute static quantities: chunk data to save memory
        self.tr_YTY = gpflow.DataHolder(np.sum(np.square(Y)))
        Mtotal = (2 * self.ms.size - 1) * X.shape[1]
        self.KufY = np.zeros((Mtotal, 1))
        self.KufKfu = np.zeros((Mtotal, Mtotal))
        for i in range(0, (X.shape[0]), 10000):
            Xchunk = X[i:i + 10000]
            Ychunk = Y[i:i + 10000]
            Kuf_chunk = np.empty((0, Xchunk.shape[0]))
            KufY_chunk = np.empty((0, Ychunk.shape[1]))
            for i, (ai, bi) in enumerate(zip(self.a, self.b)):
                assert np.all(Xchunk[:, i] > ai)
                assert np.all(Xchunk[:, i] < bi)
                Kuf = make_Kuf_np(Xchunk[:, i:i + 1], ai, bi, self.ms)
                KufY_chunk = np.vstack((KufY_chunk, np.dot(Kuf, Ychunk)))
                Kuf_chunk = np.vstack((Kuf_chunk, Kuf))
            self.KufKfu += np.dot(Kuf_chunk, Kuf_chunk.T)
            self.KufY += KufY_chunk
        self.KufY = gpflow.DataHolder(self.KufY)
        self.KufKfu = gpflow.DataHolder(self.KufKfu)
Beispiel #14
0
 def test_len(self):
     with self.test_context():
         p1 = gpflow.Param(1.2)
         p2 = gpflow.Param(np.array([3.4, 5.6], settings.float_type))
         l = gpflow.ParamList([p1, p2])
         self.assertTrue(len(l) == 2)
Beispiel #15
0
    def __init__(self,
                 latent_dim,
                 Y,
                 transitions,
                 T_latent=None,
                 inputs=None,
                 emissions=None,
                 px1_mu=None,
                 px1_cov=None,
                 Xmu=None,
                 Xchol=None,
                 n_samples=100,
                 batch_size=None,
                 seed=None,
                 name=None):

        super().__init__(latent_dim,
                         Y[0],
                         transitions,
                         T_latent=None,
                         inputs=None,
                         emissions=emissions,
                         px1_mu=px1_mu,
                         px1_cov=None,
                         Xmu=None,
                         Xchol=None,
                         n_samples=n_samples,
                         seed=seed,
                         name=name)

        self.T = [Y_s.shape[0] for Y_s in Y]
        self.T_latent = T_latent or self.T
        self.n_seq = len(self.T)
        self.T_tf = tf.constant(self.T, dtype=gp.settings.int_type)
        self.T_latent_tf = tf.constant(self.T_latent,
                                       dtype=gp.settings.int_type)
        self.sum_T = float(sum(self.T))
        self.sum_T_latent = float(sum(self.T_latent))
        self.batch_size = batch_size

        self.Y = gp.ParamList(Y, trainable=False)

        self.inputs = None if inputs is None else gp.ParamList(inputs,
                                                               trainable=False)

        _Xmu = [np.zeros((T_s, self.latent_dim))
                for T_s in self.T_latent] if Xmu is None else Xmu
        self.X = gp.ParamList(_Xmu)

        _Xchol = [np.eye(T_s * self.latent_dim)
                  for T_s in self.T_latent] if Xchol is None else Xchol
        xc_tr = lambda xc: None if xc.ndim == 1 else gtf.LowerTriangular(
            xc.shape[-1],
            num_matrices=1 if xc.ndim == 2 else xc.shape[0],
            squeeze=xc.ndim == 2)
        self.Xchol = gp.ParamList(
            [gp.Param(xc, transform=xc_tr(xc)) for xc in _Xchol])

        self.multi_diag_px1_cov = False
        if isinstance(px1_cov, list):  # different prior for each sequence
            _x1_cov = np.stack(px1_cov)
            _x1_cov = np.sqrt(
                _x1_cov) if _x1_cov.ndim == 2 else np.linalg.cholesky(_x1_cov)
            _transform = None if _x1_cov.ndim == 2 else gtf.LowerTriangular(
                self.latent_dim, num_matrices=self.n_seq)
            self.multi_diag_px1_cov = _x1_cov.ndim == 2
        elif isinstance(px1_cov, np.ndarray):  # same prior for each sequence
            assert px1_cov.ndim < 3
            _x1_cov = np.sqrt(
                px1_cov) if px1_cov.ndim == 1 else np.linalg.cholesky(px1_cov)
            _transform = None if px1_cov.ndim == 1 else gtf.LowerTriangular(
                self.latent_dim, squeeze=True)
        else:
            _x1_cov = np.eye(self.latent_dim)
            _transform = gtf.LowerTriangular(self.latent_dim, squeeze=True)

        self.px1_cov_chol = gp.Param(_x1_cov,
                                     trainable=False,
                                     transform=_transform)
Beispiel #16
0
 def __init__(self):
     gpflow.models.Model.__init__(self)
     self.param_list = gpflow.ParamList(
         [gpflow.Param(1.), gpflow.Param(12.)])
Beispiel #17
0
Datei: vgp.py Projekt: zcmail/VFF
    def __init__(self,
                 X,
                 Y,
                 ms,
                 a,
                 b,
                 kerns,
                 likelihood,
                 use_two_krons=False,
                 use_extra_ranks=0):
        """
        Here we assume the interval is [a,b]
        """
        assert a.size == b.size == len(kerns) == X.shape[1]
        for kern in kerns:
            assert isinstance(
                kern, (gpflow.kernels.Matern12, gpflow.kernels.Matern32,
                       gpflow.kernels.Matern52))
        mf = gpflow.mean_functions.Zero()
        gpflow.models.GPModel.__init__(self,
                                       X,
                                       Y,
                                       kern=None,
                                       likelihood=likelihood,
                                       mean_function=mf)
        self.num_latent = 1  # multiple columns not supported in this version
        self.a = a
        self.b = b
        self.ms = ms

        self.kerns = gpflow.ParamList(kerns)

        # initialize variational parameters
        self.Ms = []
        for kern in kerns:
            Ncos_d = self.ms.size
            Nsin_d = self.ms.size - 1
            self.Ms.append(Ncos_d + Nsin_d)

        self.q_mu = gpflow.Param(np.zeros((np.prod(self.Ms), 1)))

        # The covariance matrix
        self.q_sqrt_kron = gpflow.ParamList(
            [gpflow.Param(np.eye(M)) for M in self.Ms])
        self.use_two_krons = use_two_krons
        self.use_extra_ranks = use_extra_ranks
        assert not (use_extra_ranks and use_two_krons
                    ), "can only use one extra covariance structure at a time!"
        if use_two_krons:
            # same as above, but with different init to break symmetry
            self.q_sqrt_kron_2 = gpflow.ParamList(
                [gpflow.Param(np.eye(M) + 0.01) for M in self.Ms])
        elif use_extra_ranks:
            self.q_sqrt_W = gpflow.Param(
                np.zeros((np.prod(self.Ms), use_extra_ranks)))

        # pre-compute Kuf
        self._Kuf = [
            make_Kuf_np(X[:, i:i + 1], ai, bi, self.ms)
            for i, (ai, bi) in enumerate(zip(self.a, self.b))
        ]