本文整理汇总了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())
示例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
示例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)
示例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)
示例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
示例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
示例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
示例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))
示例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,
)
示例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?
示例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
示例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
示例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
示例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
示例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