最近很多小伙伴都在问Pythonnumpy模块-matmul()实例源码和pythonnumpy.mat这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展einsum和matmul、
最近很多小伙伴都在问Python numpy 模块-matmul() 实例源码和python numpy.mat这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展einsum 和 matmul、InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]、matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配、matmul:输入操作数 1 不匹配等相关知识,下面开始了哦!
本文目录一览:- Python numpy 模块-matmul() 实例源码(python numpy.mat)
- einsum 和 matmul
- InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]
- matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配
- matmul:输入操作数 1 不匹配
Python numpy 模块-matmul() 实例源码(python numpy.mat)
Python numpy 模块,matmul() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.matmul()。
- def svgd_kernel(self, h = -1):
- sq_dist = pdist(self.theta)
- pairwise_dists = squareform(sq_dist)**2
- if h < 0: # if h < 0,using median trick
- h = np.median(pairwise_dists)
- h = np.sqrt(0.5 * h / np.log(self.theta.shape[0]+1))
- # compute the rbf kernel
- Kxy = np.exp( -pairwise_dists / h**2 / 2)
- dxkxy = -np.matmul(Kxy, self.theta)
- sumkxy = np.sum(Kxy, axis=1)
- for i in range(self.theta.shape[1]):
- dxkxy[:, i] = dxkxy[:,i] + np.multiply(self.theta[:,i],sumkxy)
- dxkxy = dxkxy / (h**2)
- return (Kxy, dxkxy)
- def KeyGen(**kwargs):
- ''''''
- Appendix B of BLISS paper
- m_bar = m + n
- o/p:
- A: Public Key n x m'' numpy array
- S: Secret Key m''x n numpy array
- ''''''
- q, n, m, alpha = kwargs[''q''], kwargs[''n''], kwargs[''m''], kwargs[''alpha'']
- Aq_bar = util.crypt_secure_matrix(-(q-1)/2, (q-1)/2, m)
- S_bar = util.crypt_secure_matrix(-(2)**alpha, (2)**alpha, n) # alpha is small enough,we need not reduce (modq)
- S = np.vstack((S_bar, np.eye(n, dtype = int))) # dimension is m_bar x n,Elements are in Z mod(2q)
- A = np.hstack((2*Aq_bar, q * np.eye(n, dtype = int) - 2*np.matmul(Aq_bar,S_bar))) # dimension is n x m_bar,Elements are in Z mod(2q)
- #return util.matrix_to_Zq(A,2*q),S,Aq_bar,S_bar
- return util.matrix_to_Zq(A, 2*q), S
- def test():
- # Classical SIS parameters
- n, alpha, q = 128, 872, 1, 114356107
- kappa = 20
- #discrete Gaussian Parameters
- sd = 300
- eta = 1.2
- A, S = KeyGen(q = q,n = n,m = m,alpha = alpha)
- #print np.array(np.matmul(A,S) - q*np.eye(n),dtype=float)/(2*q) #to test AS = q mod(2q)
- z, c = Sign(msg = "Hello Bob",A = A,S = S,sd = sd,q = q,M = 3.0,kappa = kappa)
- print z
- print c
- print Verify(msg = "Hello Bob", A=A, m=m, n=n, sd=sd, q=q, eta=eta, z=z, c=c, kappa = kappa)
- print Verify(msg = "Hello Robert", kappa = kappa)
- print Verify(msg = "Hello Roberto", kappa = kappa)
- print Verify(msg = "Hola Roberto", kappa = kappa)
- def Verify(**kwargs):
- ''''''
- Verification for the signature
- i/p:
- msg: the string sent by the sender
- (z,c): vectors in Zq,the signature
- A : numpy array,Verification Key dimension nxm
- T : the matrix AS mod q,it is used in the Verification of the signature
- ''''''
- msg, z, c, A, T, sd, eta, k, q = kwargs[''msg''], kwargs[''z''], kwargs[''c''], kwargs[''A''], kwargs[''T''], kwargs[''sd''], kwargs[''eta''], kwargs[''k''], kwargs[''q'']
- norm_bound = eta * sd * np.sqrt(m)
- # checks for norm of z being small and that H(Az-Tc mod q,msg) hashes to c
- vec = util.vector_to_Zq(np.array(np.matmul(A,z) - np.matmul(T,c)), q)
- hashedList = util.hash_to_baseb(vec, msg, 3, k)
- print hashedList, c
- if np.sqrt(z.dot(z)) <= norm_bound and np.array_equal(c, hashedList):
- return True
- else:
- return False
- def KeyGen(n, d, q):
- ''''''
- input:
- q : polynomial size prime number
- n,m,k : dimensions specifiers
- d : SIS parameter,hardest instances are where d ~ q^(n/m)
- output:
- Signing Key S : Matrix of dimension mxk with coefficients in [-d.d]
- Verification Key A : Matrix of dimension nxm with coefficients from [-(q-1)/2,(q-1)/2]
- T : the matrix AS,it is used in the Verification of the signature
- ''''''
- S = crypt_secure_matrix(d, k)
- A = crypt_secure_matrix((q-1)/2, m)
- T = np.matmul(A, S)
- return S, T
- def transfer_color(content, style):
- import scipy.linalg as sl
- # Mean and covariance of content
- content_mean = np.mean(content, axis = (0, 1))
- content_diff = content - content_mean
- content_diff = np.reshape(content_diff, (-1, content_diff.shape[2]))
- content_covariance = np.matmul(content_diff.T, content_diff) / (content_diff.shape[0])
- # Mean and covariance of style
- style_mean = np.mean(style, 1))
- style_diff = style - style_mean
- style_diff = np.reshape(style_diff, style_diff.shape[2]))
- style_covariance = np.matmul(style_diff.T, style_diff) / (style_diff.shape[0])
- # Calculate A and b
- A = np.matmul(sl.sqrtm(content_covariance), sl.inv(sl.sqrtm(style_covariance)))
- b = content_mean - np.matmul(A, style_mean)
- # Construct new style
- new_style = np.reshape(style, style.shape[2])).T
- new_style = np.matmul(A, new_style).T
- new_style = np.reshape(new_style, style.shape)
- new_style = new_style + b
- return new_style
- def svgd_kernel(self, theta, h = -1):
- sq_dist = pdist(theta)
- pairwise_dists = squareform(sq_dist)**2
- if h < 0: # if h < 0,using median trick
- h = np.median(pairwise_dists)
- h = np.sqrt(0.5 * h / np.log(theta.shape[0]+1))
- # compute the rbf kernel
- Kxy = np.exp( -pairwise_dists / h**2 / 2)
- dxkxy = -np.matmul(Kxy, theta)
- sumkxy = np.sum(Kxy, axis=1)
- for i in range(theta.shape[1]):
- dxkxy[:,i] + np.multiply(theta[:, dxkxy)
- def run_random_sim(sim, L):
- """
- Run *L* simulations of the state space model specified by *sim*
- (see :func:`setup_random_sim`). Each simulation is added to *sim*
- index by an integer identifier.
- """
- sim[''L''] = L
- for l in range(L):
- sim[l] = defaultdict(list)
- x_i = sim[''mu''] + NP.matmul(sim[''PI_sqrt''], NP.random.randn(sim[''N'']))
- for i in range(sim[''I'']):
- sim[l][''x''].append(x_i)
- # measurement
- v_i = NP.matmul(sim[''R_sqrt''][i], NP.random.randn(sim[''M'']))
- sim[l][''y''].append(NP.matmul(sim[''H''][i], sim[l][''x''][i]) + v_i)
- # time update
- u_i = NP.matmul(sim[''Q_sqrt''][i], NP.random.randn(sim[''N'']))
- x_i = NP.matmul(sim[''F''][i], x_i) + u_i
- return sim
- def sqrt_kf_sim(sim):
- """
- Process each simulation trial generated with
- :func:`setup_random_test` with a Kalman filter and return the
- posterior state estimates and error covariances.
- """
- post = defaultdict(dict)
- for l in range(sim[''L'']):
- x_hat_l, P_sqrt_l = sqrt_kalman_filter(sim[l][''y''],
- sim[''H''],
- sim[''R_sqrt''],
- sim[''F''],
- sim[''Q_sqrt''],
- sim[''mu''],
- sim[''PI_sqrt''])
- post[l][''x_hat''] = x_hat_l
- if l == 0:
- post[''P''] = [NP.matmul(x, x.T) for x in P_sqrt_l]
- post[l][''error''] = []
- for x_i, x_hat_i in izip(sim[l][''x''], post[l][''x_hat'']):
- post[l][''error''].append(x_hat_i - x_i)
- return post
- def sqrt_kf_tu(x_hat_posterior,
- P_sqrt_posterior,
- F_i,
- Q_sqrt_i,
- z_i=None):
- """
- Square root Kalman filter time update. Given the following:
- - *x_hat_posterior*: posterior state estimate (N)
- - *P_sqrt_posterior*: posterior error covariance square root (NxN)
- - *F_i*: time update operator (NxN)
- - *Q_sqrt_i*: time update noise covariance square root (NxN)
- - *z_i*: optional) systematic time update input (N)
- Return the tuple containing the one time step prediction of the
- state and the square root of the error covariance.
- """
- N, _ = F_i.shape
- x_hat_prior = NP.matmul(F_i, x_hat_posterior)
- if z_i is not None:
- x_hat_prior += z_i
- A_T = NP.block([NP.matmul(F_i, P_sqrt_posterior), Q_sqrt_i])
- R_T = NP.linalg.qr(A_T.T, mode=''r'')
- P_sqrt_prior = R_T.T[:, :N]
- return x_hat_prior, P_sqrt_prior
- def rotate_ascii_stl(self, rotation_matrix, content, filename):
- """Rotate the mesh array and save as ASCII STL."""
- mesh = np.array(content, dtype=np.float64)
- # prefix area vector,if not already done (e.g. in STL format)
- if len(mesh[0]) == 3:
- row_number = int(len(content)/3)
- mesh = mesh.reshape(row_number, 3)
- # upgrade numpy with: "pip install numpy --upgrade"
- rotated_content = np.matmul(mesh, rotation_matrix)
- v0 = rotated_content[:, 0, :]
- v1 = rotated_content[:, :]
- v2 = rotated_content[:, 2, :]
- normals = np.cross(np.subtract(v1, v0), np.subtract(v2, v0)) \\
- .reshape(int(len(rotated_content)), 3)
- rotated_content = np.hstack((normals, rotated_content))
- tweaked = list("solid %s" % filename)
- tweaked += list(map(self.write_facett, list(rotated_content)))
- tweaked.append("\\nendsolid %s\\n" % filename)
- tweaked = "".join(tweaked)
- return tweaked
- def test_exceptions(self):
- dims = [
- ((1,), (2,)), # mismatched vector vector
- ((2, # mismatched matrix vector
- ((2, (1, 2)), # mismatched vector matrix
- ((1, 2), (3, 1)), # mismatched matrix matrix
- ((1, ()), # vector scalar
- ((), (1)), # scalar vector
- ((1, 1), # matrix scalar
- ((), # scalar matrix
- ((2, # cannot broadcast
- ]
- for dt, (dm1, dm2) in itertools.product(self.types, dims):
- a = np.ones(dm1, dtype=dt)
- b = np.ones(dm2, dtype=dt)
- assert_raises(ValueError, self.matmul, a, b)
- def test_shapes(self):
- dims = [
- ((1, # broadcast first argument
- ((2, # broadcast second argument
- ((2, # matrix stack sizes match
- ]
- for dt, dtype=dt)
- res = self.matmul(a, b)
- assert_(res.shape == (2, 1))
- # vector vector returns scalars.
- for dt in self.types:
- a = np.ones((2, dtype=dt)
- b = np.ones((2, dtype=dt)
- c = self.matmul(a, b)
- assert_(np.array(c).shape == ())
- def test_numpy_ufunc_override(self):
- # 2016-01-29: NUMPY_UFUNC_disABLED
- return
- class A(np.ndarray):
- def __new__(cls, *args, **kwargs):
- return np.array(*args, **kwargs).view(cls)
- def __numpy_ufunc__(self, ufunc, method, pos, inputs, **kwargs):
- return "A"
- class B(np.ndarray):
- def __new__(cls, **kwargs):
- return NotImplemented
- a = A([1, 2])
- b = B([1, 2])
- c = np.ones(2)
- assert_equal(self.matmul(a, b), "A")
- assert_equal(self.matmul(b, a), "A")
- assert_raises(TypeError, b, c)
- def get_symmetry_permutation(self):
- """
- This a object function to get the permutation group operators.
- Represented as a table.
- """
- sym_perm = []
- numbers = [i for i in range(self.num_count)]
- sym_mat = spglib.get_symmetry(self._spg_cell, symprec=self.symprec)
- ops = [(r, t) for r, t in zip(sym_mat[''rotations''],
- sym_mat[''translations''])]
- for r, t in ops:
- pos_new = np.transpose(np.matmul(r, self._positions.T)) + t
- perm = self._get_new_id_seq(pos_new, numbers)
- sym_perm.append(perm)
- return sym_perm
- def supercell(self, scale_mat):
- """
- Get the supercell of the origin gcell
- scale_mat is similar as H matrix in superlattice generator
- """
- # return self.__class__(...)
- sarr_lat = np.matmul(scale_mat, self.lattice)
- # coor_conv_pos = np.matmul(self.positions,self.lattice)
- # o_conv_pos = np.matmul(coor_conv_pos,np.linalg.inv(scale_mat))
- o_conv_pos = np.matmul(self.positions, np.linalg.inv(scale_mat))
- o_pos = self.get_frac_from_mat(scale_mat)
- l_of_positions = [i for i in map(lambda x: x+o_pos, list(o_conv_pos))]
- pos = np.concatenate(l_of_positions, axis=0)
- n = scale_mat.diagonal().prod()
- numbers = np.repeat(self.numbers, n)
- return self.__class__(sarr_lat, numbers)
- def ams_extractor(x, sr, win_len, shift_len, order):
- from scipy.signal import hilbert
- envelope = np.abs(hilbert(x))
- for i in range(order-1):
- envelope = np.abs(hilbert(envelope))
- envelope = envelope * 1./3.
- frames = (len(envelope) - win_len) // shift_len
- hanning_window = np.hanning(win_len)
- ams_feature = np.zeros(shape=(15, frames))
- wts = cal_triangle_window(0, sr//2, 15, 15.6, 400)
- for i in range(frames):
- one_frame = x[i*shift_len:i*shift_len+win_len]
- one_frame = one_frame * hanning_window
- frame_fft = np.abs(np.fft.fft(one_frame, win_len))
- ams_feature[:,i] = np.matmul(wts, frame_fft)
- return ams_feature
- def ams_extractor(x, order=1, decimate_coef=1./4.):
- from scipy.signal import hilbert
- envelope = np.abs(hilbert(x))
- for i in range(order-1):
- envelope = np.abs(hilbert(envelope))
- envelope = envelope * decimate_coef
- frames = (len(envelope) - win_len) // shift_len
- hanning_window = np.hanning(win_len)
- ams_feature = np.zeros(shape=(15, frame_fft)
- return ams_feature
- def unkNown_feature_extractor(x, barks, inner_win, inner_shift, win_type, method_version):
- x_spectrum = stft_extractor(x, win_type)
- coef = get_fft_bark_mat(sr, 20, sr//2)
- bark_spect = np.matmul(coef, x_spectrum)
- ams = np.zeros((barks, inner_win//2+1, (bark_spect.shape[1] - inner_win)//inner_shift))
- for i in range(barks):
- channel_stft = stft_extractor(bark_spect[i, :], ''hanning'')
- if method_version == ''v1'':
- ams[i, :, :] = 20 * np.log(np.abs(channel_stft[:inner_win//2+1, :(bark_spect.shape[1] - inner_win)//inner_shift]))
- elif method_version == ''v2'':
- channel_amplitude = np.abs(channel_stft[:inner_win//2+1, :(bark_spect.shape[1] - inner_win)//inner_shift])
- channel_angle = np.angle(channel_stft[:inner_win//2+1, :(bark_spect.shape[1] - inner_win)//inner_shift])
- channel_angle = channel_angle - (np.floor(channel_angle / (2.*np.pi)) * (2.*np.pi))
- ams[i, :] = np.power(channel_amplitude, 1./3.) * channel_angle
- else:
- ams[i, :] = np.abs(channel_stft)
- return ams
- def ams_extractor(x, :] = np.abs(channel_stft)
- return ams
- def run_test_matmul_aa_ci8_shape(self, shape, transpose=False):
- # **Todo: This currently never triggers the transpose path in the backend
- shape_complex = shape[:-1] + (shape[-1] * 2,)
- # Note: The xGPU-like correlation kernel does not support input values of -128 (only [-127:127])
- a8 = ((np.random.random(size=shape_complex) * 2 - 1) * 127).astype(np.int8)
- a_gold = a8.astype(np.float32).view(np.complex64)
- if transpose:
- a_gold = H(a_gold)
- # Note: np.matmul seems to be slow and inaccurate when there are batch dims
- c_gold = np.matmul(a_gold, H(a_gold))
- triu = np.triu_indices(shape[-2] if not transpose else shape[-1], 1)
- c_gold[..., triu[0], triu[1]] = 0
- a = a8.view(bf.DataType.ci8)
- a = bf.asarray(a, space=''cuda'')
- if transpose:
- a = H(a)
- c = bf.zeros_like(c_gold, space=''cuda'')
- self.linalg.matmul(1, None, c)
- c = c.copy(''system'')
- np.testing.assert_allclose(c, c_gold, RTOL, ATOL)
- def run_test_matmul_aa_dtype_shape(self, dtype, axes=None, conj=False):
- a = ((np.random.random(size=shape)) * 127).astype(dtype)
- if axes is None:
- axes = range(len(shape))
- aa = a.transpose(axes)
- if conj:
- aa = aa.conj()
- c_gold = np.matmul(aa, H(aa))
- triu = np.triu_indices(shape[axes[-2]], triu[1]] = 0
- a = bf.asarray(a, space=''cuda'')
- aa = a.transpose(axes)
- if conj:
- aa = aa.conj()
- c = bf.zeros_like(c_gold, aa, ATOL)
- def run_test_matmul_ab_ci8_shape(self, transpose=False):
- ashape_complex = shape[:-2] + (shape[-2], k * 2)
- bshape_complex = shape[:-2] + (k, shape[-1] * 2)
- a8 = (np.random.random(size=ashape_complex) * 255).astype(np.int8)
- b8 = (np.random.random(size=bshape_complex) * 255).astype(np.int8)
- a_gold = a8.astype(np.float32).view(np.complex64)
- b_gold = b8.astype(np.float32).view(np.complex64)
- if transpose:
- a_gold, b_gold = H(b_gold), H(a_gold)
- c_gold = np.matmul(a_gold, b_gold)
- a = a8.view(bf.DataType.ci8)
- b = b8.view(bf.DataType.ci8)
- a = bf.asarray(a, space=''cuda'')
- b = bf.asarray(b, space=''cuda'')
- if transpose:
- a, b = H(b), H(a)
- c = bf.zeros_like(c_gold, ATOL)
- def run_benchmark_matmul_aa_correlator_kernel(self, ntime, nstand, nchan):
- x_shape = (ntime, nchan, nstand*2)
- perm = [1,0,2]
- x8 = ((np.random.random(size=x_shape+(2,))*2-1)*127).astype(np.int8)
- x = x8.astype(np.float32).view(np.complex64).reshape(x_shape)
- x = x.transpose(perm)
- b_gold = np.matmul(H(x[:,[0],:]), x[:,:])
- triu = np.triu_indices(x_shape[-1], 1)
- b_gold[..., triu[1]] = 0
- x = x8.view(bf.DataType.ci8).reshape(x_shape)
- x = bf.asarray(x, space=''cuda'')
- x = x.transpose(perm)
- b = bf.zeros_like(b_gold, space=''cuda'')
- bf.device.stream_synchronize();
- t0 = time.time()
- nrep = 200
- for _ in xrange(nrep):
- self.linalg.matmul(1, x, b)
- bf.device.stream_synchronize();
- dt = time.time() - t0
- nflop = nrep * nchan * ntime * nstand*(nstand+1)/2 * 2*2 * 8
- print nstand, ''\\t'', nflop / dt / 1e9, ''GFLOP/s''
- print ''\\t\\t'', nrep*ntime*nchan / dt / 1e6, ''MHz''
- def select_negtive(self, i_feat, s_feat, sess, topN=50):
- ''''''
- Select the triplets with the largest losses \\n
- return i_feat_pos,s_feat_pos,i_feat_neg,s_feat_neg
- ''''''
- Feed_dict = {self.image_feat: i_feat, self.sentence_feat:s_feat}
- i_embed, s_embed = sess.run([self.image_fc2, self.sentence_fc2], Feed_dict=Feed_dict)
- S = np.matmul(i_embed, s_embed.T)
- i_feat_pos = i_feat.repeat(topN, axis=0)
- s_feat_pos = s_feat.repeat(topN, axis=0)
- N = S.shape[0]
- np.fill_diagonal(S, -2*np.ones(N))
- neg_s_idx = S.argsort(axis=1)[:, -topN:]
- neg_i_idx = S.argsort(axis=0)[-topN:, :]
- s_feat_neg = s_feat[neg_s_idx.flatten(''C'')]
- i_feat_neg = i_feat[neg_i_idx.flatten(''F'')]
- return i_feat_pos, s_feat_pos, i_feat_neg, s_feat_neg
- def top_K_loss(self, sentence, image, K=30, margin=0.5):
- sim_matrix = tf.matmul(sentence, transpose_b=True)
- s_square = tf.reduce_sum(tf.square(sentence), axis=1)
- im_square = tf.reduce_sum(tf.square(image), axis=1)
- d = tf.reshape(s_square,[-1,1]) - 2 * sim_matrix + tf.reshape(im_square, [1, -1])
- positive = tf.stack([tf.matrix_diag_part(d)] * K, axis=1)
- length = tf.shape(d)[-1]
- d = tf.matrix_set_diag(d, 8 * tf.ones([length]))
- sen_loss_K ,_ = tf.nn.top_k(-1.0 * d, K, sorted=False) # note: this is negative value
- im_loss_K,_ = tf.nn.top_k(tf.transpose(-1.0 * d), sorted=False) # note: this is negative value
- sentence_center_loss = tf.nn.relu(positive + sen_loss_K + margin)
- image_center_loss = tf.nn.relu(positive + im_loss_K + margin)
- self.d_neg = (sen_loss_K + im_loss_K)/-2.0
- self.d_pos = positive
- self.endpoint[''debug/im_loss_topK''] = -1.0 * im_loss_K
- self.endpoint[''debug/sen_loss_topK''] = -1.0 * sen_loss_K
- self.endpoint[''debug/d_Matrix''] = d
- self.endpoint[''debug/positive''] = positive
- self.endpoint[''debug/s_center_loss''] = sentence_center_loss
- self.endpoint[''debug/i_center_loss''] = image_center_loss
- self.endpoint[''debug/S''] = sim_matrix
- self.endpoint[''debug/sentence_square''] = s_square
- self.endpoint[''debug/image_square''] = im_square
- return tf.reduce_sum(sentence_center_loss), tf.reduce_sum(image_center_loss)
- def select_negtive(self, s_feat_neg
- def top_K_loss(self, tf.reduce_sum(image_center_loss)
- def conv_feat_map_tensor_gram(conv_fmap_tensor):
- """Compute Gram matrix of conv feature maps.
- Used in style transfer.
- """
- tf.assert_equal(tf.rank(conv_fmap_tensor), 4)
- shape = tf.shape(conv_fmap_tensor)
- num_images = shape[0]
- width = shape[1]
- height = shape[2]
- num_filters = shape[3]
- filters = tf.reshape(conv_fmap_tensor,
- tf.stack([num_images, -1, num_filters]))
- grams = tf.matmul(
- filters, filters,
- transpose_a=True) / tf.to_float(width * height * num_filters)
- return grams
- def abs_to_rel_f(vector, cell, pbc):
- """
- Converts a position vector in absolut coordinates to relative coordinates
- for a film system.
- """
- # Todo this currently only works if the z-coordinate is the one with no pbc
- # Therefore if a structure with x non pbc is given this should also work.
- # maybe write a ''tranform film to fleur_film routine''?
- if len(vector) == 3:
- if pbc[2] == False:
- # leave z coordinate absolut
- # convert only x and y.
- postionR = np.array(vector)
- postionR_f = np.array(postionR[:2])
- cell_np = np.array(cell)
- cell_np = np.array(cell_np[0:2, 0:2])
- inv_cell_np = np.linalg.inv(cell_np)
- new_xy = [i for i in np.matmul(postionR_f, inv_cell_np)]#np.matmul(inv_cell_np,postionR_f)]
- new_rel_pos_f = [new_xy[0], new_xy[1], postionR[2]]
- return new_rel_pos_f
- else:
- print ''FLEUR can not handle this type of film coordinate''
- else:
- return False
- def rel_to_abs_f(vector, cell):
- """
- Converts a position vector in interal coordinates to absolut coordinates
- in Angstroem for a film structure (2D).
- """
- # Todo this currently only works if the z-coordinate is the one with no pbc
- # Therefore if a structure with x non pbc is given this should also work.
- # maybe write a ''tranform film to fleur_film routine''?
- if len(vector) == 3:
- postionR = np.array(vector)
- postionR_f = np.array(postionR[:2])
- #print postionR_f
- cell_np = np.array(cell)
- cell_np = np.array(cell_np[0:2, 0:2])
- #print cell_np
- new_xy = [i for i in np.matmul(postionR_f, cell_np)]
- new_abs_pos_f = [new_xy[0], postionR[2]]
- return new_abs_pos_f
- else:
- return False
- def test_exceptions(self):
- dims = [
- ((1, b)
- def test_shapes(self):
- dims = [
- ((1, b)
- assert_(np.array(c).shape == ())
- def test_numpy_ufunc_override(self):
- # 2016-01-29: NUMPY_UFUNC_disABLED
- return
- class A(np.ndarray):
- def __new__(cls, c)
- def mul(self, matrix):
- ''''''Multiply this matrix by `matrix`
- The order of operation is: `this @ matrix`.
- *Parameters:*
- - `matrix`: `Matrix4`
- ''''''
- # Make a matrix4 shape to matmul function
- view1 = np.reshape(self._values, (4, 4))
- view2 = np.reshape(matrix.values, 4))
- self.tmp.shape = (4, 4)
- # np.matmul(view2,view1,out=out)
- np.matmul(view2, view1, out=self.tmp)
- self.tmp.shape = (16,)
- self._values[:] = self.tmp
- return self
- def select_negtive(self, topN=50):
- ''''''
- Select the triplets with the largest losses \\n
- return i_feat_pos,s_feat_neg
- ''''''
- Feed_dict = {self.image_feat: i_feat, self.sentence_feat:s_feat}
- i_embed, Feed_dict=Feed_dict)
- S = np.matmul(i_embed, s_embed.T)
- i_feat_pos = i_feat.repeat(topN, axis=0)
- s_feat_pos = s_feat.repeat(topN, axis=0)
- N = S.shape[0]
- np.fill_diagonal(S, -2*np.ones(N))
- neg_s_idx = S.argsort(axis=1)[:, -topN:]
- neg_i_idx = S.argsort(axis=0)[-topN:, :]
- s_feat_neg = s_feat[neg_s_idx.flatten(''C'')]
- i_feat_neg = i_feat[neg_i_idx.flatten(''F'')]
- return i_feat_pos, s_feat_neg
- def select_negtive(self, s_feat_neg
- def get_xyz(interface, xyz_from_camera):
- angles = interface.current_status.angles[0:3]
- # Get current XYZ
- P0t = dobotModel.forward_kinematics(angles)
- # Getting Desired XYZ of end effector
- Pct = np.array(CAMERA_OFFSET)
- R0t = dobotModel.R0T(angles)
- Rtc = np.array([[0, 0], [0, -1]])
- R0c = np.matmul(R0t, Rtc)
- Pta = np.matmul(R0c, xyz_from_camera) - np.matmul(R0c, Pct)
- target = np.reshape(Pta, 1)) + np.reshape(P0t, 1))
- return target
- # FUNCTION: Touch - Place the end effector on top of an AR tag
- # AR TAGS: DUCKY = 0 DUCKYBOT = 1 OBSTACLE = 2
- def __compute_valid_convolution_nd(data, kernel, dimension: int):
- convolution_shape = tuple(data.shape[i] - kernel.shape[i] + 1 for i in range(-1, -dimension - 1, -1))
- list_dimension = reduce(lambda a, b: a * b, convolution_shape)
- data_prefix = data.shape[:-dimension]
- kernel_flat = kernel.ravel()
- data_flat = numpy.zeros(data_prefix + (list_dimension, len(kernel_flat)))
- for i in range(list_dimension):
- tensor_slice_start = [0] * len(kernel.shape)
- tensor_slice = [slice(None)] * len(data.shape)
- tensor_slice_start[-1] = i
- for r in range(-1, -len(kernel.shape) - 1, -1):
- dimension_scale = data.shape[r] - kernel.shape[r] + 1
- if tensor_slice_start[r] >= dimension_scale:
- tensor_slice_start[r + 1] = tensor_slice_start[r] // dimension_scale
- tensor_slice_start[r] %= dimension_scale
- tensor_slice[r] = slice(tensor_slice_start[r], tensor_slice_start[r] + kernel.shape[r])
- sub_convolution_index = (slice(None),) * (len(data.shape) - dimension) + tuple([i, slice(None)])
- data_flat[sub_convolution_index] = data[tensor_slice].reshape(data_prefix + (reduce(lambda a, kernel.shape),))
- convolution_flat = numpy.matmul(data_flat, numpy.flip(kernel_flat, axis=0))
- convolution_nd = convolution_flat.reshape(data_prefix + convolution_shape)
- return convolution_nd
- def test_matmul_two_vars():
- x2 = ad.Variable(name=''x2'')
- x3 = ad.Variable(name=''x3'')
- y = ad.matmul(x2, x3)
- grad_x2, grad_x3 = ad.gradients(y, [x2, x3])
- executor = ad.Executor([y, grad_x2, grad_x3])
- x2_val = np.array([[1, 2], [3, 4], [5, 6]]) # 3x2
- x3_val = np.array([[7, 8, 9], [10, 11, 12]]) # 2x3
- y_val, grad_x2_val, grad_x3_val = executor.run(Feed_shapes={x2: x2_val, x3: x3_val})
- expected_yval = np.matmul(x2_val, x3_val)
- expected_grad_x2_val = np.matmul(np.ones_like(expected_yval), np.transpose(x3_val))
- expected_grad_x3_val = np.matmul(np.transpose(x2_val), np.ones_like(expected_yval))
- assert isinstance(y, ad.Node)
- assert np.array_equal(y_val, expected_yval)
- assert np.array_equal(grad_x2_val, expected_grad_x2_val)
- assert np.array_equal(grad_x3_val, expected_grad_x3_val)
- def test_matmul_var_and_param():
- x2 = ad.Variable(name="x2")
- w2_val = np.array([[7, 12]]) # 2x3
- w2 = ad.Parameter(name="w2", init=w2_val)
- y = ad.matmul(x2, w2)
- grad_x2, grad_w2 = ad.gradients(y, w2])
- executor = ad.Executor([y, grad_w2])
- x2_val = np.array([[1, 6]]) # 3x2
- y_val, grad_w2_val = executor.run(Feed_shapes={x2: x2_val})
- expected_yval = np.matmul(x2_val, w2_val)
- expected_grad_x2_val = np.matmul(np.ones_like(expected_yval), np.transpose(w2_val))
- expected_grad_x3_val = np.matmul(np.transpose(x2_val), ad.Node)
- # assert np.array_equal(y_val,expected_yval)
- # assert np.array_equal(grad_x2_val,expected_grad_x2_val)
- # assert np.array_equal(grad_w2_val,expected_grad_x3_val)
- def output_step_scan(self, dummy, new_state):
- if self.Dale_ratio:
- new_output = tf.matmul(
- tf.nn.relu(new_state),
- tf.matmul(
- tf.abs(self.W_out) * self.output_Connectivity,
- self.Dale_out,
- name="in_2"),
- transpose_b=True, name="3")\\
- + self.b_out
- else:
- new_output = tf.matmul(tf.nn.relu(new_state), self.W_out * self.output_Connectivity,
- transpose_b=True, name="3") + self.b_out
- return new_output
- def gradient(x0, X, y, alpha):
- # gradient of the logistic loss
- w, c = x0[1:137], x0[0]
- #print("c is " + str(c))
- z = X.dot(w) + c
- z = phi(y * z)
- z0 = (z - 1) * y
- grad_w = np.matmul(z0,X) / X.shape[0] + alpha * w
- grad_c = z0.sum() / X.shape[0]
- grad_c = np.array(grad_c)
- #print(grad_w[0,1:5])
- return np.c_[([grad_c], grad_w)]
- ##### stochastic Gradient Descent Optimiser ######
- def gradient(x0, grad_w)]
- ##### stochastic Gradient Descent Optimiser ######
- def test_exceptions(self):
- dims = [
- ((1, b)
- def test_shapes(self):
- dims = [
- ((1, b)
- assert_(np.array(c).shape == ())
- def test_numpy_ufunc_override(self):
- # Temporarily disable __numpy_ufunc__ for 1.10; see gh-5844
- return
- class A(np.ndarray):
- def __new__(cls, c)
- def precalc(C, R, x_bar_list, P_bar_list):
- assert C.ndim == 2
- assert R.ndim == 2
- nMeasurement, nStates = x_bar_list.shape
- nObservableState = C.shape[0]
- z_hat_list = C.dot(x_bar_list.T).T
- S_list = np.matmul(np.matmul(C, P_bar_list), C.T) + R
- S_inv_list = np.linalg.inv(S_list)
- K_list = np.matmul(np.matmul(P_bar_list, C.T), S_inv_list)
- P_hat_list = P_bar_list - np.matmul(K_list.dot(C), P_bar_list)
- assert z_hat_list.shape == (nMeasurement, nObservableState), "z_hat ERROR"
- assert S_list.shape == (nMeasurement, nObservableState, "S ERROR"
- assert S_inv_list.shape == S_list.shape, "S_inv ERROR"
- assert K_list.shape == (nMeasurement, nStates, nObservableState)
- assert P_hat_list.shape == P_bar_list.shape, "P_hat ERROR"
- return z_hat_list, S_list, S_inv_list, K_list, P_hat_list
- def correlation(task,load=True):
- self = mytask
- if load:
- self.initialize(_load=True, _logging=False, _log_dir=''other/'')
- data = []
- for batch in self.iterate_minibatches(''valid''):
- xtrain, ytrain = batch
- ytrain = np.eye(10)[ytrain]
- Feed_dict = {self.x: xtrain, self.y: ytrain, self.sigma0: 1., self.initial_keep_prob: task[''initial_keep_prob''], self.is_training: False}
- z = tf.get_collection(''log_network'')[-1]
- batch_z = self.sess.run( z, Feed_dict)
- data.append(batch_z)
- data = np.vstack(data)
- data = data.reshape(data.shape[0],-1)
- def normal_tc(c0):
- c1i = np.diag(1./np.diag(c0))
- p = np.matmul(c1i,c0)
- return - .5 * np.linalg.slogdet(p)[1] / c0.shape[0]
- c0 = np.cov( data, rowvar=False )
- tc = normal_tc(c0)
- print "Total correlation: %f" % tc
- def test_exceptions(self):
- dims = [
- ((1, b)
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}没有昂贵的比较。)
InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]
有人可以解释一下,TensorFlow的急切模式如何工作?我正在尝试建立一个简单的回归,如下所示:
import tensorflow as tftfe = tf.contrib.eagertf.enable_eager_execution()import numpy as npdef make_model(): net = tf.keras.Sequential() net.add(tf.keras.layers.Dense(4, activation=''relu'')) net.add(tf.keras.layers.Dense(1)) return netdef compute_loss(pred, actual): return tf.reduce_mean(tf.square(tf.subtract(pred, actual)))def compute_gradient(model, pred, actual): """compute gradients with given noise and input""" with tf.GradientTape() as tape: loss = compute_loss(pred, actual) grads = tape.gradient(loss, model.variables) return grads, lossdef apply_gradients(optimizer, grads, model_vars): optimizer.apply_gradients(zip(grads, model_vars))model = make_model()optimizer = tf.train.AdamOptimizer(1e-4)x = np.linspace(0,1,1000)y = x+np.random.normal(0,0.3,1000)y = y.astype(''float32'')train_dataset = tf.data.Dataset.from_tensor_slices((y.reshape(-1,1)))epochs = 2# 10batch_size = 25itr = y.shape[0] // batch_sizefor epoch in range(epochs): for data in tf.contrib.eager.Iterator(train_dataset.batch(25)): preds = model(data) grads, loss = compute_gradient(model, preds, data) print(grads) apply_gradients(optimizer, grads, model.variables)# with tf.GradientTape() as tape:# loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(preds, data))))# grads = tape.gradient(loss, model.variables)# print(grads)# optimizer.apply_gradients(zip(grads, model.variables),global_step=None)
Gradient output: [None, None, None, None, None, None]
错误如下:
----------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-3-a589b9123c80> in <module> 35 grads, loss = compute_gradient(model, preds, data) 36 print(grads)---> 37 apply_gradients(optimizer, grads, model.variables) 38 # with tf.GradientTape() as tape: 39 # loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(preds, data))))<ipython-input-3-a589b9123c80> in apply_gradients(optimizer, grads, model_vars) 17 18 def apply_gradients(optimizer, grads, model_vars):---> 19 optimizer.apply_gradients(zip(grads, model_vars)) 20 21 model = make_model()~/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/optimizer.py in apply_gradients(self, grads_and_vars, global_step, name) 589 if not var_list: 590 raise ValueError("No gradients provided for any variable: %s." %--> 591 ([str(v) for _, v, _ in converted_grads_and_vars],)) 592 with ops.init_scope(): 593 self._create_slots(var_list)ValueError: No gradients provided for any variable:
编辑
我更新了我的代码。现在,问题出在梯度计算上,它返回零。我已经检查了非零的损失值。
答案1
小编典典第1部分:
问题确实是您输入的数据类型。默认情况下,您的keras模型期望float32,但您传递的是float64。您可以更改模型的dtype或将输入更改为float32。
更改模型:
def make_model(): net = tf.keras.Sequential() net.add(tf.keras.layers.Dense(4, activation=''relu'', dtype=''float32'')) net.add(tf.keras.layers.Dense(4, activation=''relu'')) net.add(tf.keras.layers.Dense(1)) return net
更改输入: y = y.astype(''float32'')
第2部分:
您需要model(data)
在tf.GradientTape()下调用用于计算模型的函数(即)。例如,您可以compute_loss
使用以下方法替换您的方法:
def compute_loss(model, x, y): pred = model(x) return tf.reduce_mean(tf.square(tf.subtract(pred, y)))
matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配
如何解决matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配
我是 AI 和 tensorflow.js 的完全初学者。目前正在学习 Stephen Grider 的机器学习课程。我应该在下面的代码之后得到一个输出,但我得到了错误。请帮忙:
代码: 线性回归.js:
const tf = require(''@tensorflow/tfjs'');
class LinearRegression {
constructor(features,labels,options) {
this.features = tf.tensor(features);
this.labels = tf.tensor(labels);
this.features = tf.ones([this.features.shape[0],1]).concat(this.features) //generates the column of one for the horse power
this.options = Object.assign(
{ learningRate: 0.1,iterations: 1000 },options
); //default value is 0.1,if the learning rate is provided,the value is overrided... iteration no. of times gradient decent runs
this.weights = tf.zeros([2,1]); // intial tensor of both m and b are zeros
}
gradientDescent() {
const currentGuesses = this.features.matMul(this.weights); //matMul is matrix multiplication which is features * weights
const differences = currentGuesses.sub(this.labels); //(features * weights) - labels
const slopes = this.features
.transpose()
.matMul(differences)
.div(features.shape[0]); // slope of MSE with respect to both m and b. features * ((features * weights) - labels) / total no. of features.
this.weights = this.weights.sub(slopes.mul(this.options.learningRate));
}
train() {
for (let i=0; i < this.options.iterations; i++) {
this.gradientDescent();
}
/*test(testFeatures,testLabels) {
testFeatures = tf.tensor(testFeatures);
testLabels = tf.tensor(testLabels);
} */
}
}
module.exports = LinearRegression;
index.js:
require(''@tensorflow/tfjs-node'');
const tf = require(''@tensorflow/tfjs'');
const loadCSV = require(''./load-csv'');
const LinearRegression = require(''./linear-regression'');
let { features,testFeatures,testLabels } =loadCSV(''./cars.csv'',{
shuffle: true,splitTest: 50,dataColumns: [''horsepower''],labelColumns: [''mpg'']
});
const regression = new LinearRegression(features,{
learningRate: 0.002,iterations: 100
});
regression.train();
console.log(
''Updated M is:'',regression.weights.get(1,0),''Updated B is:'',regression.weights.get(0,0)
);
错误:
D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\ops\\operation.js:32
throw ex;
^
Error: Error in matMul: inner shapes (1) and (2) of Tensors with shapes 684,1 and 2,1 and transposeA=false and transposeB=false must match.
at Object.assert (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\util.js:36:15)
at matMul_ (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\ops\\matmul.js:25:10)
at Object.matMul (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\ops\\operation.js:23:29)
at Tensor.matMul (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\node_modules\\@tensorflow\\tfjs-core\\dist\\tensor.js:315:26)
at LinearRegression.gradientDescent (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\linear-regression.js:19:46)
at LinearRegression.train (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\linear-regression.js:34:18)
at Object.<anonymous> (D:\\Application Development\\MLKits-master\\MLKits-master\\regressions\\index.js:18:12)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
解决方法
错误是由
抛出的this.features.matMul(this.weights)
形状this.features
的[684,1]
和形状this.weights
的[2,1]
之间存在矩阵乘法。为了能够将矩阵 A(形状 [a,b]
)与 B(形状 [c,d]
)相乘,b
和 c
应该匹配,但此处并非如此。
要解决这里的问题,this.weights
应该转置
this.features.matMul(this.weights,false,true)
matmul:输入操作数 1 不匹配
如何解决matmul:输入操作数 1 不匹配
显示:
matmul:输入操作数 1 在其核心维度 0 中存在不匹配,其中 gufunc 签名 (n?,k),(k,m?)->(n?,m?)(大小 5 不同于 1
当我跑步时:
pre = lm.predict(y_test)
请建议做什么
关于Python numpy 模块-matmul() 实例源码和python numpy.mat的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于einsum 和 matmul、InvalidArgumentError:无法计算MatMul,因为输入#0(从零开始)应为浮点张量,但为双张量[Op:MatMul]、matMul 中的错误:形状为 684,1 和 2,1 且 transposeA=false 和 transposeB=false 的张量的内部形状 (1) 和 (2) 必须匹配、matmul:输入操作数 1 不匹配的相关知识,请在本站寻找。
本文标签: