Esempio n. 1
0
 def build_predict(self,Xnew,task_ind):
         """
         We need to assume the task_ind starts from 0
         """
         Fmean,Fvar = 0,0
         for i in np.arange(self.rank):
             for j in np.arange(self.num_latent_list[i]):
                 lat_id = np.sum(self.num_latent_list[:i],dtype = np.int64) + j
                 if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                     fmean, fvar = conditionals.gaussian_gp_predict_whitened(Xnew, self.Z[lat_id],
                                     self.kern_list[i], self.q_mu_list[lat_id],
                                      self.q_sqrt_list[lat_id], 1)
                 else:
                     fmean, fvar = conditionals.gaussian_gp_predict(Xnew, self.Z[lat_id],
                                     self.kern_list[i], self.q_mu_list[lat_id],
                                      self.q_sqrt_list[lat_id],1)
                 W_ij = tf.gather(self.W,task_ind)[lat_id]
                 Fmean += (fmean + self.mean_function_list[lat_id](Xnew))*W_ij
                 Fvar += fvar * tf.square(W_ij)
         if self.tsk:
             for i in np.arange(self.num_tasks):
                 lat_id = np.sum(self.num_latent_list,dtype = np.int64) + i
                 if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                     fmean, fvar = conditionals.gaussian_gp_predict_whitened(Xnew, self.Z[lat_id],
                                     self.tskern_list[i], self.q_mu_list[lat_id],
                                      self.q_sqrt_list[lat_id], 1)
                 else:
                     fmean, fvar = conditionals.gaussian_gp_predict(Xnew, self.Z[lat_id],
                                     self.tskern_list[i], self.q_mu_list[lat_id],
                                      self.q_sqrt_list[lat_id], 1)
                 switch = tf.cast(tf.equal(tf.to_int64(i), task_ind),tf.float64)
                 W_ij = tf.gather(self.Kappa,i)[0]*switch
                 Fmean += (fmean + self.mean_function_list[lat_id](Xnew))*W_ij
                 Fvar += fvar * tf.square(W_ij)
         return Fmean, Fvar
Esempio n. 2
0
 def build_predict(self, Xnew):
     mu = None
     var = None
     
     for d in xrange(self.X.shape[1]):
         xnew_d_as_2d = Xnew[:, d].reshape(len(Xnew), 1)
         q_mu_d = self.__getattribute__('q_mu_%d' % d)
         q_sqrt_d = self.__getattribute__('q_sqrt_%d' % d)
         Z_d = self.__getattribute__('Z_%d' % d)
         
         if self.whiten:
             mu_d, var_d = conditionals.gaussian_gp_predict_whitened(xnew_d_as_2d, Z_d, self.kern[d], q_mu_d, q_sqrt_d, self.num_latent)
         else:
             mu_d, var_d = conditionals.gaussian_gp_predict(xnew_d_as_2d, Z_d, self.kern[d], q_mu_d, q_sqrt_d, self.num_latent)
     
         mu_d += self.mean_function[d](xnew_d_as_2d)
         
         # add things up, we were too lazy to check the type of fmean_d, fvar_d
         if mu is None or var is None:
             mu = mu_d
             var = var_d
         else:
             mu += mu_d
             var += var_d
     
     return mu, var
Esempio n. 3
0
    def build_likelihood(self):
        """
        This gives a variational bound on the model likelihood.
        """

        #Get prior KL.
        KL = self.build_prior_KL()

        #Get conditionals
        if self.whiten:
            fmean, fvar = conditionals.gaussian_gp_predict_whitened(
                self._tfX, self.Z, self.kern, self.q_mu, self.q_sqrt,
                self.num_latent)
        else:
            fmean, fvar = conditionals.gaussian_gp_predict(
                self._tfX, self.Z, self.kern, self.q_mu, self.q_sqrt,
                self.num_latent)

        #add in mean function to conditionals.
        fmean += self.mean_function(self._tfX)

        #Get variational expectations.
        variational_expectations = self.likelihood.variational_expectations(
            fmean, fvar, self._tfY)

        minibatch_scale = len(self.X) / tf.cast(
            tf.shape(self._tfX)[0], tf.float64)
        return tf.reduce_sum(variational_expectations) * minibatch_scale - KL
Esempio n. 4
0
    def build_likelihood(self):
        """
        This gives a variational bound on the model likelihood.
        """

        #Get prior KL.
        KL = self.build_prior_KL()

        #Get conditionals
        if self.whiten:
            fmean, fvar = conditionals.gaussian_gp_predict_whitened(
                self.X, self.Z, self.kern, self.q_mu, self.q_sqrt,
                self.num_latent)
        else:
            fmean, fvar = conditionals.gaussian_gp_predict(
                self.X, self.Z, self.kern, self.q_mu, self.q_sqrt,
                self.num_latent)

        #add in mean function to conditionals.
        fmean += self.mean_function(self.X)

        #Get variational expectations.
        variational_expectations = self.likelihood.variational_expectations(
            fmean, fvar, self.Y)

        return tf.reduce_sum(variational_expectations) - KL
Esempio n. 5
0
 def build_predict(self, Xnew):
     if self.whiten:
         mu, var = conditionals.gaussian_gp_predict_whitened(
             Xnew, self.Z, self.kern, self.q_mu, self.q_sqrt,
             self.num_latent)
     else:
         mu, var = conditionals.gaussian_gp_predict(Xnew, self.Z, self.kern,
                                                    self.q_mu, self.q_sqrt,
                                                    self.num_latent)
     return mu + self.mean_function(Xnew), var
Esempio n. 6
0
 def build_likelihood(self):
     """
     Loglikelihood is the likelihood sum over all tasks
     the outer loop go through all the tasks and the inner loop goes through
     the latent function inside each task and calculate the loglike of this task
     as the weighted sum of loglike of latent function.
     """
     #Get prior KL.
     KL,loglike  = self.build_prior_KL(),0
 
     self.Xt = [tf.placeholder(dtype = tf.float64, shape = [None,1]) for _ in np.arange(self.num_tasks)]
     self.Yt = [tf.placeholder(dtype = tf.float64, shape = [None,1]) for _ in np.arange(self.num_tasks)]
     #Get conditionals, is this correct???
     for d in np.arange(self.num_tasks):
         ve,Fmean,Fvar = 0,0,0
         lag = self.lagging[d,0]
         for q in np.arange(self.rank):
             for i in np.arange(self.num_latent_list[q]):
                 lat_id = np.sum(self.num_latent_list[:q],dtype = np.int64) + i
                 if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                     fmean, fvar = conditionals.gaussian_gp_predict_whitened(self.Xt[d]-lag, self.Z[lat_id],
                                     self.kern_list[q], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                 else:
                     fmean, fvar = conditionals.gaussian_gp_predict(self.Xt[d]-lag,self.Z[lat_id],
                                     self.kern_list[q], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                 Fmean += (fmean + self.mean_function_list[lat_id](self.Xt[d]))*self.W[d,lat_id]
                 Fvar += fvar * tf.square(self.W[d,lat_id])
         if self.tsk:
             lat_id = np.sum(self.num_latent_list,dtype = np.int64) + d
             if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                 fmean, fvar = conditionals.gaussian_gp_predict_whitened(self.Xt[d], self.Z[lat_id],
                                 self.tskern_list[d], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
             else:
                 fmean, fvar = conditionals.gaussian_gp_predict(self.Xt[d], self.Z[lat_id],
                                 self.tskern_list[d], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
             Fmean += (fmean + self.mean_function_list[lat_id](self.Xt[d]))*self.Kappa[d,0]
             Fvar += fvar * tf.square(self.Kappa[d,0])               
         ve = self.likelihood[d].variational_expectations(Fmean, Fvar, self.Yt[d])
         loglike += tf.reduce_sum(ve)  
     """add time to event likelihood to the loglike"""    
     #return loglike - KL - self.lamda *tf.sqrt(tf.reduce_sum(tf.square(self.Kappa)))/self.num_patients
     return loglike - KL 
Esempio n. 7
0
 def build_likelihood(self):
     """
     Loglikelihood is the likelihood sum over all tasks
     the outer loop go through all the tasks and the inner loop goes through
     the latent function inside each task and calculate the loglike of this task
     as the weighted sum of loglike of latent function.
     """
     #Get prior KL.
     KL,loglike  = self.build_prior_KL(),0
     #Get conditionals
     for i in np.arange(self.num_tasks):
         ve,Fmean,Fvar = 0,0,0
         for j in np.arange(self.rank):
             for k in np.arange(self.num_latent_list[j]):
                 lat_id = np.sum(self.num_latent_list[:j],dtype = np.int64) + k
                 if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                     fmean, fvar = conditionals.gaussian_gp_predict_whitened(self.X[i], self.Z[lat_id],
                                     self.kern_list[j], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                 else:
                     fmean, fvar = conditionals.gaussian_gp_predict(self.X[i], self.Z[lat_id],
                                     self.kern_list[j], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                 Fmean += (fmean + self.mean_function_list[lat_id](self.X[i]))*self.W[i,lat_id]
                 Fvar += fvar * tf.square(self.W[i,lat_id])       
         if self.tsk:
             lat_id = np.sum(self.num_latent_list,dtype = np.int64) + i
             if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                 fmean, fvar = conditionals.gaussian_gp_predict_whitened(self.X[i], self.Z[lat_id],
                                 self.tskern_list[i], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
             else:
                 fmean, fvar = conditionals.gaussian_gp_predict(self.X[i], self.Z[lat_id],
                                 self.tskern_list[i], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
             Fmean += (fmean + self.mean_function_list[lat_id](self.X[i]))*self.Kappa[i,0]
             Fvar += fvar * tf.square(self.Kappa[i,0])
             
         ve = self.likelihood[i].variational_expectations(Fmean, Fvar, self.Y[i])
         loglike += tf.reduce_sum(ve)             
     loglike -= KL
     return loglike
Esempio n. 8
0
 def predict_task_latent(self,Xnew,task_id):
     Fmean,Fvar = 0,0
     for i in xrange(self.num_tasks):
         switch = tf.cast(tf.equal(np.int64(i),tf.to_int64(task_id)),tf.float64)
         lat_id = np.sum(self.num_latent_list) + i
         if self.whiten_list[lat_id]:
             fmean,fvar = conditionals.gaussian_gp_predict_whitened(Xnew, self.Z[lat_id],
                                 self.tskern_list[i], self.q_mu_list[lat_id],
                                  self.q_sqrt_list[lat_id],1)
         else:
             fmean, fvar = conditionals.gaussian_gp_predict(Xnew, self.Z[lat_id],
                                 self.tskern_list[i], self.q_mu_list[lat_id],
                                  self.q_sqrt_list[lat_id], 1)
         Fmean += fmean*switch
         Fvar += fvar*switch
     return Fmean, Fvar
Esempio n. 9
0
 def predict_latent(self, Xnew, group_id, ingroup_id):
     """
     Compute the posterior for one of the latent functions.
     """
     Fmean, Fvar = 0,0
     for i in np.arange(self.rank):
         switch1 = tf.cast(tf.equal(i, group_id),tf.float64)
         for j in np.arange(self.num_latent_list[i]):
             lat_id = np.sum(self.num_latent_list[:i],dtype = np.int64) + j
             if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                 fmean, fvar = conditionals.gaussian_gp_predict_whitened(Xnew, self.Z[lat_id],
                                 self.kern_list[i], self.q_mu_list[lat_id],
                                  self.q_sqrt_list[lat_id], 1)
             else:
                 fmean, fvar = conditionals.gaussian_gp_predict(Xnew, self.Z[lat_id],
                                 self.kern_list[i], self.q_mu_list[lat_id],
                                  self.q_sqrt_list[lat_id], 1)
                 switch2 = tf.cast(tf.equal(j, ingroup_id),tf.float64)
                 Fmean += (fmean + self.mean_function_list[lat_id](Xnew))*switch1*switch2
                 Fvar += fvar *switch1*switch2
     return Fmean, Fvar
Esempio n. 10
0
    def build_likelihood(self):
        """
        This gives a variational bound on the model likelihood.
        """

        #Get prior KL.
        KL  = self.build_prior_KL()
    
        #Get conditionals
        if self.whiten:
            fmean, fvar = conditionals.gaussian_gp_predict_whitened(self.X, self.Z, self.kern, self.q_mu, self.q_sqrt, self.num_latent)
        else:
            fmean, fvar = conditionals.gaussian_gp_predict(self.X, self.Z, self.kern, self.q_mu, self.q_sqrt, self.num_latent)            

        #add in mean function to conditionals.
        fmean += self.mean_function(self.X)
        
        #Get variational expectations.
        variational_expectations = self.likelihood.variational_expectations(fmean, fvar, self.Y)
        
        return tf.reduce_sum(variational_expectations) - KL
Esempio n. 11
0
    def build_likelihood(self):
        """
        This gives a variational bound on the model likelihood.
        """

        #Get prior KL.
        KL  = self.build_prior_KL()
    
        #Get conditionals
        if self.whiten:
            fmean, fvar = conditionals.gaussian_gp_predict_whitened(self._tfX, self.Z, self.kern, self.q_mu, self.q_sqrt, self.num_latent)
        else:
            fmean, fvar = conditionals.gaussian_gp_predict(self._tfX, self.Z, self.kern, self.q_mu, self.q_sqrt, self.num_latent)

        #add in mean function to conditionals.
        fmean += self.mean_function(self._tfX)
        
        #Get variational expectations.
        variational_expectations = self.likelihood.variational_expectations(fmean, fvar, self._tfY)

        minibatch_scale = len(self.X) / tf.cast(tf.shape(self._tfX)[0], tf.float64)
        return tf.reduce_sum(variational_expectations) * minibatch_scale - KL
Esempio n. 12
0
    def build_likelihood(self):
        """
        This gives a variational bound on the model likelihood.
        """

        # Get prior KL.
        KL = self.build_prior_KL()
    
        fmean = None
        fvar = None
        for d in xrange(self.X.shape[1]):
            x_d_as_2d = self.X[:, d].reshape(len(self.X), 1)
            q_mu_d = self.__getattribute__('q_mu_%d' % d)
            q_sqrt_d = self.__getattribute__('q_sqrt_%d' % d)
            Z_d = self.__getattribute__('Z_%d' % d)
            
            # Get conditionals
            if self.whiten:
                fmean_d, fvar_d = conditionals.gaussian_gp_predict_whitened(x_d_as_2d, Z_d, self.kern[d], q_mu_d, q_sqrt_d, self.num_latent)
            else:
                fmean_d, fvar_d = conditionals.gaussian_gp_predict(x_d_as_2d, Z_d, self.kern[d], q_mu_d, q_sqrt_d, self.num_latent)            
    
            # add in mean function to conditionals.
            fmean_d += self.mean_function(x_d_as_2d)
            
            # add things up, we were too lazy to check the type of fmean_d, fvar_d
            if fmean is None or fvar is None:
                fmean = fmean_d
                fvar = fvar_d
            else:
                fmean += fmean_d
                fvar += fvar_d
        
        # Get variational expectations.
        variational_expectations = self.likelihood.variational_expectations(fmean, fvar, self.Y)
        
        return tf.reduce_sum(variational_expectations) - KL
Esempio n. 13
0
 def build_predict(self, Xnew, full_cov=False):
     if self.whiten:
         mu, var =  conditionals.gaussian_gp_predict_whitened(Xnew, self.Z, self.kern, self.q_mu, self.q_sqrt, self.num_latent, full_cov)
     else:
         mu, var =  conditionals.gaussian_gp_predict(Xnew, self.Z, self.kern, self.q_mu, self.q_sqrt, self.num_latent, full_cov)
     return mu + self.mean_function(Xnew), var
Esempio n. 14
0
    def build_likelihood(self):
        """
        Loglikelihood is the likelihood sum over all tasks
        the outer loop go through all the tasks and the inner loop goes through
        the latent function inside each task and calculate the loglike of this task
        as the weighted sum of loglike of latent function.
        """
        #Get prior KL.

        def quardrature_tensorflow(a,b,num_points):
            """
            Gaussian Quardrature in 1d
            """
            x, w = leggauss(num_points)
            x = tf.reshape((b-a)/2.*x + (a+b)/2.,[-1,1])
            w = tf.reshape(w,[1,-1])*(b-a)/2
            #res = tf.matmul(w,fun(x)).
            return x,w

        KL,loglike  = self.build_prior_KL(),0
    
        self.Xt = [tf.placeholder(dtype = tf.float64, shape = [None,1]) for _ in np.arange(self.num_tasks)]
        self.Yt = [tf.placeholder(dtype = tf.float64, shape = [None,1]) for _ in np.arange(self.num_tasks)]
        self.Tt = tf.placeholder(dtype = tf.float64, shape = [1,1])
        self.invariant_t = tf.placeholder(dtype = tf.float64, shape = [None,1])
        self.event_t = tf.placeholder(dtype = tf.float64,shape = [1,1])

        x, w = quardrature_tensorflow(0,self.Tt,self.num_inducing_points)
        time2event,time2event2 = 0 , 0
        #Get conditionals, is this correct???
        Fmean_joint = np.zeros(shape = (self.num_tasks,1))
        Fvar_joint = np.zeros(shape = (self.num_tasks,self.num_tasks))
        
        """
            Can we creat a list to contain fexp_var for computing the full convariance matrix?
        """
        fexp_list = list()
        for d in np.arange(self.num_tasks):
            ve,Fmean,Fvar,fvar_joint = 0,0,0,0
            lag = self.lagging[d,0]
            for q in np.arange(self.rank):
                for i in np.arange(self.num_latent_list[q]):
                    lat_id = np.sum(self.num_latent_list[:q],dtype = np.int64) + i
                    if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                        fmean, fvar = conditionals.gaussian_gp_predict_whitened(self.Xt[d]-lag, self.Z[lat_id],
                                        self.kern_list[q], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                        if d == 0:
                            fexp, fexp_var = conditionals.gaussian_gp_predict_whitened(x, self.Z[lat_id],
                                        self.kern_list[q], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                            fexp_list.append(fexp_var)
                            Fvar_joint[d,d] += fexp_var* tf.square(self.W[d,lat_id])
                        else:
                            for k in np.arange(d):
                                fvar_joint = fexp_list[lat_id]*self.W[d,lat_id] *self.W[k,lat_id] 
                                Fvar_joint[d,k] += fvar_joint
                                Fvar_joint[k,d] += fvar_joint

                        fm = tf.gather(fexp,self.num_inducing_points-1)
                    else:
                        fmean, fvar = conditionals.gaussian_gp_predict(self.Xt[d]-lag,self.Z[lat_id],
                                        self.kern_list[q], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                        if d == 0:
                            fexp, fexp_var = conditionals.gaussian_gp_predict(x, self.Z[lat_id], self.kern_list[q], 
                                                    self.q_mu_list[lat_id], self.q_sqrt_list[lat_id],1)  ## tf.matmul(w,fexp, fexp_var)
                            fexp_list.append(fexp_var)
                            Fvar_joint[d,d] += fexp_var* tf.square(self.W[d,lat_id])
                        else:
                            for k in np.arange(d):
                                fvar_joint = fexp_list[lat_id]*self.W[d,lat_id] *self.W[k,lat_id] 
                                Fvar_joint[d,k] += fvar_joint
                                Fvar_joint[k,d] += fvar_joint

                        fm = tf.gather(fexp,self.num_inducing_points-1)

                    Fmean += (fmean + self.mean_function_list[lat_id](self.Xt[d]))*self.W[d,lat_id]
                    Fvar += fvar * tf.square(self.W[d,lat_id])
                    fexp += (fmean + self.mean_function_list[lat_id](x))*self.W[d,lat_id]*self.alpha[d]
                    time2event += fm + self.mean_function_list[lat_id](self.Tt[d])*self.W[d,lat_id]* self.alpha[d]*self.event_t 

            if self.tsk:
                lat_id = np.sum(self.num_latent_list,dtype = np.int64) + d
                if self.whiten_list[lat_id]:  # need to compute fmean and fvar by the weights
                    fmean, fvar = conditionals.gaussian_gp_predict_whitened(self.Xt[d], self.Z[lat_id],
                                    self.tskern_list[d], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)

                    fexp, fexp_var = conditionals.gaussian_gp_predict_whitened(x, self.Z[lat_id],
                                        self.kern_list[q], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)
                    Fvar_joint[d,d] += fexp_var *tf.square(self.W[d,lat_id])
                    fm = tf.gather(fexp,self.num_inducing_points-1)
                else:
                    fmean, fvar = conditionals.gaussian_gp_predict(self.Xt[d], self.Z[lat_id],
                                    self.tskern_list[d], self.q_mu_list[lat_id], self.q_sqrt_list[lat_id], 1)

                    fexp, fexp_var = conditionals.gaussian_gp_predict(x, self.Z[lat_id], self.kern_list[q],
                            self.q_mu_list[lat_id], self.q_sqrt_list[lat_id],1) #wrong ,need to rewrite
                    Fvar_joint[d,d] += fexp_var *tf.square(self.W[d,lat_id])
                    fm = tf.gather(fexp,self.num_inducing_points-1) # wrong, need to be rewrite
                
                Fmean += (fmean + self.mean_function_list[lat_id](self.Xt[d]))*self.Kappa[d,0]
                Fvar += fvar * tf.square(self.Kappa[d,0])    
                time2event += fm + self.mean_function_list[lat_id](self.Tt[d])*self.W[d,lat_id]* self.alpha[d]*self.event_t 
            
            time2event += self.gamma[d] * self.invariant_t*self.event_t
            Fmean_joint[d,0] = fexp

            ve = self.likelihood[d].variational_expectations(Fmean, Fvar, self.Yt[d])
            loglike += tf.reduce_sum(ve)  
            loglike -= tf.matmul(w,time2event2)  # minus the second part 

        loglike += tf.log(h_fun(self.Tt))*self.event_t + time2event

        """add time to event likelihood to the loglike"""    
        return loglike - KL - self.lamda *tf.sqrt(tf.reduce_sum(tf.square(self.Kappa)))