GVKun编程网logo

Python numpy 模块-einsum() 实例源码(python中numpy模块)

1

针对Pythonnumpy模块-einsum()实例源码和python中numpy模块这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展A*B+C使用np.einsum、CuPy.einsum

针对Python numpy 模块-einsum() 实例源码python中numpy模块这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展A * B + C使用np.einsum、CuPy.einsum 慢?、einsum 可以用于重塑操作数吗?、einsum 和 matmul等相关知识,希望可以帮助到你。

本文目录一览:

Python numpy 模块-einsum() 实例源码(python中numpy模块)

Python numpy 模块-einsum() 实例源码(python中numpy模块)

Python numpy 模块,einsum() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.einsum()

项目:Modeling-Cloth    作者:the3dadvantage    | 项目源码 | 文件源码
  1. def total_length_selected(ed=''empty'', coords=''empty'', ob=''empty''):
  2. ''''''Returns the total length of all edge segments''''''
  3. if ob == ''empty'':
  4. ob = bpy.context.object
  5. if coords == ''empty'':
  6. coords = get_coords(ob)
  7. if ed == ''empty'':
  8. ed = get_edge_idx(ob)
  9. edc = coords[ed]
  10. e1 = edc[:, 0]
  11. e2 = edc[:, 1]
  12. ee1 = e1 - e2
  13. sel = get_selected_edges(ob)
  14. ee = ee1[sel]
  15. leng = np.einsum(''ij,ij->i'', ee, ee)
  16. return np.sum(np.sqrt(leng))
项目:latplan    作者:guicho271828    | 项目源码 | 文件源码
  1. def transitions_old(width, height, configs=None, one_per_state=False):
  2. digit = width * height
  3. if configs is None:
  4. configs = generate_configs(digit)
  5. if one_per_state:
  6. def pickone(thing):
  7. index = np.random.randint(0,len(thing))
  8. return thing[index]
  9. transitions = np.array([
  10. generate(
  11. [c1,pickone(successors(c1,width,height))],height)
  12. for c1 in configs ])
  13. else:
  14. transitions = np.array([ generate([c1,c2],height)
  15. for c1 in configs for c2 in successors(c1,height) ])
  16. return np.einsum(''ab...->ba...'',transitions)
项目:latplan    作者:guicho271828    | 项目源码 | 文件源码
  1. def puzzle_plot(p):
  2. p.setup()
  3. def name(template):
  4. return template.format(p.__name__)
  5. from itertools import islice
  6. configs = list(islice(p.generate_configs(9), 1000)) # be careful,islice is not immutable!!!
  7. import numpy.random as random
  8. random.shuffle(configs)
  9. configs = configs[:10]
  10. puzzles = p.generate(configs, 3, 3)
  11. print(puzzles.shape, "mean", puzzles.mean(), "stdev", np.std(puzzles))
  12. plot_image(puzzles[-1], name("{}.png"))
  13. plot_image(np.clip(puzzles[-1]+np.random.normal(0,0.1,puzzles[-1].shape),0,1),name("{}+noise.png"))
  14. plot_image(np.round(np.clip(puzzles[-1]+np.random.normal(0,1)),name("{}+noise+round.png"))
  15. plot_grid(puzzles, name("{}s.png"))
  16. _transitions = p.transitions(3,3,configs=configs)
  17. print(_transitions.shape)
  18. transitions_for_show = \\
  19. np.einsum(''ba...->ab...'',_transitions) \\
  20. .reshape((-1,)+_transitions.shape[2:])
  21. print(transitions_for_show.shape)
  22. plot_grid(transitions_for_show, name("{}_transitions.png"))
项目:latplan    作者:guicho271828    | 项目源码 | 文件源码
  1. def run(ae,xs):
  2. zs = ae.encode_binary(xs)
  3. ys = ae.decode_binary(zs)
  4. mod_ys = []
  5. correlations = []
  6. print(ys.shape)
  7. print("corrlations:")
  8. print("bit \\ image {}".format(range(len(xs))))
  9. for i in range(ae.N):
  10. mod_zs = np.copy(zs)
  11. # increase the latent value from 0 to 1 and check the difference
  12. for j in range(11):
  13. mod_zs[:,i] = j / 10.0
  14. mod_ys.append(ae.decode_binary(mod_zs))
  15. zero_zs,one_zs = np.copy(zs),np.copy(zs)
  16. zero_zs[:,i] = 0.
  17. one_zs[:,i] = 1.
  18. correlation = np.mean(np.square(ae.decode_binary(zero_zs) - ae.decode_binary(one_zs)),
  19. axis=(1,2))
  20. correlations.append(correlation)
  21. print("{:>5} {}".format(i,correlation))
  22. plot_grid2(np.einsum("ib...->bi...",np.array(mod_ys)).reshape((-1,)+ys.shape[1:]),
  23. w=11,path=ae.local("dump_significance.png"))
  24. return np.einsum("ib->bi",correlations)
项目:McMurchie-Davidson    作者:jjgoings    | 项目源码 | 文件源码
  1. def buildFock(self):
  2. """Routine to build the AO basis Fock matrix"""
  3. if self.direct:
  4. if self.incFockRst: # restart incremental fock build?
  5. self.G = formPT(self.P,np.zeros_like(self.P),self.bfs,
  6. self.nbasis,self.screen,self.scrTol)
  7. self.G = 0.5*(self.G + self.G.T)
  8. self.F = self.Core.astype(''complex'') + self.G
  9. else:
  10. self.G = formPT(self.P,self.P_old,self.nbasis,
  11. self.screen,self.scrTol)
  12. self.G = 0.5*(self.G + self.G.T)
  13. self.F = self.F_old + self.G
  14.  
  15. else:
  16. self.J = np.einsum(''pqrs,sr->pq'', self.Twoe.astype(''complex''),self.P)
  17. self.K = np.einsum(''psqr,self.P)
  18. self.G = 2.*self.J - self.K
  19. self.F = self.Core.astype(''complex'') + self.G
项目:Graphene    作者:ashivni    | 项目源码 | 文件源码
  1. def pointsInRegion(regNum, vor, p, overlap=0.0):
  2. """
  3. returns the subset of points p that are inside the regNum region of the voronoi object
  4. vor. The boundaries of the region are extended by an amount given by ''overlap''.
  5. """
  6. reg = vor.regions[vor.point_region[regNum]] # region associated with the point
  7. if -1 in reg:
  8. raise Exception(''Open region associated with generator'')
  9. nVerts = len(reg) # number of verticies in the region
  10. p0 = vor.points[regNum]
  11.  
  12. for i in range(len(reg)):
  13. vert1, vert2 = vor.vertices[reg[i]], vor.vertices[reg[(i + 1) % len(reg)]]
  14. dr = vert1 - vert2 # edge
  15. dr = dr / numpy.linalg.norm(dr) # normalize
  16. dn = numpy.array([dr[1], -dr[0]]) # normal to edge
  17. dn = dn if numpy.dot(dn, vert2 - p0[:2]) > 0 else -dn # orient so that the normal is outwards
  18. d1 = numpy.einsum(''i,ji'', dn, vert2 + dn * overlap - p[:, :2])
  19. p = p[d1 * numpy.dot(dn, vert2 - p0[:2]) > 0]
  20.  
  21. return p
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def test_einsum_misc(self):
  2. # This call used to crash because of a bug in
  3. # PyArray_AssignZero
  4. a = np.ones((1, 2))
  5. b = np.ones((2, 2, 1))
  6. assert_equal(np.einsum(''ij...,j...->i...'', a, b), [[[2], [2]]])
  7.  
  8. # The iterator had an issue with buffering this reduction
  9. a = np.ones((5, 12, 4, 3), np.int64)
  10. b = np.ones((5, 11), np.int64)
  11. assert_equal(np.einsum(''ijklm,ijn,ijn->'', b,
  12. np.einsum(''ijklm, b))
  13.  
  14. # Issue #2027,was a problem in the contiguous 3-argument
  15. # inner loop implementation
  16. a = np.arange(1, 3)
  17. b = np.arange(1, 5).reshape(2, 2)
  18. c = np.arange(1, 9).reshape(4, 2)
  19. assert_equal(np.einsum(''x,yx,zx->xzy'', c),
  20. [[[1, 3], [3, 9], [5, 15], [7, 21]],
  21. [[8, 16], [16, 32], [24, 48], [32, 64]]])
项目:radar    作者:amoose136    | 项目源码 | 文件源码
  1. def test_einsum_all_contig_non_contig_output(self):
  2. # Issue gh-5907,tests that the all contiguous special case
  3. # actually checks the contiguity of the output
  4. x = np.ones((5, 5))
  5. out = np.ones(10)[::2]
  6. correct_base = np.ones(10)
  7. correct_base[::2] = 5
  8. # Always worked (inner iteration is done with 0-stride):
  9. np.einsum(''mi,mi,mi->m'', x, out=out)
  10. assert_array_equal(out.base, correct_base)
  11. # Example 1:
  12. out = np.ones(10)[::2]
  13. np.einsum(''im,im,im->m'', correct_base)
  14. # Example 2,buffering causes x to be contiguous but
  15. # special cases do not catch the operation before:
  16. out = np.ones((2, 2))[..., 0]
  17. correct_base = np.ones((2, 2))
  18. correct_base[..., 0] = 2
  19. x = np.ones((2, 2), np.float32)
  20. np.einsum(''ij,jk->ik'', correct_base)
项目:gconv_experiments    作者:tscohen    | 项目源码 | 文件源码
  1. def dihedral_transform_batch(x):
  2.  
  3. g = np.random.randint(low=0, high=8, size=x.shape[0])
  4.  
  5. h, w = x.shape[-2:]
  6. hh = (h - 1) / 2.
  7. hw = (w - 1) / 2.
  8.  
  9. I, J = np.meshgrid(np.linspace(-hh, hh, x.shape[-2]), np.linspace(-hw, hw, x.shape[-1]))
  10. C = np.r_[[I, J]]
  11. D4C = np.einsum(''...ij,jkl->...ikl'', D4, C)
  12. D4C[:, 0] += hh
  13. D4C[:, 1] += hw
  14. D4C = D4C.astype(int)
  15.  
  16. x_out = np.empty_like(x)
  17. for i in range(x.shape[0]):
  18. I, J = D4C[g[i]]
  19. x_out[i, :] = x[i][:, J, I]
  20.  
  21. return x_out
项目:quadpy    作者:nschloe    | 项目源码 | 文件源码
  1. def get_vol(simplex):
  2. # Compute the volume via the Cayley-Menger determinant
  3. # <http://mathworld.wolfram.com/Cayley-MengerDeterminant.html>. One
  4. # advantage is that it can compute the volume of the simplex indenpendent
  5. # of the dimension of the space where it''s embedded.
  6.  
  7. # compute all edge lengths
  8. edges = numpy.subtract(simplex[:, None], simplex[None, :])
  9. ei_dot_ej = numpy.einsum(''...k,...k->...'', edges, edges)
  10.  
  11. j = simplex.shape[0] - 1
  12. a = numpy.empty((j+2, j+2) + ei_dot_ej.shape[2:])
  13. a[1:, 1:] = ei_dot_ej
  14. a[0, 1:] = 1.0
  15. a[1:, 0] = 1.0
  16. a[0, 0] = 0.0
  17.  
  18. a = numpy.moveaxis(a, (0, 1), (-2, -1))
  19. det = numpy.linalg.det(a)
  20.  
  21. vol = numpy.sqrt((-1.0)**(j+1) / 2**j / math.factorial(j)**2 * det)
  22. return vol
项目:jitcdde    作者:neurophysik    | 项目源码 | 文件源码
  1. def scalar_product_interval(anchors, indizes_1, indizes_2):
  2. q = (anchors[1][0]-anchors[0][0])
  3.  
  4. vector_1 = np.vstack([
  5. anchors[0][1][indizes_1], # a_1
  6. anchors[0][2][indizes_1] * q, # b_1
  7. anchors[1][1][indizes_1], # c_1
  8. anchors[1][2][indizes_1] * q, # d_1
  9. ])
  10.  
  11. vector_2 = np.vstack([
  12. anchors[0][1][indizes_2], # a_2
  13. anchors[0][2][indizes_2] * q, # b_2
  14. anchors[1][1][indizes_2], # c_2
  15. anchors[1][2][indizes_2] * q, # d_2
  16. ])
  17.  
  18. return np.einsum(
  19. vector_1, [0,2],
  20. sp_matrix,1],
  21. vector_2, [1,2]
  22. )*q
项目:jitcdde    作者:neurophysik    | 项目源码 | 文件源码
  1. def scalar_product_partial(anchors, indizes_2, start):
  2. q = (anchors[1][0]-anchors[0][0])
  3. z = (start-anchors[1][0]) / q
  4.  
  5. vector_1 = np.vstack([
  6. anchors[0][1][indizes_1],
  7. partial_sp_matrix(z),2]
  8. )*q
项目:tensorpac    作者:EtienneCmb    | 项目源码 | 文件源码
  1. def mvl(pha, amp, optimize):
  2. """Mean Vector Length (Canolty,2006).
  3.  
  4. Parameters
  5. ----------
  6. pha : array_like
  7. Array of phases of shapes (npha,...,npts)
  8.  
  9. amp : array_like
  10. Array of amplitudes of shapes (namp,npts)
  11.  
  12. Returns
  13. -------
  14. pac : array_like
  15. PAC of shape (npha,namp,...)
  16. """
  17. # Number of time points :
  18. npts = pha.shape[-1]
  19. return np.abs(np.einsum(''i...j,k...j->ik...'', np.exp(1j * pha),
  20. optimize=optimize)) / npts
项目:tensorpac    作者:EtienneCmb    | 项目源码 | 文件源码
  1. def ps(pha, optimize):
  2. """Phase Synchrony (Penny,2008; Cohen,2008).
  3.  
  4. Parameters
  5. ----------
  6. pha : array_like
  7. Array of phases of shapes (npha,...)
  8. """
  9. # Number of time points :
  10. npts = pha.shape[-1]
  11. pac = np.einsum(''i...j, np.exp(-1j * amp),
  12. optimize=optimize)
  13. return np.abs(pac) / npts
项目:xdesign    作者:tomography    | 项目源码 | 文件源码
  1. def half_space(self):
  2. """Return the half space polytope respresentation of the infinite
  3. beam."""
  4. # add half beam width along the normal direction to each of the points
  5. half = self.normal * self.size / 2
  6. edges = [Line(self.p1 + half, self.p2 + half),
  7. Line(self.p1 - half, self.p2 - half)]
  8.  
  9. A = np.ndarray((len(edges), self.dim))
  10. B = np.ndarray(len(edges))
  11.  
  12. for i in range(0, 2):
  13. A[i, :], B[i] = edges[i].standard
  14.  
  15. # test for positive or negative side of line
  16. if np.einsum(''i,i'', self.p1._x, A[i, :]) > B[i]:
  17. A[i, :] = -A[i, :]
  18. B[i] = -B[i]
  19.  
  20. p = pt.polytope(A, B)
  21. return p
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def forward_prop_random_thru_post_mm(self, model, mx, vx, mu, Su):
  2. Kuu_noiseless = compute_kernel(
  3. 2 * model.ls, 2 * model.sf, model.zu, model.zu)
  4. Kuu = Kuu_noiseless + np.diag(jitter * np.ones((self.M, )))
  5. # Todo: remove inv
  6. Kuuinv = np.linalg.inv(Kuu)
  7. A = np.dot(Kuuinv, mu)
  8. Smm = Su + np.outer(mu, mu)
  9. B_sto = np.dot(Kuuinv, np.dot(Smm, Kuuinv)) - Kuuinv
  10. psi0 = np.exp(2.0 * model.sf)
  11. psi1, psi2 = compute_psi_weave(
  12. 2 * model.ls, model.zu)
  13. mout = np.einsum(''nm,md->nd'', psi1, A)
  14. Bpsi2 = np.einsum(''ab,nab->n'', B_sto, psi2)[:, np.newaxis]
  15. vout = psi0 + Bpsi2 - mout**2
  16. return mout, vout
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def _forward_prop_deterministic_thru_post(self, return_info=False):
  2. """Propagate deterministic inputs thru posterior
  3.  
  4. Args:
  5. x (float): input values,size K x Din
  6. return_info (bool,optional): Description
  7.  
  8. Returns:
  9. float,size K x Dout: output means
  10. float,size K x Dout: output variances
  11. """
  12. psi0 = np.exp(2 * self.sf)
  13. psi1 = compute_kernel(2 * self.ls, 2 * self.sf, self.zu)
  14. mout = np.einsum(''nm,dm->nd'', self.A)
  15. Bpsi2 = np.einsum(''dab,na,nb->nd'', self.B_det, psi1)
  16. vout = psi0 + Bpsi2
  17. if return_info:
  18. return mout, vout, psi1
  19. else:
  20. return mout, vout
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def _forward_prop_random_thru_post_mm(self, return_info=False):
  2. """Propagate uncertain inputs thru posterior,using Moment Matching
  3.  
  4. Args:
  5. mx (float): input means,size K x Din
  6. vx (TYPE): input variances,size K x Dout: output variances
  7. """
  8. psi0 = np.exp(2.0 * self.sf)
  9. psi1, psi2 = compute_psi_weave(
  10. 2 * self.ls,nab->nd'', self.B_sto, psi2)
  11. vout = psi0 + Bpsi2 - mout**2
  12. if return_info:
  13. return mout, psi2
  14. else:
  15. return mout, vout
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def sample(self, x):
  2. """Summary
  3.  
  4. Args:
  5. x (TYPE): Description
  6.  
  7. Returns:
  8. TYPE: Description
  9. """
  10. Su = self.Su
  11. mu = self.mu
  12. Lu = np.linalg.cholesky(Su)
  13. epsilon = np.random.randn(self.Dout, self.M)
  14. u_sample = mu + np.einsum(''dab,db->da'', Lu, epsilon)
  15.  
  16. kff = compute_kernel(2 * self.ls, x)
  17. kff += np.diag(JITTER * np.ones(x.shape[0]))
  18. kfu = compute_kernel(2 * self.ls, self.zu)
  19. qfu = np.dot(kfu, self.Kuuinv)
  20. mf = np.einsum(''nm, qfu, u_sample)
  21. vf = kff - np.dot(qfu, kfu.T)
  22. Lf = np.linalg.cholesky(vf)
  23. epsilon = np.random.randn(x.shape[0], self.Dout)
  24. f_sample = mf + np.einsum(''ab,bd->ad'', Lf, epsilon)
  25. return f_sample
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def _forward_prop_deterministic_thru_cav(self, x):
  2. """Propagate deterministic inputs thru cavity
  3.  
  4. Args:
  5. x (float): input values,size K x Din
  6.  
  7. Returns:
  8. float,size K x Dout: output variances
  9. float,size K x M: cross covariance matrix
  10. """
  11. kff = np.exp(2 * self.sf)
  12. kfu = compute_kernel(2 * self.ls, kfu, self.Ahat)
  13. Bkfukuf = np.einsum(''dab, self.Bhat_det, kfu)
  14. vout = kff + Bkfukuf
  15. return mout, kfu
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def _forward_prop_random_thru_cav_mm(self, vx):
  2. """Propagate uncertain inputs thru cavity,using simple Moment Matching
  3.  
  4. Args:
  5. mx (float): input means,size K x Din
  6.  
  7. Returns:
  8. output means and variances,and intermediate info for backprop
  9. """
  10. psi0 = np.exp(2 * self.sf)
  11. psi1, self.Ahat)
  12. Bhatpsi2 = np.einsum(''dab, self.Bhat_sto, psi2)
  13. vout = psi0 + Bhatpsi2 - mout**2
  14. return mout, psi2
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def psi1compDer(dL_dpsi1, _psi1, variance, lengthscale, Z, S):
  2. # here are the "statistics" for psi1
  3. # Produced intermediate results: dL_dparams w.r.t. psi1
  4. # _dL_dvariance 1
  5. # _dL_dlengthscale Q
  6. # _dL_dZ MxQ
  7. # _dL_dgamma NxQ
  8. # _dL_dmu NxQ
  9. # _dL_dS NxQ
  10.  
  11. lengthscale2 = np.square(lengthscale)
  12.  
  13. Lpsi1 = dL_dpsi1 * _psi1
  14. Zmu = Z[None, :, :] - mu[:, None, :] # NxMxQ
  15. denom = 1. / (S + lengthscale2)
  16. Zmu2_denom = np.square(Zmu) * denom[:, :] # NxMxQ
  17. _dL_dvar = Lpsi1.sum() / variance
  18. _dL_dmu = np.einsum(''nm,nmq,nq->nq'', Lpsi1, Zmu, denom)
  19. _dL_dS = np.einsum(''nm, (Zmu2_denom - 1.), denom) / 2.
  20. _dL_dZ = -np.einsum(''nm,nq->mq'', denom)
  21. _dL_dl = np.einsum(''nm,nq->q'', (Zmu2_denom +
  22. (S / lengthscale2)[:, :]), denom * lengthscale)
  23.  
  24. return _dL_dvar, _dL_dl, _dL_dZ, _dL_dmu, _dL_dS
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def kfucompDer(dL_dkfu, grad_x):
  2. # here are the "statistics" for psi1
  3. # Produced intermediate results: dL_dparams w.r.t. psi1
  4. # _dL_dvariance 1
  5. # _dL_dlengthscale Q
  6. # _dL_dZ MxQ
  7.  
  8. lengthscale2 = np.square(lengthscale)
  9.  
  10. Lpsi1 = dL_dkfu * kfu
  11. Zmu = Z[None, :] # NxMxQ
  12. _dL_dvar = Lpsi1.sum() / variance
  13. _dL_dZ = -np.einsum(''nm,nmq->mq'', Zmu / lengthscale2)
  14. _dL_dl = np.einsum(''nm,nmq->q'', np.square(Zmu) / lengthscale**3)
  15. if grad_x:
  16. _dL_dx = np.einsum(''nm,nmq->nq'', Zmu / lengthscale2)
  17. return _dL_dvar, _dL_dx
  18. else:
  19. return _dL_dvar, _dL_dZ
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def _forward_prop_deterministic_thru_cav(self, n, alpha):
  2. """Summary
  3.  
  4. Args:
  5. n (TYPE): Description
  6. x (TYPE): Description
  7. alpha (TYPE): Description
  8.  
  9. Returns:
  10. TYPE: Description
  11. """
  12. muhat, Suhat, SuinvMuhat, Suinvhat = self.compute_cavity(n, alpha)
  13. Kuuinv = self.Kuuinv
  14. Ahat = np.einsum(''ab,ndb->nda'', Kuuinv, muhat)
  15. Bhat = np.einsum(
  16. ''ab,ndbc->ndac'',
  17. Kuuinv, np.einsum(''ndab,bc->ndac'', Kuuinv)) - Kuuinv
  18. kff = np.exp(2 * self.sf)
  19. kfu = compute_kernel(2 * self.ls,ndm->nd'', Ahat)
  20. Bkfukuf = np.einsum(''ndab, Bhat, kfu)
  21. vout = kff + Bkfukuf
  22. extra_res = [muhat, Suinvhat, Ahat, Bhat]
  23. return mout, extra_res
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def _forward_prop_deterministic_thru_post(self, x):
  2. """Summary
  3.  
  4. Args:
  5. x (TYPE): Description
  6.  
  7. Returns:
  8. TYPE: Description
  9. """
  10. Kuuinv = self.Kuuinv
  11. A = np.einsum(''ab, self.mu)
  12. B = np.einsum(
  13. ''ab,dbc->dac'', np.einsum(''dab,bc->dac'', self.Su, A)
  14. Bpsi2 = np.einsum(''dab, B, kfu)
  15. vout = kff + Bpsi2
  16. return mout, vout
  17.  
  18. # Todo
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def _forward_prop_random_thru_post_mm(self, vx):
  2. """Summary
  3.  
  4. Args:
  5. mx (TYPE): Description
  6. vx (TYPE): Description
  7.  
  8. Returns:
  9. TYPE: Description
  10. """
  11. Kuuinv = self.Kuuinv
  12. A = np.einsum(''ab, self.mu)
  13. Smm = self.Su + np.einsum(''da,db->dab'', self.mu, Smm, Kuuinv)) - Kuuinv
  14. psi0 = np.exp(2.0 * self.sf)
  15. psi1, psi2)
  16. vout = psi0 + Bpsi2 - mout**2
  17. return mout, vout
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def sample(self, epsilon)
  2. return f_sample
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def compute_cavity(self, alpha=1.0):
  2. """Summary
  3.  
  4. Args:
  5. n (TYPE): Description
  6. alpha (float,optional): Description
  7.  
  8. Returns:
  9. TYPE: Description
  10. """
  11. # compute the leave one out moments
  12. t1n = self.t1[n, :]
  13. t2n = self.t2[n, :]
  14. Suinvhat = self.Suinv - alpha * t2n
  15. SuinvMuhat = self.SuinvMu - alpha * t1n
  16. Suhat = np.linalg.inv(Suinvhat)
  17. muhat = np.einsum(''ndab, SuinvMuhat)
  18. return muhat, Suinvhat
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def forward_prop_thru_post(self, vout
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def update_posterior(self, x_train=None, new_hypers=False):
  2. """Summary
  3.  
  4. Returns:
  5. TYPE: Description
  6. """
  7. # compute the posterior approximation
  8. if new_hypers and x_train is not None:
  9. Kfu = compute_kernel(2*self.ls, 2*self.sf, x_train, self.zu)
  10. KuuinvKuf = np.dot(self.Kuuinv, Kfu.T)
  11. self.Kfu = Kfu
  12. self.KuuinvKuf = KuuinvKuf
  13. self.Kff_diag = compute_kernel_diag(2*self.ls, x_train)
  14.  
  15. KuuinvKuf_div_var = np.einsum(''an,nd->dan'', self.KuuinvKuf, 1.0 / self.variances)
  16. T2u = np.einsum(''dan,bn->dab'', KuuinvKuf_div_var, self.KuuinvKuf)
  17. T1u = np.einsum(''bn,nd->db'', self.means / self.variances)
  18. Vinv = self.Kuuinv + T2u
  19. self.Suinv = Vinv
  20. self.Su = np.linalg.inv(Vinv)
  21. self.mu = np.einsum(''dab, T1u)
  22. self.gamma = np.einsum(''ab, self.Kuuinv, self.mu)
  23. self.beta = self.Kuuinv - np.einsum(''ab,
  24. self.Kuuinv,
  25. np.einsum(''dab, self.Kuuinv))
项目:geepee    作者:thangbui    | 项目源码 | 文件源码
  1. def compute_cavity(self, idxs, alpha):
  2. # deletion
  3. p_i = self.KuuinvKuf[:, idxs].T[:, np.newaxis, :]
  4. k_i = self.Kfu[idxs, :]
  5. k_ii = self.Kff_diag[idxs][:, np.newaxis]
  6. gamma = self.gamma
  7. beta = self.beta
  8. h_si = p_i - np.einsum(''dab,nb->nda'', beta, k_i)
  9. variance_i = self.variances[idxs, :]
  10. mean_i = self.means[idxs, :]
  11. dlogZd_dmi2 = 1.0 / (variance_i/alpha -
  12. np.sum(k_i[:, :] * h_si, axis=2))
  13. dlogZd_dmi = -dlogZd_dmi2 * (mean_i -
  14. np.sum(k_i[:, :] * gamma, axis=2))
  15. hd1 = h_si * dlogZd_dmi[:, np.newaxis]
  16. hd2h = np.einsum(''nda,ndb->ndab'', h_si, h_si) * dlogZd_dmi2[:, np.newaxis]
  17. gamma_si = gamma + hd1
  18. beta_si = beta - hd2h
  19.  
  20. # projection
  21. h = p_i - np.einsum(''ndab, beta_si, k_i)
  22. m_si_i = np.einsum(''na,nda->nd'', k_i, gamma_si)
  23. v_si_ii = k_ii - np.einsum(''na,ndab, k_i)
  24.  
  25. return m_si_i, v_si_ii, [h, gamma_si]
项目:semi-auto-anno    作者:moberweger    | 项目源码 | 文件源码
  1. def project3Dto2D(self, Li, idxs):
  2. """
  3. Project 3D point to 2D
  4. :param Li: joints in normalized 3D
  5. :param idxs: frames specified by subset
  6. :return: 2D points,in normalized 2D coordinates
  7. """
  8.  
  9. if not isinstance(idxs, numpy.ndarray):
  10. idxs = numpy.asarray([idxs])
  11.  
  12. # 3D -> 2D projection also shift by M to cropped window
  13. Li_glob3D = (numpy.reshape(Li, (len(idxs), self.numJoints, 3))*self.Di_scale[idxs][:, None]+self.Di_off3D[idxs][:, :]).reshape((len(idxs)*self.numJoints, 3))
  14. Li_glob3D_hom = numpy.concatenate([Li_glob3D, numpy.ones((len(idxs)*self.numJoints, dtype=''float32'')], axis=1)
  15. Li_glob2D_hom = numpy.dot(Li_glob3D_hom, self.cam_proj.T)
  16. Li_glob2D = (Li_glob2D_hom[:, 0:3] / Li_glob2D_hom[:, 3][:, None]).reshape((len(idxs), 3))
  17. Li_img2D_hom = numpy.einsum(''ijk,ikl->ijl'', Li_glob2D, self.Di_trans2D[idxs])
  18. Li_img2D = (Li_img2D_hom[:, 0:2] / Li_img2D_hom[:, 2][:, self.numJoints*2))
  19. Li_img2Dcrop = (Li_img2D - (self.Di.shape[3]/2.)) / (self.Di.shape[3]/2.)
  20. return Li_img2Dcrop
项目:blmath    作者:bodylabs    | 项目源码 | 文件源码
  1. def compute_dr_wrt(self, wrt):
  2. if wrt is not self.v:
  3. return None
  4.  
  5. v = self.v.r.reshape(-1, 3)
  6. blocks = -np.einsum(''ij,ik->ijk'', v, v) * (self.ss**(-3./2.)).reshape((-1, 1, 1))
  7. for i in range(3):
  8. blocks[:, i, i] += self.s_inv
  9.  
  10. if True: # pylint: disable=using-constant-test
  11. data = blocks.ravel()
  12. indptr = np.arange(0, (self.v.r.size+1)*3, 3)
  13. indices = col(np.arange(0, self.v.r.size))
  14. indices = np.hstack([indices, indices, indices])
  15. indices = indices.reshape((-1, 3))
  16. indices = indices.transpose((0, 1)).ravel()
  17. result = sp.csc_matrix((data, indptr), shape=(self.v.r.size, self.v.r.size))
  18. return result
  19. else:
  20. matvec = lambda x: np.einsum(''ijk,ik->ij'', blocks, x.reshape((blocks.shape[0], 3))).ravel()
  21. return sp.linalg.LinearOperator((self.v.r.size, self.v.r.size), matvec=matvec)
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def test_einsum_misc(self):
  2. # This call used to crash because of a bug in
  3. # PyArray_AssignZero
  4. a = np.ones((1, 64]]])
项目:krpcScripts    作者:jwvanderbeck    | 项目源码 | 文件源码
  1. def test_einsum_all_contig_non_contig_output(self):
  2. # Issue gh-5907, correct_base)
项目:GPflow    作者:GPflow    | 项目源码 | 文件源码
  1. def test_exKxz_pairwise(self):
  2. covall = np.array([self.Xcov, self.Xcovc])
  3. for k in self.kernels:
  4. with self.test_context():
  5. if isinstance(k, ekernels.Linear):
  6. continue
  7. k.compile()
  8. exKxz = k.compute_exKxz_pairwise(self.Z, self.Xmu, covall)
  9. Kxz = k.compute_K(self.Xmu[:-1, self.Z) # NxM
  10. xKxz = np.einsum(''nm,nd->nmd'', Kxz, self.Xmu[1:, :])
  11. self.assertTrue(np.allclose(xKxz, exKxz))
  12.  
  13. # def test_exKxz(self):
  14. # for k in self.kernels:
  15. # with self.test_session():
  16. # if isinstance(k,ekernels.Linear):
  17. # continue
  18. # k.compile()
  19. # exKxz = k.compute_exKxz(self.Z,self.Xmu,self.Xcov)
  20. # Kxz = k.compute_K(self.Xmu,self.Z) # NxM
  21. # xKxz = np.einsum(''nm,nd->nmd'',Kxz,self.Xmu)
  22. # self.assertTrue(np.allclose(xKxz,exKxz))
项目:NetPower_Testbed    作者:Vignesh2208    | 项目源码 | 文件源码
  1. def _accumulate_sufficient_statistics(self, stats, obs, framelogprob,
  2. posteriors, fwdlattice, bwdlattice):
  3. super(GaussianHMM, self)._accumulate_sufficient_statistics(
  4. stats, posteriors, bwdlattice)
  5.  
  6. if ''m'' in self.params or ''c'' in self.params:
  7. stats[''post''] += posteriors.sum(axis=0)
  8. stats[''obs''] += np.dot(posteriors.T, obs)
  9.  
  10. if ''c'' in self.params:
  11. if self.covariance_type in (''spherical'', ''diag''):
  12. stats[''obs**2''] += np.dot(posteriors.T, obs ** 2)
  13. elif self.covariance_type in (''tied'', ''full''):
  14. # posteriors: (nt,nc); obs: (nt,nf); obs: (nt,nf)
  15. # -> (nc,nf,nf)
  16. stats[''obs*obs.T''] += np.einsum(
  17. ''ij,ik,il->jkl'', obs)
项目:bayesian-matting    作者:marcoForte    | 项目源码 | 文件源码
  1. def __init__(self, matrix, w):
  2. W = np.sum(w)
  3. self.w = w
  4. self.X = matrix
  5. self.left = None
  6. self.right = None
  7. self.mu = np.einsum(''ij,i->j'', self.X, w)/W
  8. diff = self.X - np.tile(self.mu, [np.shape(self.X)[0], 1])
  9. t = np.einsum(''ij,i->ij'', diff, np.sqrt(w))
  10. self.cov = (t.T @ t)/W + 1e-5*np.eye(3)
  11. self.N = self.X.shape[0]
  12. V, D = np.linalg.eig(self.cov)
  13. self.lmbda = np.max(np.abs(V))
  14. self.e = D[np.argmax(np.abs(V))]
  15.  
  16.  
  17. # S is measurements vector - dim nxd
  18. # w is weights vector - dim n
项目:enterprise    作者:nanograv    | 项目源码 | 文件源码
  1. def _solve_2D2(self, X, Z):
  2. """Solves :math:`Z^T N^{-1}X`,where :math:`X`
  3. and :math:`Z` are 2-d arrays.
  4. """
  5.  
  6. ZNX = np.dot(Z.T / self._nvec, X)
  7. for slc, jv in zip(self._slices, self._jvec):
  8. if slc.stop - slc.start > 1:
  9. Zblock = Z[slc, :]
  10. Xblock = X[slc, :]
  11. niblock = 1 / self._nvec[slc]
  12. beta = 1.0 / (np.einsum(''i->'', niblock)+1.0/jv)
  13. zn = np.dot(niblock, Zblock)
  14. xn = np.dot(niblock, Xblock)
  15. ZNX -= beta * np.outer(zn.T, xn)
  16. return ZNX
项目:enterprise    作者:nanograv    | 项目源码 | 文件源码
  1. def ss_framerotate(mjd, planet, y, z, dz,
  2. offset=None, equatorial=False):
  3. """
  4. Rotate planet trajectory given as (n,3) tensor,
  5. by ecliptic Euler angles x,y,z,and by z rate
  6. dz. The rate has units of rad/year,and is referred
  7. to offset 2010/1/1. dates must be given in MJD.
  8. """
  9. if equatorial:
  10. planet = eq2ecl_vec(planet)
  11.  
  12. E = euler_vec(z + dz * (mjd - t_offset) / 365.25,
  13. planet.shape[0])
  14.  
  15. planet = np.einsum(''ijk, E, planet)
  16.  
  17. if offset is not None:
  18. planet = np.array(offset) + planet
  19.  
  20. if equatorial:
  21. planet = ecl2eq_vec(planet)
  22.  
  23. return planet
项目:PyBGMM    作者:junlulocky    | 项目源码 | 文件源码
  1. def _log_prod_students_t(self, log_prod_var, inv_var, v):
  2. """
  3. Return the value of the log of the product of the univariate Student''s
  4. t PDFs at `X[i]`.
  5. """
  6. delta = self.X[i, :] - mu
  7. return (
  8. self.D * (
  9. self._cached_gammaln_by_2[v + 1] - self._cached_gammaln_by_2[v]
  10. - 0.5*self._cached_log_v[v] - 0.5*self._cached_log_pi
  11. )
  12. - 0.5*log_prod_var
  13. - (v + 1.)/2. * (np.log(1. + 1./v * np.square(delta) * inv_var)).sum()
  14. )
  15.  
  16.  
  17. #-----------------------------------------------------------------------------#
  18. # UTILITY FUNCTIONS #
  19. #-----------------------------------------------------------------------------#
  20.  
  21. # Below is slightly faster than np.sum,see http://stackoverflow.com/questions/
  22. # 18365073/why-is-numpys-einsum-faster-than-numpys-built-in-functions
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
  1. def setUp(self):
  2. self.f = links.Bilinear(
  3. self.in_shape[0], self.in_shape[1], self.out_size)
  4. self.f.W.data[...] = _uniform(*self.f.W.data.shape)
  5. self.f.V1.data[...] = _uniform(*self.f.V1.data.shape)
  6. self.f.V2.data[...] = _uniform(*self.f.V2.data.shape)
  7. self.f.b.data[...] = _uniform(*self.f.b.data.shape)
  8. self.f.zerograds()
  9.  
  10. self.W = self.f.W.data.copy()
  11. self.V1 = self.f.V1.data.copy()
  12. self.V2 = self.f.V2.data.copy()
  13. self.b = self.f.b.data.copy()
  14.  
  15. self.e1 = _uniform(self.batch_size, self.in_shape[0])
  16. self.e2 = _uniform(self.batch_size, self.in_shape[1])
  17. self.gy = _uniform(self.batch_size, self.out_size)
  18.  
  19. self.y = (
  20. numpy.einsum(''ij,jkl->il'', self.e1, self.e2, self.W) +
  21. self.e1.dot(self.V1) + self.e2.dot(self.V2) + self.b)
项目:cellcomplex    作者:VirtualPlants    | 项目源码 | 文件源码
  1. def inside_triangle(point,triangles):
  2. v0 = triangles[:,2]-triangles[:,0]
  3. v1 = triangles[:,1]-triangles[:,0]
  4. v2 = point-triangles[:,0]
  5.  
  6. dot00 = np.einsum(''ij,v0,v0)
  7. dot01 = np.einsum(''ij,v1)
  8. dot02 = np.einsum(''ij,v2)
  9. dot11 = np.einsum(''ij,v1,v1)
  10. dot12 = np.einsum(''ij,v2)
  11.  
  12. invDenom = 1./(dot00 * dot11-dot01*dot01)
  13. u = np.float16((dot11 * dot02 - dot01 * dot12)*invDenom)
  14. v = np.float16((dot00 * dot12 - dot01 * dot02)*invDenom)
  15.  
  16. return (u>=0) & (v>=0) & (u+v<=1)
项目:GPfates    作者:Teichlab    | 项目源码 | 文件源码
  1. def omgp_model_bound(omgp):
  2. '''''' Calculate the part of the omgp bound which does not depend
  3. on the response variable.
  4. ''''''
  5. GP_bound = 0.0
  6.  
  7. LBs = []
  8. # Precalculate the bound minus data fit,
  9. # and LB matrices used for data fit term.
  10. for i, kern in enumerate(omgp.kern):
  11. K = kern.K(omgp.X)
  12. B_inv = np.diag(1. / ((omgp.phi[:, i] + 1e-6) / omgp.variance))
  13. Bi, LB, LBi, Blogdet = pdinv(K + B_inv)
  14. LBs.append(LB)
  15.  
  16. # Penalty
  17. GP_bound -= 0.5 * Blogdet
  18.  
  19. # Constant
  20. GP_bound -= 0.5 * omgp.D * np.einsum(''j,j->'', omgp.phi[:, i], np.log(2 * np.pi * omgp.variance))
  21.  
  22. model_bound = GP_bound + omgp.mixing_prop_bound() + omgp.H
  23.  
  24. return model_bound, LBs
项目:smt    作者:SMTorg    | 项目源码 | 文件源码
  1. def _predict_output_derivatives(self, x):
  2. n = x.shape[0]
  3. nt = self.nt
  4. ny = self.training_points[None][0][1].shape[1]
  5. num = self.num
  6.  
  7. dy_dstates = np.empty(n * num[''dof''])
  8. self.rbfc.compute_jac(n, x.flatten(), dy_dstates)
  9. dy_dstates = dy_dstates.reshape((n, num[''dof'']))
  10.  
  11. dstates_dytl = np.linalg.inv(self.mtx)
  12.  
  13. ones = np.ones(self.nt)
  14. arange = np.arange(self.nt)
  15. dytl_dyt = csc_matrix((ones, (arange, arange)), shape=(num[''dof''], self.nt))
  16.  
  17. dy_dyt = (dytl_dyt.T.dot(dstates_dytl.T).dot(dy_dstates.T)).T
  18. dy_dyt = np.einsum(''ij,k->ijk'', dy_dyt, np.ones(ny))
  19. return {None: dy_dyt}
项目:nn_mask    作者:ZitengWang    | 项目源码 | 文件源码
  1. def get_power_spectral_density_matrix(observation, mask=None):
  2. """
  3. Calculates the weighted power spectral density matrix.
  4.  
  5. This does not yet work with more than one target mask.
  6.  
  7. :param observation: Complex observations with shape (bins,sensors,frames)
  8. :param mask: Masks with shape (bins,frames) or (bins,1,frames)
  9. :return: PSD matrix with shape (bins,sensors)
  10. """
  11. bins, sensors, frames = observation.shape
  12.  
  13. if mask is None:
  14. mask = np.ones((bins, frames))
  15. if mask.ndim == 2:
  16. mask = mask[:, :]
  17.  
  18. normalization = np.maximum(np.sum(mask, axis=-1, keepdims=True), 1e-6)
  19.  
  20. psd = np.einsum(''...dt,...et->...de'', mask * observation,
  21. observation.conj())
  22. psd /= normalization
  23. return psd
项目:nn_mask    作者:ZitengWang    | 项目源码 | 文件源码
  1. def get_mvdr_vector(atf_vector, noise_psd_matrix):
  2. """
  3. Returns the MVDR beamforming vector.
  4.  
  5. :param atf_vector: Acoustic transfer function vector
  6. with shape (...,bins,sensors)
  7. :param noise_psd_matrix: Noise PSD matrix
  8. with shape (bins,sensors)
  9. :return: Set of beamforming vectors with shape (...,sensors)
  10. """
  11.  
  12. while atf_vector.ndim > noise_psd_matrix.ndim - 1:
  13. noise_psd_matrix = np.expand_dims(noise_psd_matrix, axis=0)
  14.  
  15. # Make sure matrix is hermitian
  16. noise_psd_matrix = 0.5 * (
  17. noise_psd_matrix + np.conj(noise_psd_matrix.swapaxes(-1, -2)))
  18.  
  19. numerator = solve(noise_psd_matrix, atf_vector)
  20. denominator = np.einsum(''...d,...d->...'', atf_vector.conj(), numerator)
  21. beamforming_vector = numerator / np.expand_dims(denominator, axis=-1)
  22.  
  23. return beamforming_vector
项目:nn_mask    作者:ZitengWang    | 项目源码 | 文件源码
  1. def apply_sdw_mwf(mix, target_psd_matrix, noise_psd_matrix, mu=1, corr=None):
  2. """
  3. Apply speech distortion weighted MWF: h = Tpsd * e1 / (Tpsd + mu*Npsd)
  4. :param mix: the signal complex FFT
  5. :param target_psd_matrix (bins,sensors)
  6. :param noise_psd_matrix
  7. :param mu: the lagrange factor
  8. :return
  9. """
  10. bins, frames = mix.shape
  11. ref_vector = np.zeros((sensors, dtype=np.float)
  12. if corr is None:
  13. ref_ch = 0
  14. else: # choose the channel with highest correlation with the others
  15. corr=corr.tolist()
  16. while len(corr) > sensors:
  17. corr.remove(np.min(corr))
  18. ref_ch=np.argmax(corr)
  19. ref_vector[ref_ch,0]=1
  20.  
  21. mwf_vector = solve(target_psd_matrix + mu*noise_psd_matrix, target_psd_matrix[:,:,ref_ch])
  22. return np.einsum(''...a,...at->...t'', mwf_vector.conj(), mix)
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda    作者:SignalMedia    | 项目源码 | 文件源码
  1. def test_einsum_misc(self):
  2. # This call used to crash because of a bug in
  3. # PyArray_AssignZero
  4. a = np.ones((1, 64]]])
项目:npstreams    作者:LaurentRDC    | 项目源码 | 文件源码
  1. def test_against_numpy_einsum(self):
  2. """ Test against numpy.einsum """
  3. a = np.arange(60.).reshape(3,4,5)
  4. b = np.arange(24.).reshape(4,2)
  5. stream = [a, b]
  6.  
  7. from_numpy = np.einsum(''ijk,jil->kl'', b)
  8. from_stream = last(ieinsum(stream, ''ijk,jil->kl''))
  9.  
  10. self.assertSequenceEqual(from_numpy.shape, from_stream.shape)
  11. self.assertTrue(np.allclose(from_numpy, from_stream))

A * B + C使用np.einsum

A * B + C使用np.einsum

如何解决A * B + C使用np.einsum?

我正在尝试使用 np.einsum 进行此非常简单的操作,但是我仍然很难理解如何使用它。

我有以下3个数组:

A = np.zeros((8,8))
B = np.zeros((64))
C = np.zeros((64,8,8))

首先,我首先将A * B“按通道”相乘(这意味着A与B中的每个元素相乘,所以结果将是(64,8,8)),然后我将这个结果与C相加。如何使用 np.einsum 执行此操作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

CuPy.einsum 慢?

CuPy.einsum 慢?

如何解决CuPy.einsum 慢?

我最近开始使用 CuPy。在我的应用程序中,函数 einsum 似乎没有一致的执行速度。在下面的第一个示例中,它最多比 tensordot 函数慢 5 倍(例如,对于 M = 128 和 2048)。在下面的第二个示例中,它比 dot 慢,但与 tensordot 一样快。我在使用 einsum 时是否犯了错误?

我的代码:

  1. import numpy as np
  2. import cupy as cp
  3. import timeit
  4. import tabulate
  5. nrepeats = 1000
  6. alpha_max= 11
  7. cp.random.seed(123)
  8. Ts = []
  9. Ms = 2**(1+np.arange(3,alpha_max))
  10. chi = cp.array([[0.,2.5,5.5],[2.5,0.,2.5],[5.5,0. ]])
  11. # 1st test
  12. for M in Ms:
  13. #print(M)
  14. shape = [3,M,M]
  15. ndim = len(shape)
  16. phi = cp.random.rand(*shape)
  17. t_1 = timeit.timeit(lambda: cp.einsum(''mn,n...->m...'',chi,phi),number=nrepeats) / float(nrepeats)
  18. t_2 = timeit.timeit(lambda: cp.einsum(''mn,number=nrepeats) / float(nrepeats)
  19. t_3 = timeit.timeit(lambda: cp.tensordot(chi,phi,axes=([1],[0])),number=nrepeats) / float(nrepeats)
  20. Ts.append([t_1,t_2,t_3])
  21. data = np.array(Ts)
  22. data = np.concatenate([Ms.reshape(-1,1),data],axis=1)
  23. print("1st test")
  24. print(tabulate.tabulate(data,tablefmt=''simple'',headers=[''M''] + ["t_{:d}".format(i) for i in range(1,data.shape[1])]))
  25. print("")
  26. # 2nd test
  27. Ts = []
  28. for M in Ms:
  29. #print(M)
  30. phi = cp.random.rand(M,M)
  31. mat = cp.random.rand(M,M)
  32. t_1 = timeit.timeit(lambda: cp.einsum(''mn,mat,number=nrepeats) / float(nrepeats)
  33. t_2 = timeit.timeit(lambda: cp.dot(mat,number=nrepeats) / float(nrepeats)
  34. t_3 = timeit.timeit(lambda: cp.tensordot(mat,axis=1)
  35. print("2nd test")
  36. print(tabulate.tabulate(data,data.shape[1])]))

我的结果:

  1. 1st test
  2. M t_1 t_2 t_3
  3. ---- ----------- ----------- -----------
  4. 16 0.000625728 0.000117246 2.18285e-05
  5. 32 0.000116557 0.000116172 2.16212e-05
  6. 64 0.000116819 0.000117002 2.15155e-05
  7. 128 0.000116461 0.000116047 2.16749e-05
  8. 256 0.000159753 0.000240077 7.99196e-05
  9. 512 0.00063619 0.00093989 0.000312955
  10. 1024 0.0028338 0.00372209 0.00106168
  11. 2048 0.0128821 0.0149504 0.00289213
  12. 2nd test
  13. M t_1 t_2 t_3
  14. ---- ----------- ----------- -----------
  15. 16 0.000252584 9.88275e-06 1.89785e-05
  16. 32 0.000111875 1.03006e-05 2.8993e-05
  17. 64 0.000117031 1.05121e-05 6.57232e-05
  18. 128 0.000165988 3.82686e-05 7.81205e-05
  19. 256 0.000161549 0.000117307 0.000154361
  20. 512 0.000745595 0.000655062 0.000870415
  21. 1024 0.00495882 0.00228163 0.00665659
  22. 2048 0.0411153 0.017304 0.0508659

我的系统:

  • 在基于 nvidia/cuda:11.0-runtime 的 docker 容器中运行。
  • GPU。 GeForce RTX 2080 8Gb

einsum 可以用于重塑操作数吗?

einsum 可以用于重塑操作数吗?

如何解决einsum 可以用于重塑操作数吗?

我尝试修改以下代码片段以使用 reshape

  1. a = np.random.randn(1,2,3,5)
  2. b = np.random.randn(2,5,10)
  3. np.einsum("ijkl,mjl->kim",a,b.reshape(10,5))

一开始我以为 reshape 只是将操作数转置,但似乎比这更复杂。不整形可以做这个操作吗?

einsum 和 matmul

einsum 和 matmul

如何解决einsum 和 matmul

相关问题BLAS with symmetry in higher order tensor in Fortran

我尝试使用 python 代码来利用张量收缩中的对称性, A[a,b] B[b,c,d] = C[a,d] B[b,d] = B[b,d,c] 因此 C[a,c]。 (假设爱因斯坦求和约定,即重复的 b 表示对其求和)

通过以下代码

  1. import numpy as np
  2. import time
  3. # A[a,b] * B[b,d]
  4. na = nb = nc = nd = 100
  5. A = np.random.random((na,nb))
  6. B = np.random.random((nb,nc,nd))
  7. C = np.zeros((na,nd))
  8. C2= np.zeros((na,nd))
  9. C3= np.zeros((na,nd))
  10. # symmetrize B
  11. for c in range(nc):
  12. for d in range(c):
  13. B[:,d] = B[:,c]
  14. start_time = time.time()
  15. C2 = np.einsum(''ab,bcd->acd'',A,B)
  16. finish_time = time.time()
  17. print(''time einsum'',finish_time - start_time )
  18. start_time = time.time()
  19. for c in range(nc):
  20. # c+1 is needed,since range(0) will be skipped
  21. for d in range(c+1):
  22. #C3[:,d] = np.einsum(''ab,b->a'',A[:,:],B[:,d] )
  23. C3[:,d] = np.matmul(A[:,d] )
  24. for c in range(nc):
  25. for d in range(c+1,nd):
  26. C3[:,d] = C3[:,c]
  27. finish_time = time.time()
  28. print( ''time partial einsum'',finish_time - start_time )
  29. for a in range(int(na/10)):
  30. for c in range(int(nc/10)):
  31. for d in range(int(nd/10)):
  32. if abs((C3-C2)[a,d])> 1.0e-12:
  33. print(''warning'',a,(C3-C2)[a,d])

在我看来 np.matmulnp.einsum 快,例如,通过使用 np.matmul,我得到了

  1. time einsum 0.07406115531921387
  2. time partial einsum 0.0553278923034668

通过使用 np.einsum,我得到了

  1. time einsum 0.0751657485961914
  2. time partial einsum 0.11624622344970703

上面的性能差异是不是一般?我经常认为 einsum 是理所当然的。

解决方法

作为一般规则,我希望 matmul 更快,但在更简单的情况下,einsum 似乎实际上使用了 matmul

但这里是我的时间

  1. In [20]: C2 = np.einsum(''ab,bcd->acd'',A,B)
  2. In [21]: timeit C2 = np.einsum(''ab,B)
  3. 126 ms ± 1.3 ms per loop (mean ± std. dev. of 7 runs,10 loops each)

你的对称性尝试einsum

  1. In [22]: %%timeit
  2. ...: for c in range(nc):
  3. ...: # c+1 is needed,since range(0) will be skipped
  4. ...: for d in range(c+1):
  5. ...: C3[:,c,d] = np.einsum(''ab,b->a'',A[:,:],B[:,d] )
  6. ...: #C3[:,d] = np.matmul(A[:,d] )
  7. ...:
  8. ...: for c in range(nc):
  9. ...: for d in range(c+1,nd):
  10. ...: C3[:,d] = C3[:,d,c]
  11. ...:
  12. 128 ms ± 3.39 ms per loop (mean ± std. dev. of 7 runs,10 loops each)

matmul 相同:

  1. In [23]: %%timeit
  2. ...: for c in range(nc):
  3. ...: # c+1 is needed,since range(0) will be skipped
  4. ...: for d in range(c+1):
  5. ...: #C3[:,d] )
  6. ...: C3[:,c]
  7. ...:
  8. 81.3 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs,10 loops each)

直接matmul

  1. In [24]: C4 = np.matmul(A,B.reshape(100,-1)).reshape(100,100,100)
  2. In [25]: np.allclose(C2,C4)
  3. Out[25]: True
  4. In [26]: timeit C4 = np.matmul(A,100)
  5. 14.9 ms ± 167 µs per loop (mean ± std. dev. of 7 runs,100 loops each)

einsum 也有一个 optimize 标志。我认为只有 3 个或更多参数才重要,但它似乎在这里有帮助:

  1. In [27]: timeit C2 = np.einsum(''ab,B,optimize=True)
  2. 20.3 ms ± 688 µs per loop (mean ± std. dev. of 7 runs,10 loops each)

有时当数组非常大时,某些迭代会更快,因为它降低了内存管理的复杂性。但是我认为在尝试利用对称性时不值得。其他 SO 表明,在某些情况下 matmul 可以检测对称性,并使用自定义 BLAS 调用,但我认为这里不是这种情况(它无法检测 {{1}没有昂贵的比较。)

今天关于Python numpy 模块-einsum() 实例源码python中numpy模块的介绍到此结束,谢谢您的阅读,有关A * B + C使用np.einsum、CuPy.einsum 慢?、einsum 可以用于重塑操作数吗?、einsum 和 matmul等更多相关知识的信息可以在本站进行查询。

本文标签: