def _make_graph_edges_3d(n_x, n_y, n_z): """Returns a list of edges for a 3D image. Parameters ---------- n_x: integer The size of the grid in the x direction. n_y: integer The size of the grid in the y direction n_z: integer The size of the grid in the z direction Returns ------- edges : (2, N) ndarray with the total number of edges:: N = n_x * n_y * (nz - 1) + n_x * (n_y - 1) * nz + (n_x - 1) * n_y * nz Graph edges with each column describing a node-id pair. """ vertices = cp.arange(n_x * n_y * n_z).reshape((n_x, n_y, n_z)) edges_deep = cp.stack((vertices[..., :-1].ravel(), vertices[..., 1:].ravel()), axis=0) edges_right = cp.stack((vertices[:, :-1].ravel(), vertices[:, 1:].ravel()), axis=0) edges_down = cp.stack((vertices[:-1].ravel(), vertices[1:].ravel()), axis=0) edges = cp.concatenate((edges_deep, edges_right, edges_down), axis=1) return edges
def uniform_crossover(parent_pairs, x_probability): # define random chromosome probability sequence (to decide randomly whether ot perform crossover or not) X_SEQUENCE = cupy.random.uniform(0, 1, parent_pairs.shape[0]) # define random element probability sequence (for uniform crossover) U_SEQUENCE = cupy.random.uniform( 0, 1, parent_pairs.shape[0] * parent_pairs.shape[1]).reshape( parent_pairs.shape[0], parent_pairs.shape[1]) # define element probability crossover (for uniform crossover) CONST_PROB = 0.5 # define new generation population variable population_hat = None # perform single point crossover for i in range(parent_pairs.shape[0]): X, Y = parent_pairs[i] # check chromosomes' compatibility in case there is an error compatible_chromosomes = X.shape == Y.shape # define max index boundary chromosome_shape = X.shape[0] # initialize new chromosome a, b = cupy.zeros((2, chromosome_shape), dtype=cupy.int64) if not compatible_chromosomes: print( "Error [14]: Incompatible chromosomes (at: multiple point selection)\nExiting...)" ) exit() else: # crossover with respect to the crossover probability if X_SEQUENCE[i] < x_probability: # create children for c, (k, l) in enumerate(zip(X, Y)): # repeatedly toss a coin and check with respect to CONST_PROB if CONST_PROB > U_SEQUENCE[i][c]: # gene exchange a[c] = l b[c] = k else: # NO gene exchange a[c] = k b[c] = l # append children to form the new population if i == 0: # if loop run for first time, then initialize the generation population population_hat = cupy.stack((a, b)) else: # after first time, stack chromosomes to the generation population population_hat = cupy.vstack( (population_hat, cupy.stack((a, b)))) else: # append parents to the new population if i == 0: # if loop run for first time, then initialize the generation population population_hat = cupy.stack((X, Y)) else: # after first time, stack chromosomes to the generation population population_hat = cupy.vstack( (population_hat, cupy.stack((X, Y)))) return population_hat
def train(self, dataset, outputs, epochs=10, verbose=False): # print("Input training data") # print(dataset) # print("Actual output data") # print(outputs) # Since cupy stacks have shapes, we can use those to verify whether our data fits the model we've designed n_dsequences = dataset.shape[0] n_osequences = outputs.shape[0] osize = outputs.shape[1] dsize = dataset.shape[1] assert ( n_dsequences == n_osequences ), "Number of sequences in dataset must match number of actual output sequences" assert ( osize == self.output_size ), "Output training data must be same dimensions as last layer output" assert (dsize == self.input_size ), "Input training data must be same dimensions of input layer" # print("Datset & output shapes: " + str(dataset.shape) + " " + str(outputs.shape)) batch_errors = [] n_layers = len(self.layers) for e in range(0, epochs): errors = [] # For each training example for i in range(0, len(dataset)): # print("In training loop") data = cp.copy(dataset[i]) # training example out = outputs[i] # Expected output # Feed the data forward and store the activations # print("Data: " + str(data)) zs, acts = self.feedforward(data) # print("zs: " + str(zs)) # print("acts: " + str(acts)) # Create error vector errors.append(self.cost_function(acts[-1], out)) batch_errors.append(self.cost_function(acts[-1], out)) error = self.error_function(acts[-1], out) # print("Before back propagation") #Backwards propagate with the computed error, activations, and goal output to get dw, db self.backprop(n_layers - 1, n_layers - 2, acts, zs, out) if (i % 600): print("Out: " + str(zs[-1]) + " | " + "Actual: " + str(out) + " | Error: " + str(cp.sum(error))) errors = cp.stack(errors) epoch_error = cp.sum(errors) / len(errors) # print(errors) if (verbose): print("Total epoch error: " + str(epoch_error)) batch_errors = cp.stack(batch_errors) batch_errors = cp.sum(batch_errors) / len(batch_errors) print("Average batch error: " + str(batch_errors))
def _line_profile_coordinates(src, dst, linewidth=1): """Return the coordinates of the profile of an image along a scan line. Parameters ---------- src : 2-tuple of numeric scalar (float or int) The start point of the scan line. dst : 2-tuple of numeric scalar (float or int) The end point of the scan line. linewidth : int, optional Width of the scan, perpendicular to the line Returns ------- coords : array, shape (2, N, C), float The coordinates of the profile along the scan line. The length of the profile is the ceil of the computed length of the scan line. Notes ----- This is a utility method meant to be used internally by skimage functions. The destination point is included in the profile, in contrast to standard numpy indexing. """ src_row, src_col = src dst_row, dst_col = dst d_row, d_col = (d - s for d, s in zip(dst, src)) theta = math.atan2(d_row, d_col) length = math.ceil(math.hypot(d_row, d_col) + 1) # we add one above because we include the last point in the profile # (in contrast to standard numpy indexing) line_col = cp.linspace(src_col, dst_col, length) line_row = cp.linspace(src_row, dst_row, length) # we subtract 1 from linewidth to change from pixel-counting # (make this line 3 pixels wide) to point distances (the # distance between pixel centers) col_width = (linewidth - 1) * cp.sin(-theta) / 2 row_width = (linewidth - 1) * cp.cos(theta) / 2 perp_rows = cp.stack([ cp.linspace(row_i - row_width, row_i + row_width, linewidth) for row_i in line_row ]) perp_cols = cp.stack([ cp.linspace(col_i - col_width, col_i + col_width, linewidth) for col_i in line_col ]) return cp.stack([perp_rows, perp_cols])
def gen_train(q_func, s0, a0, r1, s1, done, n_actions): train = [] gamma = GAMMA # Outputs y0 = q_func(s0).data y1 = q_func(s1).data q0 = y0[:, :n_actions] q1 = y1[:, :n_actions] h0 = cp.stack(cp.split(y0[:, n_actions:-1], n_actions, axis=1), axis=0) h1 = cp.stack(cp.split(y1[:, n_actions:-1], n_actions, axis=1), axis=0) v0 = y0[:, -1:] v1 = y1[:, -1:] # Correct actions ''' print(h0) print(a0) print(s1) ''' action_error = h0[a0, cp.arange(h0.shape[1])] - s1 print('action_error', (action_error**2).mean()) h0[a0, np.arange(h0.shape[1])] = s1 # Correct q-values q1[done, :] = 0 tt = r1 + (gamma * cp.max(q1, axis=1)) q0[a0[:, None] == cp.arange(q0.shape[1])] = tt # TODO: Correct v-values # Put them back together t = cp.hstack([ q0, cp.hstack(h0), v0, ]) train = [(s0[i], t[i]) for i in range(t.shape[0])] return train ''' ''' t = q_func(s0).data t1 = q_func(s1).data '''
def getProj(self, obs, center_pixel, rz, z, ry, rx): patch = self.getPatch(obs, center_pixel, torch.zeros_like(rz)) patch = np.round(patch.cpu().numpy(), 5) patch = cp.array(patch) projections = [] size = self.patch_size zs = cp.array(z.numpy()) + cp.array( [(-size / 2 + j) * self.heightmap_resolution for j in range(size)]) zs = zs.reshape((zs.shape[0], 1, 1, zs.shape[1])) zs = zs.repeat(size, 1).repeat(size, 2) c = patch.reshape(patch.shape[0], self.patch_size, self.patch_size, 1).repeat(size, 3) ori_occupancy = c > zs # transform into points point_w_d = cp.argwhere(ori_occupancy) rz_id = (rz.expand(-1, self.num_rz) - self.rzs).abs().argmin(1) ry_id = (ry.expand(-1, self.num_ry) - self.rys).abs().argmin(1) rx_id = (rx.expand(-1, self.num_rx) - self.rxs).abs().argmin(1) dimension = point_w_d[:, 0] point = point_w_d[:, 1:4] rz_id = cp.array(rz_id) ry_id = cp.array(ry_id) rx_id = cp.array(rx_id) mapped_point = self.map[rz_id[dimension], ry_id[dimension], rx_id[dimension], point[:, 0], point[:, 1], point[:, 2]].T rotated_point = mapped_point.T[(cp.logical_and( 0 < mapped_point.T, mapped_point.T < size)).all(1)] d = dimension[(cp.logical_and( 0 < mapped_point.T, mapped_point.T < size)).all(1)].T.astype(int) for i in range(patch.shape[0]): point = rotated_point[d == i].T occupancy = cp.zeros((size, size, size)) if point.shape[0] > 0: occupancy[point[0], point[1], point[2]] = 1 occupancy = median_filter(occupancy, size=2) occupancy = cp.ceil(occupancy) projection = cp.stack( (occupancy.sum(0), occupancy.sum(1), occupancy.sum(2))) projections.append(projection) return torch.tensor(cp.stack(projections)).float().to(self.device)
def _stack_nested_structure(structure): if isinstance(structure, _NestedStructure): args = [_stack_nested_structure(x) for x in structure.data] return structure.tuple_constructor(*args) else: return chainer.as_variable(xp.stack([xp.asarray(x) for x in structure]))
def update_new_chembl(self, north_stars, radius=2, nBits=512): north_stars = list(map(str.strip, north_stars.split(','))) north_stars = list(map(str.upper, north_stars)) missing_chembl = set(north_stars).difference(self.chembl_ids) # CHEMBL10307, CHEMBL103071, CHEMBL103072 if missing_chembl: missing_chembl = list(missing_chembl) ldf = self.create_dataframe_molecule_properties(missing_chembl) if ldf.shape[0] > 0: self.prop_df = self.prop_df.append(ldf) self.chembl_ids.extend(missing_chembl) smiles = [] for i in range(0, ldf.shape[0]): smiles.append(ldf.iloc[i]['canonical_smiles'].to_array()[0]) results = list(map(self.MorganFromSmiles, smiles)) fingerprints = cupy.stack(results).astype(np.float32) tdf = self.re_cluster(self.df, fingerprints, missing_chembl) if tdf is not None: self.df = tdf else: return None return ','.join(north_stars)
def render(swatch, points, frame): r, g, b = swatch(points[..., 0], points[..., 1], points[..., 2], frame) r = r.mean(axis=-1) g = g.mean(axis=-1) b = b.mean(axis=-1) return cp.clip(cp.stack((r, g, b), axis=-1), 0, 1)
def test_adapthist_grayscale_Nd(): """ Test for n-dimensional consistency with float images Note: Currently if img.ndim == 3, img.shape[2] > 4 must hold for the image not to be interpreted as a color image by @adapt_rgb """ # take 2d image, subsample and stack it img = util.img_as_float(cp.asarray(data.astronaut())) img = rgb2gray(img) a = 15 img2d = util.img_as_float(img[0:-1:a, 0:-1:a]) img3d = cp.stack([img2d] * (img.shape[0] // a), axis=0) # apply CLAHE adapted2d = exposure.equalize_adapthist(img2d, kernel_size=5, clip_limit=0.05) adapted3d = exposure.equalize_adapthist(img3d, kernel_size=5, clip_limit=0.05) # check that dimensions of input and output match assert img2d.shape == adapted2d.shape assert img3d.shape == adapted3d.shape # check that the result from the stack of 2d images is similar # to the underlying 2d image assert (cp.mean(cp.abs(adapted2d - adapted3d[adapted3d.shape[0] // 2])) < 0.02)
def test_args(self): dx = cp.cumsum(cp.ones(5)) dx_uneven = [1.0, 2.0, 5.0, 9.0, 11.0] f_2d = cp.arange(25).reshape(5, 5) # distances must be scalars or have size equal to gradient[axis] gradient(cp.arange(5), 3.0) gradient(cp.arange(5), cp.array(3.0)) gradient(cp.arange(5), dx) # dy is set equal to dx because scalar gradient(f_2d, 1.5) gradient(f_2d, cp.array(1.5)) gradient(f_2d, dx_uneven, dx_uneven) # mix between even and uneven spaces and # mix between scalar and vector gradient(f_2d, dx, 2) # 2D but axis specified gradient(f_2d, dx, axis=1) # 2d coordinate arguments are not yet allowed assert_raises_regex( ValueError, ".*scalars or 1d", gradient, f_2d, cp.stack([dx] * 2, axis=-1), 1, )
def _build_linear_system(data, spacing, labels, nlabels, mask, beta, multichannel): """ Build the matrix A and rhs B of the linear system to solve. A and B are two block of the laplacian of the image graph. """ if mask is None: labels = labels.ravel() else: labels = labels[mask] indices = cp.arange(labels.size) seeds_mask = labels > 0 unlabeled_indices = indices[~seeds_mask] seeds_indices = indices[seeds_mask] lap_sparse = _build_laplacian(data, spacing, mask=mask, beta=beta, multichannel=multichannel) rows = lap_sparse[unlabeled_indices, :] lap_sparse = rows[:, unlabeled_indices] B = -rows[:, seeds_indices] seeds = labels[seeds_mask] # CuPy Backend: sparse matrices are only implemented for floating point # dtypes, so have to convert bool->float32 here seeds_mask = sparse.csc_matrix( cp.stack([(seeds == lab) for lab in range(1, nlabels + 1)], axis=-1).astype(np.float32) ) rhs = B.dot(seeds_mask) return lap_sparse, rhs
def _sin_flow_gen(image0, max_motion=4.5, npics=5): """Generate a synthetic ground truth optical flow with a sinusoid as first component. Parameters: ---- image0: ndarray The base image to be warped. max_motion: float Maximum flow magnitude. npics: int Number of sinusoid pics. Returns ------- flow, image1 : ndarray The synthetic ground truth optical flow with a sinusoid as first component and the corresponding warped image. """ grid = cp.meshgrid(*[cp.arange(n) for n in image0.shape], indexing='ij') grid = cp.stack(grid) # TODO: make upstream scikit-image PR changing gt_flow dtype to float gt_flow = cp.zeros_like(grid, dtype=float) gt_flow[0, ...] = max_motion * cp.sin(grid[0] / grid[0].max() * npics * np.pi) image1 = warp(image0, grid - gt_flow, mode="nearest") return gt_flow, image1
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 test_stack_with_axis_value(self): a = testing.shaped_arange((2, 3), cupy) s = cupy.stack((a, a), axis=1) self.assertEqual(s.shape, (2, 2, 3)) cupy.testing.assert_array_equal(s[:, 0, :], a) cupy.testing.assert_array_equal(s[:, 1, :], a)
def _check_ks(self, significance_level, cupy_len, numpy_len, *args, **kwargs): assert 'size' in kwargs # cupy func = self._get_generator_func(*args, **kwargs) vals_cupy = func() assert vals_cupy.size > 0 count = 1 + (cupy_len - 1) // vals_cupy.size vals_cupy = [vals_cupy] for _ in range(1, count): vals_cupy.append(func()) vals_cupy = cupy.stack(vals_cupy).ravel() # numpy kwargs['size'] = numpy_len dtype = kwargs.pop('dtype', None) numpy_rs = numpy.random.RandomState(self.__seed) vals_numpy = getattr(numpy_rs, self.target_method)(*args, **kwargs) if dtype is not None: vals_numpy = vals_numpy.astype(dtype, copy=False) # test d_plus, d_minus, p_value = \ two_sample_Kolmogorov_Smirnov_test( cupy.asnumpy(vals_cupy), vals_numpy) if p_value < significance_level: message = '''Rejected null hypothesis: p: %f D+ (cupy < numpy): %f D- (cupy > numpy): %f''' % (p_value, d_plus, d_minus) raise AssertionError(message)
def save_to_disk(self, filename): model = { 'input_size': self.input_size, 'output_size': self.output_size, 'learn_rate': self.learn_rate, 'loss': self.loss, 'error': self.error, 'layers': None, 'decay_rate': self.decay_rate, 'decay_epochs': self.decay_epochs } layer_num = 0 json_layers = [] for layer in self.layers: weights = layer.get_weights().tolist() biases = layer.get_biases() biases = stack(biases) biases = biases.tolist() l = { '_layer_num': layer_num, 'n_nodes': layer.n_nodes, 'activation': layer.activation, 'weights': weights, 'biases': biases } json_layers.append(l) layer_num += 1 model['layers'] = json_layers json.dump(model, open(filename, "w"), sort_keys=True, indent=4)
def load_from_disk(filename): print("Loading model from filename: " + filename) json_model = json.load(open(filename, "r")) model = Model(json_model['input_size'], json_model['output_size'], learn_rate=json_model['learn_rate'], loss=json_model['loss'], error=json_model['error'], decay_rate=json_model['decay_rate'], decay_epochs=json_model['decay_epochs']) layers = json_model['layers'] # One pass to create the structure for i in range(0, len(layers)): if (i != 0): l = Layer(layers[i]['n_nodes'], activation=layers[i]['activation']) model.add_layer(l) else: model.layers[0].n_nodes = layers[0]['n_nodes'] model.layers[0].set_activation(layers[0]['activation']) # Second pass to set weights & biases for i in range(0, len(layers)): weights = layers[i]['weights'] biases = layers[i]['biases'] for j in range(0, len(weights)): weights[j] = array(weights[j]) biases[j] = array(biases[j]) weights = stack(weights) model.layers[i].set_weights(weights) model.layers[i].set_biases(biases) return model
def test_stack_with_negative_axis_value(self): a = testing.shaped_arange((2, 3), cupy) s = cupy.stack((a, a), axis=-1) self.assertEqual(s.shape, (2, 3, 2)) cupy.testing.assert_array_equal(s[:, :, 0], a) cupy.testing.assert_array_equal(s[:, :, 1], a)
def inverse(self, spectrum, in_phase=None): if in_phase is None: in_phase = self.phase else: in_phase = cp.array(in_phase) spectrum = cp.array(spectrum) self.spectrum_buffer[:, -1] = spectrum * in_phase self.absolute_buffer[:, -1] = spectrum for _ in range(self.loop_num): self.overwrap_buf *= 0 waves = cp.fft.ifft(self.spectrum_buffer, axis=2).real last = self.spectrum_buffer for i in range(self.buffer_size): self.overwrap_buf[:, i * self.wave_dif:i * self.wave_dif + self.wave_len] += waves[:, i] waves = cp.stack([ self.overwrap_buf[:, i * self.wave_dif:i * self.wave_dif + self.wave_len] * self.window for i in range(self.buffer_size) ], axis=1) spectrum = cp.fft.fft(waves, axis=2) self.spectrum_buffer = self.absolute_buffer * spectrum / ( cp.abs(spectrum) + 1e-10) self.spectrum_buffer += 0.5 * (self.spectrum_buffer - last) dst = cp.asnumpy(self.spectrum_buffer[:, 0]) self.absolute_buffer = cp.roll(self.absolute_buffer, -1, axis=1) self.spectrum_buffer = cp.roll(self.spectrum_buffer, -1, axis=1) return dst
def multiple_point_crossover(parent_pairs, x_probability): # define random probability sequence SEQUENCE = cupy.random.uniform(0, 1, parent_pairs.shape[0]) # define new generation population variable population_hat = None # perform single point crossover for i in range(parent_pairs.shape[0]): X, Y = parent_pairs[i] # check chromosomes' compatibility in case there is an error compatible_chromosomes = X.shape == Y.shape # define max index boundary chromosome_shape = X.shape[0] # initialize new chromosome a, b = cupy.zeros((2, chromosome_shape), dtype=cupy.int64) if not compatible_chromosomes: print( "Error [13]: Incompatible chromosomes (at: multiple point selection\nExiting...)" ) exit() else: # crossover random point x_idx, y_idx = cupy.sort( cupy.random.randint(0, chromosome_shape, 2)) # first child chromosome a = cupy.concatenate((X[:x_idx], Y[x_idx:y_idx], X[y_idx:])) # second child chromosome b = cupy.concatenate((Y[:x_idx], X[x_idx:y_idx], Y[y_idx:])) # crossover with respect to the crossover probability if SEQUENCE[i] < x_probability: # append children to form the new population if i == 0: # if loop run for first time, then initialize the generation population population_hat = cupy.stack((a, b)) else: # after first time, stack chromosomes to the generation population population_hat = cupy.vstack( (population_hat, cupy.stack((a, b)))) else: # append parents to the new population if i == 0: # if loop run for first time, then initialize the generation population population_hat = cupy.stack((X, Y)) else: # after first time, stack chromosomes to the generation population population_hat = cupy.vstack( (population_hat, cupy.stack((X, Y)))) return population_hat
def predict(self, Xtest, use_gpu=False): """Predict using the linear model Let :math:`B^k` be the basis vectors of class :math:`k`, and :math:`x` be the RCDT sapce feature vector of an input, the NS method performs classification by .. math:: arg\min_k \| B^k (B^k)^T x - x\|^2 Parameters ---------- Xtest : array-like, shape (n_samples, n_rows, n_columns) Image data for testing. use_gpu: boolean flag; IF TRUE, use gpu for calculations default = False. Returns ------- ndarray of shape (n_samples,) Predicted target values per element in Xtest. """ # calculate the RCDT using parallel CPUs print('\nCalculating RCDTs for testing images ...') Xrcdt = self.rcdt_parallel(Xtest) # vectorize RCDT matrix X = Xrcdt.reshape([Xrcdt.shape[0], -1]) # import cupy for using GPU if use_gpu: import cupy as cp X = cp.array(X) # find nearest subspace for each test sample print('Finding nearest subspace for each test sample ...') D = [] for class_idx in range(self.num_classes): basis = self.subspaces[class_idx] basis = basis[:self.len_subspace, :] if use_gpu: D.append( cp.linalg.norm(cp.matmul(cp.matmul(X, cp.array(basis).T), cp.array(basis)) - X, axis=1)) else: proj = X @ basis.T # (n_samples, n_basis) projR = proj @ basis # (n_samples, n_features) D.append(LA.norm(projR - X, axis=1)) if use_gpu: preds = cp.argmin(cp.stack(D, axis=0), axis=0) return cp.asnumpy(preds) else: D = np.stack(D, axis=0) preds = np.argmin(D, axis=0) return preds
def sum_duplicates(self): """Eliminate duplicate matrix entries by adding them together. .. seealso:: :func:`scipy.sparse.coo_matrix.sum_duplicates` """ if self._has_canonical_format: return if self.data.size == 0: self._has_canonical_format = True return keys = cupy.stack([self.row, self.col]) order = cupy.lexsort(keys) src_data = self.data[order] src_row = self.row[order] src_col = self.col[order] diff = cupy.ElementwiseKernel( 'raw int32 row, raw int32 col', 'int32 diff', ''' int index; if (i == 0 || row[i - 1] == row[i] && col[i - 1] == col[i]) { diff = 0; } else { diff = 1; } ''', 'sum_duplicates_diff' )(src_row, src_col, size=self.row.size) if diff[1:].all(): # All elements have different indices. data = src_data row = src_row col = src_col else: index = cupy.cumsum(diff, dtype='i') size = int(index[-1]) + 1 data = cupy.zeros(size, dtype=self.data.dtype) row = cupy.empty(size, dtype='i') col = cupy.empty(size, dtype='i') cupy.ElementwiseKernel( 'T src_data, int32 src_row, int32 src_col, int32 index', 'raw T data, raw int32 row, raw int32 col', ''' atomicAdd(&data[index], src_data); row[index] = src_row; col[index] = src_col; ''', 'sum_duplicates_assign', preamble=util._preamble_atomic_add )(src_data, src_row, src_col, index, data, row, col) self.data = data self.row = row self.col = col self._has_canonical_format = True
def _compute_gradient(self, alpha): """Computes the gradient ∇F(β) of F""" K_alpha = xp.dot(self._K, alpha) grad = -2 / self._n * xp.sum( self._y[:, xp.newaxis] * self._K * xp.max(xp.stack( (xp.zeros_like(self._y), 1 - self._y * K_alpha)), axis=0)[:, xp.newaxis], axis=0) + 2 * self._lambduh * K_alpha return grad
def test_stack_value(self): a = testing.shaped_arange((2, 3), cupy) b = testing.shaped_arange((2, 3), cupy) c = testing.shaped_arange((2, 3), cupy) s = cupy.stack((a, b, c)) self.assertEqual(s.shape, (3, 2, 3)) cupy.testing.assert_array_equal(s[0], a) cupy.testing.assert_array_equal(s[1], b) cupy.testing.assert_array_equal(s[2], c)
def get_preactivation_vector(self, data, weights=None): biases = self.get_biases() # Bias vector biases = stack(biases) if (self.is_first): z = data else: z = cp.dot(data, weights) # z += biases return z
def stack(tensors: List[Tensor], axis: int = 0) -> 'Tensor': _check_tensors(*tensors) engine = _get_engine(*tensors) return _create_tensor( *tensors, data=engine.stack(list(map(lambda x: x.data, tensors)), axis=axis), func=wrapped_partial(stack_backward, tensors=tensors, axis=axis) )
def optimized_update_positions(positions, angle, theta_sense, horizon_sense, theta_walk, horizon_walk, trace_array): """Returns the adapted physarum-positions, given initial coordinates and constants. This function is optimized by using Cupy (implementation of NumPy-compatible multi-dimensional array on CUDA)""" ### Get all possible positions to test # get the new 3 angles to test for each organism angles_to_test = np.hstack(( (angle - theta_sense) % (2 * np.pi), angle, (angle + theta_sense) % (2 * np.pi), )).reshape(-1, 3) # get positions to test based on current positions and angles pos_to_test = positions.reshape(-1, 1, 2) + np.stack( (horizon_sense * np.cos(angles_to_test), horizon_sense * np.sin(angles_to_test)), axis=-1) pos_to_test = np.remainder(pos_to_test, np.array(trace_array.shape)) ### Get all possible positions to walk to # get the new 3 angles to walk to for each organism angles_to_walk = np.hstack(( (angle - theta_walk) % (2 * np.pi), angle, (angle + theta_walk) % (2 * np.pi), )).reshape(-1, 3) # get positions to walk to based on current positions and angles pos_to_walk = positions.reshape(-1, 1, 2) + np.stack( (horizon_walk * np.cos(angles_to_walk), horizon_walk * np.sin(angles_to_walk)), axis=-1) pos_to_walk = np.remainder(pos_to_walk, np.array(trace_array.shape)) ### Get the positions to walk too based on the best positions out of the tested ones pos_to_test = np.floor(pos_to_test).astype(np.int64) - 1 # TODO notice argmax will always return first when multiple entries are equal best_indexes = trace_array[pos_to_test[:, :, 0], pos_to_test[:, :, 1]].argmax(axis=-1) new_positions = pos_to_walk[np.arange(len(pos_to_test)), best_indexes] new_angles = angles_to_walk[np.arange(len(pos_to_test)), best_indexes].reshape(-1, 1) return new_positions, new_angles
def test_border_management(func, tol): img = rgb2gray(cp.array(retina()[300:500, 700:900])) out = func(img, sigmas=[1], mode='mirror') full_std = out.std() full_mean = out.mean() inside_std = out[4:-4, 4:-4].std() inside_mean = out[4:-4, 4:-4].mean() border_std = cp.stack([out[:4, :], out[-4:, :], out[:, :4].T, out[:, -4:].T]).std() border_mean = cp.stack([out[:4, :], out[-4:, :], out[:, :4].T, out[:, -4:].T]).mean() assert abs(full_std - inside_std) < tol assert abs(full_std - border_std) < tol assert abs(inside_std - border_std) < tol assert abs(full_mean - inside_mean) < tol assert abs(full_mean - border_mean) < tol assert abs(inside_mean - border_mean) < tol
def apply(self, df): if cudf and isinstance(df, cudf.DataFrame): import cupy cols = [] for column in self.columns: nullval = numpy.nan if df[self.columns[1]].dtype.kind == 'f' else 0 cols.append(df[self.columns[0]].to_gpu_array(fillna=nullval)) return cupy.stack(cols, axis=-1) else: return numpy.stack([df[col].values for col in self.columns], axis=-1)