def train(self, x): self.n = self.n + 1 # update norms self.norm_max[x > self.norm_max] = x[x > self.norm_max] self.norm_min[x < self.norm_min] = x[x < self.norm_min] # 0-1 normalize x = (x - self.norm_min) / (self.norm_max - self.norm_min + 0.0000000000000001) if self.params.corruption_level > 0.0: tilde_x = self.get_corrupted_input(x, self.params.corruption_level) else: tilde_x = x y = self.get_hidden_values(tilde_x) z = self.get_reconstructed_input(y) L_h2 = x - z L_h1 = cupy.dot(L_h2, self.W) * y * (1 - y) L_vbias = L_h2 L_hbias = L_h1 L_W = cupy.outer(tilde_x.T, L_h1) + cupy.outer(L_h2.T, y) self.W += self.params.lr * L_W self.hbias += self.params.lr * cupy.mean(L_hbias, axis=0) self.vbias += self.params.lr * cupy.mean(L_vbias, axis=0) return cupy.sqrt(cupy.mean( L_h2**2)) #the RMSE reconstruction error during training
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 center_of_mass(self): qx, qy = np.meshgrid(np.arange(self.scan_dimensions[0]), np.arange(self.scan_dimensions[1])) comx = cp.zeros(self.scan_dimensions, dtype=cp.float32) comy = cp.zeros(self.scan_dimensions, dtype=cp.float32) no_count_indicator = np.iinfo(self.indices.dtype).max mass = cp.sum(self.counts, 2) threadsperblock = (16, 16) blockspergrid = tuple( np.ceil(np.array(self.indices.shape[:2]) / threadsperblock).astype( np.int)) qx = cp.array(qx).astype(cp.float32) qy = cp.array(qy).astype(cp.float32) center_of_mass_kernel[blockspergrid, threadsperblock](comx, comy, self.indices, self.counts.astype(cp.uint32), cp.array(self.frame_dimensions), no_count_indicator, qx, qy) comy = comy comx = comx comx /= mass + 1e-6 comy /= mass + 1e-6 comy[comy == 0] = cp.mean(comy[comy != 0]) comx[comx == 0] = cp.mean(comx[comx != 0]) comx -= cp.mean(comx) comy -= cp.mean(comy) return comy, comx
def yangDistributionDifference(aNeg, bNeg, aPos, bPos, p=1): """ Eq. (7) from : Yang, R., Jiang, Y., Mathews, S. et al. Data Min Knowl Disc (2019) 33: 995. https://doi.org/10.1007/s10618-019-00622-6 """ sampleSize = 1000 negSample = xp.random.beta(aNeg, bNeg, sampleSize) posSample = xp.random.beta(aPos, bPos, sampleSize) negPDF_NEG, posPDF_NEG, pdfDiffPos_NEG, pdfDiffNeg_NEG, pdfMax_NEG = calcDifference( negSample, aNeg, bNeg, aPos, bPos) negPDF_POS, posPDF_POS, pdfDiffPos_POS, pdfDiffPOS_POS, pdfMax_POS = calcDifference( posSample, aNeg, bNeg, aPos, bPos) numerator1 = xp.mean(pdfDiffNeg_NEG / negPDF_NEG) numerator2 = xp.mean(pdfDiffPos_POS / posPDF_POS) sumVecs = xp.power(numerator1, xp.ones_like(numerator1) * p) + xp.power( numerator2, xp.ones_like(numerator2) * p) dPHat = xp.power(sumVecs, xp.ones_like(sumVecs) * (1 / p)) dTermNeg = (posPDF_NEG * 0.5) + (negPDF_NEG * 0.5) dTermPos = (posPDF_POS * 0.5) + (negPDF_POS * 0.5) denominator = (xp.sum(pdfMax_NEG / dTermNeg) + xp.sum(pdfMax_POS / dTermPos)) / (2 * sampleSize) return dPHat / denominator
def _get_nearplane_steps(diff, dOP, dPO, A1, A4, recover_psi, recover_probe): # (22) Use least-squares to find the optimal step sizes simultaneously if recover_psi and recover_probe: b1 = cp.sum((dOP.conj() * diff).real, axis=(-2, -1)) b2 = cp.sum((dPO.conj() * diff).real, axis=(-2, -1)) A2 = cp.sum((dOP * dPO.conj()), axis=(-2, -1)) A3 = A2.conj() determinant = A1 * A4 - A2 * A3 x1 = -cp.conj(A2 * b2 - A4 * b1) / determinant x2 = cp.conj(A1 * b2 - A3 * b1) / determinant elif recover_psi: b1 = cp.sum((dOP.conj() * diff).real, axis=(-2, -1)) x1 = b1 / A1 elif recover_probe: b2 = cp.sum((dPO.conj() * diff).real, axis=(-2, -1)) x2 = b2 / A4 if recover_psi: step = 0.9 * cp.maximum(0, x1[..., None, None].real) # (27b) Object update weighted_step_psi = cp.mean(step, keepdims=True, axis=-5) if recover_probe: step = 0.9 * cp.maximum(0, x2[..., None, None].real) weighted_step_probe = cp.mean(step, axis=-5, keepdims=True) else: weighted_step_probe = None return weighted_step_psi, weighted_step_probe
def function_wrapper(x, b, axis=0, **kwargs): # add the padding to the array xsize = x.shape[axis] if 'pad' in kwargs and kwargs['pad']: npad = b.shape[axis] // 2 padd = cp.take(x, cp.arange(npad), axis=axis) * 0 if kwargs['pad'] == 'zeros': x = cp.concatenate((padd, x, padd), axis=axis) if kwargs['pad'] == 'constant': x = cp.concatenate((padd * 0 + cp.mean(x[:npad]), x, padd + cp.mean(x[-npad:])), axis=axis) if kwargs['pad'] == 'flip': pad_in = cp.flip(cp.take(x, cp.arange(1, npad + 1), axis=axis), axis=axis) pad_out = cp.flip(cp.take(x, cp.arange(xsize - npad - 1, xsize - 1), axis=axis), axis=axis) x = cp.concatenate((pad_in, x, pad_out), axis=axis) # run the convolution y = fcn_convolve(x, b, **kwargs) # remove padding from both arrays (necessary for x ?) if 'pad' in kwargs and kwargs['pad']: # remove the padding y = cp.take(y, cp.arange(npad, x.shape[axis] - npad), axis=axis) x = cp.take(x, cp.arange(npad, x.shape[axis] - npad), axis=axis) assert xsize == x.shape[axis] assert xsize == y.shape[axis] return y
def model(X_train, Y_train, X_test, Y_test, num_iterations=3000, learning_rate=0.5, print_cost=False): # Initialize parameters w = cp.zeros((X_train.shape[0], 1)) b = 0 # Gradient descent parameters, _, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost) # Retrieve parameters w = parameters["w"] b = parameters["b"] # Predict test/train set examples Y_prediction_test = predict(w, b, X_test) Y_prediction_train = predict(w, b, X_train) # Print train/test Errors print("train accuracy: {} %".format( 100 - cp.mean(cp.abs(Y_prediction_train - Y_train)) * 100)) print("test accuracy: {} %".format( 100 - cp.mean(cp.abs(Y_prediction_test - Y_test)) * 100))
def do_back_prop(self, X, Y, X_cv, Y_cv, optimiser, gamma, numepochs, learning_rate, batch_size, beta, epsilon, beta1, beta2, l2_reg_param): layers = len(self.structure) - 1 update = {} step_count = 0 for i in range(numepochs): X_cpu = cp.asnumpy(X) Y_cpu = cp.asnumpy(Y) wandb.log({"Sample Data":[wandb.Image(X_cpu[:,jj].reshape(28,28),caption=dataset_labels[Y_cpu[jj]])\ for jj in range(20*i,20*i+20)]},commit = False) for j in tqdm(range(math.ceil(X.shape[1] / batch_size))): X_pass = X[:, j * batch_size:min(X.shape[1], (j + 1) * batch_size)] Y_pass = Y[j * batch_size:min(X.shape[1], (j + 1) * batch_size)] step_count += 1 update = (self.optimisers[optimiser])( X_pass, Y_pass, update, learning_rate, gamma = gamma, beta = beta,\ beta1 = beta1, beta2 = beta2, epsilon = epsilon, l2_reg_param = l2_reg_param, step_num = step_count) Y_pred = self.predict(X) self.accuracies.append( cp.asnumpy(cp.mean(cp.argmax(Y_pred, axis=0) == Y))) self.cvaccuracies.append( cp.asnumpy( cp.mean(self.predict(X_cv, returnclass=1) == Y_cv))) self.losses.append(self.get_loss(None, Y, l2_reg_param, Y_pred)) self.cvlosses.append(self.get_loss(X_cv, Y_cv, l2_reg_param)) wandb.log({"train_acc":self.accuracies[-1],"train_loss":self.losses[-1],"val_acc":self.cvaccuracies[-1],\ "val_loss":self.cvlosses[-1],"step_count":step_count})
def image_complexity(self, X, masks): # 복잡도 기준 threshold = 25.0 # reshape #masks = cp.reshape(masks, masks.shape[:-1]) R, G, B = X[:, :, :, 0:1], X[:, :, :, 1:2], X[:, :, :, 2:] # compute rg = R - G rg = cp.absolute(R - G) # compute yb = 0.5 * (R + G) - B yb = cp.absolute(0.5 * (R + G) - B) _masks = cp.logical_not(masks) # compute the mean and standard deviation of both `rg` and `yb` () # no masked_where on cupy rb_masked = np.ma.masked_where(_masks, rg) (rb_mean, rb_std) = (cp.mean(rb_masked, axis=(1, 2, 3)), cp.std(rb_masked, axis=(1, 2, 3))) yb_masked = np.ma.masked_where(_masks, yb) (yb_mean, yb_std) = (cp.mean(yb_masked, axis=(1, 2, 3)), cp.std(yb_masked, axis=(1, 2, 3))) # combine the mean and standard deviations std_root = cp.sqrt((rb_std**2) + (yb_std**2)) mean_root = cp.sqrt((rb_mean**2) + (yb_mean**2)) # derive the "colorfulness" metric and return it complexity = std_root + (0.3 * mean_root) # 수치 수정 print('image_complexity Done.') return (complexity >= threshold).astype(cp.uint8)
def mean(x, axis=0): if x.ndim == 1: return cp.mean(x) if x.size else cp.nan else: s = list(x.shape) del s[axis] return (cp.mean(x, axis=axis) if x.shape[axis] > 0 else cp.zeros( s, dtype=x.dtype, order='F'))
def checkReset(): """ make sure reset worked """ if not np.mean(stockPool) == np.mean( config._stockPool) and np.mean(hurstPool) == np.mean( _config.stockPool): raise Exception("reset not work")
def attr_selection(self, test): start = self.x_train.shape[1] mean = np.mean(self.x_train, axis=0) # means of features mask = (self.x_train > 0.25 * np.mean(mean)) self.x_train = np.asarray(self.x_train[:, mask.any(axis=0)]) x_test = test[:, mask.any(axis=0)] print("Dropped " + str(start - self.x_train.shape[1]) + " features.") return x_test
def eval_acc(best_preds, y, problem_type): if problem_type == ProblemType.CLASS_SINGLE: return cp.mean(abs(y - best_preds) < .5) elif problem_type == ProblemType.CLASS_ONEHOT: return cp.mean( cp.argmax(best_preds, axis=1) == cp.argmax(y, axis=1)) elif problem_type == ProblemType.REGRESS: return cp.mean(abs(y - best_preds) < .5)
def _t_test(self, with_sample, without_sample): """ compute one-tailed two-sample t-test with a test statistics according to t_j: \frac{\mu_j - \bar{\mu_j}}{\sqrt{\frac{s^2_j}{\norm{I_j}} + \frac{\bar{s^2_j}}{\norm{\bar{I_j}}}}} """ return (cp.mean(with_sample) - cp.mean(without_sample)) /\ cp.sqrt(cp.var(with_sample)**2 / len(with_sample) + cp.var(without_sample)**2 / len(without_sample))
def __check_qualityvalue(self, qtable_y, qtable_c): """calculates the approximate quality value from the computed qunatization tables""" # calculate mean of all entrys and subtract 1/64 * first entry to exclude avg_y = np.mean(qtable_y) - qtable_y[0,0]/64 avg_c = np.mean(qtable_c) - qtable_c[0,0]/64 m = (avg_y + 2*avg_c) / 3 D = (abs(avg_y-avg_c) * 0.49) * 2 Q = 100 - m + D return Q.astype(np.float32)
def get_gait_updates(self, forward_pass, targets, ortho_weighting=0.0, gamma=0.001): # Updates will be stored and returned weight_updates = [] bias_updates = [] nb_layers = len(self.layers) inverse = targets mult_factor = 1.0 for layer_index in range(nb_layers)[::-1]: error = mult_factor * (forward_pass[layer_index + 1] - inverse) if self.layers[layer_index].linear: layer_derivatives = xp.ones((error.shape)) else: layer_derivatives = self.layers[ layer_index].transfer_derivative_func( self.layers[layer_index].transfer_inverse_func( forward_pass[layer_index + 1])) # Calculate updates for this layer weight_update = xp.mean(xp.einsum( 'nj, ni -> nij', layer_derivatives * error, forward_pass[layer_index][:, :self.net_structure[layer_index + 1]]), axis=0) bias_update = xp.mean(layer_derivatives * error, axis=0) # Calculating a weight update based upon a soft orthogonal regularizer if ortho_weighting != 0.0: weight_update += self.ortho_gradients(ortho_weighting, layer_index) # Collect updates weight_updates.append(-weight_update) bias_updates.append(-bias_update) grad_adjusted_inc_factor = gamma * layer_derivatives * layer_derivatives inverse = self.layers[layer_index].inverse( ((1.0 - grad_adjusted_inc_factor) * forward_pass[layer_index + 1] + grad_adjusted_inc_factor * inverse)) mult_factor = mult_factor / gamma # Adding the auxilliary neurons on inverse = xp.hstack([ inverse, forward_pass[layer_index][:, self.net_structure[layer_index + 1]:] ]) return weight_updates[::-1], bias_updates[::-1]
def fit(self, train_data, k=1, learning_rate=0.01, num_epochs=5, batch_size=64, test_data=None): num_samples = train_data.shape[0] indices = np.arange(num_samples) np.random.shuffle(indices) loglikelihood_train = [] loglikelihood = [] for epoch in range(1, num_epochs + 1): # compute train & test negative log-likelihood nll_train = np.mean( np.array([rbm.free_energy(v) for v in train_data])) loglikelihood_train.append(nll_train) log.info( f"[{epoch} / {NUM_EPOCHS}] NLL (train): {nll_train:>20.5f}") if test_data is not None: nll = np.mean(np.array([rbm.free_energy(v) for v in test_data])) log.info(f"[{epoch} / {NUM_EPOCHS}] NLL (test): {nll:>20.5f}") loglikelihood.append(nll) # iterate through dataset in batches bar = tqdm(total=num_samples) for start in range(0, num_samples, batch_size): # ensure we don't go out-of-bounds end = min(start + batch_size, num_samples) # take a gradient-step rbm.step(train_data[start:end], k=k, lr=learning_rate) # update progress bar.update(end - start) bar.close() # shuffle indices for next epoch np.random.shuffle(indices) # compute train & test negative log-likelihood of final batch nll_train = np.mean([rbm.free_energy(v) for v in train_data]) loglikelihood_train.append(nll_train) log.info(f"[{epoch} / {NUM_EPOCHS}] NLL (train): {nll_train:>20.5f}") if test_data is not None: nll = np.mean([rbm.free_energy(v) for v in test_data]) log.info(f"[{epoch} / {NUM_EPOCHS}] NLL (test): {nll:>20.5f}") loglikelihood.append(nll) return loglikelihood_train, loglikelihood
def performance(net, input_data, target_data): output = net.forward_pass(input_data) if is_cupy: correct_mask = xp.asnumpy(xp.argmax(output[-1][:,:10], axis=1)) == xp.asnumpy(xp.argmax(target_data, axis=1)) loss = float(xp.sum(xp.asnumpy((xp.mean(output[-1][:,:10]) - target_data) ** 2))) else: correct_mask = xp.argmax(output[-1][:,:10], axis=1) == xp.argmax(target_data, axis=1) loss = float(xp.sum(xp.mean((output[-1][:,:10]) - target_data) ** 2)) accuracy = (np.sum(correct_mask) / np.size(correct_mask)) # Assuming MSE loss return accuracy, loss
def get_gait_updates(self, forward_pass, targets, ortho_weighting=0.0, gamma=0.001): # Updates will be stored and returned weight_updates = [] bias_updates = [] # We must compute errors layer-wise. # In our formulation, each layer's error is partly difference nb_layers = len(self.layers) # Calculating the inverse target inverse = targets mult_factor = 1.0 # Running backwards through layers for layer_index in range(nb_layers)[::-1]: error = mult_factor * (forward_pass[layer_index + 1] - inverse) if self.layers[layer_index].linear: layer_derivatives = xp.ones((error.shape)) else: layer_derivatives = self.layers[ layer_index].transfer_derivative_func( self.layers[layer_index].transfer_inverse_func( forward_pass[layer_index + 1])) error *= layer_derivatives # Calculate updates for this layer weight_update = xp.mean(xp.einsum('nj, ni -> nij', error, forward_pass[layer_index]), axis=0) bias_update = xp.mean(error, axis=0) # Calculating a weight update based upon a soft orthogonal regularizer if ortho_weighting != 0.0: weight_update += self.ortho_gradients(ortho_weighting, layer_index) # Collect updates weight_updates.append(-weight_update) bias_updates.append(-bias_update) # Adjust and calculate the next layers target grad_adjusted_inc_factor = gamma * layer_derivatives * layer_derivatives inverse = self.layers[layer_index].inverse( ((1.0 - grad_adjusted_inc_factor) * forward_pass[layer_index + 1] + grad_adjusted_inc_factor * inverse)) mult_factor = mult_factor / gamma return weight_updates[::-1], bias_updates[::-1]
def nrms(data_fit, data_true): """ Normalized root mean square error. """ root_mean_squared_error = np.mean( np.linalg.norm(data_fit - data_true, axis=0)) # RMSE normalization_factor = 2 * np.linalg.norm( data_true - np.mean(data_true, axis=1), axis=0).max() return (normalization_factor - root_mean_squared_error) / normalization_factor
def main(tEnd): """ N-body simulation """ # Simulation parameters N = 3 # Number of particles t = 0 # current time of the simulation tEnd = tEnd # time at which simulation ends dt = 0.0005 # timestep softening = 0.1 # softening length G = 1.0 # Newton's Gravitational Constant # Generate Initial Conditions cp.random.seed(17) # set the random number generator seed mass = 20.0 * cp.ones((N, 1)) / N # total mass of particles is 20 pos = cp.random.randn(N, 3) # randomly selected positions and velocities vel = cp.random.randn(N, 3) # Convert to Center-of-Mass frame vel -= cp.mean(mass * vel, 0) / cp.mean(mass) # calculate initial gravitational accelerations acc = get_acc(pos, mass, G, softening) # number of timesteps Nt = int(cp.ceil(tEnd / dt)) # Simulation Main Loop for i in range(Nt): # (1/2) kick vel += acc * dt / 2.0 # drift pos += vel * dt # update accelerations acc = get_acc(pos, mass, G, softening) # (1/2) kick vel += acc * dt / 2.0 # update time t += dt dt = tEnd - Nt vel += acc * dt / 2.0 pos += vel * dt acc = get_acc(pos, mass, G, softening) vel += acc * dt / 2.0 cp.cuda.Stream.null.synchronize() return 0
def _update_position( scan, position_options, position_update_numerator, position_update_denominator, alpha=0.05, max_shift=1, ): step = position_update_numerator / ( (1 - alpha) * position_update_denominator + alpha * position_update_denominator.max()) step_x = step[..., 0] step_y = step[..., 1] if position_options.use_adaptive_moment: logger.info( "position correction with ADAptive Momemtum acceleration enabled.") step_x, position_options.vx, position_options.mx = adam( step_x, position_options.vx, position_options.mx, vdecay=position_options.vdecay, mdecay=position_options.mdecay) step_y, position_options.vy, position_options.my = adam( step_y, position_options.vy, position_options.my, vdecay=position_options.vdecay, mdecay=position_options.mdecay) # Step limit for stability _max_shift = cp.minimum( max_shift, _mad( cp.concatenate((step_x, step_y), axis=-1), axis=-1, keepdims=True, ), ) step_x = cp.maximum(-_max_shift, cp.minimum(step_x, _max_shift)) step_y = cp.maximum(-_max_shift, cp.minimum(step_y, _max_shift)) # Ensure net movement is zero step_x -= cp.mean(step_x, axis=-1, keepdims=True) step_y -= cp.mean(step_y, axis=-1, keepdims=True) logger.info('position update norm is %+.3e', tike.linalg.norm(step_x)) scan[..., 0] -= step_x scan[..., 1] -= step_y return scan, position_options
def getPositionsRectangular2d(cls, Nae, ae_spacing, height): sq = xp.floor(xp.sqrt(Nae)) x = xp.arange(Nae) % sq y = xp.floor(xp.arange(Nae) / sq) z = xp.repeat(xp.array(height), Nae) x *= ae_spacing y *= ae_spacing x -= xp.mean(x) y -= xp.mean(y) return x, y, z
def loss(self, samples_true, per_sample_hidden=100): """ Computes the difference in empirical distributions observed in `samples_true` and samples of visible units obtained from the model, using the same initial state. Parameters ---------- samples_true: array-like True samples of visible units to compare to. per_sample_hidden: int Number of `h` to "partially marginalize" over when computing p(v) = p(v | h) p(h). Returns ------- loss: float Difference between the two distributions. Notes ----- Can be very inaccurate estimate of how well the model is performing since one is NOT completely marginalizing out all hidden variables. """ k = per_sample_hidden # the loss is the log energy-difference between the p(v) and p(v_k), where `v_k` is the Gibbs sampled visible unit return np.mean([ self.free_energy(v) - self.free_energy(self.contrastive_divergence(v, k)[1]) for v in samples_true ])
def _sampleDistribution(params, numSamples, verbosity=0): """ Given the parameters of a distribution, generate numSamples points from it. This routine is mostly for testing. :returns: A numpy array of samples. """ if params.has_key("name"): if params["name"] == "normal": samples = numpy.random.normal(loc=params["mean"], scale=math.sqrt(params["variance"]), size=numSamples) elif params["name"] == "pareto": samples = numpy.random.pareto(params["alpha"], size=numSamples) elif params["name"] == "beta": samples = numpy.random.beta(a=params["alpha"], b=params["beta"], size=numSamples) else: raise ValueError("Undefined distribution: " + params["name"]) else: raise ValueError("Bad distribution params: " + str(params)) if verbosity > 0: print "\nSampling from distribution:", params print "After estimation, mean=", cupy.mean(samples), \ "var=", cupy.var(samples), "stdev=", math.sqrt(cupy.var(samples)) return samples
def corrmaxloc_gpu(ini_gpu, obj_gpu, win=1.0): xsize = ini_gpu.shape[0] ysize = ini_gpu.shape[1] initmp = (ini_gpu - cp.mean(ini_gpu))*win inifft = cp.fft.fft2(initmp) objtmp = (obj_gpu - cp.mean(obj_gpu))*win objfft = cp.fft.fft2(objtmp) corr_gpu = cp.real(cp.fft.fftshift(cp.fft.ifft2(cp.conj(objfft)*inifft))) maxid = np.where(corr_gpu == cp.max(corr_gpu)) shiftxy = [xsize//2-maxid[0][0], ysize//2-maxid[1][0]] return shiftxy, corr_gpu
def save_mean_and_variance(self): subset_size = self.num_observations_per_file new_total_size = self.total_images + subset_size co1 = self.total_images / new_total_size co2 = subset_size / new_total_size images = cp.asarray(self.images) subset_mean = cp.mean(images, axis=(0, 1)) subset_var = cp.var(images, axis=(0, 1)) new_dataset_mean = subset_mean if self.dataset_mean is None else co1 * self.dataset_mean + co2 * subset_mean new_dataset_var = subset_var if self.dataset_var is None else co1 * ( self.dataset_var + self.dataset_mean**2) + co2 * ( subset_var + subset_mean**2) - new_dataset_mean**2 # avoid negative value new_dataset_var[new_dataset_var < 0] = 0 self.dataset_var = new_dataset_var self.dataset_mean = new_dataset_mean self.dataset_std = cp.sqrt(self.dataset_var) cp.save(os.path.join(self.path, "mean.npy"), self.dataset_mean) cp.save(os.path.join(self.path, "std.npy"), self.dataset_std)
def corrmaxloc_gpu(ini_gpu, obj_gpu, win=1.0): cp.cuda.Device(0).use() ini_gpu = cp.array(ini_gpu, dtype='<f4') obj_gpu = cp.array(obj_gpu, dtype='<f4') xsize = ini_gpu.shape[0] ysize = ini_gpu.shape[1] initmp = (ini_gpu - cp.mean(ini_gpu)) * win inifft = cp.fft.fft2(initmp) objtmp = (obj_gpu - cp.mean(obj_gpu)) * win objfft = cp.fft.fft2(objtmp) corr_gpu = cp.real(cp.fft.fftshift(cp.fft.ifft2(cp.conj(objfft) * inifft))) maxid = cp.where(corr_gpu == cp.max(corr_gpu)) #最大值的索引 shiftxy = [xsize // 2 - maxid[0][0], ysize // 2 - maxid[1][0]] return shiftxy, corr_gpu
def infer(self): info = EvaluationMetrics(['Time/Step', 'Time/Item', 'Loss', 'Top1']) for idx, (data, label) in enumerate(self.val_loader): st = time.time() self.model.clear() data = cp.asarray(data) label = cp.asarray(label) output = self.model(data) loss = self.criterion(output, label) free_memory() elapsed = time.time() - st info.update('Time/Step', elapsed) info.update('Time/Item', elapsed / data.shape[0]) info.update('Loss', cp.asnumpy(loss)) output = output.reshape(np.prod(label.shape), -1) pred = cp.argmax(output, axis=-1).reshape(*label.shape) top1 = cp.mean(label == pred) info.update('Top1', cp.asnumpy(top1)) if self.logger is not None: self.logger.scalar_summary(info.avg, self.step, self.name)
def median(a, axis=0): """Compute the median of a CuPy array on the GPU.""" a = cp.asarray(a) if axis is None: sz = a.size else: sz = a.shape[axis] if sz % 2 == 0: szh = sz // 2 kth = [szh - 1, szh] else: kth = [(sz - 1) // 2] part = cp.partition(a, kth, axis=axis) if part.shape == (): # make 0-D arrays work return part.item() if axis is None: axis = 0 indexer = [slice(None)] * part.ndim index = part.shape[axis] // 2 if part.shape[axis] % 2 == 1: # index with slice to allow mean (below) to work indexer[axis] = slice(index, index + 1) else: indexer[axis] = slice(index - 1, index + 1) return cp.mean(part[indexer], axis=axis)
def forward(self, is_training=True): inputs = self.input_tensor # self.input_shape =inputs.shape gamma, beta = self.variables N, C, H, W = inputs.shape self.shape_field = tuple([i for i in range(2, inputs.ndim)]) x_group = cp.reshape(inputs, (N, self.G, C // self.G, H, W)) mean = cp.mean(x_group, axis=self.shape_field, keepdims=True) var = cp.var(x_group, axis=self.shape_field, keepdims=True) xgmu = x_group - mean sqrtvar = cp.sqrt(var + self.epsilon) x_group_norm = xgmu / sqrtvar x_norm = cp.reshape(x_group_norm, (N, C, H, W)) outputs = gamma.output_tensor * x_norm + beta.output_tensor self.cache = (xgmu, sqrtvar, x_norm) self.output_tensor = outputs if self.require_grads: self.grads = cp.zeros_like(self.output_tensor) super().forward(is_training)