예제 #1
0
 def __init__(self,
              X,
              Y,
              Z,
              kernels,
              likelihood,
              mu_M=None,
              S_M=None,
              whitened_prior=False,
              initialized_Zs=False,
              arrow=True,
              stripes=True,
              **kwargs):
     all_Zs, all_mfs = init_linear(X,
                                   Z,
                                   kernels,
                                   initialized_Zs=initialized_Zs)
     layers = Fast_Stripes_Arrow_Layers(X,
                                        Y,
                                        Z,
                                        kernels,
                                        all_mfs,
                                        all_Zs,
                                        mu_M=mu_M,
                                        S_M=S_M,
                                        whitened_prior=whitened_prior,
                                        arrow=arrow,
                                        stripes=stripes)
     Full_DGP_Base.__init__(self, X, Y, likelihood, layers, **kwargs)
예제 #2
0
 def __init__(self,
              X,
              Y,
              Z,
              kernels,
              likelihood,
              mu_M=None,
              S_M=None,
              whitened_prior=False,
              initialized_Zs=False,
              **kwargs):
     """
     Inputs exactly as for Full_DGP
     """
     #mean functions need to be initialized first,
     all_Zs, all_mfs = init_linear(X,
                                   Z,
                                   kernels,
                                   initialized_Zs=initialized_Zs)
     layers = Fully_Coupled_Sampling_Layers(X,
                                            Y,
                                            Z,
                                            kernels,
                                            all_mfs,
                                            all_Zs,
                                            mu_M=mu_M,
                                            S_M=S_M,
                                            whitened_prior=whitened_prior)
     Full_DGP_Base.__init__(self, X, Y, likelihood, layers, **kwargs)
예제 #3
0
    def myKL2(self):
        X = np.array([[1., 2., 3.], [1., 2.1, 3.], [1.1, 2., 3.],
                      [1., 2., 3.1]])
        Y = np.array([[1.], [2.], [.2], [3.]])
        Z = np.array([[1., 2., 3.], [1.3, 2.2, 3.1]])
        A = np.tril(np.random.rand(6, 6))  #"cholesky" of S_M
        B = np.random.rand(6, 1)  #mu_M
        all_kernels = [
            kernels.RBF(3),
            kernels.RBF(2, lengthscales=3., variance=2.)
        ]
        all_Zs, all_mfs = init_linear(X, Z, all_kernels)
        mylayers = Fully_Coupled_Layers(X,
                                        Y,
                                        Z,
                                        all_kernels,
                                        all_mfs,
                                        all_Zs,
                                        mu_M=B,
                                        S_M=A)
        kl = mylayers.KL()
        session = get_default_session()
        kl = session.run(kl)

        Kmm1 = all_kernels[0].compute_K_symm(
            all_Zs[0]) + np.eye(Z.shape[0]) * settings.jitter
        Kmm2 = all_kernels[1].compute_K_symm(
            all_Zs[1]) + np.eye(all_Zs[1].shape[0]) * settings.jitter
        K_big = scipy.linalg.block_diag(Kmm1, Kmm1, Kmm2)
        tfKL = gauss_kl(tf.constant(B),
                        tf.constant(A[np.newaxis]),
                        K=tf.constant(K_big))

        sess = tf.Session()
        return kl, sess.run(tfKL)
 def full_SM2(self):
     def K(X1, X2):
         
         X1, X2 = np.array(X1), np.array(X2)
         if X1.ndim == 1:
             X1 = np.array([X1])
         if X2.ndim == 1:
             X2 = np.array([X2])
         
         if np.size(X1,0) != np.size(X2,0):
             print("Sizes of input matrices don't match")
             sys.exit()
         
         m1, m2 = np.size(X1,1), np.size(X2,1)
             
         matK = np.zeros((m1,m2))
         
         for i in range(m1):
             for j in range(m2):
                 matK[i, j] = np.exp(-1/2 * (X1[:,i] - X2[:,j]).dot(X1[:,i] - X2[:,j]))
                     
         return matK
     
     
     X = np.array([[1.,2.,3.],[1.,2.1,3.],[1.1,2.,3.],[1.,2.,3.1]])
     Y = np.array([[1.],[2.],[.2],[3.]])
     Z = np.array([[1.,2.,3.],[1.3,2.2,3.1]])
     z=np.array([[0.1,0.5],[-0.3,0.2],[1.,-1.3],[2.,0.]])
     kernels = [gpflow.kernels.RBF(3),gpflow.kernels.RBF(2)]
     sm_sqrt = np.array([[1.,0.,0.,0.,0.,0.],[0.5,1.,0.,0.,0.,0.],[0.1,0.3,1.,0.,0.,0.],[0.7,0.4,0.95,1.,0.,0.],
                    [0.8,0.2,0.35,0.45,1.,0.],[0.15,0.25,0.6,0.9,0.1,1.]])
     sm = np.matmul(sm_sqrt,sm_sqrt.T)
     
     all_Zs, all_mfs = init_linear(X,Z,kernels)
     mylayers = autoflow_Layer(X,Y,Z,kernels,all_mfs,all_Zs,S_M = sm_sqrt, mu_M = np.array([[1.,2.,3.,4.,5.,6.]]).T)        
     full_f, _,_,_,_,_ = mylayers.autoflow_sample(X,1,z)
     
     full_test_f = []
     for k in range(4):
         knm = np.kron(np.eye(2),np.matmul(K(X.T,Z.T)[k],np.linalg.inv(K(Z.T,Z.T))))
         smtilde = np.matmul(np.matmul(knm, sm[:4,:4]),knm.T)
         smtilde += np.kron(np.eye(2),1-np.matmul(np.matmul(K(X.T,Z.T)[k],np.linalg.inv(K(Z.T,Z.T))),K(X.T,Z.T)[k].T))
         l = np.linalg.cholesky(smtilde)
         full_test_f.append((np.matmul(knm,np.array([[1,2,3,4]]).T) + np.matmul(l,np.array([z[k]]).T)).T)
     
     return full_f, np.array(full_test_f)[:,0,:] + mylayers.autoflow_mean(X) #+X due to linear mean function
 def full_SM(self):
     def K(X1, X2):
         
         X1, X2 = np.array(X1), np.array(X2)
         if X1.ndim == 1:
             X1 = np.array([X1])
         if X2.ndim == 1:
             X2 = np.array([X2])
         
         if np.size(X1,0) != np.size(X2,0):
             print("Size of input matrices don't match")
             sys.exit()
         
         m1, m2 = np.size(X1,1), np.size(X2,1)
             
         matK = np.zeros((m1,m2))
         
         for i in range(m1):
             for j in range(m2):
                 matK[i, j] = np.exp(-1/2 * (X1[:,i] - X2[:,j]).dot(X1[:,i] - X2[:,j]))
                     
         return matK
     
     X = np.array([[1.,2.],[1.,2.1],[1.3,2.],[1.2,2.4]])
     Y = X.copy()
     Z = X.copy()
     A = np.tril(np.random.rand(16,16)) #"cholesky" of S_M 
     B = np.random.rand(16,1)
     z=np.array([[0.1,0.5],[-0.3,0.2],[1.,-1.3],[2.,0.]])
     kernels = [gpflow.kernels.RBF(2),gpflow.kernels.RBF(2,lengthscales=2.)]
     all_Zs, all_mfs = init_linear(X,Z,kernels)
     mylayers = autoflow_Layer(X,Y,Z,kernels,all_mfs,all_Zs,S_M = A, mu_M = B)        
     full_f, _,_,_,_,_ = mylayers.autoflow_sample(X,1,z)
     
     full_test_f = []
     sm = A @ A.T
     for k in range(4):
         knm = np.kron(np.eye(2),np.matmul(K(X.T,Z.T)[k],np.linalg.inv(K(Z.T,Z.T))))
         smtilde = np.matmul(np.matmul(knm, sm[:8,:8]),knm.T)
         smtilde += np.kron(np.eye(2),1-np.matmul(np.matmul(K(X.T,Z.T)[k],np.linalg.inv(K(Z.T,Z.T))),K(X.T,Z.T)[k].T))
         l = np.linalg.cholesky(smtilde)
         full_test_f.append((np.matmul(knm,B[:8]) + np.matmul(l,np.array([z[k]]).T)).T)
         
     return full_f, np.array(full_test_f)[:,0,:] + X #+X due to linear mean function
예제 #6
0
 def __init__(self,
              X,
              Y,
              Z,
              kernels,
              likelihood,
              mu_M=None,
              S_M=None,
              whitened_prior=False,
              initialized_Zs=False,
              **kwargs):
     """
     X, (N,D) input points
     Y, (N,1) output points
     Z, (M,D) inducing points
     kernels, a list of kernels (one for every layer)
     likelihood, lieklihood object from gpflow
     
     optional initializations:
     mu_M, (M*T,1) the means of the varational distribution q(f_M)
     S_M, (M*T,M*T) actually rather S_M_sqrt, the lower triangular Cholesky factor of S_M,
                    the covariance matrix of q(f_M)
     
     whitened_prior, flag for whitening the prior
     initialized_Zs, flag needed for passing initialized Z values (for all layers)
                     in this case Z needs to be a list of all inducing points of all layers
     """
     #mean functions need to be initialized first
     all_Zs, all_mfs = init_linear(X,
                                   Z,
                                   kernels,
                                   initialized_Zs=initialized_Zs)
     layers = Fully_Coupled_Layers(X,
                                   Y,
                                   Z,
                                   kernels,
                                   all_mfs,
                                   all_Zs,
                                   mu_M=mu_M,
                                   S_M=S_M,
                                   whitened_prior=whitened_prior)
     Full_DGP_Base.__init__(self, X, Y, likelihood, layers, **kwargs)
 def diag_SM(self):
     X = np.array([[1.,2.,3.],[1.,2.1,3.],[1.1,2.,3.],[1.,2.,3.1]])
     Y = np.array([[1.],[2.],[.2],[3.]])
     Z = np.array([[1.,2.,3.],[1.3,2.2,3.1]])
     z=np.array([[0.1,0.5],[-0.3,0.2],[1.,-1.3],[2.,0.]])
     kernels = [gpflow.kernels.RBF(3),gpflow.kernels.RBF(2)]
     sm_sqrt = np.array([[1.,0.,0.,0.,0.,0.],[0.5,1.,0.,0.,0.,0.],[0.,0.,1.,0.,0.,0.],[0.,0.,0.95,1.,0.,0.],
                    [0.8,0.2,0.35,0.45,1.,0.],[0.15,0.25,0.6,0.9,0.1,1.]])
     
     all_Zs, all_mfs = init_linear(X,Z,kernels)
     mylayers = autoflow_Layer(X,Y,Z,kernels,all_mfs,all_Zs,S_M = sm_sqrt, mu_M = np.array([[1.,2.,3.,4.,5.,6.]]).T)        
     diag_f, _,_,_,_,_ = mylayers.autoflow_sample(X,1,z)
     diag_f = diag_f[:,:2]
     
     Saldgp = autoflow_saldgp(X,Y,Z,kernels,Gaussian())
     myqsqrt = np.array([[[1.,0.],[0.5,1.]],[[1.,0.],[0.95,1.]]])
     myqmu = np.array([[1.,3.],[2.,4.]])
     Saldgp.set_qsqrt(myqsqrt,myqmu)
     sal_f, _, _ = Saldgp.get_var(X,z)
     sal_f = sal_f[0]        
     
     return sal_f, diag_f
예제 #8
0
 def __init__(self,
              X,
              Y,
              Z,
              kernels,
              likelihood,
              mu_M=None,
              S_M=None,
              whitened_prior=False,
              initialized_Zs=False,
              arrow=True,
              stripes=True,
              **kwargs):
     """
     additional inputs:
     arrow, flag for including the arrowheads of the STAR covariance matrix
     stripes, flag for including the off-diagonal stripes of the STAR covariance matrix
     
     note that if both flags are False, the mean-field dgp from
     https://github.com/ICL-SML/Doubly-Stochastic-DGP/ is reproduced
     """
     all_Zs, all_mfs = init_linear(X,
                                   Z,
                                   kernels,
                                   initialized_Zs=initialized_Zs)
     layers = Stripes_Arrow_Layers(X,
                                   Y,
                                   Z,
                                   kernels,
                                   all_mfs,
                                   all_Zs,
                                   mu_M=mu_M,
                                   S_M=S_M,
                                   whitened_prior=whitened_prior,
                                   arrow=arrow,
                                   stripes=stripes)
     Full_DGP_Base.__init__(self, X, Y, likelihood, layers, **kwargs)