當前位置: 首頁>>代碼示例>>Python>>正文


Python numpy.diag方法代碼示例

本文整理匯總了Python中autograd.numpy.diag方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.diag方法的具體用法?Python numpy.diag怎麽用?Python numpy.diag使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在autograd.numpy的用法示例。


在下文中一共展示了numpy.diag方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def __init__(self, generate_diagonal, matrix_class):
        matrix_pairs = {}
        rng = np.random.RandomState(SEED)
        for sz in SIZES:
            diagonal = generate_diagonal(sz, rng)
            matrix_pairs[sz] = (matrix_class(diagonal), np.diag(diagonal))

        if AUTOGRAD_AVAILABLE:

            def param_func(param, matrix):
                return anp.diag(param)

            def get_param(matrix):
                return matrix.diagonal

        else:
            param_func, get_param = None, None

        super().__init__(matrix_pairs, get_param, param_func, rng) 
開發者ID:matt-graham,項目名稱:mici,代碼行數:21,代碼來源:test_matrices.py

示例2: fit_gaussian_draw

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def fit_gaussian_draw(X, J, seed=28, reg=1e-7, eig_pow=1.0):
    """
    Fit a multivariate normal to the data X (n x d) and draw J points 
    from the fit. 
    - reg: regularizer to use with the covariance matrix
    - eig_pow: raise eigenvalues of the covariance matrix to this power to construct 
        a new covariance matrix before drawing samples. Useful to shrink the spread 
        of the variance.
    """
    with NumpySeedContext(seed=seed):
        d = X.shape[1]
        mean_x = np.mean(X, 0)
        cov_x = np.cov(X.T)
        if d==1:
            cov_x = np.array([[cov_x]])
        [evals, evecs] = np.linalg.eig(cov_x)
        evals = np.maximum(0, np.real(evals))
        assert np.all(np.isfinite(evals))
        evecs = np.real(evecs)
        shrunk_cov = evecs.dot(np.diag(evals**eig_pow)).dot(evecs.T) + reg*np.eye(d)
        V = np.random.multivariate_normal(mean_x, shrunk_cov, J)
    return V 
開發者ID:wittawatj,項目名稱:kernel-gof,代碼行數:24,代碼來源:util.py

示例3: multivariate_normal_density

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def multivariate_normal_density(mean, cov, X):
        """
        Exact density (not log density) of a multivariate Gaussian.
        mean: length-d array
        cov: a dxd covariance matrix
        X: n x d 2d-array
        """
        
        evals, evecs = np.linalg.eigh(cov)
        cov_half_inv = evecs.dot(np.diag(evals**(-0.5))).dot(evecs.T)
    #     print(evals)
        half_evals = np.dot(X-mean, cov_half_inv)
        full_evals = np.sum(half_evals**2, 1)
        unden = np.exp(-0.5*full_evals)
        
        Z = np.sqrt(np.linalg.det(2.0*np.pi*cov))
        den = unden/Z
        assert len(den) == X.shape[0]
        return den 
開發者ID:wittawatj,項目名稱:kernel-gof,代碼行數:21,代碼來源:density.py

示例4: moran_transition

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def moran_transition(t, n):
    assert t >= 0.0
    P, d, Pinv = moran_eigensystem(n)
    D = diag(exp(t * d))
    return check_probs_matrix(dot(P, dot(D, Pinv))) 
開發者ID:popgenmethods,項目名稱:momi2,代碼行數:7,代碼來源:moran_model.py

示例5: rate_matrix

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def rate_matrix(n, sparse_format="csr"):
    i = np.arange(n + 1)
    diag = i * (n - i) / 2.
    diags = [diag[:-1], -2 * diag, diag[1:]]
    M = scipy.sparse.diags(
        diags, [1, 0, -1], (n + 1, n + 1), format=sparse_format)
    return M 
開發者ID:popgenmethods,項目名稱:momi2,代碼行數:9,代碼來源:moran_model.py

示例6: ll_m2_exact

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def ll_m2_exact(muw, Sigw, Siginv, x):
  L = np.linalg.cholesky(Siginv)
  Rho = np.dot(np.dot(L.T, Sigw), L)

  crho = 2*(Rho**2).sum() + (np.diag(Rho)*np.diag(Rho)[:, np.newaxis]).sum()

  mu = np.dot(L.T, (x - muw).T).T
  musq = (mu**2).sum(axis=1)

  return 0.25*(crho + musq*musq[:, np.newaxis] + np.diag(Rho).sum()*(musq + musq[:,np.newaxis]) + 4*np.dot(np.dot(mu, Rho), mu.T))

#Var[Log N(x;, mu, Sig)] under mu ~ N(muw, Sigw) 
開發者ID:trevorcampbell,項目名稱:bayesian-coresets,代碼行數:14,代碼來源:test_gaussian.py

示例7: ll_m2_exact_diag

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def ll_m2_exact_diag(muw, Sigw, Siginv, x):
  L = np.linalg.cholesky(Siginv)
  Rho = np.dot(np.dot(L.T, Sigw), L)

  crho = 2*(Rho**2).sum() + (np.diag(Rho)*np.diag(Rho)[:, np.newaxis]).sum()

  mu = np.dot(L.T, (x - muw).T).T
  musq = (mu**2).sum(axis=1)

  return 0.25*(crho + musq**2 + 2*np.diag(Rho).sum()*musq + 4*(np.dot(mu, Rho)*mu).sum(axis=1)) 
開發者ID:trevorcampbell,項目名稱:bayesian-coresets,代碼行數:12,代碼來源:test_gaussian.py

示例8: log_prop

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def log_prop(self, t, Xc, Xp, y, prop_params, model_params):
        mu0, Sigma0, A, Q, C, R = model_params
        mut, lint, log_s2t = prop_params[t]
        s2t = np.exp(log_s2t)
        
        if t > 0:
            mu = mut + np.dot(A, Xp.T).T*lint
        else:
            mu = mut + lint*mu0
        
        return self.log_normal(Xc, mu, np.diag(s2t)) 
開發者ID:blei-lab,項目名稱:variational-smc,代碼行數:13,代碼來源:lgss_example.py

示例9: callback

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def callback(params, t, g):
        print("Iteration {} lower bound {}".format(t, -objective(params, t)))

        plt.cla()
        target_distribution = lambda x : np.exp(log_density(x, t))
        plot_isocontours(ax, target_distribution)

        mean, log_std = unpack_params(params)
        variational_contour = lambda x: mvn.pdf(x, mean, np.diag(np.exp(2*log_std)))
        plot_isocontours(ax, variational_contour)
        plt.draw()
        plt.pause(1.0/30.0) 
開發者ID:HIPS,項目名稱:autograd,代碼行數:14,代碼來源:black_box_svi.py

示例10: test_diag

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def test_diag():     combo_check(np.diag, [0])([R(5, 5)], k=[-1, 0, 1]) 
開發者ID:HIPS,項目名稱:autograd,代碼行數:3,代碼來源:test_systematic.py

示例11: test_diag_flat

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def test_diag_flat():combo_check(np.diag, [0])([R(5)],    k=[-1, 0, 1]) 
開發者ID:HIPS,項目名稱:autograd,代碼行數:3,代碼來源:test_systematic.py

示例12: test_diag

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def test_diag():
    def fun(x): return np.diag(x)
    mat = npr.randn(10, 10)
    check_grads(fun)(mat) 
開發者ID:HIPS,項目名稱:autograd,代碼行數:6,代碼來源:test_numpy.py

示例13: test_make_diagonal

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def test_make_diagonal():
    def fun(D):
        return np.make_diagonal(D, axis1=-1, axis2=-2)

    D = np.random.randn(4)
    A = np.make_diagonal(D, axis1=-1, axis2=-2)
    assert np.allclose(np.diag(A), D)
    check_grads(fun)(D)

    D = np.random.randn(3, 4)
    A = np.make_diagonal(D, axis1=-1, axis2=-2)
    assert all([np.allclose(np.diag(A[i]), D[i]) for i in range(3)])
    check_grads(fun)(D) 
開發者ID:HIPS,項目名稱:autograd,代碼行數:15,代碼來源:test_numpy.py

示例14: __init__

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def __init__(self, mean, cov):
        """
        mean: a numpy array of length d.
        cov: d x d numpy array for the covariance.
        """
        self.mean = mean 
        self.cov = cov
        assert mean.shape[0] == cov.shape[0]
        assert cov.shape[0] == cov.shape[1]
        E, V = np.linalg.eigh(cov)
        if np.any(np.abs(E) <= 1e-7):
            raise ValueError('covariance matrix is not full rank.')
        # The precision matrix
        self.prec = np.dot(np.dot(V, np.diag(old_div(1.0,E))), V.T)
        #print self.prec 
開發者ID:wittawatj,項目名稱:kernel-gof,代碼行數:17,代碼來源:density.py

示例15: likelihood

# 需要導入模塊: from autograd import numpy [as 別名]
# 或者: from autograd.numpy import diag [as 別名]
def likelihood(self, hyp): 
        M = self.M 
        Z = self.Z
        m = self.m
        S = self.S
        X_batch = self.X_batch
        y_batch = self.y_batch
        jitter = self.jitter 
        jitter_cov = self.jitter_cov
        N = X_batch.shape[0]
        
        
        logsigma_n = hyp[-1]
        sigma_n = np.exp(logsigma_n)
        
        # Compute K_u_inv
        K_u = kernel(Z, Z, hyp[:-1])    
        # K_u_inv = np.linalg.solve(K_u + np.eye(M)*jitter_cov, np.eye(M))
        L = np.linalg.cholesky(K_u + np.eye(M)*jitter_cov)    
        K_u_inv = np.linalg.solve(L.T, np.linalg.solve(L,np.eye(M)))
        
        self.K_u_inv = K_u_inv
          
        # Compute mu
        psi = kernel(Z, X_batch, hyp[:-1])    
        K_u_inv_m = np.matmul(K_u_inv,m)   
        MU = np.matmul(psi.T,K_u_inv_m)
        
        # Compute cov
        Alpha = np.matmul(K_u_inv,psi)
        COV = kernel(X_batch, X_batch, hyp[:-1]) - np.matmul(psi.T, np.matmul(K_u_inv,psi)) + \
                np.matmul(Alpha.T, np.matmul(S,Alpha))
        
        COV_inv = np.linalg.solve(COV  + np.eye(N)*sigma_n + np.eye(N)*jitter, np.eye(N))
        # L = np.linalg.cholesky(COV  + np.eye(N)*sigma_n + np.eye(N)*jitter) 
        # COV_inv = np.linalg.solve(np.transpose(L), np.linalg.solve(L,np.eye(N)))
        
        # Compute cov(Z, X)
        cov_ZX = np.matmul(S,Alpha)
        
        # Update m and S
        alpha = np.matmul(COV_inv, cov_ZX.T)
        m = m + np.matmul(cov_ZX, np.matmul(COV_inv, y_batch-MU))    
        S = S - np.matmul(cov_ZX, alpha)
        
        self.m = m
        self.S = S
        
        # Compute NLML                
        K_u_inv_m = np.matmul(K_u_inv,m)
        NLML = 0.5*np.matmul(m.T,K_u_inv_m) + np.sum(np.log(np.diag(L))) + 0.5*np.log(2.*np.pi)*M
        
        return NLML[0,0] 
開發者ID:maziarraissi,項目名稱:ParametricGP,代碼行數:55,代碼來源:parametric_GP.py


注:本文中的autograd.numpy.diag方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。