Python numpy.square() Examples
The following are 30
code examples of numpy.square().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: panda_lift.py From robosuite with MIT License | 6 votes |
def _gripper_visualization(self): """ Do any needed visualization here. Overrides superclass implementations. """ # color the gripper site appropriately based on distance to cube if self.gripper_visualization: # get distance to cube cube_site_id = self.sim.model.site_name2id("cube") dist = np.sum( np.square( self.sim.data.site_xpos[cube_site_id] - self.sim.data.get_site_xpos("grip_site") ) ) # set RGBA for the EEF site here max_dist = 0.1 scaled = (1.0 - min(dist / max_dist, 1.)) ** 15 rgba = np.zeros(4) rgba[0] = 1 - scaled rgba[1] = scaled rgba[3] = 0.5 self.sim.model.site_rgba[self.eef_site_id] = rgba
Example #2
Source File: results_itrPCRNet.py From pointnet-registration-framework with MIT License | 6 votes |
def find_errors(gt_pose, final_pose): # Simple euler distand between translation part. gt_position = gt_pose[0:3] predicted_position = final_pose[0:3] translation_error = np.sqrt(np.sum(np.square(gt_position - predicted_position))) # Convert euler angles rotation matrix. gt_euler = gt_pose[3:6] pt_euler = final_pose[3:6] gt_mat = t3d.euler2mat(gt_euler[2],gt_euler[1],gt_euler[0],'szyx') pt_mat = t3d.euler2mat(pt_euler[2],pt_euler[1],pt_euler[0],'szyx') # Multiply inverse of one rotation matrix with another rotation matrix. error_mat = np.dot(pt_mat,np.linalg.inv(gt_mat)) _,angle = transforms3d.axangles.mat2axangle(error_mat) # Convert matrix to axis angle representation and that angle is error. return translation_error, abs(angle*(180/np.pi))
Example #3
Source File: nn_matching.py From deep_sort with GNU General Public License v3.0 | 6 votes |
def _pdist(a, b): """Compute pair-wise squared distance between points in `a` and `b`. Parameters ---------- a : array_like An NxM matrix of N samples of dimensionality M. b : array_like An LxM matrix of L samples of dimensionality M. Returns ------- ndarray Returns a matrix of size len(a), len(b) such that eleement (i, j) contains the squared distance between `a[i]` and `b[j]`. """ a, b = np.asarray(a), np.asarray(b) if len(a) == 0 or len(b) == 0: return np.zeros((len(a), len(b))) a2, b2 = np.square(a).sum(axis=1), np.square(b).sum(axis=1) r2 = -2. * np.dot(a, b.T) + a2[:, None] + b2[None, :] r2 = np.clip(r2, 0., float(np.inf)) return r2
Example #4
Source File: Gradients.py From sopt with MIT License | 6 votes |
def run(self): variables = self.init_variables self.s = np.zeros(self.variables_num) for i in range(self.epochs): grads = gradients(self.func,variables) self.s += np.square(grads) if self.func_type == gradients_config.func_type_min: variables -= self.lr*grads/(np.sqrt(self.s+self.eps)) else: variables += self.lr*grads/(np.sqrt(self.s+self.eps)) self.generations_points.append(variables) self.generations_targets.append(self.func(variables)) if self.func_type == gradients_config.func_type_min: self.global_best_target = np.min(np.array(self.generations_targets)) self.global_best_index = np.argmin(np.array(self.generations_targets)) self.global_best_point = self.generations_points[int(self.global_best_index)] else: self.global_best_target = np.max(np.array(self.generations_targets)) self.global_best_index = np.argmax(np.array(self.generations_targets)) self.global_best_point = self.generations_points[int(self.global_best_index)]
Example #5
Source File: Gradients.py From sopt with MIT License | 6 votes |
def run(self): variables = self.init_variables self.s = np.zeros(self.variables_num) for i in range(self.epochs): grads = gradients(self.func,variables) self.s = self.beta*self.s + (1-self.beta)*np.square(grads) if self.func_type == gradients_config.func_type_min: variables -= self.lr*grads/(np.sqrt(self.s+self.eps)) else: variables += self.lr*grads/(np.sqrt(self.s+self.eps)) self.generations_points.append(variables) self.generations_targets.append(self.func(variables)) if self.func_type == gradients_config.func_type_min: self.global_best_target = np.min(np.array(self.generations_targets)) self.global_best_index = np.argmin(np.array(self.generations_targets)) self.global_best_point = self.generations_points[int(self.global_best_index)] else: self.global_best_target = np.max(np.array(self.generations_targets)) self.global_best_index = np.argmax(np.array(self.generations_targets)) self.global_best_point = self.generations_points[int(self.global_best_index)]
Example #6
Source File: mxnet_export_test.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_square(): input1 = np.random.randint(1, 10, (2, 3)).astype("float32") ipsym = mx.sym.Variable("input1") square = mx.sym.square(data=ipsym) model = mx.mod.Module(symbol=square, data_names=['input1'], label_names=None) model.bind(for_training=False, data_shapes=[('input1', np.shape(input1))], label_shapes=None) model.init_params() args, auxs = model.get_params() params = {} params.update(args) params.update(auxs) converted_model = onnx_mxnet.export_model(square, params, [np.shape(input1)], np.float32, "square.onnx") sym, arg_params, aux_params = onnx_mxnet.import_model(converted_model) result = forward_pass(sym, arg_params, aux_params, ['input1'], input1) numpy_op = np.square(input1) npt.assert_almost_equal(result, numpy_op)
Example #7
Source File: Gradients.py From sopt with MIT License | 6 votes |
def run(self): variables = self.init_variables self.s = np.zeros(self.variables_num) self.m = np.zeros(self.variables_num) for i in range(self.epochs): grads = gradients(self.func,variables) self.m = self.beta1*self.m +(1-self.beta1)*grads self.s = self.beta2*self.s + (1-self.beta2)*np.square(grads) self.m /= (1-self.beta1**(i+1)) self.s /= (1-self.beta2**(i+1)) if self.func_type == gradients_config.func_type_min: variables -= self.lr*self.m/(np.sqrt(self.s+self.eps)) else: variables += self.lr*self.m/(np.sqrt(self.s+self.eps)) self.generations_points.append(variables) self.generations_targets.append(self.func(variables)) if self.func_type == gradients_config.func_type_min: self.global_best_target = np.min(np.array(self.generations_targets)) self.global_best_index = np.argmin(np.array(self.generations_targets)) self.global_best_point = self.generations_points[int(self.global_best_index)] else: self.global_best_target = np.max(np.array(self.generations_targets)) self.global_best_index = np.argmax(np.array(self.generations_targets)) self.global_best_point = self.generations_points[int(self.global_best_index)]
Example #8
Source File: graphTools.py From graph-neural-networks with GNU General Public License v3.0 | 6 votes |
def adjacencyToLaplacian(W): """ adjacencyToLaplacian: Computes the Laplacian from an Adjacency matrix Input: W (np.array): adjacency matrix Output: L (np.array): Laplacian matrix """ # Check that the matrix is square assert W.shape[0] == W.shape[1] # Compute the degree vector d = np.sum(W, axis = 1) # And build the degree matrix D = np.diag(d) # Return the Laplacian return D - W
Example #9
Source File: optimizers.py From lightnn with Apache License 2.0 | 6 votes |
def minimize(self, params, grads): shapes = [p.shape for p in params] # accumulate gradients accumulators = [np.zeros(shape) for shape in shapes] # accumulate updates delta_accumulators = [np.zeros(shape) for shape in shapes] self.weights = accumulators + delta_accumulators self.updates = [] for p, g in zip(params, grads): a = self.__accmulators.setdefault(id(p), np.zeros_like(p)) d_a = self.__delta_accumulators.setdefault(id(p), np.zeros_like(p)) # update accumulator new_a = self.rho * a + (1. - self.rho) * np.square(g) # use the new accumulator and the *old* delta_accumulator update = g * np.sqrt(d_a + self.epsilon) / np.sqrt(new_a + self.epsilon) p -= self.lr * update # update delta_accumulator new_d_a = self.rho * d_a + (1 - self.rho) * np.square(update) self.__accmulators[id(p)] = new_a self.__delta_accumulators[id(p)] = new_d_a super(Adadelta, self).update()
Example #10
Source File: graphTools.py From graph-neural-networks with GNU General Public License v3.0 | 6 votes |
def normalizeAdjacency(W): """ NormalizeAdjacency: Computes the degree-normalized adjacency matrix Input: W (np.array): adjacency matrix Output: A (np.array): degree-normalized adjacency matrix """ # Check that the matrix is square assert W.shape[0] == W.shape[1] # Compute the degree vector d = np.sum(W, axis = 1) # Invert the square root of the degree d = 1/np.sqrt(d) # And build the square root inverse degree matrix D = np.diag(d) # Return the Normalized Adjacency return D @ W @ D
Example #11
Source File: sawyer_lift.py From robosuite with MIT License | 6 votes |
def _gripper_visualization(self): """ Do any needed visualization here. Overrides superclass implementations. """ # color the gripper site appropriately based on distance to cube if self.gripper_visualization: # get distance to cube cube_site_id = self.sim.model.site_name2id("cube") dist = np.sum( np.square( self.sim.data.site_xpos[cube_site_id] - self.sim.data.get_site_xpos("grip_site") ) ) # set RGBA for the EEF site here max_dist = 0.1 scaled = (1.0 - min(dist / max_dist, 1.)) ** 15 rgba = np.zeros(4) rgba[0] = 1 - scaled rgba[1] = scaled rgba[3] = 0.5 self.sim.model.site_rgba[self.eef_site_id] = rgba
Example #12
Source File: profiler_ndarray.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_broadcast(): sample_num = 1000 def test_broadcast_to(): for i in range(sample_num): ndim = np.random.randint(1, 6) target_shape = np.random.randint(1, 11, size=ndim) shape = target_shape.copy() axis_flags = np.random.randint(0, 2, size=ndim) axes = [] for (axis, flag) in enumerate(axis_flags): if flag: shape[axis] = 1 dat = np.random.rand(*shape) - 0.5 numpy_ret = dat ndarray_ret = mx.nd.array(dat).broadcast_to(shape=target_shape) if type(ndarray_ret) is mx.ndarray.NDArray: ndarray_ret = ndarray_ret.asnumpy() assert (ndarray_ret.shape == target_shape).all() err = np.square(ndarray_ret - numpy_ret).mean() assert err < 1E-8 test_broadcast_to()
Example #13
Source File: profiler_ndarray.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_ndarray_elementwise(): np.random.seed(0) nrepeat = 10 maxdim = 4 all_type = [np.float32, np.float64, np.float16, np.uint8, np.int32] real_type = [np.float32, np.float64, np.float16] for repeat in range(nrepeat): for dim in range(1, maxdim): check_with_uniform(lambda x, y: x + y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x - y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x * y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x / y, 2, dim, type_list=real_type) check_with_uniform(lambda x, y: x / y, 2, dim, rmin=1, type_list=all_type) check_with_uniform(mx.nd.sqrt, 1, dim, np.sqrt, rmin=0) check_with_uniform(mx.nd.square, 1, dim, np.square, rmin=0) check_with_uniform(lambda x: mx.nd.norm(x).asscalar(), 1, dim, np.linalg.norm)
Example #14
Source File: SpectralClustering.py From sparse-subspace-clustering-python with MIT License | 6 votes |
def SpectralClustering(CKSym, n): # This is direct port of JHU vision lab code. Could probably use sklearn SpectralClustering. CKSym = CKSym.astype(float) N, _ = CKSym.shape MAXiter = 1000 # Maximum number of iterations for KMeans REPlic = 20 # Number of replications for KMeans DN = np.diag(np.divide(1, np.sqrt(np.sum(CKSym, axis=0) + np.finfo(float).eps))) LapN = identity(N).toarray().astype(float) - np.matmul(np.matmul(DN, CKSym), DN) _, _, vN = np.linalg.svd(LapN) vN = vN.T kerN = vN[:, N - n:N] normN = np.sqrt(np.sum(np.square(kerN), axis=1)) kerNS = np.divide(kerN, normN.reshape(len(normN), 1) + np.finfo(float).eps) km = KMeans(n_clusters=n, n_init=REPlic, max_iter=MAXiter, n_jobs=-1).fit(kerNS) return km.labels_
Example #15
Source File: stt_datagenerator.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def preprocess_sample_normalize(self, threadIndex, audio_paths, overwrite, return_dict): if len(audio_paths) > 0: audio_clip = audio_paths[0] feat = self.featurize(audio_clip=audio_clip, overwrite=overwrite) feat_squared = np.square(feat) count = float(feat.shape[0]) dim = feat.shape[1] if len(audio_paths) > 1: for audio_path in audio_paths[1:]: next_feat = self.featurize(audio_clip=audio_path, overwrite=overwrite) next_feat_squared = np.square(next_feat) feat_vertically_stacked = np.concatenate((feat, next_feat)).reshape(-1, dim) feat = np.sum(feat_vertically_stacked, axis=0, keepdims=True) feat_squared_vertically_stacked = np.concatenate( (feat_squared, next_feat_squared)).reshape(-1, dim) feat_squared = np.sum(feat_squared_vertically_stacked, axis=0, keepdims=True) count += float(next_feat.shape[0]) return_dict[threadIndex] = {'feat': feat, 'feat_squared': feat_squared, 'count': count}
Example #16
Source File: utils.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def pred_test(testing_data, exe, param_list=None, save_path=""): ret = numpy.zeros((testing_data.shape[0], 2)) if param_list is None: for i in range(testing_data.shape[0]): exe.arg_dict['data'][:] = testing_data[i, 0] exe.forward(is_train=False) ret[i, 0] = exe.outputs[0].asnumpy() ret[i, 1] = numpy.exp(exe.outputs[1].asnumpy()) numpy.savetxt(save_path, ret) else: for i in range(testing_data.shape[0]): pred = numpy.zeros((len(param_list),)) for j in range(len(param_list)): exe.copy_params_from(param_list[j]) exe.arg_dict['data'][:] = testing_data[i, 0] exe.forward(is_train=False) pred[j] = exe.outputs[0].asnumpy() ret[i, 0] = pred.mean() ret[i, 1] = pred.std()**2 numpy.savetxt(save_path, ret) mse = numpy.square(ret[:, 0] - testing_data[:, 0] **3).mean() return mse, ret
Example #17
Source File: graphTools.py From graph-neural-networks with GNU General Public License v3.0 | 6 votes |
def normalizeLaplacian(L): """ NormalizeLaplacian: Computes the degree-normalized Laplacian matrix Input: L (np.array): Laplacian matrix Output: normL (np.array): degree-normalized Laplacian matrix """ # Check that the matrix is square assert L.shape[0] == L.shape[1] # Compute the degree vector (diagonal elements of L) d = np.diag(L) # Invert the square root of the degree d = 1/np.sqrt(d) # And build the square root inverse degree matrix D = np.diag(d) # Return the Normalized Laplacian return D @ L @ D
Example #18
Source File: test_icp.py From pointnet-registration-framework with MIT License | 6 votes |
def find_errors(gt_pose, final_pose): # Simple euler distand between translation part. gt_position = gt_pose[0:3] predicted_position = final_pose[0:3] translation_error = np.sqrt(np.sum(np.square(gt_position - predicted_position))) # Convert euler angles rotation matrix. gt_euler = gt_pose[3:6] pt_euler = final_pose[3:6] gt_mat = t3d.euler2mat(gt_euler[2],gt_euler[1],gt_euler[0],'szyx') pt_mat = t3d.euler2mat(pt_euler[2],pt_euler[1],pt_euler[0],'szyx') # Multiply inverse of one rotation matrix with another rotation matrix. error_mat = np.dot(pt_mat,np.linalg.inv(gt_mat)) _,angle = transforms3d.axangles.mat2axangle(error_mat) # Convert matrix to axis angle representation and that angle is error. return translation_error, abs(angle*(180/np.pi)) # Store all the results. # if not os.path.exists(LOG_DIR): os.mkdir(LOG_DIR)
Example #19
Source File: helper_analysis.py From pointnet-registration-framework with MIT License | 6 votes |
def find_errors(self, gt_pose, final_pose): import transforms3d gt_position = gt_pose[0,0:3] predicted_position = final_pose[0,0:3] translation_error = np.sqrt(np.sum(np.square(gt_position - predicted_position))) print("Translation Error: {}".format(translation_error)) gt_euler = gt_pose[0,3:6] pt_euler = final_pose[0,3:6] gt_mat = t3d.euler2mat(gt_euler[2],gt_euler[1],gt_euler[0],'szyx') pt_mat = t3d.euler2mat(pt_euler[2],pt_euler[1],pt_euler[0],'szyx') error_mat = np.dot(pt_mat,np.linalg.inv(gt_mat)) _,angle = transforms3d.axangles.mat2axangle(error_mat) print("Rotation Error: {}".format(abs(angle*(180/np.pi)))) return translation_error, angle*(180/np.pi)
Example #20
Source File: dataset.py From neural-combinatorial-optimization-rl-tensorflow with MIT License | 6 votes |
def reward(tsptw_sequence,speed): # Convert sequence to tour (end=start) tour = np.concatenate((tsptw_sequence,np.expand_dims(tsptw_sequence[0],0))) # Compute tour length inter_city_distances = np.sqrt(np.sum(np.square(tour[:-1,:2]-tour[1:,:2]),axis=1)) distance = np.sum(inter_city_distances) # Compute develiry times at each city and count late cities elapsed_time = -10 late_cities = 0 for i in range(tsptw_sequence.shape[0]-1): travel_time = inter_city_distances[i]/speed tw_open = tour[i+1,2] tw_close = tour[i+1,3] elapsed_time += travel_time if elapsed_time <= tw_open: elapsed_time = tw_open elif elapsed_time > tw_close: late_cities += 1 # Reward return distance + 100000000*late_cities # Swap city[i] with city[j] in sequence
Example #21
Source File: mpi_moments.py From lirpg with MIT License | 5 votes |
def mpi_moments(x, axis=0, comm=None, keepdims=False): x = np.asarray(x) assert x.ndim > 0 mean, count = mpi_mean(x, axis=axis, comm=comm, keepdims=True) sqdiffs = np.square(x - mean) meansqdiff, count1 = mpi_mean(sqdiffs, axis=axis, comm=comm, keepdims=True) assert count1 == count std = np.sqrt(meansqdiff) if not keepdims: newshape = mean.shape[:axis] + mean.shape[axis+1:] mean = mean.reshape(newshape) std = std.reshape(newshape) return mean, std, count
Example #22
Source File: feature_verification.py From torch-toolbox with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update(self, embeddings0, embeddings1, labels): embeddings0, embeddings1, labels = map( to_numpy, (embeddings0, embeddings1, labels)) if self.dist_type is 'euclidean': diff = np.subtract(embeddings0, embeddings1) dists = np.sqrt(np.sum(np.square(diff), 1)) else: dists = 1 - np.sum(np.multiply(embeddings0, embeddings1), axis=1) / (np.linalg.norm(embeddings0, axis=1) * np.linalg.norm(embeddings1, axis=1)) self.dists.extend(dists) self.issame.extend(labels)
Example #23
Source File: mpi_running_mean_std.py From lirpg with MIT License | 5 votes |
def update(self, x): x = x.astype('float64') n = int(np.prod(self.shape)) totalvec = np.zeros(n*2+1, 'float64') addvec = np.concatenate([x.sum(axis=0).ravel(), np.square(x).sum(axis=0).ravel(), np.array([len(x)],dtype='float64')]) MPI.COMM_WORLD.Allreduce(addvec, totalvec, op=MPI.SUM) self.incfiltparams(totalvec[0:n].reshape(self.shape), totalvec[n:2*n].reshape(self.shape), totalvec[2*n])
Example #24
Source File: mpi_moments.py From HardRLWithYoutube with MIT License | 5 votes |
def mpi_moments(x, axis=0, comm=None, keepdims=False): x = np.asarray(x) assert x.ndim > 0 mean, count = mpi_mean(x, axis=axis, comm=comm, keepdims=True) sqdiffs = np.square(x - mean) meansqdiff, count1 = mpi_mean(sqdiffs, axis=axis, comm=comm, keepdims=True) assert count1 == count std = np.sqrt(meansqdiff) if not keepdims: newshape = mean.shape[:axis] + mean.shape[axis+1:] mean = mean.reshape(newshape) std = std.reshape(newshape) return mean, std, count
Example #25
Source File: normalizer.py From HardRLWithYoutube with MIT License | 5 votes |
def update(self, v): v = v.reshape(-1, self.size) with self.lock: self.local_sum += v.sum(axis=0) self.local_sumsq += (np.square(v)).sum(axis=0) self.local_count[0] += v.shape[0]
Example #26
Source File: normalizer.py From lirpg with MIT License | 5 votes |
def update(self, v): v = v.reshape(-1, self.size) with self.lock: self.local_sum += v.sum(axis=0) self.local_sumsq += (np.square(v)).sum(axis=0) self.local_count[0] += v.shape[0]
Example #27
Source File: running_mean_std.py From lirpg with MIT License | 5 votes |
def update_from_moments(self, batch_mean, batch_var, batch_count): delta = batch_mean - self.mean tot_count = self.count + batch_count new_mean = self.mean + delta * batch_count / tot_count m_a = self.var * (self.count) m_b = batch_var * (batch_count) M2 = m_a + m_b + np.square(delta) * self.count * batch_count / (self.count + batch_count) new_var = M2 / (self.count + batch_count) new_count = batch_count + self.count self.mean = new_mean self.var = new_var self.count = new_count
Example #28
Source File: mpi_running_mean_std.py From HardRLWithYoutube with MIT License | 5 votes |
def __init__(self, epsilon=1e-2, shape=()): self._sum = tf.get_variable( dtype=tf.float64, shape=shape, initializer=tf.constant_initializer(0.0), name="runningsum", trainable=False) self._sumsq = tf.get_variable( dtype=tf.float64, shape=shape, initializer=tf.constant_initializer(epsilon), name="runningsumsq", trainable=False) self._count = tf.get_variable( dtype=tf.float64, shape=(), initializer=tf.constant_initializer(epsilon), name="count", trainable=False) self.shape = shape self.mean = tf.to_float(self._sum / self._count) self.std = tf.sqrt( tf.maximum( tf.to_float(self._sumsq / self._count) - tf.square(self.mean) , 1e-2 )) newsum = tf.placeholder(shape=self.shape, dtype=tf.float64, name='sum') newsumsq = tf.placeholder(shape=self.shape, dtype=tf.float64, name='var') newcount = tf.placeholder(shape=[], dtype=tf.float64, name='count') self.incfiltparams = U.function([newsum, newsumsq, newcount], [], updates=[tf.assign_add(self._sum, newsum), tf.assign_add(self._sumsq, newsumsq), tf.assign_add(self._count, newcount)])
Example #29
Source File: mpi_running_mean_std.py From HardRLWithYoutube with MIT License | 5 votes |
def update(self, x): x = x.astype('float64') n = int(np.prod(self.shape)) totalvec = np.zeros(n*2+1, 'float64') addvec = np.concatenate([x.sum(axis=0).ravel(), np.square(x).sum(axis=0).ravel(), np.array([len(x)],dtype='float64')]) MPI.COMM_WORLD.Allreduce(addvec, totalvec, op=MPI.SUM) self.incfiltparams(totalvec[0:n].reshape(self.shape), totalvec[n:2*n].reshape(self.shape), totalvec[2*n])
Example #30
Source File: running_stat.py From HardRLWithYoutube with MIT License | 5 votes |
def test_running_stat(): for shp in ((), (3,), (3,4)): li = [] rs = RunningStat(shp) for _ in range(5): val = np.random.randn(*shp) rs.push(val) li.append(val) m = np.mean(li, axis=0) assert np.allclose(rs.mean, m) v = np.square(m) if (len(li) == 1) else np.var(li, ddof=1, axis=0) assert np.allclose(rs.var, v)