def evol(s, B, U, chi, d): for i_bond in [0, 1]: ia = np.mod(i_bond - 1, 2) ib = np.mod(i_bond, 2) ic = np.mod(i_bond + 1, 2) chia = B[ib].shape[1] chic = B[ic].shape[2] # Construct theta matrix and time evolution # theta = cp.tensordot(B[ib], B[ic], axes=(2, 1)) # i a j b theta = cp.tensordot(U, theta, axes=([2, 3], [0, 2])) # ip jp a b theta = cp.tensordot(cp.diag(s[ia]), theta, axes=([1, 2])) # a ip jp b theta = cp.reshape(cp.transpose(theta, (1, 0, 2, 3)), (d * chia, d * chic)) # ip a jp b # Schmidt decomposition # X, Y, Z = cp.linalg.svd(theta, full_matrices=0) chi2 = np.min([cp.sum(Y > 10.**(-10)).get(), chi]) piv = cp.zeros(len(Y), cp.bool) piv[(cp.argsort(Y)[::-1])[:chi2]] = True Y = Y[piv] invsq = cp.sqrt(sum(Y**2)) X = X[:, piv] Z = Z[piv, :] # Obtain the new values for B and s # s[ib] = Y / invsq X = cp.reshape(X, (d, chia, chi2)) X = cp.transpose(cp.tensordot(cp.diag(s[ia]**(-1)), X, axes=(1, 1)), (1, 0, 2)) B[ib] = cp.tensordot(X, cp.diag(s[ib]), axes=(2, 0)) B[ic] = cp.transpose(cp.reshape(Z, (chi2, d, chic)), (1, 0, 2)) return s, B
def get_SpikeSample(dataRAW, row, col, params): """ given a batch of data (time by channels), and some time (row) and channel (col) indices for spikes, this function returns the 1D time clips of voltage around those spike times """ nT, nChan = dataRAW.shape # times around the peak to consider dt = cp.arange(params.nt0) # the negativity is expected at nt0min, so we align the detected peaks there dt = -params.nt0min + dt # temporal indices (awkward way to index into full matrix of data) indsT = row + dt[:, np.newaxis] + 1 # broadcasting indsC = col indsC[indsC < 0] = 0 # anything that's out of bounds just gets set to the limit indsC[ indsC >= nChan] = nChan - 1 # only needed for channels not time (due to time buffer) indsT = cp.transpose(cp.atleast_3d(indsT), [0, 2, 1]) indsC = cp.transpose(cp.atleast_3d(indsC), [2, 0, 1]) # believe it or not, these indices grab just the right timesamples forour spikes ix = indsT + indsC * nT # grab the data and reshape it appropriately (time samples by channels by num spikes) clips = dataRAW.T.ravel()[ix[:, 0, :]].reshape((dt.size, row.size), order='F') # HERE return clips
def HaversineLocal(busMatrix, lineMatrix, haversine=True): MatrizOnibus = cp.copy(busMatrix) MatrizLinhas = cp.copy(lineMatrix) MatrizLinhas = cp.dsplit(MatrizLinhas, 2) MatrizOnibus = cp.dsplit(MatrizOnibus, 2) infVector = cp.squeeze(cp.sum(cp.isnan(MatrizLinhas[0]), axis=1), axis=-1) MatrizLinhas[0] = cp.expand_dims(MatrizLinhas[0], axis=-1) MatrizLinhas[1] = cp.expand_dims(MatrizLinhas[1], axis=-1) MatrizOnibus[0] = cp.expand_dims(MatrizOnibus[0], axis=-1) MatrizOnibus[1] = cp.expand_dims(MatrizOnibus[1], axis=-1) MatrizOnibus[0] *= cp.pi / 180 MatrizOnibus[1] *= cp.pi / 180 MatrizLinhas[1] = cp.transpose(MatrizLinhas[1], [2, 3, 0, 1]) * cp.pi / 180 MatrizLinhas[0] = cp.transpose(MatrizLinhas[0], [2, 3, 0, 1]) * cp.pi / 180 # Haversine or euclidian, based on <haversine> if haversine: results = 1000*2*6371.0088*cp.arcsin( cp.sqrt( (cp.sin((MatrizOnibus[0] - MatrizLinhas[0])*0.5)**2 + \ cp.cos(MatrizOnibus[0])* cp.cos(MatrizLinhas[0]) * cp.sin((MatrizOnibus[1] - MatrizLinhas[1])*0.5)**2) )) else: results = cp.sqrt((MatrizOnibus[0] - MatrizLinhas[0])**2 + (MatrizOnibus[1] - MatrizLinhas[1])**2) return results, infVector
def my_sum(S1, sig, varargin=None): # returns a running sum applied sequentially across a choice of dimensions and bin sizes # S1 is the matrix to be filtered # sig is either a scalar or a sequence of scalars, one for each axis to be filtered. # it's the plus/minus bin length for the summing filter # varargin can be the dimensions to do filtering, if len(sig) != x.shape # if sig is scalar and no axes are provided, the default axis is 2 idims = 1 if varargin is not None: idims = varargin idims = _make_vect(idims) if _is_vect(idims) and _is_vect(sig): sigall = sig else: sigall = np.tile(sig, len(idims)) for sig, idim in zip(sigall, idims): Nd = S1.ndim S1 = cp.transpose(S1, [idim] + list(range(0, idim)) + list(range(idim + 1, Nd))) dsnew = S1.shape S1 = cp.reshape(S1, (S1.shape[0], -1), order='F') dsnew2 = S1.shape S1 = cp.concatenate((cp.full( (sig, dsnew2[1]), 0), S1, cp.full((sig, dsnew2[1]), 0)), axis=0) Smax = S1[:dsnew2[0], :] for j in range(1, 2 * sig + 1): Smax = Smax + S1[j:j + dsnew2[0], :] S1 = cp.reshape(Smax, dsnew, order='F') S1 = cp.transpose( S1, list(range(1, idim + 1)) + [0] + list(range(idim + 1, Nd))) return S1
def calc_inv(ir): ir = np.transpose(ir) q = np.linalg.inv(np.matmul(np.transpose(ir), ir)) hh = np.matmul(ir, np.transpose(q)) print(hh) print(hh.shape) return hh.tolist()
def backward_cuda(self, grad): # the same routine assert self.is_compiled batch_sz = grad.shape[0] flat_grad_cuda = grad.reshape((-1, self.output_ch)) d_W_cuda = cp.matmul(cp.transpose(self.last_input_col_cuda), flat_grad_cuda) self.grads_cuda["w"] = d_W_cuda.reshape(self.kernel_shape) self.grads_cuda["b"] = cp.sum(flat_grad_cuda, axis=0) d_X_cuda = cp.matmul(grad, cp.transpose(self.W_cuda)) d_in_cuda = cp.zeros(shape=(batch_sz, *self.input_shape), dtype=np.float32) # if this part could be re-written, it would be better for i, r in enumerate(range(0, self.output_sz)): for j, c in enumerate(range(0, self.output_sz)): # patch = d_X_cuda[:, i, j, :] # patch = patch.reshape((batch_sz, self.kernel_sz, self.kernel_sz, self.input_ch)) # d_in_cuda[:, r:r + self.kernel_sz, c:c + self.kernel_sz, :] += patch d_in_cuda[:, r:r + self.kernel_sz, c:c + self.kernel_sz, :] += d_X_cuda[:, i, j, :].reshape( (batch_sz, self.kernel_sz, self.kernel_sz, self.input_ch)) return d_in_cuda
def backward_pass(x, y, output, hidden_output, W_output): output_error = -(y - output) # Calculate error output_over_net = output*(1 - output) # Derivative of sigmoid function sigmoid_on_error = cp.multiply(output_error, output_over_net) # Calculate the sigmoid function's affect on error W_output = cp.transpose(W_output) hidden_error = cp.dot(sigmoid_on_error, W_output) # Calculate the affect of output weights on hidden weights' error hidden_over_net = hidden_output*(1 - hidden_output) # Derivative of sigmoid function sigmoid_on_hidden_error = cp.multiply(hidden_error, hidden_over_net) # Calculate the sigmoid function's affect on error # Correctly arrange matrices for calculations x = cp.atleast_2d(x) hidden_output = cp.atleast_2d(hidden_output) x_transpose = cp.transpose(x) hidden_output_transpose = cp.transpose(hidden_output) sigmoid_on_hidden_error = sigmoid_on_hidden_error.reshape(1, sigmoid_on_hidden_error.size) sigmoid_on_error = sigmoid_on_error.reshape(1, sigmoid_on_error.size) # Calculate weight changes W_hidden_c = cp.dot(x_transpose, sigmoid_on_hidden_error) W_output_c = cp.dot(hidden_output_transpose, sigmoid_on_error) # Calculate bias changes B_hidden_c = sigmoid_on_hidden_error B_output_c = sigmoid_on_error return W_output_c, W_hidden_c, B_hidden_c, B_output_c
def local_cov_bet_class_NN(self,key,label,nb_class,batchsize,k): key_broadcast=cp.broadcast_to(key,(batchsize,batchsize,key.shape[1])) key_broadcast_transpose=cp.transpose(cp.broadcast_to(key,(batchsize,batchsize,key.shape[1])),axes=(1,0,2)) sub_key_broadcast=key_broadcast-key_broadcast_transpose norm_sub_broadcast=cp.linalg.norm(sub_key_broadcast,axis=2) sorted_d=cp.sort(norm_sub_broadcast,axis=0) kth_d=sorted_d[k] kth_d=kth_d.reshape([batchsize,1]) sigma=cp.matmul(kth_d,cp.transpose(kth_d)) batchsize_per_class=batchsize//nb_class index=cp.arange(key.shape[0]) xx,yy=cp.meshgrid(index,index) sub=key[xx]-key[yy] norm_sub=cp.linalg.norm(sub,axis=2) a1=cp.exp(-norm_sub*norm_sub/sigma) lindex=cp.arange(label.shape[0]) lx,ly=cp.meshgrid(lindex,lindex) l=(label[lx]==label[ly]) a1=a1*l*(1.0/(batchsize*nb_class)-1.0/batchsize_per_class) l2=(label[lx]!=label[ly]) a2=l2*(1.0/batchsize) a=a1+a2 a=a.reshape([a.shape[0],a.shape[1],1]) a_sub=a*sub Sb=cp.einsum('ijk,ijl->kl',a_sub,sub,dtype='float32')*0.5 return Sb
def update_render(self) -> QPixmap: # TODO img = cp.zeros( (self.shader_resolution[1], self.shader_resolution[0], 3), cp.uint8) v_color = cp.array([255, 255, 255], cp.uint8) if self.render_method == self.PROJECTION_PERSPECTIVE: for model in self.models: v_extend = cp.concatenate( (model.v, cp.ones((model.v_size, 1), cp.float32)), axis=1) v_im = cp.transpose( cp.matmul(self.im, cp.matmul(self.em, cp.transpose(v_extend)))) v_im = v_im / v_im[:, 2:] v_im = v_im[:, :2] v_im = v_im.astype(cp.int32) for i in range(len(v_im)): if 0 <= v_im[i, 0] <= self.shader_resolution[0] and 0 <= v_im[ i, 1] <= self.shader_resolution[1]: img[v_im[i, 1], v_im[i, 0], :] = v_color img = cp.asnumpy(img) pixmap = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888) pixmap = QPixmap(pixmap) return pixmap
def my_conv2(x, sig, varargin=None, **kwargs): # TODO: Fix so output matches my_conv2_cpu # x is the matrix to be filtered along a choice of axes # sig is either a scalar or a sequence of scalars, one for each axis to be filtered # varargin can be the dimensions to do filtering, if len(sig) != x.shape # if sig is scalar and no axes are provided, the default axis is 2 if sig <= .25: return x idims = 1 if varargin is not None: idims = varargin idims = _make_vect(idims) if _is_vect(idims) and _is_vect(sig): sigall = sig else: sigall = np.tile(sig, len(idims)) for sig, idim in zip(sigall, idims): Nd = x.ndim x = cp.transpose(x, [idim] + list(range(0, idim)) + list(range(idim + 1, Nd))) dsnew = x.shape x = cp.reshape(x, (x.shape[0], -1), order='F') tmax = ceil(4 * sig) dt = cp.arange(-tmax, tmax + 1) gaus = cp.exp(-dt**2 / (2 * sig**2)) gaus = gaus / cp.sum(gaus) y = convolve_gpu(x, gaus, **kwargs) y = y.reshape(dsnew, order='F') y = cp.transpose( y, list(range(1, idim + 1)) + [0] + list(range(idim + 1, Nd))) return y
def power_iteration(self, A, sample_vect_shape, num_iters): bk = np.random.randn(sample_vect_shape[0], sample_vect_shape[1]) for i in range(0, num_iters): bk1 = A(bk) bk1_norm = np.linalg.norm(bk1) bk = bk1 / bk1_norm Mx = A(bk) xx = np.transpose(np.dot(bk.ravel(), bk.ravel())) eig_b = np.transpose(bk.ravel()).dot(Mx.ravel()) / xx return eig_b
def chw_cupy_random_rotate(chw_imageset, return_param=False): if len(chw_imageset.shape) == 3: chw_imageset = cupy.expand_dims(chw_imageset, axis=0) k = cupy.random.randint(4) pil_imageset = cupy.transpose(chw_imageset, axes=(0, 2, 3, 1)) pil_rotated_imageset = cupy.rot90( pil_imageset, k, axes=(1, 2)) #pil_imgはn,x,y,colorになっている。axis=(1,2)で指定 chw_rotated_imageset = cupy.transpose(pil_rotated_imageset, axes=(0, 3, 1, 2)) if return_param: return chw_rotated_imageset, {'k': k} else: return chw_rotated_imageset
def my_conv2(S1, sig, varargin=None): # S1 is the matrix to be filtered along a choice of axes # sig is either a scalar or a sequence of scalars, one for each axis to be filtered # varargin can be the dimensions to do filtering, if len(sig) != x.shape # if sig is scalar and no axes are provided, the default axis is 2 if sig <= .25: return S1 idims = 1 if varargin is not None: idims = varargin idims = _make_vect(idims) if _is_vect(idims) and _is_vect(sig): sigall = sig else: sigall = np.tile(sig, len(idims)) for sig, idim in zip(sigall, idims): Nd = S1.ndim S1 = cp.transpose(S1, [idim] + list(range(0, idim)) + list(range(idim + 1, Nd))) dsnew = S1.shape S1 = cp.reshape(S1, (S1.shape[0], -1), order='F') dsnew2 = S1.shape tmax = ceil(4 * sig) dt = np.arange(-tmax, tmax + 1) gaus = np.exp(-dt**2 / (2 * sig**2)) gaus = gaus[:, np.newaxis] / np.sum(gaus) cNorm = lfilter(gaus, 1, cp.concatenate( (cp.ones(dsnew2[0]), cp.zeros(tmax)))[:, np.newaxis], axis=0) cNorm = cNorm[tmax:, :] S1 = lfilter(gaus, 1, cp.asfortranarray( cp.concatenate( (S1, cp.zeros((tmax, dsnew2[1]), order='F')), axis=0)), axis=0) S1 = S1[tmax:, :] S1 = S1.reshape(dsnew, order='F') S1 = S1 / cNorm S1 = cp.transpose( S1, list(range(1, idim + 1)) + [0] + list(range(idim + 1, Nd))) return S1
def col2im(col, pad, stride, block, x_shape, channels): """ Function to convert a column matrix back to the multi-channel image Currently not used """ im = cp.zeros(x_shape) for k, (i, j) in enumerate(product(range(block), repeat=2)): im[j::stride, i::stride, :] = cp.transpose(col[k, :].reshape( block, block, channels), axes=(1, 0, 2)) if pad: return cp.transpose(im, axes=(1, 0, 2))[:-1, :-1, :] else: return cp.transpose(im, axes=(1, 0, 2))
def my_conv2(S1, sig, varargin=None): # S1 is the matrix to be filtered along a choice of axes # sig is either a scalar or a sequence of scalars, one for each axis to be filtered # varargin can be the dimensions to do filtering, if len(sig) != x.shape # if sig is scalar and no axes are provided, the default axis is 2 if sig <= .25: return S1 idims = 1 if varargin is not None: idims = varargin idims = _make_vect(idims) if _is_vect(idims) and _is_vect(sig): sigall = sig else: sigall = np.tile(sig, len(idims)) for sig, idim in zip(sigall, idims): Nd = S1.ndim S1 = cp.transpose(S1, [idim] + list(range(0, idim)) + list(range(idim + 1, Nd))) dsnew = S1.shape S1 = cp.reshape(S1, (S1.shape[0], -1), order='F') dsnew2 = S1.shape tmax = ceil(4 * sig) dt = cp.arange(-tmax, tmax + 1) gaus = cp.exp(-dt**2 / (2 * sig**2)) gaus = gaus[:, cp.newaxis] / cp.sum(gaus) # This GPU FFT-based convolution leads to a splitting step 3.5x faster than the # custom GPU lfilter implementation below. cNorm = convolve(cp.ones((dsnew2[0], 1)), gaus).ravel()[:, cp.newaxis] S1 = convolve(S1, gaus) # Slow Custom GPU lfilter implementation: # cNorm = _apply_lfilter( # _gaus_lfilter(sig), # cp.concatenate((cp.ones(dsnew2[0]), cp.zeros(tmax)))[:, np.newaxis]) # cNorm = cNorm[tmax:, :] # S1 = _apply_lfilter(_gaus_lfilter(sig), cp.asfortranarray(cp.concatenate( # (S1, cp.zeros((tmax, dsnew2[1]), order='F')), axis=0))) # S1 = S1[tmax:, :] S1 = S1.reshape(dsnew, order='F') S1 = S1 / cNorm S1 = cp.transpose( S1, list(range(1, idim + 1)) + [0] + list(range(idim + 1, Nd))) return S1
def regularize_conv(kernel, input_shape, clip_to, devices=-1): kernel = cp.transpose(kernel, (3, 2, 1, 0)) # tensor flow format kernel = cp.rot90(kernel, k=2, axes=(0, 1)) # normalizing the back prop kernel = cp.transpose(kernel, (0, 1, 3, 2)) kernel = cp_regularize_conv(kernel, input_shape, clip_to, devices) kernel = cp.transpose(kernel, (0, 1, 3, 2)) kernel = cp.rot90(kernel, k=2, axes=(0, 1)) kernel = cp.transpose(kernel, (3, 2, 1, 0)) # back to chainer format return kernel
def diagonal(a, offset=0, axis1=0, axis2=1): """Returns specified diagonals. This function extracts the diagonals along two specified axes. The other axes are not changed. This function returns a writable view of this array as NumPy 1.10 will do. Args: a (cupy.ndarray): Array from which the diagonals are taken. offset (int): Index of the diagonals. Zero indicates the main diagonals, a positive value upper diagonals, and a negative value lower diagonals. axis1 (int): The first axis to take diagonals from. axis2 (int): The second axis to take diagonals from. Returns: cupy.ndarray: A view of the diagonals of ``a``. .. seealso:: :func:`numpy.diagonal` """ if axis1 < axis2: min_axis, max_axis = axis1, axis2 else: min_axis, max_axis = axis2, axis1 tr = list(six.moves.range(a.ndim)) del tr[max_axis] del tr[min_axis] if offset >= 0: a = cupy.transpose(a, tr + [axis1, axis2]) else: a = cupy.transpose(a, tr + [axis2, axis1]) offset = -offset diag_size = max(0, min(a.shape[-2], a.shape[-1] - offset)) ret_shape = a.shape[:-2] + (diag_size,) if diag_size == 0: return cupy.empty(ret_shape, dtype=a.dtype) a = a[..., :diag_size, offset:offset + diag_size] ret = a.view() ret._shape = a.shape[:-2] + (diag_size,) ret._strides = a.strides[:-2] + (a.strides[-1] + a.strides[-2],) ret._size = internal.prod(ret._shape) ret._c_contiguous = -1 ret._f_contiguous = -1 return ret
def fn(): with cupy.cuda.Device(node.device_id): if transX == 1: #xt = chainer.functions.transpose(x) xt = cupy.transpose(x) else: xt = x if transW == 1: #wt = chainer.functions.transpose(w) wt = cupy.transpose(w) else: wt = w z = cupy.dot(alpha * xt, wt) cupy.add(z, beta * b, out=y)
def my_conv2_cpu(x, sig, varargin=None, **kwargs): # (Alternative conv2 function for testing) # x is the matrix to be filtered along a choice of axes # sig is either a scalar or a sequence of scalars, one for each axis to be filtered # varargin can be the dimensions to do filtering, if len(sig) != x.shape # if sig is scalar and no axes are provided, the default axis is 2 if sig <= .25: return x idims = 1 if varargin is not None: idims = varargin idims = _make_vect(idims) if _is_vect(idims) and _is_vect(sig): sigall = sig else: sigall = np.tile(sig, len(idims)) for sig, idim in zip(sigall, idims): Nd = x.ndim x = cp.transpose(x, [idim] + list(range(0, idim)) + list(range(idim + 1, Nd))) dsnew = x.shape x = cp.reshape(x, (x.shape[0], -1), order='F') tmax = ceil(4 * sig) dt = np.arange(-tmax, tmax + 1) gaus = np.exp(-dt**2 / (2 * sig**2)) gaus = gaus / np.sum(gaus) cNorm = lfilter(gaus, np.array([1.]), np.concatenate((np.ones(dsnew[0]), np.zeros(tmax)))) cNorm = cNorm[tmax:] x_n = cp.asnumpy(x) x_n = lfilter(gaus, np.array([1.]), np.concatenate((x_n, np.zeros((tmax, dsnew[1])))), axis=0) x_n = x_n[tmax:] x_n = np.reshape(x_n, dsnew) x_n = x_n / cNorm.reshape(-1, 1) x = cp.array(x_n) x = cp.transpose( x, list(range(1, idim + 1)) + [0] + list(range(idim + 1, Nd))) return x
def MWU_game_algorithm_experiment(payoff_mat, phi=1/2, steps_number=10000): payoff_mat = np.array(payoff_mat) rows_number = payoff_mat.shape[0] cols_number = payoff_mat.shape[1] p_0 = np.ones((1, rows_number)) p_0 = p_0/rows_number p_t = p_0 j_sumed = np.zeros((cols_number, 1)) smallest_column_payoff = 1 p_best = p_0 p_t_sum = np.zeros((1, rows_number)) start = time.time() row_row = [] col_col = [] row_col = [] times = [] curr_index = 125 for i in range (1, steps_number + 1): payoffs = np.matmul(p_t, payoff_mat) j_best_response = np.argmax(payoffs) if(payoffs[0, j_best_response] < smallest_column_payoff): smallest_column_payoff = payoffs[0, j_best_response] p_best = p_t j_sumed[j_best_response] += 1 m_t = payoff_mat[:,j_best_response] m_t_negative = (m_t < 0) p_t_significant = (p_t > SIGNIFICANCE_CONST) to_update = np.logical_or(m_t_negative, p_t_significant[0]) m_t_updating = np.where(to_update,m_t,0) p_t_updating = np.where(to_update,p_t,0) p_t = np.multiply((1 - phi * m_t_updating), p_t_updating) p_t = p_t/p_t.sum() p_t_sum = p_t_sum + p_t if(i == curr_index): j_distribution = j_sumed / j_sumed.sum() print(i) now = time.time() times.append(now - start) row_row.append(max(epsilon_value(p_best, np.transpose(p_best), payoff_mat))) col_col.append(max(epsilon_value(np.transpose(j_distribution), j_distribution, payoff_mat))) row_col.append(max(epsilon_value(p_best, j_distribution, payoff_mat))) start -= (time.time() - now) curr_index *= 2 # game_value = np.matmul(np.matmul(p_best, payoff_mat), j_distribution)[0][0] # print() return times, row_row, col_col, row_col
def _expand_dims_transpose(arr, mode, mode_out): """Return a reshaped and transposed array. The input array ``arr`` having ``mode`` as its modes is reshaped and transposed so that modes of the output becomes ``mode_out``. Example >>> import cupy >>> a = cupy.zeros((10, 20)) >>> mode_a = ('A', 'B') >>> mode_out = ('B', 'C', 'A') >>> out = cupy.linalg.einsum._expand_dims_transpose(a, mode_a, ... mode_out) >>> out.shape (20, 1, 10) Args: arr (cupy.ndarray): mode (tuple or list): The modes of input array. mode_out (tuple or list): The modes of output array. Returns: cupy.ndarray: The reshaped and transposed array. """ mode = list(mode) shape = list(arr.shape) axes = [] for i in mode_out: if i not in mode: mode.append(i) shape.append(1) axes.append(mode.index(i)) return cupy.transpose(arr.reshape(shape), axes)
def _gpu_calc_skewness(psd, power, vel_bins, velocity, spec_width, dV, block_size=100): shp = psd.shape times = shp[0] skewness = np.zeros((shp[0], shp[1])) for k in range(0, times, block_size): the_max = min([k + block_size, times]) gpu_array = cp.array(psd[k:the_max, :, :], dtype=float) power_array = cp.array(power[k:the_max, :], dtype=float) spec_width_array = cp.array(spec_width[k:the_max, :], dtype=float) power_array *= spec_width_array**3 del spec_width_array velocity_array = cp.array(velocity[k:the_max, :]) velocity_array = cp.transpose(cp.tile(velocity_array, (shp[2], 1, 1)), [1, 2, 0]) vel_bins_tiled = cp.tile(vel_bins, (the_max - k, shp[1], 1)) gpu_array = 10**(gpu_array / 10. * dV) gpu_array = 1 / power_array * cp.sum( (vel_bins_tiled - velocity_array)**3 * gpu_array, axis=2) skewness[k:the_max, :] = gpu_array.get() return skewness
def encode(self, x): # print(x.shape) # print(x.shape) a, b, c = x.shape x = x.reshape(a, 1, c).astype(xp.float32) # x = xp.hstack([x[:,:,i:b-440+i:221] for i in range(441)]) * hamming x = xp.concatenate([ x[:, :, :-221].reshape(a, -1, 1, 442), x[:, :, 221:].reshape( a, -1, 1, 442) ], axis=2).reshape(a, -1, 442) * self.mado # print(x) x = xp.fft.fft(x, axis=-1) # xp.fft.fft(xp.arange(100).reshape(2,5,10),axis=-1) x = xp.concatenate( [x.real.reshape(a, 1, -1, 442), x.imag.reshape(a, 1, -1, 442)], axis=1) #.reshape(a, 2, -1, 442) # xp.concatenate([s.real.reshape(2,5,1,10),s.imag.reshape(2,5,1,10)],axis=2) # print(x.shape) x = xp.transpose(x, axes=(0, 1, 3, 2)) # print(x.dtype) return x
def make_prediction(img, weights): win_size = 64 win_size_tuple = (win_size, win_size) cell_size = 8 cell_size_tuple = (cell_size, cell_size) block_size = (cell_size * 2, cell_size * 2) block_stride = (cell_size, cell_size) nbins = 9 feature_size = int(9 * (4 + ((((win_size / cell_size) - 2) * 4) * 2) + ((((win_size / cell_size) - 2) * ((win_size / cell_size) - 2)) * 4))) img = cv2.resize(img, win_size_tuple) hog = cv2.HOGDescriptor(win_size_tuple, block_size, block_stride, cell_size_tuple, nbins) out = hog.compute(img) out = cp.transpose(out) out = cp.array(out) net_hidden = cp.dot(out, weights[1]) + weights[3] out_hidden = sigmoid(net_hidden) net_output = cp.dot(out_hidden, weights[0]) + weights[2] out_output = sigmoid(net_output) prediction = cp.argmax(out_output) return prediction
def mvdr(x, sv): """ Minimum variance distortionless response (MVDR) beamformer weights Parameters ---------- x : ndarray Received signal, assume 2D array with size [num_sensors, num_samples] sv: ndarray Steering vector, assume 1D array with size [num_sensors, 1] Note: Unlike MATLAB where input matrix x is of size MxN where N represents the number of array elements, we assume row-major formatted data where each row is assumed to be complex-valued data from a given sensor (i.e. NxM) """ if x.shape[0] > x.shape[1]: raise ValueError('Matrix has more sensors than samples. Consider \ transposing and remember cuSignal is row-major, unlike MATLAB') if x.shape[0] != sv.shape[0]: raise ValueError('Steering Vector and input data do not align') R = cp.cov(x) R_inv = cp.linalg.inv(R) svh = cp.transpose(cp.conj(sv)) wB = cp.matmul(R_inv, sv) # wA is a 1x1 scalar wA = cp.matmul(svh, wB) w = wB / wA return w
def GlobalReg(X, T, sigma2, outliers): """ :params: :return """ [N, D] = X.shape M = T.shape[0] # Calculate P matrix # Nominator of P P_num = cp.sum((X[None, :, :] - T[:, None, :])**2, axis=2) P_num = cp.exp(-P_num / (2 * sigma2)) # Denominator of P P_den = cp.sum(P_num, axis=0) P_den = cp.tile(P_den, (M, 1)) P_den[P_den == 0] = 2.220446049250313e-16 c = ((((2 * cp.pi * sigma2)**D / 2) * (outliers / (1 - outliers))) * (M / N)) P_den += c P = cp.divide(P_num, P_den) P1 = cp.sum(P, axis=1) Pt1 = cp.sum(P, axis=0) c1 = c * cp.ones(N) K1 = cp.dot(cp.transpose(P_num), cp.ones(M)) a = cp.tile(cp.divide(1, K1 + c1).reshape(N, 1), D) Px = cp.dot(P_num, (cp.multiply(a, X))) return P1, Pt1, Px
def tensordot_adjoint_0(B, G, axes, A_ndim, B_ndim): # The adjoint of the operator # A |--> np.tensordot(A, B, axes) if B_ndim == 0: return G * B G_axes = ocp.arange(ocp.ndim(G)) if type(axes) is int: axes = max(axes, 0) B_axes = ocp.arange(B_ndim) return ocp.tensordot(G, B, [G_axes[A_ndim - axes:], B_axes[axes:]]) elif type(axes[0]) is int: axes = [axes[0] % A_ndim, axes[1] % B_ndim] B_axes = ocp.arange(B_ndim) return ocp.tensordot( G, B, [G_axes[A_ndim - 1:], ocp.delete(B_axes, axes[1])]) # noqa: E501 else: A_axes = ocp.arange(A_ndim) B_axes = ocp.arange(B_ndim) summed_axes = [ ocp.asarray(axes[0]) % A_ndim, ocp.asarray(axes[1]) % B_ndim, ] # noqa: E501 other_axes = [ ocp.delete(A_axes, summed_axes[0]), ocp.delete(B_axes, summed_axes[1]), # noqa: E501 ] out = ocp.tensordot(G, B, [G_axes[len(other_axes[0]):], other_axes[1]]) perm = ocp.argsort( ocp.concatenate( (other_axes[0], summed_axes[0][ocp.argsort(summed_axes[1])]))) return ocp.transpose(out, perm)
def _symmetric_compute_eigenvalues(S_elems): """Compute eigenvalues from the upperdiagonal entries of a symmetric matrix Parameters ---------- S_elems : list of ndarray The upper-diagonal elements of the matrix, as returned by `hessian_matrix` or `structure_tensor`. Returns ------- eigs : ndarray The eigenvalues of the matrix, in decreasing order. The eigenvalues are the leading dimension. That is, ``eigs[i, j, k]`` contains the ith-largest eigenvalue at position (j, k). """ if len(S_elems) == 3: # Use fast Cython code for 2D eigs = cp.stack(_image_orthogonal_matrix22_eigvals(*S_elems)) else: matrices = _symmetric_image(S_elems) # eigvalsh returns eigenvalues in increasing order. We want decreasing eigs = cp.linalg.eigvalsh(matrices)[..., ::-1] leading_axes = tuple(range(eigs.ndim - 1)) eigs = cp.transpose(eigs, (eigs.ndim - 1, ) + leading_axes) return eigs
def nvst_srstdcal_temp(self): sitfline = fits.open(self.sitfpath)[0].data sitfsize = sitfline.shape R0_num = sitfsize[1] cp.cuda.Device(0).use setf = cp.ndarray([self.srsize, self.srsize]) sitf = cp.ndarray([self.srsize, self.srsize]) sitfcube = cp.ndarray([self.srsize, self.srsize, R0_num]) self.sr_standard = cp.ndarray([self.srsize, self.srsize, R0_num]) multipre = sitfsize[0] * self.maxfre * 2.0 / self.srsize index_matrix = cp.ndarray([self.srsize, self.srsize]) x = cp.transpose(cp.mgrid[0:self.srsize, 0:self.srsize][0]) - self.srsize / 2 y = cp.mgrid[0:self.srsize, 0:self.srsize][0] - self.srsize / 2 indexmatrix = cp.round_(cp.sqrt(x**2 + y**2) * multipre) for index in range(R0_num): self.R0 = self.start_r0 + self.step_r0 * index sitfcube[:, :, index] = sitfline[index, indexmatrix].reshape( self.srsize, self.srsize) sitf = sitfcube[:, :, index] sitf = sitf / sitf[self.srsize / 2, self.srsize / 2] setf = nvst_tel_sotf() sr = setf**2 / (sitf + 0.0000001 * cp.max(sitf)) sr = sr / sr[self.srsize / 2, self.srsize / 2] self.sr_standard[:, :, index] = sr print('标准谱比计算完成')
def backprop(self, gradient): if self.activation_fun == "relu": #leaky relu dx = np.ones_like(self.output) dx[self.output < 0] = 0.01 dout = gradient * dx elif self.activation_fun == "linear": dout = gradient dout = np.reshape(dout, (dout.shape[0], 1)) self.data_in = np.reshape(self.data_in, (self.data_in.shape[0], 1)) self.dw += cp.dot(dout, cp.transpose( self.data_in)) + self.lamb * self.weights self.db += cp.sum(dout, axis=1, keepdims=True) self.Ddata_in = cp.dot(cp.transpose(self.weights), dout) self.Ddata_in = np.reshape(self.Ddata_in, self.Ddata_in.shape[0])
def _get_trans_args(args, trans, shape, params=None): if trans == tuple(six_range(len(shape))): return args, shape if params is not None and any(p.raw for p in params): raise NotImplementedError('Illegal conditions') args = [cupy.transpose(a, trans) if isinstance(a, cupy.ndarray) else a for a in args] shape = tuple([shape[i] for i in trans]) return args, shape
def rot90(a, k=1, axes=(0, 1)): """Rotate an array by 90 degrees in the plane specified by axes. Note that ``axes`` argument has been introduced since NumPy v1.12. The contents of this document is the same as the original one. Args: a (~cupy.ndarray): Array of two or more dimensions. k (int): Number of times the array is rotated by 90 degrees. axes: (tuple of ints): The array is rotated in the plane defined by the axes. Axes must be different. Returns: ~cupy.ndarray: Output array. .. seealso:: :func:`numpy.rot90` """ a_ndim = a.ndim if a_ndim < 2: raise ValueError('Input must be >= 2-d') axes = tuple(axes) if len(axes) != 2: raise ValueError('len(axes) must be 2') if axes[0] == axes[1] or abs(axes[0] - axes[1]) == a_ndim: raise ValueError('axes must be different') if not (-a_ndim <= axes[0] < a_ndim and -a_ndim <= axes[1] < a_ndim): raise ValueError('axes must be >= %d and < %d' % (-a_ndim, a_ndim)) k = k % 4 if k == 0: return a[:] if k == 2: return _flip(_flip(a, axes[0]), axes[1]) axes_t = list(range(0, a_ndim)) axes_t[axes[0]], axes_t[axes[1]] = axes_t[axes[1]], axes_t[axes[0]] if k == 1: return cupy.transpose(_flip(a, axes[1]), axes_t) else: return _flip(cupy.transpose(a, axes_t), axes[1])
def _move_axes_to_head(a, axes): # This function moves the axes of ``s`` to the head of the shape. for idx, axis in enumerate(axes): if idx != axis: break else: return a return cupy.transpose( a, axes + [i for i in six.moves.range(a.ndim) if i not in axes])