python类tril()的实例源码

model.py 文件源码 项目:qrn 作者: uwnlp 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__(self, batch_size, mem_size, hidden_size):
        self.hidden_size = hidden_size
        self.mem_size = mem_size
        self.batch_size = batch_size
        N, M, d = batch_size, mem_size, hidden_size
        self.L = np.tril(np.ones([M, M], dtype='float32'))
        self.sL = np.tril(np.ones([M, M], dtype='float32'), k=-1)
model.py 文件源码 项目:qrn 作者: uwnlp 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def __init__(self, batch_size, mem_size, hidden_size):
        self.hidden_size = hidden_size
        self.mem_size = mem_size
        self.batch_size = batch_size
        N, M, d = batch_size, mem_size, hidden_size
        self.L = np.tril(np.ones([M, M], dtype='float32'))
        self.sL = np.tril(np.ones([M, M], dtype='float32'), k=-1)
model.py 文件源码 项目:qrn 作者: uwnlp 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, batch_size, mem_size, hidden_size):
        self.hidden_size = hidden_size
        self.mem_size = mem_size
        self.batch_size = batch_size
        N, M, d = batch_size, mem_size, hidden_size
        self.L = np.tril(np.ones([M, M], dtype='float32'))
        self.sL = np.tril(np.ones([M, M], dtype='float32'), k=-1)
model.py 文件源码 项目:qrn 作者: uwnlp 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def __init__(self, batch_size, mem_size, hidden_size):
        self.hidden_size = hidden_size
        self.mem_size = mem_size
        self.batch_size = batch_size
        N, M, d = batch_size, mem_size, hidden_size
        self.L = np.tril(np.ones([M, M], dtype='float32'))
        self.sL = np.tril(np.ones([M, M], dtype='float32'), k=-1)
ROI_Kalman_smoothing.py 文件源码 项目:MEEG_connectivity 作者: YingYang 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def grad_AQ(A, Gamma, B1_B3, B2, T,q, prior_A, prior_Q, flag_A_time_vary,
            A_flag = True, Q_flag = True):
    grad_A = np.zeros(A.shape)
    grad_Gamma = np.zeros(Gamma.shape)
    eig_Q_sqrt, inv_Q = _util_obj_grad_AQ(Gamma)
    if A_flag:
        if flag_A_time_vary:
            for t in range(T):
                # 2 Q^-1(-B2 + A B_3)
                grad_A[t] = 2.0*np.dot(inv_Q, (-B2[t]+ A[t].dot(B1_B3[t])))
                if prior_A is not None:
                    # gradient of lambda0||A_t||_F^2 + lambda1||A_t-A_t_1||_F^2 
                    # 2 lambda0 A_t + 2 lambda1 ((A_t_1 + A_t+1)-2 A_t)                    
                    grad_A[t] += 2.0* prior_A['lambda0'] *A[t]
                    if t > 0 and t < T-1:
                        #grad_A[t] += 2.0*prior_A['lambda1']*(A[t-1]+A[t+1]-2.0*A[t])
                        # correction on May 19
                        grad_A[t] += 2.0*prior_A['lambda1']*(2.0*A[t]-A[t-1]-A[t+1])
                    elif t == 0:
                        grad_A[t] += 2.0*prior_A['lambda1']*(A[0]-A[1])
                    else: # t = t-1
                        grad_A[t] += 2.0*prior_A['lambda1']*(A[t]-A[t-1])
        else:
            grad_A = 2.0*np.dot(inv_Q, (-B2.sum(axis=0)+A.dot((B1_B3[0:T]).sum(axis = 0))))
            if prior_A is not None:
                grad_A += 2.0*prior_A['lambda0']        
    if Q_flag:
        # gradient = Q^{-1}(qT I-\sum_T B1t-AtB2t^T-B2tAt^T + AtB3tAt^T)Q^{-1}) Gamma
        tmp = _util_obj_grad_AQ_sum_B123(A, B1_B3, B2, T, flag_A_time_vary)
        grad_Gamma = reduce(np.dot, [inv_Q, (np.eye(inv_Q.shape[0])*np.float(q)*np.float(T)-tmp.dot(inv_Q)), Gamma])
        if prior_Q is not None:
            grad_Gamma += grad_prior_Q(Gamma, prior_Q)
        grad_Gamma = np.tril(grad_Gamma)                    
    return grad_A, grad_Gamma

#=========== gradient descent with back tracking for AQ =============
ROI_Kalman_smoothing.py 文件源码 项目:MEEG_connectivity 作者: YingYang 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def grad_Q0(Gamma0, B7, q, prior_Q0):
    eig_Q_sqrt, inv_Q = _util_obj_grad_AQ(Gamma0)
    grad_Gamma0 = reduce(np.dot, [inv_Q, np.float(q)*np.eye(Gamma0.shape[0])- B7.dot(inv_Q), Gamma0])
    if prior_Q0 is not None:
        grad_Gamma0 += grad_prior_Q(Gamma0, prior_Q0)
    grad_Gamma0 = np.tril(grad_Gamma0)
    return grad_Gamma0
# gradient descent
Bayesian_Source_Space_Connectivity_InvWishart_Prior.py 文件源码 项目:MEEG_connectivity 作者: YingYang 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def get_neg_log_post_grad_Qu(Phi, G, MMT, q, Sigma_E, L,Sigma_J, nu, V, GL, prior_on = False):
    """
        Just get the gradient for Qu, ignore other variables
    """
    p = Phi.shape[0]
    Qu = Phi.dot(Phi.T)
    G_Sigma_G = np.zeros(MMT.shape)
    for i in range(Sigma_J.size):
        G_Sigma_G += Sigma_J[i]* np.outer(G[:,i], G[:,i])
    cov = Sigma_E + G_Sigma_G + GL.dot(Qu).dot(GL.T) 
    inv_cov = np.linalg.inv(cov)
    GLT_inv_cov = np.dot(GL.T, inv_cov)
    invQ = np.linalg.inv(Qu)
    if prior_on:
        grad0 = (q*  GL.T.dot(inv_cov).dot(GL) - GLT_inv_cov.dot(MMT).dot(GLT_inv_cov.T) \
           + invQ.dot( (nu+p+1) *np.eye(p) - V.dot(invQ)))
    else:
        grad0 = q* GL.T.dot(inv_cov).dot(GL) - GLT_inv_cov.dot(MMT).dot(GLT_inv_cov.T)
    grad1 = 2.0* grad0.dot(Phi)
    # cholesky decomposition is lower triangular
    grad = np.tril(grad1)
    return grad

#==============================================================================
# gradient descent optimization, using back track
# only update Qu
ROI_Kalman_smoothing.py 文件源码 项目:MEEG_connectivity 作者: YingYang 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def grad_Q0(Gamma0, B7, q, prior_Q0):
    eig_Q_sqrt, inv_Q = _util_obj_grad_AQ(Gamma0)
    grad_Gamma0 = reduce(np.dot, [inv_Q, np.float(q)*np.eye(Gamma0.shape[0])- B7.dot(inv_Q), Gamma0])
    if prior_Q0 is not None:
        grad_Gamma0 += grad_prior_Q(Gamma0, prior_Q0)
    grad_Gamma0 = np.tril(grad_Gamma0)
    return grad_Gamma0
# gradient descent
Bayesian_Source_Space_Connectivity_InvWishart_Prior.py 文件源码 项目:MEEG_connectivity 作者: YingYang 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def get_neg_log_post_grad_Qu(Phi, G, MMT, q, Sigma_E, L,Sigma_J, nu, V, GL, prior_on = False):
    """
        Just get the gradient for Qu, ignore other variables
    """
    p = Phi.shape[0]
    Qu = Phi.dot(Phi.T)
    G_Sigma_G = np.zeros(MMT.shape)
    for i in range(Sigma_J.size):
        G_Sigma_G += Sigma_J[i]* np.outer(G[:,i], G[:,i])
    cov = Sigma_E + G_Sigma_G + GL.dot(Qu).dot(GL.T) 
    inv_cov = np.linalg.inv(cov)
    GLT_inv_cov = np.dot(GL.T, inv_cov)
    invQ = np.linalg.inv(Qu)
    if prior_on:
        grad0 = (q*  GL.T.dot(inv_cov).dot(GL) - GLT_inv_cov.dot(MMT).dot(GLT_inv_cov.T) \
           + invQ.dot( (nu+p+1) *np.eye(p) - V.dot(invQ)))
    else:
        grad0 = q* GL.T.dot(inv_cov).dot(GL) - GLT_inv_cov.dot(MMT).dot(GLT_inv_cov.T)
    grad1 = 2.0* grad0.dot(Phi)
    # cholesky decomposition is lower triangular
    grad = np.tril(grad1)
    return grad

#==============================================================================
# gradient descent optimization, using back track
# only update Qu
Timbral_Roughness.py 文件源码 项目:timbral_models 作者: AudioCommons 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def plomp(f1, f2):
    b1 = 3.51
    b2 = 5.75
    xstar = 0.24
    s1 = 0.0207
    s2 = 18.96
    s = np.tril(xstar / ((s1 * np.minimum(f1, f2)) + s2))
    pd = np.exp(-b1 * s * np.abs(f2 - f1)) - np.exp(-b2 * s * np.abs(f2 - f1))
    return pd
test_twodim_base.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_tril_triu_ndim2():
    for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
        a = np.ones((2, 2), dtype=dtype)
        b = np.tril(a)
        c = np.triu(a)
        yield assert_array_equal, b, [[1, 0], [1, 1]]
        yield assert_array_equal, c, b.T
        # should return the same dtype as the original array
        yield assert_equal, b.dtype, a.dtype
        yield assert_equal, c.dtype, a.dtype
test_twodim_base.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_tril_triu_with_inf():
    # Issue 4859
    arr = np.array([[1, 1, np.inf],
                    [1, 1, 1],
                    [np.inf, 1, 1]])
    out_tril = np.array([[1, 0, 0],
                         [1, 1, 0],
                         [np.inf, 1, 1]])
    out_triu = out_tril.T
    assert_array_equal(np.triu(arr), out_triu)
    assert_array_equal(np.tril(arr), out_tril)
optnet-single.py 文件源码 项目:optnet 作者: locuslab 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def prof_instance(nz, neq, nineq, nIter, cuda):
    L = np.tril(npr.uniform(0,1, (nz,nz))) + np.eye(nz,nz)
    G = npr.randn(nineq,nz)
    A = npr.randn(neq,nz)
    z0 = npr.randn(nz)
    s0 = np.ones(nineq)
    p = npr.randn(nz)

    p, L, G, A, z0, s0 = [torch.Tensor(x) for x in [p, L, G, A, z0, s0]]
    Q = torch.mm(L, L.t())+0.001*torch.eye(nz).type_as(L)
    if cuda:
        p, L, Q, G, A, z0, s0 = [x.cuda() for x in [p, L, Q, G, A, z0, s0]]

    af = adact.AdactFunction()

    start = time.time()
    # One-time cost for numpy conversion.
    p_np, L_np, G_np, A_np, z0_np, s0_np = [adact.toNp(v) for v in [p, L, G, A, z0, s0]]
    cp = time.time()-start
    for i in range(nIter):
        start = time.time()
        zhat, nu, lam = af.forward_single_np(p_np, L_np, G_np, A_np, z0_np, s0_np)
        cp += time.time()-start

    b = torch.mv(A, z0) if neq > 0 else None
    h = torch.mv(G, z0)+s0
    L_Q, L_S, R = aip.pre_factor_kkt(Q, G, A, nineq, neq)
    pdipm = []
    for i in range(nIter):
        start = time.time()
        zhat_ip, nu_ip, lam_ip = aip.forward_single(p, Q, G, A, b, h, L_Q, L_S, R)
        pdipm.append(time.time()-start)
    return cp, np.sum(pdipm)
test_uncertain_conditional.py 文件源码 项目:GPflow 作者: GPflow 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def gen_L(rng, n, *shape):
    return np.array([np.tril(rng.randn(*shape)) for _ in range(n)])
test_uncertain_conditional.py 文件源码 项目:GPflow 作者: GPflow 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def gen_q_sqrt(rng, D_out, *shape):
    q_sqrt = np.array([np.tril(rng.randn(*shape)) for _ in range(D_out)])
    return np.transpose(q_sqrt, [1, 2, 0])
models_learners.py 文件源码 项目:smp_base 作者: x75 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def update(self, x):
        """Single step learning update"""
        # print x.shape
        x = x.reshape((self.ndims, 1))
        y = np.dot(self.w.T, x)
        # GHA rule in matrix form
        d_w = self.anneal(self.cnt) * self.eta * (np.dot(x, y.T) - np.dot(self.w, np.tril(np.dot(y, y.T))))
        self.w += d_w
        self.cnt += 1
        return y
lingam.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def _cost_strictly_lower_triangular(b):
        return np.sum((np.tril(b, -1) - b) ** 2)
communityDetectionEngine.py 文件源码 项目:ECoG-ClusterFlow 作者: sugeerth 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def histogram(self,matrix):
        matrix= np.tril(matrix)

        matrix1 = matrix.flatten()
        nonZeroValues = np.flatnonzero(matrix1)
        matrix1 = matrix1[nonZeroValues]

        unique, counts = np.unique(matrix1, return_counts=True)

        self.HistogramValues = dict()
        for i,j in np.asarray((unique, counts)).T: 
            self.HistogramValues[float(format(i, '.1f'))] = j
GraphDataStructure.py 文件源码 项目:ECoG-ClusterFlow 作者: sugeerth 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def Find_HighlightedEdges(self,weight = 0):
        self.ThresholdData = np.copy(self.data)
        # low_values_indices = self.ThresholdData < weight  # Where values are low
        # self.ThresholdData[low_values_indices] = 0
    # graterindices = [ (i,j) for i,j in np.ndenumerate(self.ThresholdData) if any(i > j) ] 
        # self.ThresholdData[graterindices[:1]] = 0
        # self.ThresholdData = np.tril(self.ThresholdData)
        # print self.ThresholdData, "is the data same??" 
        """
        test 2 highlighted edges there
        """
        # np.savetxt('test2.txt', self.ThresholdData, delimiter=',', fmt='%1.4e')
        self.g = nx.from_numpy_matrix(self.ThresholdData)
weight_initializer.py 文件源码 项目:Sisyphus 作者: davidbrandfonbrener 项目源码 文件源码 阅读 72 收藏 0 点赞 0 评论 0
def feed_forward(self,sigma=.1):
        '''Generate random feedforward wrec (lower triangular)'''

        N_in    = self.N_in
        N_rec   = self.N_rec
        N_out   = self.N_out

        weights_path = self.init_weights_path

        #Uniform between -.1 and .1
        W_in = .2*np.random.rand(N_rec,N_in) - .1
        W_out = .2*np.random.rand(N_out,N_rec) - .1

        b_rec = np.zeros(N_rec)
        b_out = np.zeros(N_out)

        init_state = .1 + .01*np.random.randn(N_rec)

        W_rec = np.tril(sigma*np.random.randn(N_rec,N_rec),-1)

        input_Connectivity = np.ones([N_rec,N_in])
        rec_Connectivity = np.ones([N_rec,N_rec])
        output_Connectivity = np.ones([N_out,N_rec])

        if not self.autapses:
            W_rec[np.eye(N_rec)==1] = 0
            rec_Connectivity[np.eye(N_rec)==1] = 0

        np.savez(weights_path, W_in = W_in,
                                W_rec = W_rec,
                                W_out = W_out,
                                b_rec = b_rec,
                                b_out = b_out,
                                init_state = init_state,
                                input_Connectivity = input_Connectivity,
                                rec_Connectivity= rec_Connectivity,
                                output_Connectivity=output_Connectivity)

        return weights_path


问题


面经


文章

微信
公众号

扫码关注公众号