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


Python numpy.triu方法代碼示例

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


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

示例1: test_dc_gain_integrator

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def test_dc_gain_integrator(self):
        """DC gain when eigenvalue at DC returns appropriately sized array of nan."""
        # the SISO case is also tested in test_dc_gain_{cont,discr}
        import itertools
        # iterate over input and output sizes, and continuous (dt=None) and discrete (dt=True) time
        for inputs, outputs, dt in itertools.product(range(1, 6), range(1, 6), [None, True]):
            states = max(inputs, outputs)

            # a matrix that is singular at DC, and has no "useless" states as in
            # _remove_useless_states
            a = np.triu(np.tile(2, (states, states)))
            # eigenvalues all +2, except for ...
            a[0, 0] = 0 if dt is None else 1
            b = np.eye(max(inputs, states))[:states, :inputs]
            c = np.eye(max(outputs, states))[:outputs, :states]
            d = np.zeros((outputs, inputs))
            sys = StateSpace(a, b, c, d, dt)
            dc = np.squeeze(np.tile(np.nan, (outputs, inputs)))
            np.testing.assert_array_equal(dc, sys.dcgain()) 
開發者ID:python-control,項目名稱:python-control,代碼行數:21,代碼來源:statesp_test.py

示例2: __init__

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def __init__(self,in_channel):
        super(InvConv,self).__init__()

        weight=np.random.randn(in_channel,in_channel)
        q,_=linalg.qr(weight)
        w_p,w_l,w_u=linalg.lu(q.astype(np.float32))
        w_s=np.diag(w_u)
        w_u=np.triu(w_u,1)
        u_mask=np.triu(np.ones_like(w_u),1)
        l_mask=u_mask.T

        self.register_buffer('w_p',torch.from_numpy(w_p))
        self.register_buffer('u_mask',torch.from_numpy(u_mask))
        self.register_buffer('l_mask',torch.from_numpy(l_mask))
        self.register_buffer('l_eye',torch.eye(l_mask.shape[0]))
        self.register_buffer('s_sign',torch.sign(torch.from_numpy(w_s)))
        self.w_l=torch.nn.Parameter(torch.from_numpy(w_l))
        self.w_s=torch.nn.Parameter(torch.log(1e-7+torch.abs(torch.from_numpy(w_s))))
        self.w_u=torch.nn.Parameter(torch.from_numpy(w_u))

        self.weight=None
        self.invweight=None

        return 
開發者ID:joansj,項目名稱:blow,代碼行數:26,代碼來源:blow.py

示例3: test_tril_triu_ndim3

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def test_tril_triu_ndim3():
    for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
        a = np.array([
            [[1, 1], [1, 1]],
            [[1, 1], [1, 0]],
            [[1, 1], [0, 0]],
            ], dtype=dtype)
        a_tril_desired = np.array([
            [[1, 0], [1, 1]],
            [[1, 0], [1, 0]],
            [[1, 0], [0, 0]],
            ], dtype=dtype)
        a_triu_desired = np.array([
            [[1, 1], [0, 1]],
            [[1, 1], [0, 0]],
            [[1, 1], [0, 0]],
            ], dtype=dtype)
        a_triu_observed = np.triu(a)
        a_tril_observed = np.tril(a)
        assert_array_equal(a_triu_observed, a_triu_desired)
        assert_array_equal(a_tril_observed, a_tril_desired)
        assert_equal(a_triu_observed.dtype, a.dtype)
        assert_equal(a_tril_observed.dtype, a.dtype) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:25,代碼來源:test_twodim_base.py

示例4: test_tril_triu_dtype

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def test_tril_triu_dtype():
    # Issue 4916
    # tril and triu should return the same dtype as input
    for c in np.typecodes['All']:
        if c == 'V':
            continue
        arr = np.zeros((3, 3), dtype=c)
        assert_equal(np.triu(arr).dtype, arr.dtype)
        assert_equal(np.tril(arr).dtype, arr.dtype)

    # check special cases
    arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
                    ['2004-01-01T12:00', '2003-01-03T13:45']],
                   dtype='datetime64')
    assert_equal(np.triu(arr).dtype, arr.dtype)
    assert_equal(np.tril(arr).dtype, arr.dtype)

    arr = np.zeros((3,3), dtype='f4,f4')
    assert_equal(np.triu(arr).dtype, arr.dtype)
    assert_equal(np.tril(arr).dtype, arr.dtype) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:22,代碼來源:test_twodim_base.py

示例5: test_qr_li

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def test_qr_li():
    cutoff = 1.e-10
    for shape in [(5, 4), (4, 5)]:
        print('shape =', shape)
        A = np.arange(20).reshape(shape)  # linearly dependent: only two rows/columns independent
        A[3, :] = np.random.random() * (cutoff / 100)  # nearly linear dependent
        q, r = tools.math.qr_li(A)
        assert np.linalg.norm(r - np.triu(r)) == 0.
        qdq = q.T.conj().dot(q)
        assert np.linalg.norm(qdq - np.eye(len(qdq))) < 1.e-13
        assert np.linalg.norm(q.dot(r) - A) < cutoff * 20
        r, q = tools.math.rq_li(A)
        assert np.linalg.norm(r - np.triu(r, r.shape[1] - r.shape[0])) == 0.
        qqd = q.dot(q.T.conj())
        assert np.linalg.norm(qqd - np.eye(len(qqd))) < 1.e-13
        assert np.linalg.norm(r.dot(q) - A) < cutoff * 20 
開發者ID:tenpy,項目名稱:tenpy,代碼行數:18,代碼來源:test_tools.py

示例6: test_tril_triu_ndim3

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def test_tril_triu_ndim3():
    for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
        a = np.array([
            [[1, 1], [1, 1]],
            [[1, 1], [1, 0]],
            [[1, 1], [0, 0]],
            ], dtype=dtype)
        a_tril_desired = np.array([
            [[1, 0], [1, 1]],
            [[1, 0], [1, 0]],
            [[1, 0], [0, 0]],
            ], dtype=dtype)
        a_triu_desired = np.array([
            [[1, 1], [0, 1]],
            [[1, 1], [0, 0]],
            [[1, 1], [0, 0]],
            ], dtype=dtype)
        a_triu_observed = np.triu(a)
        a_tril_observed = np.tril(a)
        yield assert_array_equal, a_triu_observed, a_triu_desired
        yield assert_array_equal, a_tril_observed, a_tril_desired
        yield assert_equal, a_triu_observed.dtype, a.dtype
        yield assert_equal, a_tril_observed.dtype, a.dtype 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:25,代碼來源:test_twodim_base.py

示例7: _get_attn_subsequent_mask

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def _get_attn_subsequent_mask(self, size):
        """
        Get an attention mask to avoid using the subsequent info.

        Args:
            size: int

        Returns:
            (`LongTensor`):

            * subsequent_mask `[1 x size x size]`
        """
        attn_shape = (1, size, size)
        subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
        subsequent_mask = torch.from_numpy(subsequent_mask)
        return subsequent_mask 
開發者ID:lizekang,項目名稱:ITDD,代碼行數:18,代碼來源:transformer.py

示例8: test_bravyi_kitaev_fast_edgeoperator_Bi

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def test_bravyi_kitaev_fast_edgeoperator_Bi(self):
        # checking the edge operators
        edge_matrix = numpy.triu(numpy.ones((4, 4)))
        edge_matrix_indices = numpy.array(
            numpy.nonzero(numpy.triu(edge_matrix) -
                          numpy.diag(numpy.diag(edge_matrix))))

        correct_operators_b0 = ((0, 'Z'), (1, 'Z'), (2, 'Z'))
        correct_operators_b1 = ((0, 'Z'), (3, 'Z'), (4, 'Z'))
        correct_operators_b2 = ((1, 'Z'), (3, 'Z'), (5, 'Z'))
        correct_operators_b3 = ((2, 'Z'), (4, 'Z'), (5, 'Z'))

        qterm_b0 = QubitOperator(correct_operators_b0, 1)
        qterm_b1 = QubitOperator(correct_operators_b1, 1)
        qterm_b2 = QubitOperator(correct_operators_b2, 1)
        qterm_b3 = QubitOperator(correct_operators_b3, 1)
        self.assertTrue(qterm_b0 ==
                        _bksf.edge_operator_b(edge_matrix_indices, 0))
        self.assertTrue(qterm_b1 ==
                        _bksf.edge_operator_b(edge_matrix_indices, 1))
        self.assertTrue(qterm_b2 ==
                        _bksf.edge_operator_b(edge_matrix_indices, 2))
        self.assertTrue(qterm_b3 ==
                        _bksf.edge_operator_b(edge_matrix_indices, 3)) 
開發者ID:quantumlib,項目名稱:OpenFermion,代碼行數:26,代碼來源:_bksf_test.py

示例9: _cov_matrix_diagonalization

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def _cov_matrix_diagonalization(self):
        # Decomposition of cov_matrix into coords_matrix*diag(scaling_diag.^2)*coords_matrix'
        # (diagonalization)
        if (
            self._count_eval - self.n_eigen_eval
            > self.pop_size / (self.lr_covrank1_const + self.lr_mu_const) / self.n_dims / 10
        ):
            self.n_eigen_eval = self._count_eval
            self.cov_matrix = numpy.triu(self.cov_matrix) + numpy.triu(self.cov_matrix, 1).T
            eigvals, eigvects = numpy.linalg.eig(self.cov_matrix)
            self.scaling_diag = numpy.diag(eigvals)  # [::-1])
            self.coords_matrix = eigvects  # [:, ::-1]
            assert numpy.abs(numpy.imag(self.coords_matrix).sum()) == 0, self.coords_matrix
            assert numpy.abs(numpy.imag(self.scaling_diag).sum()) == 0, self.scaling_diag
            self.scaling_diag = numpy.sqrt(numpy.diag(self.scaling_diag)).reshape(-1, 1)
            self.invsqrtC = numpy.matmul(
                numpy.matmul(self.coords_matrix, numpy.diag(self.scaling_diag.flatten() ** -1)),
                self.coords_matrix.T,
            ) 
開發者ID:FragileTech,項目名稱:fragile,代碼行數:21,代碼來源:models.py

示例10: test_syrk

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def test_syrk(self):
        for f in _get_func('syrk'):
            c = f(a=self.a, alpha=1.)
            assert_array_almost_equal(np.triu(c), np.triu(self.t))

            c = f(a=self.a, alpha=1., lower=1)
            assert_array_almost_equal(np.tril(c), np.tril(self.t))

            c0 = np.ones(self.t.shape)
            c = f(a=self.a, alpha=1., beta=1., c=c0)
            assert_array_almost_equal(np.triu(c), np.triu(self.t+c0))

            c = f(a=self.a, alpha=1., trans=1)
            assert_array_almost_equal(np.triu(c), np.triu(self.tt))

    #prints '0-th dimension must be fixed to 3 but got 5', FIXME: suppress?
    # FIXME: how to catch the _fblas.error? 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:19,代碼來源:test_blas.py

示例11: subsequent_mask

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def subsequent_mask(size):
    "Mask out subsequent positions."
    attn_shape = (1, size, size)
    subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
    return torch.from_numpy(subsequent_mask) == 0 
開發者ID:Nrgeup,項目名稱:controllable-text-attribute-transfer,代碼行數:7,代碼來源:model2.py

示例12: edgeFailSampling

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def edgeFailSampling(W, p):
    """
    edgeFailSampling: randomly delete the edges of a given graph
    
    Input:
        W (np.array): adjacency matrix
        p (float): probability of deleting an edge
    
    Output:
        W (np.array): adjacency matrix with some edges randomly deleted
        
    Obs.: The resulting graph need not be connected (even if the input graph is)
    """
    
    assert 0 <= p <= 1
    N = W.shape[0]
    assert W.shape[1] == N
    undirected = np.allclose(W, W.T, atol = zeroTolerance)
    
    maskEdges = np.random.rand(N, N)
    maskEdges = (maskEdges > p).astype(W.dtype) # Put a 1 with probability 1-p
    
    W = maskEdges * W
    if undirected:
        W = np.triu(W)
        W = W + W.T
        
    return W 
開發者ID:alelab-upenn,項目名稱:graph-neural-networks,代碼行數:30,代碼來源:graphTools.py

示例13: __init__

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def __init__(self, graphType, N, graphOptions):
        assert N > 0
        #\\\ Create the graph (Outputs adjacency matrix):
        self.W = createGraph(graphType, N, graphOptions)
        # TODO: Let's start easy: make it just an N x N matrix. We'll see later
        # the rest of the things just as handling multiple features and stuff.
        #\\\ Number of nodes:
        self.N = (self.W).shape[0]
        #\\\ Bool for graph being undirected:
        self.undirected = np.allclose(self.W, (self.W).T, atol = zeroTolerance)
        #   np.allclose() gives true if matrices W and W.T are the same up to
        #   atol.
        #\\\ Bool for graph having self-loops:
        self.selfLoops = True \
                        if np.sum(np.abs(np.diag(self.W)) > zeroTolerance) > 0 \
                        else False
        #\\\ Degree matrix:
        self.D = np.diag(np.sum(self.W, axis = 1))
        #\\\ Number of edges:
        self.M = int(np.sum(np.triu(self.W)) if self.undirected \
                                                    else np.sum(self.W))
        #\\\ Unweighted adjacency:
        self.A = (np.abs(self.W) > 0).astype(self.W.dtype)
        #\\\ Laplacian matrix:
        #   Only if the graph is undirected and has no self-loops
        if self.undirected and not self.selfLoops:
            self.L = adjacencyToLaplacian(self.W)
        else:
            self.L = None
        #\\\ GSO (Graph Shift Operator):
        #   The weighted adjacency matrix by default
        self.S = self.W
        #\\\ GFT: Declare variables but do not compute it unless specifically
        # requested
        self.E = None # Eigenvalues
        self.V = None # Eigenvectors 
開發者ID:alelab-upenn,項目名稱:graph-neural-networks,代碼行數:38,代碼來源:graphTools.py

示例14: subsequent_mask

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def subsequent_mask(size):
    """Mask out subsequent positions."""
    attn_shape = (1, size, size)
    subseq_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
    return torch.from_numpy(subseq_mask) == 0 
開發者ID:dolphin-zs,項目名稱:Doc2EDAG,代碼行數:7,代碼來源:transformer.py

示例15: subsequent_mask

# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import triu [as 別名]
def subsequent_mask(size):
    """Mask out subsequent positions."""
    attn_shape = (1, size, size)
    subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8')
    return torch.from_numpy(subsequent_mask) == 0 
開發者ID:nadavbh12,項目名稱:Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch,代碼行數:7,代碼來源:annotated_attention.py


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