コード例 #1
0
def test_multivariate_normal(session_tf, x, mu, cov_sqrt):
    cov = np.dot(cov_sqrt, cov_sqrt.T)
    L = np.linalg.cholesky(cov)

    if len(x.shape) != 2 or len(mu.shape) != 2:
        with pytest.raises(Exception) as e_info:
            gp_result = logdensities.multivariate_normal(
                tf.convert_to_tensor(x),
                tf.convert_to_tensor(mu),
                tf.convert_to_tensor(L))
    else:
        x_tf = tf.placeholder(settings.float_type)
        mu_tf = tf.placeholder(settings.float_type)
        gp_result = logdensities.multivariate_normal(
            x_tf, mu_tf, tf.convert_to_tensor(L))

        gp_result = session_tf.run(gp_result, feed_dict={x_tf: x, mu_tf: mu})

        if mu.shape[1] > 1:
            if x.shape[1] > 1:
                sp_result = [mvn.logpdf(x[:,i], mu[:,i], cov) for i in range(mu.shape[1])]
            else:
                sp_result = [mvn.logpdf(x.ravel(), mu[:, i], cov) for i in range(mu.shape[1])]
        else:
            sp_result = mvn.logpdf(x.T, mu.ravel(), cov)
        assert_allclose(gp_result, sp_result)
コード例 #2
0
def test_multivariate_normal(session_tf, x, mu, cov_sqrt):
    cov = np.dot(cov_sqrt, cov_sqrt.T)
    L = np.linalg.cholesky(cov)

    if len(x.shape) != 2 or len(mu.shape) != 2:
        with pytest.raises(Exception) as e_info:
            gp_result = logdensities.multivariate_normal(
                tf.convert_to_tensor(x), tf.convert_to_tensor(mu),
                tf.convert_to_tensor(L))
    else:
        x_tf = tf.placeholder(settings.float_type)
        mu_tf = tf.placeholder(settings.float_type)
        gp_result = logdensities.multivariate_normal(x_tf, mu_tf,
                                                     tf.convert_to_tensor(L))

        gp_result = session_tf.run(gp_result, feed_dict={x_tf: x, mu_tf: mu})

        if mu.shape[1] > 1:
            if x.shape[1] > 1:
                sp_result = [
                    mvn.logpdf(x[:, i], mu[:, i], cov)
                    for i in range(mu.shape[1])
                ]
            else:
                sp_result = [
                    mvn.logpdf(x.ravel(), mu[:, i], cov)
                    for i in range(mu.shape[1])
                ]
        else:
            sp_result = mvn.logpdf(x.T, mu.ravel(), cov)
        assert_allclose(gp_result, sp_result)
コード例 #3
0
    def _build_likelihood(self):
        """
        Construct tensorflow fucntion to compute the marginal likelihood

        :returns: TF tensor
        """

        transfer_param = 2 * (1 / (1 + self.mu) ** self.b) - 1
        K_cross = transfer_param * self.kern.K(self.X_source, self.X)
        K_source = self.kern.K(self.X_source) \
                   + tf.eye(tf.shape(self.X_source)[0], dtype=settings.float_type)\
                   * self.source_likelihood.variance
        K_target = self.kern.K(self.X) \
                   + tf.eye(tf.shape(self.X)[0], dtype=settings.float_type)\
                   * self.likelihood.variance

        m, C = base_conditional(K_cross, K_source, K_target, self.Y_source, full_cov=True)
        # L_source = tf.cholesky(K_source)
        # A = tf.matrix_triangular_solve(L_source, K_cross, lower=True)
        # A = tf.matrix_triangular_solve(tf.transpose(L_source), A, lower=True)
        # m = self.mean_function(self.X) + tf.matmul(A, self.Y_source, transpose_a=True)
        #
        # C = K_target - tf.transpose(tf.matmul(A, K_cross))
        m = self.mean_function(self.X) + m
        L = tf.cholesky(C)[0]

        logpdf = multivariate_normal(self.Y, m, L)

        return tf.reduce_sum(logpdf)
コード例 #4
0
	def _build_likelihood(self):
		K = self.kern.K(self.X) + tf.eye(tf.shape(self.X)[0], dtype=settings.float_type) * self.likelihood.variance
#		K = tf.Print(K, [tf.sqrt(tf.reduce_mean((K-tf.transpose(K))**2))], message="K: ", summarize=10)
#		K = tf.Print(K, [tf.reduce_min(K), tf.reduce_max(K)], message="K: ", summarize=10)
		L = tf.cholesky(K)
		m = self.mean_function(self.X)
		logpdf = multivariate_normal(self.Y, m, L)  # (R,) log-likelihoods for each independent dimension of Y
		return tf.reduce_sum(logpdf)
コード例 #5
0
ファイル: layers.py プロジェクト: nips20-2515/DGP-Graph
 def build_likelihood(self):
     ## modified from GPR
     K = self.kern.K(self._X_mean) + tf.eye(
         tf.shape(self._X_mean)[0],
         dtype=settings.float_type) * self._lik_variance
     L = tf.cholesky(K)
     m = self.mean_function(self._X_mean)
     return tf.reduce_sum(multivariate_normal(self._Y, m, L))
コード例 #6
0
    def log_marginal_likelihood(self) -> tf.Tensor:
        r"""
        Computes the log marginal likelihood.

        .. math::
            \log p(Y | \theta).

        """
        L = tf.linalg.cholesky(self.likelihood.add_to(self.KXX))
        return multivariate_normal(self._Y, self._mean, L)
コード例 #7
0
ファイル: HGPLVM.py プロジェクト: michaelStettler/h-GPLVM
    def hierarchy_ll(self):
        x, h, y = self.data
        K = self.kernel0(x)
        num_data = x.shape[0]
        k_diag = tf.linalg.diag_part(K)
        s_diag = tf.fill([num_data], self.likelihood0.variance)
        ks = tf.linalg.set_diag(K, k_diag + s_diag)
        L = tf.linalg.cholesky(ks)
        m = self.mean_function(x)

        return multivariate_normal(h, m, L)
コード例 #8
0
def test_shape_asserts(session_tf):
    A = np.random.randn(5)
    B = np.random.randn(5)
    L = np.tril(np.random.randn(5, 5))

    # Static shape check:
    with pytest.raises(ValueError):
        tA = tf.identity(A)
        tB = tf.identity(B)
        tL = tf.identity(L)
        res = logdensities.multivariate_normal(tA, tB, tL)

    # Dynamic shape check:
    # the following results in a segfault before PR#964
    with pytest.raises(tf.errors.InvalidArgumentError):
        vA = tf.placeholder(tf.float64)
        vB = tf.placeholder(tf.float64)
        vL = tf.placeholder(tf.float64)
        res = logdensities.multivariate_normal(vA, vB, vL)
        session_tf.run(res, {vA: A, vB: B, vL: L})
コード例 #9
0
ファイル: gpr_models.py プロジェクト: rm4216/BayesOpt
 def _build_likelihood(self):  # mean_function !
     K = self.kern.K(self.X) + tf.eye(
         tf.shape(self.X)[0],
         dtype=settings.float_type) * self.likelihood.variance
     jitter = self.stable_jitter(K)
     L = tf.cholesky(
         K +
         tf.eye(tf.shape(self.X)[0], dtype=settings.float_type) * jitter)
     m = self.mean_function(self.X)
     # (R,) log-likelihoods for each independent dimension of Y
     logpdf = logdensities.multivariate_normal(self.Y, m, L)
     return tf.reduce_sum(logpdf)
コード例 #10
0
def test_multivariate_normal(x, mu, cov_sqrt):
    cov = np.dot(cov_sqrt, cov_sqrt.T)
    L = np.linalg.cholesky(cov)

    gp_result = logdensities.multivariate_normal(x, mu, L)

    if mu.shape[1] > 1:
        if x.shape[1] > 1:
            sp_result = [mvn.logpdf(x[:, i], mu[:, i], cov) for i in range(mu.shape[1])]
        else:
            sp_result = [mvn.logpdf(x.ravel(), mu[:, i], cov) for i in range(mu.shape[1])]
    else:
        sp_result = mvn.logpdf(x.T, mu.ravel(), cov)
    assert_allclose(gp_result, sp_result)
コード例 #11
0
ファイル: mlgp.py プロジェクト: shafiahmed/PAML
    def _build_likelihood_psi(self, H_sample):
        r"""
        Construct configuration tensorflow function to compute the likelihood.

            \log p(Y | theta).

        """

        K = self.configuration_kernel.K(H_sample) + tf.eye(
            tf.shape(H_sample)[0],
            dtype=settings.float_type) * self.configuration_likelihood.variance
        L = tf.cholesky(K)
        m = self.mean_psi(H_sample)
        logpdf = multivariate_normal(
            self.psi_ph, m,
            L)  # (R,) log-likelihoods for each independent dimension of Y

        return tf.reduce_sum(logpdf)
        def log_likelihood(self):
            """
            Computes the log likelihood.
            """
            x, y = self.data
            K = self.kernel(x)
            num_data = x.shape[0]
            k_diag = tf.linalg.diag_part(K)
            s_diag = tf.convert_to_tensor(self.likelihood.variance)
            jitter = tf.cast(tf.fill([num_data], default_jitter()),
                             'float64')  # stabilize K matrix w/jitter
            ks = tf.linalg.set_diag(K, k_diag + s_diag + jitter)
            L = tf.linalg.cholesky(ks)
            m = self.mean_function(x)

            # [R,] log-likelihoods for each independent dimension of Y
            log_prob = multivariate_normal(y, m, L)
            return tf.reduce_sum(log_prob)
コード例 #13
0
    def log_marginal_likelihood(self) -> tf.Tensor:
        r"""
        Computes the log marginal likelihood.
        .. math::
            \log p(Y | \theta).
        """
        X, Y = self.data
        K = self.kernel(X)
        num_data = X.shape[0]
        k_diag = tf.linalg.diag_part(K)
        s_diag = tf.convert_to_tensor(self.likelihood.variance)
        
        ks = tf.linalg.set_diag(K, k_diag + s_diag)
        L = tf.linalg.cholesky(ks)
        m = self.mean_function(X)

        # [R,] log-likelihoods for each independent dimension of Y
        log_prob = multivariate_normal(Y, m, L)
        return tf.reduce_sum(log_prob)
コード例 #14
0
    def log_likelihood(self):
        r"""
        todo

        """
        
        log_prob = 0
        for dataset in self.data:
            x, y = dataset
            K = self.kernel(x)            
            num_data = x.shape[0]
            k_diag = tf.linalg.diag_part(K)
            s_diag = tf.fill([num_data], self.likelihood.variance)
            ks = tf.linalg.set_diag(K, k_diag + s_diag)
            L = tf.linalg.cholesky(ks)
            m = self.mean_function(x)

            # [R,] log-likelihoods for each independent dimension of Y
            log_prob += multivariate_normal(y, m, L)
        return tf.reduce_sum(log_prob)
コード例 #15
0
ファイル: gpr_models.py プロジェクト: rm4216/BayesOpt
    def _build_likelihood(self):
        """
        Construct a tensorflow function to compute the likelihood.

            \log p(Y | theta).

        """
        K = self.kern.K(self.X) + tf.eye(
            tf.shape(self.X)[0],
            dtype=settings.float_type) * self.likelihood.variance
        jitter = self.stable_jitter(K)
        L = tf.cholesky(
            K +
            tf.eye(tf.shape(self.X)[0], dtype=settings.float_type) * jitter)
        m = self.mean_function(self.X)
        logpdf = logdensities.multivariate_normal(
            self.Y, m,
            L)  # (R,) log-likelihoods for each independent dimension of Y

        return tf.reduce_sum(logpdf)
コード例 #16
0
def gauss_kl(q_mu, q_sqrt, K=None):
    """
    Wrapper for gauss_kl from gpflow that returns the negative log prob if q_sqrt is None. This can be  
    for use in HMC: all that is required is to set q_sqrt to None and this function substitues the
    negative log prob instead of the KL (so no need to set q_mu.prior = gpflow.priors.Gaussian(0, 1)). 
    Also, this allows the use of HMC in the unwhitened case. 
    """
    if q_sqrt is None:
        # return negative log prob with q_mu as 'x', with mean 0 and cov K (or I, if None)
        M, D = tf.shape(q_mu)[0], tf.shape(q_mu)[1]
        I = tf.eye(M, dtype=settings.float_type)

        if K is None:
            L = I
        else:
            L = tf.cholesky(K + I * settings.jitter)

        return -tf.reduce_sum(multivariate_normal(q_mu, tf.zeros_like(q_mu), L))

    else:
        # return kl
        return gauss_kl_gpflow(q_mu, q_sqrt, K=K)
コード例 #17
0
ファイル: HGPLVM.py プロジェクト: michaelStettler/h-GPLVM
    def log_likelihood(self):
        """
        Computes the log likelihood.

        .. math::
            \log p(Y | \theta).

        """
        x, h, y = self.data
        K = self.kernel1(h)
        num_data = h.shape[0]
        k_diag = tf.linalg.diag_part(K)
        s_diag = tf.fill([num_data], self.likelihood1.variance)
        ks = tf.linalg.set_diag(K, k_diag + s_diag)
        L = tf.linalg.cholesky(ks)
        m = self.mean_function(h)

        # m = zeros((np.shape(m)[0], 1))
        log_prob = multivariate_normal(y, m, L)
        log_prob_h = self.hierarchy_ll()
        log_likelihood = tf.reduce_sum(log_prob) + tf.reduce_sum(log_prob_h)
        self.iter += 1
        return log_likelihood
コード例 #18
0
    def log_marginal_likelihood(self, batch=None) -> tf.Tensor:
        r"""
        Computes the log marginal likelihood.
        Allow calculation across batches of data 
        .. math::
            \log p(Y | \theta).
        """
        if batch is not None:
            X, Y = batch
            Y = tf.reshape(Y, (-1, 1))
        else:
            X, Y = self.data
        K = self.kernel(X)
        num_data = tf.shape(X)[0]
        k_diag = tf.linalg.diag_part(K)
        s_diag = tf.fill([num_data], self.likelihood.variance)
        ks = tf.linalg.set_diag(K, k_diag + s_diag)
        L = tf.linalg.cholesky(ks)
        m = self.mean_function(X)

        # [R,] log-likelihoods for each independent dimension of Y
        log_prob = multivariate_normal(Y, m, L)
        print(tf.reduce_sum(log_prob))
        return tf.reduce_sum(log_prob)
コード例 #19
0
 def _log_prob(self, F, Y):
     return tf.reduce_sum(multivariate_normal(tf.reshape(Y, self.split_axis_shape(Y)),
                                              tf.reshape(F, self.split_axis_shape(F)),
                                              self.variance.cholesky))
コード例 #20
0
 def _predict_log_density(self, Fmu, Fvar, Y):
     return tf.reduce_sum(multivariate_normal(Y, Fmu, tf.linalg.cholesky(self.add_to(Fvar))))