本文整理汇总了Python中numpy.identity方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.identity方法的具体用法?Python numpy.identity怎么用?Python numpy.identity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.identity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def __init__(self, n, order=5, mu=0.1, eps=0.001, w="random"):
self.kind = "AP filter"
self.n = self.check_int(
n,'The size of filter must be an integer')
self.order = self.check_int(
order, 'The order of projection must be an integer')
self.mu = self.check_float_param(mu, 0, 1000, "mu")
self.eps = self.check_float_param(eps, 0, 1000, "eps")
self.init_weights(w, self.n)
self.w_history = False
self.x_mem = np.zeros((self.n, self.order))
self.d_mem = np.zeros(order)
self.ide_eps = self.eps * np.identity(self.order)
self.ide = np.identity(self.order)
self.y_mem = False
self.e_mem = False
示例2: optimize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def optimize(self, sess, feed_dict):
reg_input, reg_weight, old_values, targets = sess.run(
[self.inputs, self.regression_weight, self.values, self.targets],
feed_dict=feed_dict)
intended_values = targets * self.mix_frac + old_values * (1 - self.mix_frac)
# taken from rllab
reg_coeff = 1e-5
for _ in range(5):
best_fit_weight = np.linalg.lstsq(
reg_input.T.dot(reg_input) +
reg_coeff * np.identity(reg_input.shape[1]),
reg_input.T.dot(intended_values))[0]
if not np.any(np.isnan(best_fit_weight)):
break
reg_coeff *= 10
if len(best_fit_weight.shape) == 1:
best_fit_weight = np.expand_dims(best_fit_weight, -1)
sess.run(self.update_regression_weight,
feed_dict={self.new_regression_weight: best_fit_weight})
示例3: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def __init__(self, kernel_size, segment_num=None):
"""
Args:
input_size: dimention of input embedding
kernel_size: kernel_size for CNN
padding: padding for CNN
hidden_size: hidden size
"""
super().__init__()
self.segment_num = segment_num
if self.segment_num != None:
self.mask_embedding = nn.Embedding(segment_num + 1, segment_num)
self.mask_embedding.weight.data.copy_(torch.FloatTensor(np.concatenate([np.zeros((1, segment_num)), np.identity(segment_num)], axis=0)))
self.mask_embedding.weight.requires_grad = False
self._minus = -100
self.pool = nn.MaxPool1d(kernel_size)
示例4: quat2mat
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def quat2mat(quaternion):
"""
Converts given quaternion (x, y, z, w) to matrix.
Args:
quaternion: vec4 float angles
Returns:
3x3 rotation matrix
"""
q = np.array(quaternion, dtype=np.float32, copy=True)[[3, 0, 1, 2]]
n = np.dot(q, q)
if n < EPS:
return np.identity(3)
q *= math.sqrt(2.0 / n)
q = np.outer(q, q)
return np.array(
[
[1.0 - q[2, 2] - q[3, 3], q[1, 2] - q[3, 0], q[1, 3] + q[2, 0]],
[q[1, 2] + q[3, 0], 1.0 - q[1, 1] - q[3, 3], q[2, 3] - q[1, 0]],
[q[1, 3] - q[2, 0], q[2, 3] + q[1, 0], 1.0 - q[1, 1] - q[2, 2]],
]
)
示例5: test_linear_sum_assignment_input_validation
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def test_linear_sum_assignment_input_validation():
assert_raises(ValueError, linear_sum_assignment, [1, 2, 3])
C = [[1, 2, 3], [4, 5, 6]]
assert_array_equal(linear_sum_assignment(C), linear_sum_assignment(np.asarray(C)))
# assert_array_equal(linear_sum_assignment(C),
# linear_sum_assignment(matrix(C)))
I = np.identity(3)
assert_array_equal(linear_sum_assignment(I.astype(np.bool)), linear_sum_assignment(I))
assert_raises(ValueError, linear_sum_assignment, I.astype(str))
I[0][0] = np.nan
assert_raises(ValueError, linear_sum_assignment, I)
I = np.identity(3)
I[1][1] = np.inf
assert_raises(ValueError, linear_sum_assignment, I)
示例6: lowdin
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def lowdin(s):
e, v = numpy.linalg.eigh(s)
return numpy.dot(v/numpy.sqrt(e), v.T.conj())
#def scdmU(coeff,ova):
# aux = numpy.identity(ova.shape[0])
# #aux = lowdin(ova)
# no = coeff.shape[1]
# ova = reduce(numpy.dot,(coeff.T,ova,aux))
# # ova = no*nb
# q,r,piv = scipy.linalg.qr(ova, pivoting=True)
# # In fact, it is just "Lowdin-orthonormalized PAO".
# bc = ova[:,piv[:no]]
# ova = numpy.dot(bc.T,bc)
# s12inv = lowdin(ova)
# u = numpy.dot(bc,s12inv)
# return u
#------------------------------------------------
# Boys/PM-Localization
#------------------------------------------------
示例7: lowdinPop
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def lowdinPop(mol,coeff,ova,enorb,occ):
print '\nLowdin population for LMOs:'
nb,nc = coeff.shape
s12 = sqrtm(ova)
lcoeff = s12.dot(coeff)
diff = reduce(numpy.dot,(lcoeff.T,lcoeff)) - numpy.identity(nc)
print 'diff=',numpy.linalg.norm(diff)
pthresh = 0.05
labels = mol.ao_labels(None)
nelec = 0.0
for iorb in range(nc):
vec = lcoeff[:,iorb]**2
idx = list(numpy.argwhere(vec>pthresh))
print ' iorb=',iorb,' occ=',occ[iorb],' <i|F|i>=',enorb[iorb]
for iao in idx:
print ' iao=',labels[iao],' pop=',vec[iao]
nelec += occ[iorb]
print 'nelec=',nelec
return 0
示例8: _test_ip_diag
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def _test_ip_diag(self,kmf,kshift=0):
cc = kccsd.KUCCSD(kmf)
Ecc = cc.kernel()[0]
eom = kccsd_uhf.EOMIP(cc)
imds = eom.make_imds()
t1a,t1b = imds.t1
nkpts, nocc_a, nvir_a = t1a.shape
nkpts, nocc_b, nvir_b = t1b.shape
nocc = nocc_a + nocc_b
diag = kccsd_uhf.ipccsd_diag(eom,kshift,imds=imds)
I = np.identity(diag.shape[0],dtype=complex)
indices = np.arange(diag.shape[0])
H = np.zeros((I.shape[0],len(indices)),dtype=complex)
for j,idx in enumerate(indices):
H[:,j] = kccsd_uhf.ipccsd_matvec(eom,I[:,idx],kshift,imds=imds)
diag_ref = np.zeros(len(indices),dtype=complex)
diag_out = np.zeros(len(indices),dtype=complex)
for j,idx in enumerate(indices):
diag_ref[j] = H[idx,j]
diag_out[j] = diag[idx]
diff = np.linalg.norm(diag_ref - diag_out)
self.assertTrue(abs(diff) < KGCCSD_TEST_THRESHOLD,"Difference in IP diag: {}".format(diff))
示例9: _test_ip_diag
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def _test_ip_diag(self,cc):
eom = kccsd_ghf.EOMIP(cc)
imds = eom.make_imds()
nkpts, nocc, nvir = imds.t1.shape
diag = kccsd_ghf.ipccsd_diag(eom,0,imds=imds)
I = np.identity(diag.shape[0],dtype=complex)
indices = np.arange(len(diag))
H = np.zeros((I.shape[0],len(indices)),dtype=complex)
for j,idx in enumerate(indices):
H[:,j] = kccsd_ghf.ipccsd_matvec(eom,I[:,idx],0,imds=imds)
diag_ref = np.zeros(len(indices),dtype=complex)
diag_out = np.zeros(len(indices),dtype=complex)
for j,idx in enumerate(indices):
diag_ref[j] = H[idx,j]
diag_out[j] = diag[idx]
diff = np.linalg.norm(diag_ref - diag_out)
self.assertTrue(abs(diff) < KGCCSD_TEST_THRESHOLD,"Difference in IP diag: {}".format(diff))
示例10: _test_ea_diag
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def _test_ea_diag(self,cc):
eom = kccsd_ghf.EOMEA(cc)
imds = eom.make_imds()
nkpts, nocc, nvir = imds.t1.shape
diag = kccsd_ghf.eaccsd_diag(eom,0,imds=imds)
I = np.identity(diag.shape[0],dtype=complex)
indices = np.arange(len(diag))
H = np.zeros((I.shape[0],len(indices)),dtype=complex)
for j,idx in enumerate(indices):
H[:,j] = kccsd_ghf.eaccsd_matvec(eom,I[:,idx],0,imds=imds)
diag_ref = np.zeros(len(indices),dtype=complex)
diag_out = np.zeros(len(indices),dtype=complex)
for j,idx in enumerate(indices):
diag_ref[j] = H[idx,j]
diag_out[j] = diag[idx]
diff = np.linalg.norm(diag_ref - diag_out)
self.assertTrue(abs(diff) < KGCCSD_TEST_THRESHOLD,"Difference in EA diag: {}".format(diff))
示例11: test_cell_n2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def test_cell_n2(L=5, mesh=[9]*3):
cell = pbcgto.Cell()
cell.unit = 'B'
cell.atom.extend([['O', (L/2., L/2., L/2.)],
['H', (L/2.-0.689440, L/2.+0.578509, L/2.)],
['H', (L/2.+0.689440, L/2.-0.578509, L/2.)],
])
cell.a = L * np.identity(3)
cell.basis = 'sto-3g'
cell.pseudo = 'gth-pade'
cell.mesh = mesh
cell.output = '/dev/null'
cell.build()
return cell
示例12: _make_dm123
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def _make_dm123(self, state, norb, nelec, link_index=None, **kwargs):
r"""Note this function does NOT compute the standard density matrix.
The density matrices are reordered to match the the fci.rdm.make_dm123
function (used by NEVPT code).
The returned "2pdm" is :math:`\langle p^\dagger q r^\dagger s\rangle`;
The returned "3pdm" is :math:`\langle p^\dagger q r^\dagger s t^\dagger u\rangle`.
"""
onepdm, twopdm, threepdm = self.make_rdm123(state, norb, nelec, None, **kwargs)
threepdm = numpy.einsum("mkijln->ijklmn", threepdm).copy()
threepdm += numpy.einsum("jk,lm,in->ijklmn", numpy.identity(norb), numpy.identity(norb), onepdm)
threepdm += numpy.einsum("jk,miln->ijklmn", numpy.identity(norb), twopdm)
threepdm += numpy.einsum("lm,kijn->ijklmn", numpy.identity(norb), twopdm)
threepdm += numpy.einsum("jm,kinl->ijklmn", numpy.identity(norb), twopdm)
twopdm = numpy.einsum("iklj->ijkl", twopdm) + numpy.einsum("li,jk->ijkl", onepdm, numpy.identity(norb))
return onepdm, twopdm, threepdm
示例13: _make_dm123
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def _make_dm123(self, state, norb, nelec, link_index=None, **kwargs):
r'''Note this function does NOT compute the standard density matrix.
The density matrices are reordered to match the the fci.rdm.make_dm123
function (used by NEVPT code).
The returned "2pdm" is :math:`\langle p^\dagger q r^\dagger s\rangle`;
The returned "3pdm" is :math:`\langle p^\dagger q r^\dagger s t^\dagger u\rangle`.
'''
onepdm, twopdm, threepdm = self.make_rdm123(state, norb, nelec, None, **kwargs)
threepdm = numpy.einsum('mkijln->ijklmn',threepdm).copy()
threepdm += numpy.einsum('jk,lm,in->ijklmn',numpy.identity(norb),numpy.identity(norb),onepdm)
threepdm += numpy.einsum('jk,miln->ijklmn',numpy.identity(norb),twopdm)
threepdm += numpy.einsum('lm,kijn->ijklmn',numpy.identity(norb),twopdm)
threepdm += numpy.einsum('jm,kinl->ijklmn',numpy.identity(norb),twopdm)
twopdm =(numpy.einsum('iklj->ijkl',twopdm)
+ numpy.einsum('li,jk->ijkl',onepdm,numpy.identity(norb)))
return onepdm, twopdm, threepdm
示例14: get_init_guess
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def get_init_guess(self, nroots=1, diag=None, ascending = True):
if diag is None :
diag = self.ea_adc_diag()
idx = None
if ascending:
idx = np.argsort(diag)
else:
idx = np.argsort(diag)[::-1]
guess = np.zeros((diag.shape[0], nroots))
min_shape = min(diag.shape[0], nroots)
guess[:min_shape,:min_shape] = np.identity(min_shape)
g = np.zeros((diag.shape[0], nroots))
g[idx] = guess.copy()
guess = []
for p in range(g.shape[1]):
guess.append(g[:,p])
return guess
示例15: cluster
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import identity [as 别名]
def cluster(self, vectors, assign_clusters=False, trace=False):
assert len(vectors) > 0
# normalise the vectors
if self._should_normalise:
vectors = map(self._normalise, vectors)
# use SVD to reduce the dimensionality
if self._svd_dimensions and self._svd_dimensions < len(vectors[0]):
[u, d, vt] = linalg.svd(numpy.transpose(array(vectors)))
S = d[:self._svd_dimensions] * \
numpy.identity(self._svd_dimensions, numpy.Float64)
T = u[:,:self._svd_dimensions]
Dt = vt[:self._svd_dimensions,:]
vectors = numpy.transpose(numpy.matrixmultiply(S, Dt))
self._Tt = numpy.transpose(T)
# call abstract method to cluster the vectors
self.cluster_vectorspace(vectors, trace)
# assign the vectors to clusters
if assign_clusters:
print self._Tt, vectors
return [self.classify(vector) for vector in vectors]