针对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模块)
- A * B + C使用np.einsum
- CuPy.einsum 慢?
- einsum 可以用于重塑操作数吗?
- einsum 和 matmul
Python numpy 模块-einsum() 实例源码(python中numpy模块)
Python numpy 模块,einsum() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.einsum()。
- def total_length_selected(ed=''empty'', coords=''empty'', ob=''empty''):
- ''''''Returns the total length of all edge segments''''''
- if ob == ''empty'':
- ob = bpy.context.object
- if coords == ''empty'':
- coords = get_coords(ob)
- if ed == ''empty'':
- ed = get_edge_idx(ob)
- edc = coords[ed]
- e1 = edc[:, 0]
- e2 = edc[:, 1]
- ee1 = e1 - e2
- sel = get_selected_edges(ob)
- ee = ee1[sel]
- leng = np.einsum(''ij,ij->i'', ee, ee)
- return np.sum(np.sqrt(leng))
- def transitions_old(width, height, configs=None, one_per_state=False):
- digit = width * height
- if configs is None:
- configs = generate_configs(digit)
- if one_per_state:
- def pickone(thing):
- index = np.random.randint(0,len(thing))
- return thing[index]
- transitions = np.array([
- generate(
- [c1,pickone(successors(c1,width,height))],height)
- for c1 in configs ])
- else:
- transitions = np.array([ generate([c1,c2],height)
- for c1 in configs for c2 in successors(c1,height) ])
- return np.einsum(''ab...->ba...'',transitions)
- def puzzle_plot(p):
- p.setup()
- def name(template):
- return template.format(p.__name__)
- from itertools import islice
- configs = list(islice(p.generate_configs(9), 1000)) # be careful,islice is not immutable!!!
- import numpy.random as random
- random.shuffle(configs)
- configs = configs[:10]
- puzzles = p.generate(configs, 3, 3)
- print(puzzles.shape, "mean", puzzles.mean(), "stdev", np.std(puzzles))
- plot_image(puzzles[-1], name("{}.png"))
- plot_image(np.clip(puzzles[-1]+np.random.normal(0,0.1,puzzles[-1].shape),0,1),name("{}+noise.png"))
- plot_image(np.round(np.clip(puzzles[-1]+np.random.normal(0,1)),name("{}+noise+round.png"))
- plot_grid(puzzles, name("{}s.png"))
- _transitions = p.transitions(3,3,configs=configs)
- print(_transitions.shape)
- transitions_for_show = \\
- np.einsum(''ba...->ab...'',_transitions) \\
- .reshape((-1,)+_transitions.shape[2:])
- print(transitions_for_show.shape)
- plot_grid(transitions_for_show, name("{}_transitions.png"))
- def run(ae,xs):
- zs = ae.encode_binary(xs)
- ys = ae.decode_binary(zs)
- mod_ys = []
- correlations = []
- print(ys.shape)
- print("corrlations:")
- print("bit \\ image {}".format(range(len(xs))))
- for i in range(ae.N):
- mod_zs = np.copy(zs)
- # increase the latent value from 0 to 1 and check the difference
- for j in range(11):
- mod_zs[:,i] = j / 10.0
- mod_ys.append(ae.decode_binary(mod_zs))
- zero_zs,one_zs = np.copy(zs),np.copy(zs)
- zero_zs[:,i] = 0.
- one_zs[:,i] = 1.
- correlation = np.mean(np.square(ae.decode_binary(zero_zs) - ae.decode_binary(one_zs)),
- axis=(1,2))
- correlations.append(correlation)
- print("{:>5} {}".format(i,correlation))
- plot_grid2(np.einsum("ib...->bi...",np.array(mod_ys)).reshape((-1,)+ys.shape[1:]),
- w=11,path=ae.local("dump_significance.png"))
- return np.einsum("ib->bi",correlations)
- def buildFock(self):
- """Routine to build the AO basis Fock matrix"""
- if self.direct:
- if self.incFockRst: # restart incremental fock build?
- self.G = formPT(self.P,np.zeros_like(self.P),self.bfs,
- self.nbasis,self.screen,self.scrTol)
- self.G = 0.5*(self.G + self.G.T)
- self.F = self.Core.astype(''complex'') + self.G
- else:
- self.G = formPT(self.P,self.P_old,self.nbasis,
- self.screen,self.scrTol)
- self.G = 0.5*(self.G + self.G.T)
- self.F = self.F_old + self.G
- else:
- self.J = np.einsum(''pqrs,sr->pq'', self.Twoe.astype(''complex''),self.P)
- self.K = np.einsum(''psqr,self.P)
- self.G = 2.*self.J - self.K
- self.F = self.Core.astype(''complex'') + self.G
- def pointsInRegion(regNum, vor, p, overlap=0.0):
- """
- returns the subset of points p that are inside the regNum region of the voronoi object
- vor. The boundaries of the region are extended by an amount given by ''overlap''.
- """
- reg = vor.regions[vor.point_region[regNum]] # region associated with the point
- if -1 in reg:
- raise Exception(''Open region associated with generator'')
- nVerts = len(reg) # number of verticies in the region
- p0 = vor.points[regNum]
- for i in range(len(reg)):
- vert1, vert2 = vor.vertices[reg[i]], vor.vertices[reg[(i + 1) % len(reg)]]
- dr = vert1 - vert2 # edge
- dr = dr / numpy.linalg.norm(dr) # normalize
- dn = numpy.array([dr[1], -dr[0]]) # normal to edge
- dn = dn if numpy.dot(dn, vert2 - p0[:2]) > 0 else -dn # orient so that the normal is outwards
- d1 = numpy.einsum(''i,ji'', dn, vert2 + dn * overlap - p[:, :2])
- p = p[d1 * numpy.dot(dn, vert2 - p0[:2]) > 0]
- return p
- def test_einsum_misc(self):
- # This call used to crash because of a bug in
- # PyArray_AssignZero
- a = np.ones((1, 2))
- b = np.ones((2, 2, 1))
- assert_equal(np.einsum(''ij...,j...->i...'', a, b), [[[2], [2]]])
- # The iterator had an issue with buffering this reduction
- a = np.ones((5, 12, 4, 3), np.int64)
- b = np.ones((5, 11), np.int64)
- assert_equal(np.einsum(''ijklm,ijn,ijn->'', b,
- np.einsum(''ijklm, b))
- # Issue #2027,was a problem in the contiguous 3-argument
- # inner loop implementation
- a = np.arange(1, 3)
- b = np.arange(1, 5).reshape(2, 2)
- c = np.arange(1, 9).reshape(4, 2)
- assert_equal(np.einsum(''x,yx,zx->xzy'', c),
- [[[1, 3], [3, 9], [5, 15], [7, 21]],
- [[8, 16], [16, 32], [24, 48], [32, 64]]])
- def test_einsum_all_contig_non_contig_output(self):
- # Issue gh-5907,tests that the all contiguous special case
- # actually checks the contiguity of the output
- x = np.ones((5, 5))
- out = np.ones(10)[::2]
- correct_base = np.ones(10)
- correct_base[::2] = 5
- # Always worked (inner iteration is done with 0-stride):
- np.einsum(''mi,mi,mi->m'', x, out=out)
- assert_array_equal(out.base, correct_base)
- # Example 1:
- out = np.ones(10)[::2]
- np.einsum(''im,im,im->m'', correct_base)
- # Example 2,buffering causes x to be contiguous but
- # special cases do not catch the operation before:
- out = np.ones((2, 2))[..., 0]
- correct_base = np.ones((2, 2))
- correct_base[..., 0] = 2
- x = np.ones((2, 2), np.float32)
- np.einsum(''ij,jk->ik'', correct_base)
- def dihedral_transform_batch(x):
- g = np.random.randint(low=0, high=8, size=x.shape[0])
- h, w = x.shape[-2:]
- hh = (h - 1) / 2.
- hw = (w - 1) / 2.
- I, J = np.meshgrid(np.linspace(-hh, hh, x.shape[-2]), np.linspace(-hw, hw, x.shape[-1]))
- C = np.r_[[I, J]]
- D4C = np.einsum(''...ij,jkl->...ikl'', D4, C)
- D4C[:, 0] += hh
- D4C[:, 1] += hw
- D4C = D4C.astype(int)
- x_out = np.empty_like(x)
- for i in range(x.shape[0]):
- I, J = D4C[g[i]]
- x_out[i, :] = x[i][:, J, I]
- return x_out
- def get_vol(simplex):
- # Compute the volume via the Cayley-Menger determinant
- # <http://mathworld.wolfram.com/Cayley-MengerDeterminant.html>. One
- # advantage is that it can compute the volume of the simplex indenpendent
- # of the dimension of the space where it''s embedded.
- # compute all edge lengths
- edges = numpy.subtract(simplex[:, None], simplex[None, :])
- ei_dot_ej = numpy.einsum(''...k,...k->...'', edges, edges)
- j = simplex.shape[0] - 1
- a = numpy.empty((j+2, j+2) + ei_dot_ej.shape[2:])
- a[1:, 1:] = ei_dot_ej
- a[0, 1:] = 1.0
- a[1:, 0] = 1.0
- a[0, 0] = 0.0
- a = numpy.moveaxis(a, (0, 1), (-2, -1))
- det = numpy.linalg.det(a)
- vol = numpy.sqrt((-1.0)**(j+1) / 2**j / math.factorial(j)**2 * det)
- return vol
- def scalar_product_interval(anchors, indizes_1, indizes_2):
- q = (anchors[1][0]-anchors[0][0])
- vector_1 = np.vstack([
- anchors[0][1][indizes_1], # a_1
- anchors[0][2][indizes_1] * q, # b_1
- anchors[1][1][indizes_1], # c_1
- anchors[1][2][indizes_1] * q, # d_1
- ])
- vector_2 = np.vstack([
- anchors[0][1][indizes_2], # a_2
- anchors[0][2][indizes_2] * q, # b_2
- anchors[1][1][indizes_2], # c_2
- anchors[1][2][indizes_2] * q, # d_2
- ])
- return np.einsum(
- vector_1, [0,2],
- sp_matrix,1],
- vector_2, [1,2]
- )*q
- def scalar_product_partial(anchors, indizes_2, start):
- q = (anchors[1][0]-anchors[0][0])
- z = (start-anchors[1][0]) / q
- vector_1 = np.vstack([
- anchors[0][1][indizes_1],
- partial_sp_matrix(z),2]
- )*q
- def mvl(pha, amp, optimize):
- """Mean Vector Length (Canolty,2006).
- Parameters
- ----------
- pha : array_like
- Array of phases of shapes (npha,...,npts)
- amp : array_like
- Array of amplitudes of shapes (namp,npts)
- Returns
- -------
- pac : array_like
- PAC of shape (npha,namp,...)
- """
- # Number of time points :
- npts = pha.shape[-1]
- return np.abs(np.einsum(''i...j,k...j->ik...'', np.exp(1j * pha),
- optimize=optimize)) / npts
- def ps(pha, optimize):
- """Phase Synchrony (Penny,2008; Cohen,2008).
- Parameters
- ----------
- pha : array_like
- Array of phases of shapes (npha,...)
- """
- # Number of time points :
- npts = pha.shape[-1]
- pac = np.einsum(''i...j, np.exp(-1j * amp),
- optimize=optimize)
- return np.abs(pac) / npts
- def half_space(self):
- """Return the half space polytope respresentation of the infinite
- beam."""
- # add half beam width along the normal direction to each of the points
- half = self.normal * self.size / 2
- edges = [Line(self.p1 + half, self.p2 + half),
- Line(self.p1 - half, self.p2 - half)]
- A = np.ndarray((len(edges), self.dim))
- B = np.ndarray(len(edges))
- for i in range(0, 2):
- A[i, :], B[i] = edges[i].standard
- # test for positive or negative side of line
- if np.einsum(''i,i'', self.p1._x, A[i, :]) > B[i]:
- A[i, :] = -A[i, :]
- B[i] = -B[i]
- p = pt.polytope(A, B)
- return p
- def forward_prop_random_thru_post_mm(self, model, mx, vx, mu, Su):
- Kuu_noiseless = compute_kernel(
- 2 * model.ls, 2 * model.sf, model.zu, model.zu)
- Kuu = Kuu_noiseless + np.diag(jitter * np.ones((self.M, )))
- # Todo: remove inv
- Kuuinv = np.linalg.inv(Kuu)
- A = np.dot(Kuuinv, mu)
- Smm = Su + np.outer(mu, mu)
- B_sto = np.dot(Kuuinv, np.dot(Smm, Kuuinv)) - Kuuinv
- psi0 = np.exp(2.0 * model.sf)
- psi1, psi2 = compute_psi_weave(
- 2 * model.ls, model.zu)
- mout = np.einsum(''nm,md->nd'', psi1, A)
- Bpsi2 = np.einsum(''ab,nab->n'', B_sto, psi2)[:, np.newaxis]
- vout = psi0 + Bpsi2 - mout**2
- return mout, vout
- def _forward_prop_deterministic_thru_post(self, return_info=False):
- """Propagate deterministic inputs thru posterior
- Args:
- x (float): input values,size K x Din
- return_info (bool,optional): Description
- Returns:
- float,size K x Dout: output means
- float,size K x Dout: output variances
- """
- psi0 = np.exp(2 * self.sf)
- psi1 = compute_kernel(2 * self.ls, 2 * self.sf, self.zu)
- mout = np.einsum(''nm,dm->nd'', self.A)
- Bpsi2 = np.einsum(''dab,na,nb->nd'', self.B_det, psi1)
- vout = psi0 + Bpsi2
- if return_info:
- return mout, vout, psi1
- else:
- return mout, vout
- def _forward_prop_random_thru_post_mm(self, return_info=False):
- """Propagate uncertain inputs thru posterior,using Moment Matching
- Args:
- mx (float): input means,size K x Din
- vx (TYPE): input variances,size K x Dout: output variances
- """
- psi0 = np.exp(2.0 * self.sf)
- psi1, psi2 = compute_psi_weave(
- 2 * self.ls,nab->nd'', self.B_sto, psi2)
- vout = psi0 + Bpsi2 - mout**2
- if return_info:
- return mout, psi2
- else:
- return mout, vout
- def sample(self, x):
- """Summary
- Args:
- x (TYPE): Description
- Returns:
- TYPE: Description
- """
- Su = self.Su
- mu = self.mu
- Lu = np.linalg.cholesky(Su)
- epsilon = np.random.randn(self.Dout, self.M)
- u_sample = mu + np.einsum(''dab,db->da'', Lu, epsilon)
- kff = compute_kernel(2 * self.ls, x)
- kff += np.diag(JITTER * np.ones(x.shape[0]))
- kfu = compute_kernel(2 * self.ls, self.zu)
- qfu = np.dot(kfu, self.Kuuinv)
- mf = np.einsum(''nm, qfu, u_sample)
- vf = kff - np.dot(qfu, kfu.T)
- Lf = np.linalg.cholesky(vf)
- epsilon = np.random.randn(x.shape[0], self.Dout)
- f_sample = mf + np.einsum(''ab,bd->ad'', Lf, epsilon)
- return f_sample
- def _forward_prop_deterministic_thru_cav(self, x):
- """Propagate deterministic inputs thru cavity
- Args:
- x (float): input values,size K x Din
- Returns:
- float,size K x Dout: output variances
- float,size K x M: cross covariance matrix
- """
- kff = np.exp(2 * self.sf)
- kfu = compute_kernel(2 * self.ls, kfu, self.Ahat)
- Bkfukuf = np.einsum(''dab, self.Bhat_det, kfu)
- vout = kff + Bkfukuf
- return mout, kfu
- def _forward_prop_random_thru_cav_mm(self, vx):
- """Propagate uncertain inputs thru cavity,using simple Moment Matching
- Args:
- mx (float): input means,size K x Din
- Returns:
- output means and variances,and intermediate info for backprop
- """
- psi0 = np.exp(2 * self.sf)
- psi1, self.Ahat)
- Bhatpsi2 = np.einsum(''dab, self.Bhat_sto, psi2)
- vout = psi0 + Bhatpsi2 - mout**2
- return mout, psi2
- def psi1compDer(dL_dpsi1, _psi1, variance, lengthscale, Z, S):
- # here are the "statistics" for psi1
- # Produced intermediate results: dL_dparams w.r.t. psi1
- # _dL_dvariance 1
- # _dL_dlengthscale Q
- # _dL_dZ MxQ
- # _dL_dgamma NxQ
- # _dL_dmu NxQ
- # _dL_dS NxQ
- lengthscale2 = np.square(lengthscale)
- Lpsi1 = dL_dpsi1 * _psi1
- Zmu = Z[None, :, :] - mu[:, None, :] # NxMxQ
- denom = 1. / (S + lengthscale2)
- Zmu2_denom = np.square(Zmu) * denom[:, :] # NxMxQ
- _dL_dvar = Lpsi1.sum() / variance
- _dL_dmu = np.einsum(''nm,nmq,nq->nq'', Lpsi1, Zmu, denom)
- _dL_dS = np.einsum(''nm, (Zmu2_denom - 1.), denom) / 2.
- _dL_dZ = -np.einsum(''nm,nq->mq'', denom)
- _dL_dl = np.einsum(''nm,nq->q'', (Zmu2_denom +
- (S / lengthscale2)[:, :]), denom * lengthscale)
- return _dL_dvar, _dL_dl, _dL_dZ, _dL_dmu, _dL_dS
- def kfucompDer(dL_dkfu, grad_x):
- # here are the "statistics" for psi1
- # Produced intermediate results: dL_dparams w.r.t. psi1
- # _dL_dvariance 1
- # _dL_dlengthscale Q
- # _dL_dZ MxQ
- lengthscale2 = np.square(lengthscale)
- Lpsi1 = dL_dkfu * kfu
- Zmu = Z[None, :] # NxMxQ
- _dL_dvar = Lpsi1.sum() / variance
- _dL_dZ = -np.einsum(''nm,nmq->mq'', Zmu / lengthscale2)
- _dL_dl = np.einsum(''nm,nmq->q'', np.square(Zmu) / lengthscale**3)
- if grad_x:
- _dL_dx = np.einsum(''nm,nmq->nq'', Zmu / lengthscale2)
- return _dL_dvar, _dL_dx
- else:
- return _dL_dvar, _dL_dZ
- def _forward_prop_deterministic_thru_cav(self, n, alpha):
- """Summary
- Args:
- n (TYPE): Description
- x (TYPE): Description
- alpha (TYPE): Description
- Returns:
- TYPE: Description
- """
- muhat, Suhat, SuinvMuhat, Suinvhat = self.compute_cavity(n, alpha)
- Kuuinv = self.Kuuinv
- Ahat = np.einsum(''ab,ndb->nda'', Kuuinv, muhat)
- Bhat = np.einsum(
- ''ab,ndbc->ndac'',
- Kuuinv, np.einsum(''ndab,bc->ndac'', Kuuinv)) - Kuuinv
- kff = np.exp(2 * self.sf)
- kfu = compute_kernel(2 * self.ls,ndm->nd'', Ahat)
- Bkfukuf = np.einsum(''ndab, Bhat, kfu)
- vout = kff + Bkfukuf
- extra_res = [muhat, Suinvhat, Ahat, Bhat]
- return mout, extra_res
- def _forward_prop_deterministic_thru_post(self, x):
- """Summary
- Args:
- x (TYPE): Description
- Returns:
- TYPE: Description
- """
- Kuuinv = self.Kuuinv
- A = np.einsum(''ab, self.mu)
- B = np.einsum(
- ''ab,dbc->dac'', np.einsum(''dab,bc->dac'', self.Su, A)
- Bpsi2 = np.einsum(''dab, B, kfu)
- vout = kff + Bpsi2
- return mout, vout
- # Todo
- def _forward_prop_random_thru_post_mm(self, vx):
- """Summary
- Args:
- mx (TYPE): Description
- vx (TYPE): Description
- Returns:
- TYPE: Description
- """
- Kuuinv = self.Kuuinv
- A = np.einsum(''ab, self.mu)
- Smm = self.Su + np.einsum(''da,db->dab'', self.mu, Smm, Kuuinv)) - Kuuinv
- psi0 = np.exp(2.0 * self.sf)
- psi1, psi2)
- vout = psi0 + Bpsi2 - mout**2
- return mout, vout
- def sample(self, epsilon)
- return f_sample
- def compute_cavity(self, alpha=1.0):
- """Summary
- Args:
- n (TYPE): Description
- alpha (float,optional): Description
- Returns:
- TYPE: Description
- """
- # compute the leave one out moments
- t1n = self.t1[n, :]
- t2n = self.t2[n, :]
- Suinvhat = self.Suinv - alpha * t2n
- SuinvMuhat = self.SuinvMu - alpha * t1n
- Suhat = np.linalg.inv(Suinvhat)
- muhat = np.einsum(''ndab, SuinvMuhat)
- return muhat, Suinvhat
- def forward_prop_thru_post(self, vout
- def update_posterior(self, x_train=None, new_hypers=False):
- """Summary
- Returns:
- TYPE: Description
- """
- # compute the posterior approximation
- if new_hypers and x_train is not None:
- Kfu = compute_kernel(2*self.ls, 2*self.sf, x_train, self.zu)
- KuuinvKuf = np.dot(self.Kuuinv, Kfu.T)
- self.Kfu = Kfu
- self.KuuinvKuf = KuuinvKuf
- self.Kff_diag = compute_kernel_diag(2*self.ls, x_train)
- KuuinvKuf_div_var = np.einsum(''an,nd->dan'', self.KuuinvKuf, 1.0 / self.variances)
- T2u = np.einsum(''dan,bn->dab'', KuuinvKuf_div_var, self.KuuinvKuf)
- T1u = np.einsum(''bn,nd->db'', self.means / self.variances)
- Vinv = self.Kuuinv + T2u
- self.Suinv = Vinv
- self.Su = np.linalg.inv(Vinv)
- self.mu = np.einsum(''dab, T1u)
- self.gamma = np.einsum(''ab, self.Kuuinv, self.mu)
- self.beta = self.Kuuinv - np.einsum(''ab,
- self.Kuuinv,
- np.einsum(''dab, self.Kuuinv))
- def compute_cavity(self, idxs, alpha):
- # deletion
- p_i = self.KuuinvKuf[:, idxs].T[:, np.newaxis, :]
- k_i = self.Kfu[idxs, :]
- k_ii = self.Kff_diag[idxs][:, np.newaxis]
- gamma = self.gamma
- beta = self.beta
- h_si = p_i - np.einsum(''dab,nb->nda'', beta, k_i)
- variance_i = self.variances[idxs, :]
- mean_i = self.means[idxs, :]
- dlogZd_dmi2 = 1.0 / (variance_i/alpha -
- np.sum(k_i[:, :] * h_si, axis=2))
- dlogZd_dmi = -dlogZd_dmi2 * (mean_i -
- np.sum(k_i[:, :] * gamma, axis=2))
- hd1 = h_si * dlogZd_dmi[:, np.newaxis]
- hd2h = np.einsum(''nda,ndb->ndab'', h_si, h_si) * dlogZd_dmi2[:, np.newaxis]
- gamma_si = gamma + hd1
- beta_si = beta - hd2h
- # projection
- h = p_i - np.einsum(''ndab, beta_si, k_i)
- m_si_i = np.einsum(''na,nda->nd'', k_i, gamma_si)
- v_si_ii = k_ii - np.einsum(''na,ndab, k_i)
- return m_si_i, v_si_ii, [h, gamma_si]
- def project3Dto2D(self, Li, idxs):
- """
- Project 3D point to 2D
- :param Li: joints in normalized 3D
- :param idxs: frames specified by subset
- :return: 2D points,in normalized 2D coordinates
- """
- if not isinstance(idxs, numpy.ndarray):
- idxs = numpy.asarray([idxs])
- # 3D -> 2D projection also shift by M to cropped window
- 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))
- Li_glob3D_hom = numpy.concatenate([Li_glob3D, numpy.ones((len(idxs)*self.numJoints, dtype=''float32'')], axis=1)
- Li_glob2D_hom = numpy.dot(Li_glob3D_hom, self.cam_proj.T)
- Li_glob2D = (Li_glob2D_hom[:, 0:3] / Li_glob2D_hom[:, 3][:, None]).reshape((len(idxs), 3))
- Li_img2D_hom = numpy.einsum(''ijk,ikl->ijl'', Li_glob2D, self.Di_trans2D[idxs])
- Li_img2D = (Li_img2D_hom[:, 0:2] / Li_img2D_hom[:, 2][:, self.numJoints*2))
- Li_img2Dcrop = (Li_img2D - (self.Di.shape[3]/2.)) / (self.Di.shape[3]/2.)
- return Li_img2Dcrop
- def compute_dr_wrt(self, wrt):
- if wrt is not self.v:
- return None
- v = self.v.r.reshape(-1, 3)
- blocks = -np.einsum(''ij,ik->ijk'', v, v) * (self.ss**(-3./2.)).reshape((-1, 1, 1))
- for i in range(3):
- blocks[:, i, i] += self.s_inv
- if True: # pylint: disable=using-constant-test
- data = blocks.ravel()
- indptr = np.arange(0, (self.v.r.size+1)*3, 3)
- indices = col(np.arange(0, self.v.r.size))
- indices = np.hstack([indices, indices, indices])
- indices = indices.reshape((-1, 3))
- indices = indices.transpose((0, 1)).ravel()
- result = sp.csc_matrix((data, indptr), shape=(self.v.r.size, self.v.r.size))
- return result
- else:
- matvec = lambda x: np.einsum(''ijk,ik->ij'', blocks, x.reshape((blocks.shape[0], 3))).ravel()
- return sp.linalg.LinearOperator((self.v.r.size, self.v.r.size), matvec=matvec)
- def test_einsum_misc(self):
- # This call used to crash because of a bug in
- # PyArray_AssignZero
- a = np.ones((1, 64]]])
- def test_einsum_all_contig_non_contig_output(self):
- # Issue gh-5907, correct_base)
- def test_exKxz_pairwise(self):
- covall = np.array([self.Xcov, self.Xcovc])
- for k in self.kernels:
- with self.test_context():
- if isinstance(k, ekernels.Linear):
- continue
- k.compile()
- exKxz = k.compute_exKxz_pairwise(self.Z, self.Xmu, covall)
- Kxz = k.compute_K(self.Xmu[:-1, self.Z) # NxM
- xKxz = np.einsum(''nm,nd->nmd'', Kxz, self.Xmu[1:, :])
- self.assertTrue(np.allclose(xKxz, exKxz))
- # def test_exKxz(self):
- # for k in self.kernels:
- # with self.test_session():
- # if isinstance(k,ekernels.Linear):
- # continue
- # k.compile()
- # exKxz = k.compute_exKxz(self.Z,self.Xmu,self.Xcov)
- # Kxz = k.compute_K(self.Xmu,self.Z) # NxM
- # xKxz = np.einsum(''nm,nd->nmd'',Kxz,self.Xmu)
- # self.assertTrue(np.allclose(xKxz,exKxz))
- def _accumulate_sufficient_statistics(self, stats, obs, framelogprob,
- posteriors, fwdlattice, bwdlattice):
- super(GaussianHMM, self)._accumulate_sufficient_statistics(
- stats, posteriors, bwdlattice)
- if ''m'' in self.params or ''c'' in self.params:
- stats[''post''] += posteriors.sum(axis=0)
- stats[''obs''] += np.dot(posteriors.T, obs)
- if ''c'' in self.params:
- if self.covariance_type in (''spherical'', ''diag''):
- stats[''obs**2''] += np.dot(posteriors.T, obs ** 2)
- elif self.covariance_type in (''tied'', ''full''):
- # posteriors: (nt,nc); obs: (nt,nf); obs: (nt,nf)
- # -> (nc,nf,nf)
- stats[''obs*obs.T''] += np.einsum(
- ''ij,ik,il->jkl'', obs)
- def __init__(self, matrix, w):
- W = np.sum(w)
- self.w = w
- self.X = matrix
- self.left = None
- self.right = None
- self.mu = np.einsum(''ij,i->j'', self.X, w)/W
- diff = self.X - np.tile(self.mu, [np.shape(self.X)[0], 1])
- t = np.einsum(''ij,i->ij'', diff, np.sqrt(w))
- self.cov = (t.T @ t)/W + 1e-5*np.eye(3)
- self.N = self.X.shape[0]
- V, D = np.linalg.eig(self.cov)
- self.lmbda = np.max(np.abs(V))
- self.e = D[np.argmax(np.abs(V))]
- # S is measurements vector - dim nxd
- # w is weights vector - dim n
- def _solve_2D2(self, X, Z):
- """Solves :math:`Z^T N^{-1}X`,where :math:`X`
- and :math:`Z` are 2-d arrays.
- """
- ZNX = np.dot(Z.T / self._nvec, X)
- for slc, jv in zip(self._slices, self._jvec):
- if slc.stop - slc.start > 1:
- Zblock = Z[slc, :]
- Xblock = X[slc, :]
- niblock = 1 / self._nvec[slc]
- beta = 1.0 / (np.einsum(''i->'', niblock)+1.0/jv)
- zn = np.dot(niblock, Zblock)
- xn = np.dot(niblock, Xblock)
- ZNX -= beta * np.outer(zn.T, xn)
- return ZNX
- def ss_framerotate(mjd, planet, y, z, dz,
- offset=None, equatorial=False):
- """
- Rotate planet trajectory given as (n,3) tensor,
- by ecliptic Euler angles x,y,z,and by z rate
- dz. The rate has units of rad/year,and is referred
- to offset 2010/1/1. dates must be given in MJD.
- """
- if equatorial:
- planet = eq2ecl_vec(planet)
- E = euler_vec(z + dz * (mjd - t_offset) / 365.25,
- planet.shape[0])
- planet = np.einsum(''ijk, E, planet)
- if offset is not None:
- planet = np.array(offset) + planet
- if equatorial:
- planet = ecl2eq_vec(planet)
- return planet
- def _log_prod_students_t(self, log_prod_var, inv_var, v):
- """
- Return the value of the log of the product of the univariate Student''s
- t PDFs at `X[i]`.
- """
- delta = self.X[i, :] - mu
- return (
- self.D * (
- self._cached_gammaln_by_2[v + 1] - self._cached_gammaln_by_2[v]
- - 0.5*self._cached_log_v[v] - 0.5*self._cached_log_pi
- )
- - 0.5*log_prod_var
- - (v + 1.)/2. * (np.log(1. + 1./v * np.square(delta) * inv_var)).sum()
- )
- #-----------------------------------------------------------------------------#
- # UTILITY FUNCTIONS #
- #-----------------------------------------------------------------------------#
- # Below is slightly faster than np.sum,see http://stackoverflow.com/questions/
- # 18365073/why-is-numpys-einsum-faster-than-numpys-built-in-functions
- def setUp(self):
- self.f = links.Bilinear(
- self.in_shape[0], self.in_shape[1], self.out_size)
- self.f.W.data[...] = _uniform(*self.f.W.data.shape)
- self.f.V1.data[...] = _uniform(*self.f.V1.data.shape)
- self.f.V2.data[...] = _uniform(*self.f.V2.data.shape)
- self.f.b.data[...] = _uniform(*self.f.b.data.shape)
- self.f.zerograds()
- self.W = self.f.W.data.copy()
- self.V1 = self.f.V1.data.copy()
- self.V2 = self.f.V2.data.copy()
- self.b = self.f.b.data.copy()
- self.e1 = _uniform(self.batch_size, self.in_shape[0])
- self.e2 = _uniform(self.batch_size, self.in_shape[1])
- self.gy = _uniform(self.batch_size, self.out_size)
- self.y = (
- numpy.einsum(''ij,jkl->il'', self.e1, self.e2, self.W) +
- self.e1.dot(self.V1) + self.e2.dot(self.V2) + self.b)
- def inside_triangle(point,triangles):
- v0 = triangles[:,2]-triangles[:,0]
- v1 = triangles[:,1]-triangles[:,0]
- v2 = point-triangles[:,0]
- dot00 = np.einsum(''ij,v0,v0)
- dot01 = np.einsum(''ij,v1)
- dot02 = np.einsum(''ij,v2)
- dot11 = np.einsum(''ij,v1,v1)
- dot12 = np.einsum(''ij,v2)
- invDenom = 1./(dot00 * dot11-dot01*dot01)
- u = np.float16((dot11 * dot02 - dot01 * dot12)*invDenom)
- v = np.float16((dot00 * dot12 - dot01 * dot02)*invDenom)
- return (u>=0) & (v>=0) & (u+v<=1)
- def omgp_model_bound(omgp):
- '''''' Calculate the part of the omgp bound which does not depend
- on the response variable.
- ''''''
- GP_bound = 0.0
- LBs = []
- # Precalculate the bound minus data fit,
- # and LB matrices used for data fit term.
- for i, kern in enumerate(omgp.kern):
- K = kern.K(omgp.X)
- B_inv = np.diag(1. / ((omgp.phi[:, i] + 1e-6) / omgp.variance))
- Bi, LB, LBi, Blogdet = pdinv(K + B_inv)
- LBs.append(LB)
- # Penalty
- GP_bound -= 0.5 * Blogdet
- # Constant
- GP_bound -= 0.5 * omgp.D * np.einsum(''j,j->'', omgp.phi[:, i], np.log(2 * np.pi * omgp.variance))
- model_bound = GP_bound + omgp.mixing_prop_bound() + omgp.H
- return model_bound, LBs
- def _predict_output_derivatives(self, x):
- n = x.shape[0]
- nt = self.nt
- ny = self.training_points[None][0][1].shape[1]
- num = self.num
- dy_dstates = np.empty(n * num[''dof''])
- self.rbfc.compute_jac(n, x.flatten(), dy_dstates)
- dy_dstates = dy_dstates.reshape((n, num[''dof'']))
- dstates_dytl = np.linalg.inv(self.mtx)
- ones = np.ones(self.nt)
- arange = np.arange(self.nt)
- dytl_dyt = csc_matrix((ones, (arange, arange)), shape=(num[''dof''], self.nt))
- dy_dyt = (dytl_dyt.T.dot(dstates_dytl.T).dot(dy_dstates.T)).T
- dy_dyt = np.einsum(''ij,k->ijk'', dy_dyt, np.ones(ny))
- return {None: dy_dyt}
- def get_power_spectral_density_matrix(observation, mask=None):
- """
- Calculates the weighted power spectral density matrix.
- This does not yet work with more than one target mask.
- :param observation: Complex observations with shape (bins,sensors,frames)
- :param mask: Masks with shape (bins,frames) or (bins,1,frames)
- :return: PSD matrix with shape (bins,sensors)
- """
- bins, sensors, frames = observation.shape
- if mask is None:
- mask = np.ones((bins, frames))
- if mask.ndim == 2:
- mask = mask[:, :]
- normalization = np.maximum(np.sum(mask, axis=-1, keepdims=True), 1e-6)
- psd = np.einsum(''...dt,...et->...de'', mask * observation,
- observation.conj())
- psd /= normalization
- return psd
- def get_mvdr_vector(atf_vector, noise_psd_matrix):
- """
- Returns the MVDR beamforming vector.
- :param atf_vector: Acoustic transfer function vector
- with shape (...,bins,sensors)
- :param noise_psd_matrix: Noise PSD matrix
- with shape (bins,sensors)
- :return: Set of beamforming vectors with shape (...,sensors)
- """
- while atf_vector.ndim > noise_psd_matrix.ndim - 1:
- noise_psd_matrix = np.expand_dims(noise_psd_matrix, axis=0)
- # Make sure matrix is hermitian
- noise_psd_matrix = 0.5 * (
- noise_psd_matrix + np.conj(noise_psd_matrix.swapaxes(-1, -2)))
- numerator = solve(noise_psd_matrix, atf_vector)
- denominator = np.einsum(''...d,...d->...'', atf_vector.conj(), numerator)
- beamforming_vector = numerator / np.expand_dims(denominator, axis=-1)
- return beamforming_vector
- def apply_sdw_mwf(mix, target_psd_matrix, noise_psd_matrix, mu=1, corr=None):
- """
- Apply speech distortion weighted MWF: h = Tpsd * e1 / (Tpsd + mu*Npsd)
- :param mix: the signal complex FFT
- :param target_psd_matrix (bins,sensors)
- :param noise_psd_matrix
- :param mu: the lagrange factor
- :return
- """
- bins, frames = mix.shape
- ref_vector = np.zeros((sensors, dtype=np.float)
- if corr is None:
- ref_ch = 0
- else: # choose the channel with highest correlation with the others
- corr=corr.tolist()
- while len(corr) > sensors:
- corr.remove(np.min(corr))
- ref_ch=np.argmax(corr)
- ref_vector[ref_ch,0]=1
- mwf_vector = solve(target_psd_matrix + mu*noise_psd_matrix, target_psd_matrix[:,:,ref_ch])
- return np.einsum(''...a,...at->...t'', mwf_vector.conj(), mix)
- def test_einsum_misc(self):
- # This call used to crash because of a bug in
- # PyArray_AssignZero
- a = np.ones((1, 64]]])
- def test_against_numpy_einsum(self):
- """ Test against numpy.einsum """
- a = np.arange(60.).reshape(3,4,5)
- b = np.arange(24.).reshape(4,2)
- stream = [a, b]
- from_numpy = np.einsum(''ijk,jil->kl'', b)
- from_stream = last(ieinsum(stream, ''ijk,jil->kl''))
- self.assertSequenceEqual(from_numpy.shape, from_stream.shape)
- self.assertTrue(np.allclose(from_numpy, from_stream))
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
似乎没有一致的执行速度。在下面的第一个示例中,它最多比 tensordot
函数慢 5 倍(例如,对于 M = 128 和 2048)。在下面的第二个示例中,它比 dot
慢,但与 tensordot
一样快。我在使用 einsum
时是否犯了错误?
我的代码:
import numpy as np
import cupy as cp
import timeit
import tabulate
nrepeats = 1000
alpha_max= 11
cp.random.seed(123)
Ts = []
Ms = 2**(1+np.arange(3,alpha_max))
chi = cp.array([[0.,2.5,5.5],[2.5,0.,2.5],[5.5,0. ]])
# 1st test
for M in Ms:
#print(M)
shape = [3,M,M]
ndim = len(shape)
phi = cp.random.rand(*shape)
t_1 = timeit.timeit(lambda: cp.einsum(''mn,n...->m...'',chi,phi),number=nrepeats) / float(nrepeats)
t_2 = timeit.timeit(lambda: cp.einsum(''mn,number=nrepeats) / float(nrepeats)
t_3 = timeit.timeit(lambda: cp.tensordot(chi,phi,axes=([1],[0])),number=nrepeats) / float(nrepeats)
Ts.append([t_1,t_2,t_3])
data = np.array(Ts)
data = np.concatenate([Ms.reshape(-1,1),data],axis=1)
print("1st test")
print(tabulate.tabulate(data,tablefmt=''simple'',headers=[''M''] + ["t_{:d}".format(i) for i in range(1,data.shape[1])]))
print("")
# 2nd test
Ts = []
for M in Ms:
#print(M)
phi = cp.random.rand(M,M)
mat = cp.random.rand(M,M)
t_1 = timeit.timeit(lambda: cp.einsum(''mn,mat,number=nrepeats) / float(nrepeats)
t_2 = timeit.timeit(lambda: cp.dot(mat,number=nrepeats) / float(nrepeats)
t_3 = timeit.timeit(lambda: cp.tensordot(mat,axis=1)
print("2nd test")
print(tabulate.tabulate(data,data.shape[1])]))
我的结果:
1st test
M t_1 t_2 t_3
---- ----------- ----------- -----------
16 0.000625728 0.000117246 2.18285e-05
32 0.000116557 0.000116172 2.16212e-05
64 0.000116819 0.000117002 2.15155e-05
128 0.000116461 0.000116047 2.16749e-05
256 0.000159753 0.000240077 7.99196e-05
512 0.00063619 0.00093989 0.000312955
1024 0.0028338 0.00372209 0.00106168
2048 0.0128821 0.0149504 0.00289213
2nd test
M t_1 t_2 t_3
---- ----------- ----------- -----------
16 0.000252584 9.88275e-06 1.89785e-05
32 0.000111875 1.03006e-05 2.8993e-05
64 0.000117031 1.05121e-05 6.57232e-05
128 0.000165988 3.82686e-05 7.81205e-05
256 0.000161549 0.000117307 0.000154361
512 0.000745595 0.000655062 0.000870415
1024 0.00495882 0.00228163 0.00665659
2048 0.0411153 0.017304 0.0508659
我的系统:
- 在基于
nvidia/cuda:11.0-runtime
的 docker 容器中运行。 - GPU。 GeForce RTX 2080 8Gb
einsum 可以用于重塑操作数吗?
如何解决einsum 可以用于重塑操作数吗?
我尝试修改以下代码片段以不使用 reshape
a = np.random.randn(1,2,3,5)
b = np.random.randn(2,5,10)
np.einsum("ijkl,mjl->kim",a,b.reshape(10,5))
一开始我以为 reshape 只是将操作数转置,但似乎比这更复杂。不整形可以做这个操作吗?
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
表示对其求和)
通过以下代码
import numpy as np
import time
# A[a,b] * B[b,d]
na = nb = nc = nd = 100
A = np.random.random((na,nb))
B = np.random.random((nb,nc,nd))
C = np.zeros((na,nd))
C2= np.zeros((na,nd))
C3= np.zeros((na,nd))
# symmetrize B
for c in range(nc):
for d in range(c):
B[:,d] = B[:,c]
start_time = time.time()
C2 = np.einsum(''ab,bcd->acd'',A,B)
finish_time = time.time()
print(''time einsum'',finish_time - start_time )
start_time = time.time()
for c in range(nc):
# c+1 is needed,since range(0) will be skipped
for d in range(c+1):
#C3[:,d] = np.einsum(''ab,b->a'',A[:,:],B[:,d] )
C3[:,d] = np.matmul(A[:,d] )
for c in range(nc):
for d in range(c+1,nd):
C3[:,d] = C3[:,c]
finish_time = time.time()
print( ''time partial einsum'',finish_time - start_time )
for a in range(int(na/10)):
for c in range(int(nc/10)):
for d in range(int(nd/10)):
if abs((C3-C2)[a,d])> 1.0e-12:
print(''warning'',a,(C3-C2)[a,d])
在我看来 np.matmul
比 np.einsum
快,例如,通过使用 np.matmul
,我得到了
time einsum 0.07406115531921387
time partial einsum 0.0553278923034668
通过使用 np.einsum
,我得到了
time einsum 0.0751657485961914
time partial einsum 0.11624622344970703
上面的性能差异是不是一般?我经常认为 einsum
是理所当然的。
解决方法
作为一般规则,我希望 matmul
更快,但在更简单的情况下,einsum
似乎实际上使用了 matmul
。
但这里是我的时间
In [20]: C2 = np.einsum(''ab,bcd->acd'',A,B)
In [21]: timeit C2 = np.einsum(''ab,B)
126 ms ± 1.3 ms per loop (mean ± std. dev. of 7 runs,10 loops each)
你的对称性尝试einsum
:
In [22]: %%timeit
...: for c in range(nc):
...: # c+1 is needed,since range(0) will be skipped
...: for d in range(c+1):
...: C3[:,c,d] = np.einsum(''ab,b->a'',A[:,:],B[:,d] )
...: #C3[:,d] = np.matmul(A[:,d] )
...:
...: for c in range(nc):
...: for d in range(c+1,nd):
...: C3[:,d] = C3[:,d,c]
...:
128 ms ± 3.39 ms per loop (mean ± std. dev. of 7 runs,10 loops each)
与 matmul
相同:
In [23]: %%timeit
...: for c in range(nc):
...: # c+1 is needed,since range(0) will be skipped
...: for d in range(c+1):
...: #C3[:,d] )
...: C3[:,c]
...:
81.3 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs,10 loops each)
直接matmul
:
In [24]: C4 = np.matmul(A,B.reshape(100,-1)).reshape(100,100,100)
In [25]: np.allclose(C2,C4)
Out[25]: True
In [26]: timeit C4 = np.matmul(A,100)
14.9 ms ± 167 µs per loop (mean ± std. dev. of 7 runs,100 loops each)
einsum
也有一个 optimize
标志。我认为只有 3 个或更多参数才重要,但它似乎在这里有帮助:
In [27]: timeit C2 = np.einsum(''ab,B,optimize=True)
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等更多相关知识的信息可以在本站进行查询。
本文标签: