def cmplx_sort(p): """Sort roots based on magnitude. Parameters ---------- p : array_like The roots to sort, as a 1-D array. Returns ------- p_sorted : ndarray Sorted roots. indx : ndarray Array of indices needed to sort the input `p`. Examples -------- >>> import cusignal >>> vals = [1, 4, 1+1.j, 3] >>> p_sorted, indx = cusignal.cmplx_sort(vals) >>> p_sorted array([1.+0.j, 1.+1.j, 3.+0.j, 4.+0.j]) >>> indx array([0, 2, 3, 1]) """ p = cp.asarray(p) if cp.iscomplexobj(p): indx = cp.argsort(abs(p)) else: indx = cp.argsort(p) return cp.take(p, indx, 0), indx
def getShapeMatrix(ro=1.3, N=1000, gpu=False): if gpu: import cupy as np else: import numpy as np def f(ro, N): ''' give the surface map matrix ''' # polar coordinate theta = np.linspace(0, 2 * np.pi, N) x = ro * np.cos(theta) y = ro * np.sin(theta) u, v = np.meshgrid(x, y) RO = u**2 + v**2 # map z to u,v Z = 1 - u / RO - RO return x, y, Z x, y, Z = f(ro, N) # remap z to the grid index index_x, index_y = np.argsort(x), np.argsort(y) Y, X = np.meshgrid( index_x, index_y) # meshgrid always will use numpy array NOT mxnet NDArray Z = Z[X, Y] return Z
def test_template(): size = 100 # Float prefactors ensure that image range is between 0 and 1 image = np.full((400, 400), 0.5) target = 0.1 * (np.tri(size) + np.tri(size)[::-1]) target_positions = [(50, 50), (200, 200)] for x, y in target_positions: image[x:x + size, y:y + size] = target np.random.seed(1) image += 0.1 * np.random.uniform(size=(400, 400)) image = cp.asarray(image) target = cp.asarray(target) result = match_template(image, target) delta = 5 positions = peak_local_max(result, min_distance=delta) if len(positions) > 2: # Keep the two maximum peaks. intensities = result[tuple(positions.T)] i_maxsort = cp.argsort(intensities)[::-1] positions = positions[i_maxsort][:2] # Sort so that order matches `target_positions`. positions = positions[cp.argsort(positions[:, 0])] for xy_target, xy in zip(target_positions, positions): assert_array_almost_equal(xy, xy_target)
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 test_indices_with_labels(self): image = cp.asarray(np.random.uniform(size=(40, 60))) i, j = cp.mgrid[0:40, 0:60] labels = 1 + (i >= 20) + (j >= 30) * 2 i, j = cp.mgrid[-3:4, -3:4] footprint = i * i + j * j <= 9 expected = cp.zeros(image.shape, float) for imin, imax in ((0, 20), (20, 40)): for jmin, jmax in ((0, 30), (30, 60)): expected[imin:imax, jmin:jmax] = ndi.maximum_filter(image[imin:imax, jmin:jmax], footprint=footprint) expected = cp.column_stack(cp.nonzero(expected == image)) expected = expected[cp.argsort(image[tuple(expected.T)])[::-1]] result = peak.peak_local_max( image, labels=labels, min_distance=1, threshold_rel=0, footprint=footprint, indices=True, exclude_border=False, ) result = result[cp.argsort(image[tuple(result.T)])[::-1]] assert (result == expected).all()
def calculate_survival_chance(self): # 首先获取适应度 scores = self.scores # 升序排列,数字越高,对应的适应度越高 order = cp.argsort(cp.argsort(scores)) # probs[i] 是第 i 个个体被选入下一代的概率 self.probs = cp.divide(order, cp.sum(order))
def svdecon(X, nPC0=None): """ Input: X : m x n matrix Output: X = U*S*V' Description: Does equivalent to svd(X,'econ') but faster Vipin Vijayan (2014) """ m, n = X.shape nPC = nPC0 or min(m, n) if m <= n: C = cp.dot(X, X.T) D, U = cp.linalg.eigh(C, 'U') ix = cp.argsort(np.abs(D))[::-1] d = D[ix] U = U[:, ix] d = d[:nPC] U = U[:, :nPC] V = cp.dot(X.T, U) s = cp.sqrt(d) V = V / s.T S = cp.diag(s) else: C = cp.dot(X.T, X) D, V = cp.linalg.eigh(C) ix = cp.argsort(cp.abs(D))[::-1] d = D[ix] V = V[:, ix] # convert evecs from X'*X to X*X'. the evals are the same. U = cp.dot(X, V) s = cp.sqrt(d) U = U / s.T S = cp.diag(s) return U, S, V
def loss_orth(self): penalty = 0 W = self.U.T Wt = W.T WWt = F.matmul(W, Wt) I = cupy.identity(WWt.shape[0]) penalty = penalty + F.sum((WWt - I)**2) W = self.V Wt = W.T WWt = F.matmul(W, Wt) I = cupy.identity(WWt.shape[0]) penalty = penalty + F.sum((WWt - I)**2) spectral_penalty = 0 if self.mode in (4, 5): if (self.D.size > 1): sd2 = 0.1**2 _d = self.D[cupy.argsort(self.D.data)] spectral_penalty += F.mean((1 - _d[:-1])**2 / sd2 - F.log( (_d[1:] - _d[:-1]) + 1e-7)) * 0.05 elif self.mode == 6: spectral_penalty += F.mean(self.D * F.log(self.D)) elif self.mode == 7: spectral_penalty += F.mean(F.exp(self.D)) elif self.mode == 8: spectral_penalty += -F.mean(F.log(self.D)) return penalty + spectral_penalty * 0.1
def Fisher(self, key, label, batchsize, nb_class, convert_dim, dimension, affinity): label = cp.array(label) if (self.n_shot == 1): Sw = cp.identity(dimension, dtype='float32') else: Sw = self.local_cov_in_class(key.data, label, nb_class, batchsize, affinity) #Sw=self.local_cov_in_class_NN(key.data,label,nb_class,batchsize,5) Sb = self.local_cov_bet_class(key.data, label, nb_class, batchsize, affinity) #Sb=self.local_cov_bet_class_NN(key.data,label,nb_class,batchsize,5) Sb_Sw = Sb - 0.5 * Sw if (self.n_shot == 1): Sb_Sw = Sb lam, v = np.linalg.eigh(Sb_Sw) lam = cp.asarray(lam) v = cp.asarray(v) eigen_id = cp.argsort(lam)[::-1] eigen_value = lam[eigen_id] eigen_vector = v[:, eigen_id] W = eigen_vector[:, :convert_dim] W = cp.reshape(W, [dimension, convert_dim]) W = W / cp.reshape(cp.linalg.norm(W, axis=0), [1, convert_dim]) W = F.transpose(W) return W
def _minor_index_fancy(self, idx): """Index along the minor axis where idx is an array of ints. """ idx_dtype = self.indices.dtype idx = cupy.asarray(idx, dtype=idx_dtype).ravel() M, N = self._swap(*self.shape) k = len(idx) new_shape = self._swap(M, k) if k == 0: return self.__class__(new_shape) # pass 1: count idx entries and compute new indptr col_order = cupy.argsort(idx).astype(idx_dtype, copy=False) index1_outs = _index._csr_column_index1(idx, self.indptr, self.indices) res_indptr, indices_mask, col_counts, sort_idxs = index1_outs # pass 2: copy indices/data for selected idxs res_indices, res_data = _index._csr_column_index2( col_order, col_counts, sort_idxs, self.indptr, indices_mask, self.data, res_indptr) return self.__class__((res_data, res_indices, res_indptr), shape=new_shape, copy=False)
def test_normalization(): """Test that `match_template` gives the correct normalization. Normalization gives 1 for a perfect match and -1 for an inverted-match. This test adds positive and negative squares to a zero-array and matches the array with a positive template. """ n = 5 N = 20 ipos, jpos = (2, 3) ineg, jneg = (12, 11) image = cp.full((N, N), 0.5) image[ipos:ipos + n, jpos:jpos + n] = 1 image[ineg:ineg + n, jneg:jneg + n] = 0 # white square with a black border template = cp.zeros((n + 2, n + 2)) template[1:1 + n, 1:1 + n] = 1 result = match_template(image, template) # get the max and min results. sorted_result = cp.argsort(result.ravel()) iflat_min = cp.asnumpy(sorted_result[0]) iflat_max = cp.asnumpy(sorted_result[-1]) min_result = np.unravel_index(iflat_min, result.shape) max_result = np.unravel_index(iflat_max, result.shape) # shift result by 1 because of template border assert np.all((np.array(min_result) + 1) == (ineg, jneg)) assert np.all((np.array(max_result) + 1) == (ipos, jpos)) assert cp.allclose(result.ravel()[iflat_min], -1) assert cp.allclose(result.ravel()[iflat_max], 1)
def test_pad_input(): """Test `match_template` when `pad_input=True`. This test places two full templates (one with values lower than the image mean, the other higher) and two half templates, which are on the edges of the image. The two full templates should score the top (positive and negative) matches and the centers of the half templates should score 2nd. """ # Float prefactors ensure that image range is between 0 and 1 template = 0.5 * diamond(2) image = 0.5 * cp.ones((9, 19)) mid = slice(2, 7) image[mid, :3] -= template[:, -3:] # half min template centered at 0 image[mid, 4:9] += template # full max template centered at 6 image[mid, -9:-4] -= template # full min template centered at 12 image[mid, -3:] += template[:, :3] # half max template centered at 18 result = match_template(image, template, pad_input=True, constant_values=float(image.mean())) # get the max and min results. sorted_result = cp.argsort(result.ravel()) i, j = cp.unravel_index(sorted_result[:2], result.shape) assert_array_equal(j, (12, 0)) i, j = cp.unravel_index(sorted_result[-2:], result.shape) assert_array_equal(j, (18, 6))
def __init__(self, xx, yy, minimum=xp.nan, maximum=xp.nan, name=None, latex_label=None, unit=None, boundary=None): self.xx = xp.asarray(xx) self.min_limit = float(xp.amin(self.xx)) self.max_limit = float(xp.amax(self.xx)) # In order to use np/cp.interp, we need to make sure that xx is ordered sorted_idxs = xp.argsort(self.xx) self.xx = self.xx[sorted_idxs] self._yy = xp.asarray(yy)[sorted_idxs] if self._yy.ndim != 1: raise TypeError("yy must be 1D. A {}-D array given.".format( self.yy.dim)) self.YY = None self.probability_density = None self.cumulative_distribution = None self.inverse_cumulative_distribution = None self.__all_interpolated = Interp(self.xx, self._yy) minimum = float(xp.nanmax(xp.array([self.min_limit, minimum]))) maximum = float(xp.nanmin(xp.array([self.max_limit, maximum]))) bilby.core.prior.Prior.__init__(self, name=name, latex_label=latex_label, unit=unit, minimum=minimum, maximum=maximum, boundary=boundary) self._update_instance()
def eval_emo_lex(derived_emo_lex, emo_lex, trans, induct_emos_file, induct_emos_eval_file, emotion): print("Number of derived emotion ratings:", len(derived_emo_lex)) derived_emos = [] real_emos = [] words = [] trans = {word_src: tgt_words for word_src, tgt_words in trans} for word, emo in derived_emo_lex.items(): translations = ",".join([t[0] for t in trans[word]]) induct_emos_file.write(f"{word}\t{translations}\t{emotion}\t{emo}\n") real_emo = emo_lex.get(word, None) if real_emo: induct_emos_eval_file.write(f"{word}\t{translations}\t{emotion}\t{emo}\t{real_emo}\n") derived_emos.append(emo) real_emos.append(real_emo) words.append(word) print("Coverage in test set:", len(derived_emos) / len(derived_emo_lex)) derived_emos = np.array(derived_emos, dtype=float) real_emos = np.array(real_emos, dtype=float) corr_coeff = np.corrcoef(derived_emos, real_emos, rowvar=False) top_words = np.argsort(-derived_emos)[:10] print(derived_emos[top_words]) top_words = [words[int(idx)] for idx in top_words] print(top_words) corr_coeff = np.around(corr_coeff[0, 1], 3) print("Correlation:", corr_coeff) return [corr_coeff, len(derived_emo_lex), derived_emos.shape[0]]
def evaluate(y_hat, y, problem_type): fitness = DAGNN.eval_fitness(y_hat, y, problem_type) order = cp.argsort(fitness) best_loss = fitness[order[0]] best_preds = y_hat[order[0]] best_acc = DAGNN.eval_acc(best_preds, y, problem_type) return fitness, best_loss, best_acc
def compute_all_results(experiment): global seq_uid_map, frame_no_map, seq_uid_count_map cuda_embeddings = cp.asarray(experiment.embeddings) embeddings = experiment.embeddings chunk_size = 500 chunks = [] results = [] chunk_size = 100 for i in tqdm(range(0, int(len(embeddings) / chunk_size) + 1)): start = i * chunk_size end = i * chunk_size + chunk_size if end > len(embeddings): end = len(embeddings) #start = chunk_size #end = chunk_size+chunk_size chunk_similarity = cp.dot(cuda_embeddings[start:end], cuda_embeddings.T) chunk_similarity[np.arange(chunk_size), np.arange(chunk_size) + start] = 0 chunk_matches = cp.argsort(chunk_similarity, axis=1) chunk_matches = cp.flip(chunk_matches, axis=1) chunk_matches = chunk_matches.get() chunk_results = get_results_for_chunk(chunk_matches, start, end) results.append(chunk_results) compiled_results = {} for result in results: compiled_results.update(result) #compiled_results df = pd.DataFrame.from_dict(compiled_results).T df = df.rename(columns={0: "reid", 1: "jitter", 2: "AUC", 3: "seq_uid"}) return df
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 sortBatches2(ccb0): # takes as input a matrix of nBatches by nBatches containing # dissimilarities. # outputs a matrix of sorted batches, and the sorting order, such that # ccb1 = ccb0(isort, isort) # put this matrix on the GPU ccb0 = cp.asarray(ccb0, order='F') # compute its svd on the GPU (this might also be fast enough on CPU) u, s, v = svdecon(ccb0) # HACK: consistency with MATLAB u = u * cp.sign(u[0, 0]) v = v * cp.sign(u[0, 0]) # initialize the positions xs of the batch embeddings to be very small but proportional to # the first PC xs = .01 * u[:, 0] / cp.std(u[:, 0], ddof=1) # 200 iterations of gradient descent should be enough # TODO: move_to_config niB = 200 # this learning rate should usually work fine, since it scales with the average gradient # and ccb0 is z-scored # TODO: move_to_config eta = 1 for k in tqdm(range(niB), desc="Sorting %d batches" % ccb0.shape[0]): # euclidian distances between 1D embedding positions ds = (xs - xs[:, np.newaxis])**2 # the transformed distances go through this function W = cp.log(1 + ds) # the error is the difference between ccb0 and W err = ccb0 - W # ignore the mean value of ccb0 err = err - cp.mean(err, axis=0) # backpropagate the gradients err = err / (1 + ds) err2 = err * (xs[:, np.newaxis] - xs) D = cp.mean( err2, axis=1) # one half of the gradients is along this direction E = cp.mean(err2, axis=0) # the other half is along this direction # we don't need to worry about the gradients for the diagonal because those are 0 # final gradients for the embedding variable dx = -D + E.T # take a gradient step xs = xs - eta * dx # sort the embedding positions xs isort = cp.argsort(xs, axis=0) # sort the matrix of dissimilarities ccb1 = ccb0[isort, :][:, isort] return ccb1, isort
def sort_by_size(clusters, min_size): """ Relabel clustering in order of descending cluster size. New labels are consecutive integers beginning at 0 Clusters that are smaller than min_size are assigned to -1. Copied from https://github.com/jacoblevine/PhenoGraph. Parameters ---------- clusters: numpy array Either numpy or cupy array of cluster labels. min_size: int Minimum cluster size. Returns ------- relabeled: cupy array Array of cluster labels re-labeled by size. """ relabeled = cp.zeros(clusters.shape, dtype=cp.int) sizes = cp.array([cp.sum(clusters == x) for x in cp.unique(clusters)]) o = cp.argsort(sizes)[::-1] for i, c in enumerate(o): if sizes[c] > min_size: relabeled[clusters == c] = i else: relabeled[clusters == c] = -1 return relabeled
def getMeWtW(W, U0, Nnearest=None): # this function computes correlation between templates at ALL timelags from each other # takes the max over timelags to obtain a similarity score # also returns lists of most similar templates to each template # takes as input the low-rank factorization of templates (W for time and U0 # for space) # W is timesamples (default = 61 ), by number of templates, by rank (default = 3) nt0, Nfilt, Nrank = W.shape Params = [1, Nfilt, 0, 0, 0, 0, 0, 0, 0, nt0] # initialize correlation matrix for all timelags WtW = cp.zeros((Nfilt, Nfilt, 2 * nt0 - 1), dtype=np.float32, order='F') for i in range(Nrank): for j in range(Nrank): # the dot product factorizes into separable products for each spatio-temporal component utu0 = cp.dot(U0[:, :, i].T, U0[:, :, j]) # spatial products # temporal convolutions get multiplied wit hthe spatial products wtw0 = mexWtW2(Params, W[:, :, i], W[:, :, j], utu0) # add it to the full correlation array WtW = WtW + wtw0 # the maximum across timelags accounts for sample alignment mismatch cc = cp.max(WtW, axis=2) if Nnearest: isort = cp.argsort(cc, axis=0)[::-1] # if we don't have enough templates yet, just wrap the indices around the range 1:Nfilt iNear = cp.mod(cp.arange(Nnearest), Nfilt) iList = isort[iNear, :] # return the list of pairs for each template return WtW, iList else: return WtW
def getClosestChannels(probe, sigma, NchanClosest): # this function outputs the closest channels to each channel, # as well as a Gaussian-decaying mask as a function of pairwise distances # sigma is the standard deviation of this Gaussian-mask # compute distances between all pairs of channels xc = cp.asarray(probe.xc, dtype=np.float32, order='F') yc = cp.asarray(probe.yc, dtype=np.float32, order='F') C2C = (xc[:, np.newaxis] - xc)**2 + (yc[:, np.newaxis] - yc)**2 C2C = cp.sqrt(C2C) Nchan = C2C.shape[0] # sort distances isort = cp.argsort(C2C, axis=0) # take NchanCLosest neighbors for each primary channel iC = isort[:NchanClosest, :] # in some cases we want a mask that decays as a function of distance between pairs of channels # this is an awkward indexing to get the corresponding distances ix = iC + cp.arange(0, Nchan**2, Nchan) mask = cp.exp(-C2C.T.ravel()[ix]**2 / (2 * sigma**2)) # masks should be unit norm for each channel mask = mask / cp.sqrt(1e-3 + cp.sum(mask**2, axis=0)) return iC, mask, C2C
def _permutation(self, num): """Returns a permuted range.""" sample = cupy.empty((num), dtype=numpy.int32) curand.generate(self._generator, sample.data.ptr, num) if 128 < num <= 32 * 1024 * 1024: array = cupy.arange(num, dtype=numpy.int32) # apply sort of cache blocking block_size = 1 * 1024 * 1024 # The block size above is a value determined from the L2 cache size # of GP100 (L2 cache size / size of int = 4MB / 4B = 1M). It may be # better to change the value base on the L2 cache size of the GPU # you use. # When num > block_size, cupy kernel: _cupy_permutation is to be # launched multiple times. However, it is observed that performance # will be degraded if the launch count is too many. Therefore, # the block size is adjusted so that launch count will not exceed # twelve Note that this twelve is the value determined from # measurement on GP100. while num // block_size > 12: block_size *= 2 for j_start in range(0, num, block_size): j_end = j_start + block_size _cupy_permutation(sample, j_start, j_end, array, size=num) else: # When num > 32M, argsort is used, because it is faster than # custom kernel. See https://github.com/cupy/cupy/pull/603. array = cupy.argsort(sample) return array
def getMeWtW2(W, U0, Nnearest=None): # this function compute the correlation between any two pairs of templates # it relies on the fact that the W and U0 are unit normalized, so that the product of a # template with itself is 1, as it should be if we're trying to calculate correlations # takes input the temporal and spatial factors of the low-rank template, as # well as the number of most similar template pairs desired to be output in # iList nt0, Nfilt, Nrank = W.shape WtW = cp.zeros((Nfilt, Nfilt), dtype=np.float32, order='F') # since the templates are factorized into orthonormal components, we can compute dot products # one dimension at a time for i in range(Nrank): for j in range(Nrank): # this computes the spatial dot product utu0 = cp.dot(U0[:, :, i].T, U0[:, :, j]) # this computes the temporal dot product wtw0 = cp.dot(W[:, :, i].T, W[:, :, j]) # the element-wise product of these is added to the matrix of correlatioons WtW = WtW + wtw0 * utu0 # also return a list of most correlated template pairs isort = cp.argsort(WtW, axis=0)[::-1] if Nnearest: # if we don't have enough templates yet, just wrap the indices around the range 1:Nfilt iNear = cp.mod(cp.arange(Nnearest), Nfilt) iList = isort[iNear, :] # return the list of pairs for each template return WtW, iList else: return WtW
def _binary_clf_curve(y_true, y_score): if y_true.dtype.kind == 'f' and np.any(y_true != y_true.astype(int)): raise ValueError("Continuous format of y_true " "is not supported.") ids = cp.argsort(-y_score) sorted_score = y_score[ids] ones = y_true[ids].astype('float32') # for calculating true positives zeros = 1 - ones # for calculating predicted positives # calculate groups group = _group_same_scores(sorted_score) num = int(group[-1]) tps = cp.zeros(num, dtype='float32') fps = cp.zeros(num, dtype='float32') tps = _addup_x_in_group(group, ones, tps) fps = _addup_x_in_group(group, zeros, fps) tps = cp.cumsum(tps) fps = cp.cumsum(fps) thresholds = cp.unique(y_score) return fps, tps, thresholds
def evaluate(data_source, embedict, data_dict, ntokens): # Turn on evaluation mode which disables dropout. model.eval() total_loss = 0 k = 3 eval_batch_size = len(data_source) len_src = len(data_source) hidden = model.init_hidden(eval_batch_size) for i in range(0, len_src): data_source = data_source.view(1, -1) data = index2embed(data_source, embedict, corpus.dictionary, args.emsize) output, hidden = model(data, hidden) output_flat = output.view(-1, ntokens) preds = output_flat[len_src - 1, :] preds = preds.data.cpu().numpy() # retrive top k preds = cp.array(preds) topk_idx = cp.argsort(preds)[ntokens - k:] topk = [] for top in topk_idx: term = corpus.dictionary[int(top)] topk.append(term) # show a gradual prediction for each word hidden = repackage_hidden(hidden) return topk
def generate_negatives(neg_users, true_mat, item_range, sort=False, use_trick=False): """ Generate negative samples for data augmentation """ neg_u = [] neg_i = [] # If using the shortcut, generate negative items without checking if the associated # user has interacted with it. Speeds up training significantly with very low impact # on accuracy. if use_trick: neg_items = cp.random.randint(0, high=item_range, size=neg_users.shape[0]) return neg_users, neg_items # Otherwise, generate negative items, check if associated user has interacted with it, # then generate a new one if true while len(neg_users) > 0: neg_items = cp.random.randint(0, high=item_range, size=neg_users.shape[0]) neg_mask = true_mat[neg_users, neg_items] neg_u.append(neg_users[neg_mask]) neg_i.append(neg_items[neg_mask]) neg_users = neg_users[cp.logical_not(neg_mask)] neg_users = cp.concatenate(neg_u) neg_items = cp.concatenate(neg_i) if sort == False: return neg_users, neg_items sorted_users = cp.sort(neg_users) sort_indices = cp.argsort(neg_users) return sorted_users, neg_items[sort_indices]
def _eigsh_solve_ritz(alpha, beta, beta_k, k, which): t = cupy.diag(alpha) t = t + cupy.diag(beta[:-1], k=1) t = t + cupy.diag(beta[:-1], k=-1) if beta_k is not None: t[k, :k] = beta_k t[:k, k] = beta_k w, s = cupy.linalg.eigh(t) # Pick-up k ritz-values and ritz-vectors if which == 'LA': idx = cupy.argsort(w) elif which == 'LM': idx = cupy.argsort(cupy.absolute(w)) wk = w[idx[-k:]] sk = s[:, idx[-k:]] return wk, sk
def _get_indx(_lambda, num, largest): """Get `num` indices into `_lambda` depending on `largest` option.""" ii = cupy.argsort(_lambda) if largest: ii = ii[:-num - 1:-1] else: ii = ii[:num] return ii
def buildNeighborList(self, test): n_mat = np.sqrt(np.asarray(np.dot(x_test, x_train.T), dtype=np.float64)) print("Built neighbor matrix.") srtd = np.argsort(n_mat, axis=1)[:, :self.k] # build the knn matrix and weights self.k_neighbors(n_mat, srtd) self.weights()
def assemble_sparse(self, ke, tri, perm, n_pts, ref=0): ''' function that assembles the global stiffness matrix from all element stiffness matrices takes: ke - stiffness on each element matrix - array shape (n_triangles, n_vertices, n_vertices) tri - array with all indices (in pts array) of triangle vertices - shape (num_triangles, 3) perm - array with permittivity in each element - array shape (num_triangles,) n_pts - number of nodes - int ref - electrode on which reference value is placed returns: K - global stiffness matrix - (n_pts, n_pts) ''' n_tri, n_vertices = tri.shape row = cp.tile(tri, (1, n_vertices)) i = cp.array([0, 3, 6, 1, 4, 7, 2, 5, 8]) row = row[:, i].ravel() col = cp.tile(tri, (n_vertices)).reshape( (tri.shape[0] * tri.shape[1] * n_vertices)) admittanceMatrixC2 = self.admittanceMatrixC2() data = cp.multiply(ke[:], perm[:, None, None]) indexElectrode = cp.sort(self.tri[self.twoFromElectrode][self.isValid], axis=1)[:, 0] // self.n_per_el data[self.twoFromElectrode][self.isValid] = ( data[self.twoFromElectrode][self.isValid] + ((1 / self.z[indexElectrode]))[:, None, None] * admittanceMatrixC2) data = data.ravel() ind = cp.argsort(row) row = row[ind] col = col[ind] data = data[ind] unique, counts = cp.unique(row, return_counts=True) index_pointer = cp.zeros(n_pts + 1) sum_count = cp.cumsum(counts) index_pointer[unique[:] + 1] = sum_count[:] K = sp.csr_matrix((data, col, index_pointer), shape=(n_pts, n_pts), dtype=perm.dtype) K = K.toarray() A = cp.empty((self.n_pts + self.ne, self.n_pts + self.ne), dtype='f8') if 0 <= self.ref < n_pts: K[self.ref, :] = 0. K[:, self.ref] = 0. K[self.ref, self.ref] = 1. A[:self.n_pts, :self.n_pts] = K[:] admittanceMatrixE = self.admittanceMatrixE() A[self.n_pts:, :self.n_pts] = admittanceMatrixE.T A[:self.n_pts, self.n_pts:] = admittanceMatrixE A[self.n_pts:, self.n_pts:] = self.admittanceMatrixD() return A
def beam_search(model, X, params, return_alphas=False, eos_sym=0, null_sym=2, model_ensemble=False, n_models=0): """ Beam search method for Cond models. (https://en.wikibooks.org/wiki/Artificial_Intelligence/Search/Heuristic_search/Beam_search) The algorithm in a nutshell does the following: 1. k = beam_size 2. open_nodes = [[]] * k 3. while k > 0: 3.1. Given the inputs, get (log) probabilities for the outputs. 3.2. Expand each open node with all possible output. 3.3. Prune and keep the k best nodes. 3.4. If a sample has reached the <eos> symbol: 3.4.1. Mark it as final sample. 3.4.2. k -= 1 3.5. Build new inputs (state_below) and go to 1. 4. return final_samples, final_scores :param model: Model to use :param X: Model inputs :param params: Search parameters :param return_alphas: Whether we should return attention weights or not. :param eos_sym: <eos> symbol :param null_sym: <null> symbol :param model_ensemble: Whether we are using several models in an ensemble :param n_models; Number of models in the ensemble. :return: UNSORTED list of [k_best_samples, k_best_scores] (k: beam size) """ k = params['beam_size'] samples = [] sample_scores = [] pad_on_batch = params['pad_on_batch'] dead_k = 0 # samples that reached eos live_k = 1 # samples that did not yet reach eos hyp_samples = [[]] * live_k hyp_scores = cp.zeros(live_k, dtype='float32') ret_alphas = return_alphas or params['pos_unk'] if ret_alphas: sample_alphas = [] hyp_alphas = [[]] * live_k if pad_on_batch: maxlen = int(len(X[params['dataset_inputs'][0]][0]) * params['output_max_length_depending_on_x_factor']) if \ params['output_max_length_depending_on_x'] else params['maxlen'] minlen = int( len(X[params['dataset_inputs'][0]][0]) / params['output_min_length_depending_on_x_factor'] + 1e-7) if \ params['output_min_length_depending_on_x'] else 0 else: minlen = int(np.argmax(X[params['dataset_inputs'][0]][0] == eos_sym) / params['output_min_length_depending_on_x_factor'] + 1e-7) if \ params['output_min_length_depending_on_x'] else 0 maxlen = int(np.argmax(X[params['dataset_inputs'][0]][0] == eos_sym) * params[ 'output_max_length_depending_on_x_factor']) if \ params['output_max_length_depending_on_x'] else params['maxlen'] maxlen = min(params['state_below_maxlen'] - 1, maxlen) # we must include an additional dimension if the input for each timestep are all the generated "words_so_far" if params['words_so_far']: if k > maxlen: raise NotImplementedError("BEAM_SIZE can't be higher than MAX_OUTPUT_TEXT_LEN on the current implementation.") state_below = np.asarray([[null_sym]] * live_k) if pad_on_batch else np.asarray([np.zeros((maxlen, maxlen))] * live_k) else: state_below = np.asarray([null_sym] * live_k) if pad_on_batch else np.asarray([np.zeros(params['state_below_maxlen']) + null_sym] * live_k) prev_out = [None] * n_models if model_ensemble else None for ii in range(maxlen): # for every possible live sample calc prob for every possible label if params['optimized_search']: # use optimized search model if available if model_ensemble: [probs, prev_out, alphas] = model.predict_cond_optimized(X, state_below, params, ii, prev_out) else: [probs, prev_out] = model.predict_cond_optimized(X, state_below, params, ii, prev_out) if ret_alphas: alphas = prev_out[-1][0] # Shape: (k, n_steps) prev_out = prev_out[:-1] else: probs = model.predict_cond(X, state_below, params, ii) log_probs = cp.log(probs) if minlen > 0 and ii < minlen: log_probs[:, eos_sym] = -cp.inf # total score for every sample is sum of -log of word prb cand_scores = hyp_scores[:, None] - log_probs cand_flat = cand_scores.flatten() # Find the best options by calling argsort of flatten array ranks_flat = cp.argsort(cand_flat)[:(k - dead_k)] # Decypher flatten indices voc_size = log_probs.shape[1] trans_indices = ranks_flat // voc_size # index of row word_indices = ranks_flat % voc_size # index of col costs = cand_flat[ranks_flat] best_cost = costs[0] if cupy: trans_indices = cp.asnumpy(trans_indices) word_indices = cp.asnumpy(word_indices) if ret_alphas: alphas = cp.asnumpy(alphas) # Form a beam for the next iteration new_hyp_samples = [] new_trans_indices = [] new_hyp_scores = cp.zeros(k - dead_k, dtype='float32') if ret_alphas: new_hyp_alphas = [] for idx, [ti, wi] in list(enumerate(zip(trans_indices, word_indices))): if params['search_pruning']: if costs[idx] < k * best_cost: new_hyp_samples.append(hyp_samples[ti] + [wi]) new_trans_indices.append(ti) new_hyp_scores[idx] = copy.copy(costs[idx]) if ret_alphas: new_hyp_alphas.append(hyp_alphas[ti] + [alphas[ti]]) else: dead_k += 1 else: new_hyp_samples.append(hyp_samples[ti] + [wi]) new_trans_indices.append(ti) new_hyp_scores[idx] = copy.copy(costs[idx]) if ret_alphas: new_hyp_alphas.append(hyp_alphas[ti] + [alphas[ti]]) # check the finished samples new_live_k = 0 hyp_samples = [] hyp_scores = [] hyp_alphas = [] indices_alive = [] for idx in range(len(new_hyp_samples)): if new_hyp_samples[idx][-1] == eos_sym: # finished sample samples.append(new_hyp_samples[idx]) sample_scores.append(new_hyp_scores[idx]) if ret_alphas: sample_alphas.append(new_hyp_alphas[idx]) dead_k += 1 else: indices_alive.append(new_trans_indices[idx]) new_live_k += 1 hyp_samples.append(new_hyp_samples[idx]) hyp_scores.append(new_hyp_scores[idx]) if ret_alphas: hyp_alphas.append(new_hyp_alphas[idx]) hyp_scores = cp.array(np.asarray(hyp_scores, dtype='float32'), dtype='float32') live_k = new_live_k if new_live_k < 1: break if dead_k >= k: break state_below = np.asarray(hyp_samples, dtype='int64') state_below = np.hstack((np.zeros((state_below.shape[0], 1), dtype='int64') + null_sym, state_below)) \ if pad_on_batch else \ np.hstack((np.zeros((state_below.shape[0], 1), dtype='int64') + null_sym, state_below, np.zeros((state_below.shape[0], max(params['state_below_maxlen'] - state_below.shape[1] - 1, 0)), dtype='int64'))) # we must include an additional dimension if the input for each timestep are all the generated words so far if params['words_so_far']: state_below = np.expand_dims(state_below, axis=0) if params['optimized_search'] and ii > 0: # filter next search inputs w.r.t. remaining samples if model_ensemble: for n_model in range(n_models): # filter next search inputs w.r.t. remaining samples for idx_vars in range(len(prev_out[n_model])): prev_out[n_model][idx_vars] = prev_out[n_model][idx_vars][indices_alive] else: for idx_vars in range(len(prev_out)): prev_out[idx_vars] = prev_out[idx_vars][indices_alive] # dump every remaining one if live_k > 0: for idx in range(live_k): samples.append(hyp_samples[idx]) sample_scores.append(hyp_scores[idx]) if ret_alphas: sample_alphas.append(hyp_alphas[idx]) if ret_alphas: return samples, sample_scores, np.asarray(sample_alphas) else: return samples, sample_scores, None
import cProfile import StringIO import pstats import skimage.io import skimage.color import cupy import numpy if __name__ == '__main__': pr = cProfile.Profile() filename = '/home/dzhoshkun/data/sample-images/1920x1080.jpg' img_orig = skimage.io.imread(filename) img_lab = skimage.color.rgb2lab(img_orig) d_img_a = cupy.array(img_lab[:, :, 1]) pr.enable() img_a_idx = numpy.argsort(img_lab[:, :, 1].flatten()) d_img_a_idx = cupy.argsort(d_img_a.flatten()) pr.disable() s = StringIO.StringIO() sortby = 'tottime' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print(s.getvalue()) print(cupy.max(d_img_a_idx), img_orig.shape[0] * img_orig.shape[1]) print(d_img_a_idx.shape)