本文整理汇总了Python中numpy.tensordot函数的典型用法代码示例。如果您正苦于以下问题:Python tensordot函数的具体用法?Python tensordot怎么用?Python tensordot使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tensordot函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: backward_cpu
def backward_cpu(self, inputs, grad_outputs):
x, W = inputs[:2]
b = inputs[2] if len(inputs) == 3 else None
if not type_check.same_types(*inputs):
if b is not None:
raise ValueError('numpy and cupy must not be used together\n'
'type(W): {0}, type(x): {1}, type(b): {2}'
.format(type(W), type(x), type(b)))
else:
raise ValueError('numpy and cupy must not be used together\n'
'type(W): {0}, type(x): {1}'
.format(type(W), type(x)))
gy = grad_outputs[0]
h, w = x.shape[2:]
gW = numpy.tensordot(
gy, self.col, ((0, 2, 3), (0, 4, 5))).astype(W.dtype, copy=False)
if not self.requires_x_grad:
gx = None
else:
gcol = numpy.tensordot(W, gy, (0, 1)).astype(x.dtype, copy=False)
gcol = numpy.rollaxis(gcol, 3)
gx = conv.col2im_cpu(gcol, self.sy, self.sx, self.ph, self.pw,
h, w)
if b is None:
return gx, gW
else:
gb = gy.sum(axis=(0, 2, 3))
return gx, gW, gb
开发者ID:delta2323,项目名称:chainer,代码行数:33,代码来源:convolution_2d.py
示例2: innerProductOBC
def innerProductOBC(mpsA,mpsB):
""" Inner product <A|B> using transfer matrices
where A and B are MPS representations of }A> and }B>
with open boundary conditions (OBC).
"""
# Take adjoint of |A> to get <A|
A = []
for a in mpsA:
A.append(np.conj(a))
B = mpsB
N = len(A) # Number of qubits
d = A[1].shape[1] # d = 2 for qubits
# Construct list of transfer matrices by contracting pairs of
# tensors from A and B.
transfer = []
t = np.tensordot(A[0],B[0],axes=(0,0))
t = np.reshape(t,A[0].shape[1]*B[0].shape[1])
transfer.append(t)
for i in xrange(1,N-1):
t = np.tensordot(A[i],B[i],axes=(1,1))
t = np.transpose(t,axes=(0,2,1,3))
t = np.reshape(t,(A[i].shape[0]*B[i].shape[0],
A[i].shape[2]*B[i].shape[2]))
transfer.append(t)
t = np.tensordot(A[N-1],B[N-1],axes=(1,1))
t = np.reshape(t,A[N-1].shape[0]*B[N-1].shape[0])
transfer.append(t)
# Contract the transfer matrices.
prod = transfer[0]
for i in xrange(1,N-1):
prod = np.dot(prod,transfer[i])
prod = np.dot(prod,transfer[N-1])
return prod
开发者ID:ehua7365,项目名称:RibbonOperators,代码行数:35,代码来源:mpstest15.py
示例3: r_log_spiral
def r_log_spiral(self,phi):
"""
return distance from center for angle phi of logarithmic spiral
Parameters
----------
phi: scalar or np.array with polar angle values
Returns
-------
r(phi) = rx * exp(b * phi) as np.array
Notes
-----
see http://en.wikipedia.org/wiki/Logarithmic_spiral
"""
if np.isscalar(phi):
phi = np.array([phi])
ones = np.ones(phi.shape[0])
# self.rx.shape = 8
# phi.shape = p
# then result is given as (8,p)-dim array, each row stands for one rx
result = np.tensordot(self.rx , np.exp((phi - 3.*pi*ones) / np.tan(pi/2. - self.idisk)),axes = 0)
result = np.vstack((result, np.tensordot(self.rx , np.exp((phi - pi*ones) / np.tan(pi/2. - self.idisk)),axes = 0) ))
result = np.vstack((result, np.tensordot(self.rx , np.exp((phi + pi*ones) / np.tan(pi/2. - self.idisk)),axes = 0) ))
return np.vstack((result, np.tensordot(self.rx , np.exp((phi + 3.*pi*ones) / np.tan(pi/2. - self.idisk)),axes = 0) ))
开发者ID:me-manu,项目名称:gmf,代码行数:28,代码来源:gmf.py
示例4: tensordot_adjoint_1
def tensordot_adjoint_1(A, G, axes, A_ndim, B_ndim):
# The adjoint of the operator
# B |--> np.tensordot(A, B, axes)
if A_ndim == 0:
return G * A
G_axes = onp.arange(onp.ndim(G))
if type(axes) is int:
axes = max(axes, 0)
A_axes = onp.arange(A_ndim)
return onp.tensordot(A, G, [A_axes[:A_ndim-axes], G_axes[:A_ndim-axes]])
elif type(axes[0]) is int:
axes = [axes[0] % A_ndim, axes[1] % B_ndim]
A_axes = onp.arange(A_ndim)
return onp.tensordot(A, G, [onp.delete(A_axes, axes[0]), G_axes[:A_ndim-1]])
else:
A_axes = onp.arange(A_ndim)
B_axes = onp.arange(B_ndim)
summed_axes = [onp.asarray(axes[0]) % A_ndim,
onp.asarray(axes[1]) % B_ndim]
other_axes = [onp.delete(A_axes, summed_axes[0]),
onp.delete(B_axes, summed_axes[1])]
out = onp.tensordot(A, G, [other_axes[0], G_axes[:len(other_axes[0])]])
perm = onp.argsort(onp.concatenate(
(summed_axes[1][onp.argsort(summed_axes[0])], other_axes[1])))
return onp.transpose(out, perm)
开发者ID:j-towns,项目名称:autograd,代码行数:26,代码来源:numpy_vjps.py
示例5: energy
def energy(self,R1,M0):
R0=np.tensordot(M0,self.MPO[0],axes=(0,0))
R0=np.tensordot(R0,np.conj(M0),axes=(2,0))
R0=np.transpose(R0,[0,2,4,1,3,5])
energy1=np.squeeze(np.tensordot(R0,R1,axes=([3,4,5],[0,1,2])))
norm=np.tensordot(M0,np.conj(M0),axes=([0,1,2],[0,1,2]))
return energy1/norm
开发者ID:sylvialee12,项目名称:TensorNetworkIntro,代码行数:7,代码来源:DMRG.py
示例6: concprof
def concprof(n0, T, r, phi):
kb = 8.61733034e-5
beta = 1.0 / (kb * T)
ux = strain(r, phi)
st = np.zeros((3, 3))
for i in xrange(3):
for j in xrange(3):
for k in xrange(3):
for l in xrange(3):
st[i, j] = Cijkl[i, j, k, l] * ux[k, l] + st[i, j]
F1 = -float(np.tensordot(dpl1, ux))
F2 = -float(np.tensordot(dpl2, ux))
chi1 = n0 / (1 - n0) * np.exp(-F1 * beta)
chi2 = n0 / (1 - n0) * np.exp(-F2 * beta)
n1 = chi1 / (1 + chi1)
n2 = chi2 / (1 + chi2)
n1 = n0 / (np.exp(F1 * beta) * (1 - n0) + n0)
n2 = n0 / (np.exp(F2 * beta) * (1 - n0) + n0)
dn = (n1 + n2) - n0
return dn
开发者ID:HGeerlings,项目名称:dislocat,代码行数:29,代码来源:solutedistr.py
示例7: learn
def learn(self, Xd):
# Initialize a data particle
X = [Xd]+[self.X[l]*0+self.O[l]
for l in range(1, len(self.X))]
# Alternate gibbs sampler on data and free particles
for l in (range(1, len(self.X), 2)+range(2, len(self.X), 2))*5:
self.gibbs(X, l)
for l in (range(1, len(self.X), 2)+range(0, len(self.X), 2))*1:
self.gibbs(self.X, l)
# Parameter update
self.W[0] += lr*(np.tensordot(X[0]-self.O[0], X[1]-self.O[1], axes=([0],[0])) -
np.tensordot(self.X[0]-self.O[0], self.X[1]-self.O[1], axes=([0],[0])))/len(Xd)
for i in range(1, len(self.W)):
self.W[i] += lr*(numpy.dot((X[i]-self.O[i]).T, X[i+1]-self.O[i+1]) -
numpy.dot((self.X[i]-self.O[i]).T, self.X[i+1]-self.O[i+1]))/len(Xd)
for i in range(0, len(self.B)):
self.B[i] += lr*(X[i]-self.X[i]).mean(axis=0)
# Reparameterization
for l in range(0, len(self.B)):
self.reparamB(X, l)
for l in range(0, len(self.O)):
self.reparamO(X, l)
开发者ID:smoitra87,项目名称:deepbio,代码行数:26,代码来源:dbm.py
示例8: princ2
def princ2(cauchyStress,C):
"""
Calculate the principal values using
Sig = C:T:s form
Arguments
---------
cauchyStress - the linearly transformed stress Sigma
C (6x6) tensor for linear transformation
Returns
-------
S1
S2
S3
"""
sqrt=np.sqrt
s = cauchyStress.copy()
T = cpb_lib.returnT()
Sig = np.tensordot(C,np.tensordot(T,s,axes=(1,0)),axes=(1,0))
Sxx,Syy,Szz,Sxy = Sig[0], Sig[1], Sig[2], Sig[5]
S1 = 0.5*(Sxx + Syy + sqrt( (Sxx-Syy)**2 + 4*Sxy**2))
S2 = 0.5*(Sxx + Syy - sqrt( (Sxx-Syy)**2 + 4*Sxy**2))
S3 = Szz
return S1, S2, S3
开发者ID:youngung,项目名称:MK,代码行数:27,代码来源:cpb_aniso.py
示例9: discretizedGaussian
def discretizedGaussian(amp, mu, cov, grid):
'''Convenience method for discretized Gaussian evaluation'''
#eigenvalue decomposition of precision matrix
P = la.inv(cov) #precision matrix
evl, M = la.eig(P)
#assert np.allclose(np.diag(evl), iM.dot(cov).dot(M))
#check if covariance is positive definite
if np.any(evl < 0):
raise ValueError('Covariance matrix should be positive definite')
#make column vector for arithmetic
mu = np.array(mu, ndmin=grid.ndim, dtype=float).T
evl = np.array(evl, ndmin=grid.ndim, dtype=float).T
xm = grid - mu #(2,...) shape
#return M, xm
f = np.sqrt(2 / evl)
pf = np.sqrt(np.pi / evl)
td0 = np.tensordot(M, xm + 0.5, 1)
td1 = np.tensordot(M, xm - 0.5, 1)
w = pf*(erf(f*td0) - erf(f*td1))
return amp * np.prod(w, axis=0)
开发者ID:apodemus,项目名称:obstools,代码行数:27,代码来源:psf.py
示例10: get_chisquareds
def get_chisquareds(self, n):
dd = self.get_datum(n)
if n == 0:
foo = np.sum(np.sum(np.tensordot(dd, self.get_ivarts(), axes=(1,1)) * dd[:,None,:],
axis=2), axis=0)
return np.sum(np.sum(np.tensordot(dd, self.get_ivarts(), axes=(1,1)) * dd[:,None,:],
axis=2), axis=0) # should be length T
开发者ID:davidwhogg,项目名称:DiffractionMicroscopy,代码行数:7,代码来源:gaussian.py
示例11: update
def update(self):
check_inputs_synchronized(self)
y_dot = self.input.y_dot
y = self.input.y
u = self.input.u
T = self.T
check_multiple([
('shape(x),(array[HxW]|array[N])', y),
('shape(x),(array[HxW]|array[N])', y_dot),
('array[K]', u),
('array[Kx2xHxW]|array[Kx1xN]', T), # TODO: use references
])
K = u.size
# update covariance of gradients
gy = generalized_gradient(y)
Tgy = np.tensordot([1, 1], T * gy , axes=(0, 1))
y_dot_pred = np.tensordot(u, Tgy, axes=(0, 0))
assert y_dot_pred.ndim == 2
error = np.maximum(0, -y_dot_pred * y_dot)
# error = np.abs(np.sign(y_dot_pred) - np.sign(y_dot))
self.output.y_dot_pred = y_dot_pred
self.output.error = error
self.output.error_sensel = np.sum(error, axis=0)
开发者ID:AndreaCensi,项目名称:be1008,代码行数:31,代码来源:generic_predictor.py
示例12: dev_trapz
def dev_trapz(ds):
d = ds.variables['development'][:]/1e4
t = ds.variables['time'][:]
t_floor = np.floor(t).astype(int)
#now set up a loop to solve the index for each whole integer or iterate up
#we can figure out if we have a leap year or not also by looking at the max day
#i.e. np.max(t_floor) == 365 , means this is a leap year dataset
max_days = np.max(t_floor) + 1
z,m,n = d.shape
daily_dev = np.zeros((max_days, m,n))
start = 0 #first integration point
k_array = np.zeros(max_days)
for k in range(max_days):
ones = np.ones((m,n))
if k == max_days-1:
tm = np.tensordot(t[start:], ones, axes=0)
daily_dev[k] = np.trapz(d[start:],tm,axis=0)
else:
end = np.argmax(t_floor>k)
tm = np.tensordot(t[start:end+1], ones, axes=0) #plus one to integrate the end point
daily_dev[k] = np.trapz(d[start:end+1],tm,axis=0)
#define the new start
start = end
k_array[k] = k+1 #counter for each day
return(daily_dev, k_array)
开发者ID:tonychangmsu,项目名称:Python_Scripts,代码行数:26,代码来源:dev_completion_v2_20160113.py
示例13: four_index_transform_cholesky
def four_index_transform_cholesky(ao_integrals, orb0, orb1=None, method='tensordot'):
"""Perform four index transformation on a Cholesky-decomposed four-index object.
Parameters
----------
oa_integrals : np.ndarray, shape=(nvec, nbasis, nbasis)
Cholesky decomposition of four-index object in the AO basis.
orb0
A Orbitals object with molecular orbitals.
orb1
Can be provided to transform the second index differently.
method
Either ``einsum`` or ``tensordot`` (default).
"""
if orb1 is None:
orb1 = orb0
result = np.zeros(ao_integrals.shape)
if method == 'einsum':
result = np.einsum('ai,kac->kic', orb0.coeffs, ao_integrals)
result = np.einsum('cj,kic->kij', orb1.coeffs, result)
elif method == 'tensordot':
result = np.tensordot(ao_integrals, orb0.coeffs, axes=([1],[0]))
result = np.tensordot(result, orb1.coeffs, axes=([1],[0]))
else:
raise ValueError('The method must either be \'einsum\' or \'tensordot\'.')
return result
开发者ID:QuantumElephant,项目名称:horton,代码行数:26,代码来源:indextransform.py
示例14: d2y_SS
def d2y_SS(z_i):
DF,df,Hf = self.DF(z_i),self.df(z_i),self.Hf(z_i)
d = self.get_d(z_i)
DFi = DF[n:]
return np.tensordot(np.linalg.inv(DFi[:,y] + DFi[:,e].dot(df) + DFi[:,v].dot(Ivy)),
-self.HFhat[S,S](z_i) - np.tensordot(DFi[:,e],quadratic_dot(Hf,d[y,S],d[y,S]),1)
, axes=1)
开发者ID:dgevans,项目名称:IdioApprox,代码行数:7,代码来源:approximate_begs.py
示例15: test_solve_fredholm_reconstr_ac
def test_solve_fredholm_reconstr_ac():
"""
here we see that the reconstruction quality is independent of the integration weights
differences occur when checking validity of the interpolated time continuous Fredholm equation
"""
_WC_ = 2
def lac(t):
return np.exp(- np.abs(t) - 1j*_WC_*t)
t_max = 10
tol = 2e-10
for ng in range(11,500,30):
t, w = sp.method_kle.get_mid_point_weights_times(t_max, ng)
r = lac(t.reshape(-1,1)-t.reshape(1,-1))
_eig_val, _eig_vec = sp.method_kle.solve_hom_fredholm(r, w)
_eig_vec_ast = np.conj(_eig_vec) # (N_gp, N_ev)
tmp = _eig_val.reshape(1, -1) * _eig_vec # (N_gp, N_ev)
recs_bcf = np.tensordot(tmp, _eig_vec_ast, axes=([1], [1]))
rd = np.max(np.abs(recs_bcf - r) / np.abs(r))
assert rd < tol, "rd={} >= {}".format(rd, tol)
t, w = sp.method_kle.get_simpson_weights_times(t_max, ng)
r = lac(t.reshape(-1, 1) - t.reshape(1, -1))
_eig_val, _eig_vec = sp.method_kle.solve_hom_fredholm(r, w)
_eig_vec_ast = np.conj(_eig_vec) # (N_gp, N_ev)
tmp = _eig_val.reshape(1, -1) * _eig_vec # (N_gp, N_ev)
recs_bcf = np.tensordot(tmp, _eig_vec_ast, axes=([1], [1]))
rd = np.max(np.abs(recs_bcf - r) / np.abs(r))
assert rd < tol, "rd={} >= {}".format(rd, tol)
开发者ID:cimatosa,项目名称:stocproc,代码行数:29,代码来源:test_method_kle.py
示例16: rot_symm
def rot_symm(symm_grp_ax, bp_norms_go1, file_path):
"""
Returns the symmetrically equivalent boundary plane normals
Parameters
----------
symm_grp_ax: principle axes for the bicrystal fundamental zone
* numpy array of size (3 x 3)
bp_norms_go1: normalized boundary plane normals in the po1 reference frame
* numpy array of size (n x 3)
file_path: path to the relevant symmetry operations containing pickle file
* string
Returns
-------
symm_bpn_go1: symmetrically equivalent boundary plane normals in the po1 reference frame
* numpy array of size (m x n x 3); m == order of bicrystal point group symmetry group
Notes
------
"""
bpn_rot = np.dot(np.linalg.inv(symm_grp_ax), bp_norms_go1.transpose()).transpose()
symm_mat = pickle.load(open(file_path, 'rb'))
### np.dot returns the sum product of the last axis of the first matrix with the second to last axis of the second
### advisable to use np.tensordot instead to avoid confusion !!
symm_bpn_rot_gop1 = np.tensordot(symm_mat, bpn_rot.transpose(), 1).transpose((1, 2, 0))
symm_bpn_go1 = np.tensordot(symm_grp_ax, symm_bpn_rot_gop1, 1).transpose(2, 1, 0)
return symm_bpn_go1
开发者ID:spatala,项目名称:GB_Lammps,代码行数:32,代码来源:rot_symm.py
示例17: test1
def test1():
import axis as ax
import numpy as np
a = ax.Axis((7,), np.ndarray)
ia = ax.IrrepAxis("C2v", (4,0,1,2), np.ndarray)
A = Array(a*a*ia*ia)
B = Array(ia*ia*a*a)
a = np.random.rand(7,7,7,7)
b = np.random.rand(7,7,7,7)
c = np.tensordot(a, b, axes=((2,3),(0,1)))
it_a = zip(range(1),(slice(7),),(slice(7),))
it_ia = zip(range(4),(slice(4),slice(0),slice(1),slice(2)),(slice(0,4),slice(4,4),slice(4,5),slice(5,7)))
for h1, i1, j1 in it_a:
for h2, i2, j2 in it_a:
for h3, i3, j3 in it_ia:
for h4, i4, j4 in it_ia:
A[h1,h2,h3,h4][i1,i2,i3,i4] = a[j1,j2,j3,j4]
B[h3,h4,h1,h2][i3,i4,i1,i2] = b[j3,j4,j1,j2]
col_axes = A.multiaxis.axes[:2]
trc_axes = A.multiaxis.axes[2:]
row_axes = B.multiaxis.axes[2:]
C = Array(col_axes + row_axes)
for col in np.prod(col_axes).iter_array_keytups():
for row in np.prod(row_axes).iter_array_keytups():
for trc in MultiAxis(trc_axes).iter_array_keytups():
if not (0 in A[col+trc].shape or 0 in B[trc+row].shape):
C[col+row] += np.tensordot(A[col+trc], B[trc+row], axes=((2,3),(0,1)))
print np.linalg.norm(C[0,0,0,0] - c)
开发者ID:avcopan,项目名称:spinblock,代码行数:35,代码来源:tensor.py
示例18: expect
def expect(mps,opl): #opl is a list of matrices including identities.but this is not necessary
#contract s first
expect=np.array([[1.]])
for i in range(len(mps.Ms)):
overlap=np.tensordot(mps.Ms[i].conjugate().transpose(),np.tensordot(expect,mps.Ms[i],1),1)
expect=np.tensordot(opl[i],overlap,axes=([0,1],[1,2]))
return float(expect)
开发者ID:Lynn-015,项目名称:NJU_DMRG,代码行数:7,代码来源:mps.py
示例19: H_chol
def H_chol(self):
H = np.tensordot(self.DWt(),self.Wc(),axes=(1,0))
H = np.transpose(H, (0, 2, 1))
H = np.tensordot(self.Wr(), H, axes=(0,0))
H = H.reshape((self.rank_c * self.rank_r, self.rank_c * self.rank_r), order='F')
H+= np.eye(self.rank_r * self.rank_c)
return la.cholesky(H).T
开发者ID:BioinformaticsArchive,项目名称:limix,代码行数:7,代码来源:cov3kronSumLR.py
示例20: J
def J(theta, hidden_layer_size, num_labels, feat, y, lamb):
"""cost function"""
# Reshape theta into matrices.
# The i stands for 'internal' to indicate that these
# variables are internal to this function.
Theta1i = theta[:hidden_layer_size * feat.shape[1]].reshape( \
hidden_layer_size, feat.shape[1])
Theta2i = theta[hidden_layer_size * feat.shape[1]:].reshape( \
num_labels, hidden_layer_size+1)
# compute the probabilities h by going through the three layers
# layer 1, the input layer
a = feat.T
# layer 2, the hidden layer
z = np.dot(Theta1i,a)
a = g(z)
a = np.vstack((np.ones(a.shape[1]),a))
# layer 3, the output layer
z = np.dot(Theta2i,a)
a = g(z)
h = a.T
# compute the cost
m = feat.shape[0]
cost = -1. / m * (np.tensordot(y, np.log(h)) \
+ np.tensordot(1-y, np.log(1-h)))
# add regularization
cost += lamb * .5 / m * (np.sum(Theta1i[:,1:]**2) \
+ np.sum(Theta2i[:,1:]**2))
return cost
开发者ID:ktaliaferro,项目名称:NeuralNetwork,代码行数:31,代码来源:neural_network.py
注:本文中的numpy.tensordot函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论