def visualize_grid(Xs, ubound=255.0, padding=1): """ Reshape a 4D tensor of image data to a grid for easy visualization. Inputs: - Xs: Data of shape (N, H, W, C) - ubound: Output grid will have values scaled to the range [0, ubound] - padding: The number of blank pixels between elements of the grid """ (N, H, W, C) = Xs.shape grid_size = int(ceil(sqrt(N))) grid_height = H * grid_size + padding * (grid_size - 1) grid_width = W * grid_size + padding * (grid_size - 1) grid = np.zeros((grid_height, grid_width, C)) next_idx = 0 y0, y1 = 0, H for y in range(grid_size): x0, x1 = 0, W for x in range(grid_size): if next_idx < N: img = Xs[next_idx] low, high = np.min(img), np.max(img) grid[y0:y1, x0:x1] = ubound * (img - low) / (high - low) # grid[y0:y1, x0:x1] = Xs[next_idx] next_idx += 1 x0 += W + padding x1 += W + padding y0 += H + padding y1 += H + padding # grid_max = np.max(grid) # grid_min = np.min(grid) # grid = ubound * (grid - grid_min) / (grid_max - grid_min) return grid
def compute_distances_two_loops(self, X): """ Compute the distance between each test point in X and each training point in self.X_train using a nested loop over both the training data and the test data. Inputs: - X: A numpy array of shape (num_test, D) containing test data. Returns: - dists: A numpy array of shape (num_test, num_train) where dists[i, j] is the Euclidean distance between the ith test point and the jth training point. """ num_test = X.shape[0] num_train = self.X_train.shape[0] dists = np.zeros((num_test, num_train)) for i in range(num_test): for j in range(num_train): ##################################################################### # TODO: # # Compute the l2 distance between the ith test point and the jth # # training point, and store the result in dists[i, j]. You should # # not use a loop over dimension. # ##################################################################### #dists[i, j] = sum((self.X_train[j] - X[i]) ** 2) ** 0.5 dists[i, j] = np.sqrt(np.sum((self.X_train[j] - X[i]) ** 2)) #pass ##################################################################### # END OF YOUR CODE # ##################################################################### return dists
def test_rotor_between_lines(self): # Make a big array of data n_mvs = 1000 mv_a_array = np.array([random_line() for i in range(n_mvs)]) mv_b_array = np.array([random_line() for i in range(n_mvs)]) mv_c_array = np.zeros(mv_b_array.shape) mv_d_array = np.zeros(mv_b_array.shape) print('Starting kernel') t = time.time() blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) rotor_between_lines_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(mv_a_array.shape[0]): mv_d_array[i, :] = val_rotor_between_lines(mv_a_array[i, :], mv_b_array[i, :]) print(time.time() - t) np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def maxN(a, n): if not isinstance(a, list) or not isinstance(a, ndarray): return False if n>len(a): n=len(a) b = a[:] for i in range(len(a)): b[i] = (b[i], i) b.sort(key = lambda x: x[0], reverse = True) return array([b[i][0] for i in range(n)]), array(map(int, [b[i][1] for i in range(n)]))
def test_square_root_of_rotor_kernel(self): n_mvs = 500 mv_a_list = [random_line() for i in range(n_mvs)] mv_b_list = [random_line() for i in range(n_mvs)] rotor_list = [rotor_between_objects(C1,C2) for C1,C2 in zip(mv_a_list,mv_b_list)] rotor_list_array = np.array(rotor_list) rotor_root_array = np.zeros(rotor_list_array.shape) mv_d_array = np.zeros(rotor_list_array.shape) print('Starting kernel') t = time.time() blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) square_root_of_rotor_kernel[griddim, blockdim](rotor_list_array, rotor_root_array) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(rotor_list_array.shape[0]): mv_d_array[i, :] = square_roots_of_rotor(rotor_list[i])[0].value print(time.time() - t) np.testing.assert_almost_equal(rotor_root_array, mv_d_array)
def forkJoin(logDir, numProcesses, fun, *someArgs): # pylint: disable=invalid-name,missing-docstring,missing-return-doc # pylint: disable=missing-return-type-doc def showFile(fn): # pylint: disable=invalid-name,missing-docstring print("==== %s ====" % fn) print() with open(fn) as f: for line in f: print(line.rstrip()) print() # Fork a bunch of processes print("Forking %d children..." % numProcesses) ps = [] # pylint: disable=invalid-name for i in range(numProcesses): p = multiprocessing.Process( # pylint: disable=invalid-name target=redirectOutputAndCallFun, args=[logDir, i, fun, someArgs], name="Parallel process " + str(i)) p.start() ps.append(p) # Wait for them all to finish, and splat their outputs for i in range(numProcesses): p = ps[i] # pylint: disable=invalid-name print("=== Waiting for child #%d (%d) to finish... ===" % (i, p.pid)) p.join() print("=== Child process #%d exited with code %d ===" % (i, p.exitcode)) print() showFile(logFileName(logDir, i, "out")) showFile(logFileName(logDir, i, "err")) print()
def run_job(L): chunk_size = 5000 list_binary = Parallel(n_jobs=cmd_args.data_gen_threads, verbose=50)( delayed(process_chunk)(L[start:start + chunk_size]) for start in range(0, len(L), chunk_size)) #process_chunk(L[start: start + chunk_size] for start in range(0, len(L), chunk_size)) all_onehot = np.zeros((len(L), cmd_args.max_decode_steps, DECISION_DIM), dtype=np.byte) all_masks = np.zeros((len(L), cmd_args.max_decode_steps, DECISION_DIM), dtype=np.byte) for start, b_pair in zip(range(0, len(L), chunk_size), list_binary): all_onehot[start:start + chunk_size, :, :] = b_pair[0] all_masks[start:start + chunk_size, :, :] = b_pair[1] f_smiles = '.'.join(cmd_args.smiles_file.split('/')[-1].split('.')[0:-1]) out_file = '%s/%s-%d.h5' % (cmd_args.data_save_dir, f_smiles, cmd_args.skip_deter) h5f = h5py.File(out_file, 'w') h5f.create_dataset('x', data=all_onehot) h5f.create_dataset('masks', data=all_masks) h5f.close()
def test_rotor_between_objects(self): # Make a big array of data n_mvs = 1000 generator_list = [random_point_pair, random_line, random_circle, \ random_sphere, random_plane] for generator in generator_list: mv_a_array = np.array([generator() for i in range(n_mvs)], dtype=np.double) mv_b_array = np.array([generator() for i in range(n_mvs)], dtype=np.double) mv_c_array = np.zeros(mv_b_array.shape, dtype=np.double) mv_d_array = np.zeros(mv_b_array.shape, dtype=np.double) print('Starting kernel') t = time.time() blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) rotor_between_objects_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(mv_a_array.shape[0]): mv_a = cf.MultiVector(self.layout, mv_a_array[i, :]) mv_b = cf.MultiVector(self.layout, mv_b_array[i, :]) mv_d_array[i, :] = rotor_between_objects(mv_a, mv_b).value print(time.time() - t) np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def forkJoin(logDir, numProcesses, fun, *someArgs): # pylint: disable=invalid-name,missing-docstring def showFile(fn): # pylint: disable=invalid-name,missing-docstring print("==== %s ====" % fn) print() with io.open(str(fn), "r", encoding="utf-8", errors="replace") as f: for line in f: print(line.rstrip()) print() # Fork a bunch of processes print("Forking %s children..." % str(numProcesses)) ps = [] # pylint: disable=invalid-name for i in range(numProcesses): p = multiprocessing.Process( # pylint: disable=invalid-name target=redirectOutputAndCallFun, args=[logDir, i, fun, someArgs], name="Parallel process " + str(i)) p.start() ps.append(p) # Wait for them all to finish, and splat their outputs for i in range(numProcesses): p = ps[i] # pylint: disable=invalid-name print("=== Waiting for child #%s (%s) to finish... ===" % (str(i), str(p.pid))) p.join() print("=== Child process #%s exited with code %s ===" % (str(i), str(p.exitcode))) print() showFile(log_name(logDir, i, "out")) showFile(log_name(logDir, i, "err")) print()
def test_sequential_rotor_estimation_kernel(self): n_mvs = 1000 query_model = [random_line() for i in range(n_mvs)] r = random_rotation_translation_rotor() reference_model = [(r*a*~r).normal() for a in query_model] query_model_array = np.array(query_model) reference_model_array = np.array(reference_model) n_samples = 100 n_objects_per_sample = 100 output = np.zeros((n_samples, 32)) mv_d_array = np.zeros(output.shape) print('Starting kernel') t = time.time() output, cost_array = sequential_rotor_estimation_cuda(reference_model_array, query_model_array, n_samples, n_objects_per_sample) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(output.shape[0]): mv_d_array[i, :] = sequential_object_rotor_estimation_convergence_detection(reference_model, query_model)[0].value print(time.time() - t) print(cost_array) np.testing.assert_almost_equal(output, mv_d_array, 5)
def compute_distances_two_loops(self, X): """ Compute the distance between each test point in X and each training point in self.X_train using a nested loop over both the training data and the test data. Inputs: - X: A numpy array of shape (num_test, D) containing test data. Returns: - dists: A numpy array of shape (num_test, num_train) where dists[i, j] is the Euclidean distance between the ith test point and the jth training point. """ num_test = X.shape[0] num_train = self.X_train.shape[0] dists = np.zeros((num_test, num_train)) for i in range(num_test): for j in range(num_train): ##################################################################### # TODO: # 计算测试集中第i个样本和训练集中第j个样本的L2距离 # 保存在dists[i,j]中 ##################################################################### # X.shape = (num_test,D) # X_train.shape = (num_train, D) dists[i][j] = np.sqrt(np.sum(np.square(X[i] - self.X_train[j]))) ##################################################################### # END OF YOUR CODE ##################################################################### return dists
def test_rotor_cost(self): # Make a big array of data n_mvs = 500 mv_a_array = np.array([random_line() for i in range(n_mvs)]) mv_b_array = np.array([random_line() for i in range(n_mvs)]) mv_c_array = np.zeros(n_mvs) mv_d_array = np.zeros(n_mvs) # Multiply together a load of them and see how long it takes print('Starting kernel') t = time.time() blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) cost_line_to_line_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(mv_a_array.shape[0]): mv_d_array[i] = val_object_cost_function(mv_a_array[i, :], mv_b_array[i, :]) print(time.time() - t) np.testing.assert_almost_equal(mv_c_array, mv_d_array, 5)
def test_REFORM_cuda(self): object_generator = random_line n_objects_per_cluster = 20 objects_per_sample = 10 n_samples = 100 iterations = 100 n_runs = 5 error_count = 0 for i in range(n_runs): # Make a cluster cluster_objects = generate_random_object_cluster(n_objects_per_cluster, object_generator, max_cluster_trans=0.5, max_cluster_rot=np.pi / 3) # Rotate and translate the cluster disturbance_rotor = random_rotation_translation_rotor(maximum_translation=2, maximum_angle=np.pi / 8) target = [apply_rotor(c, disturbance_rotor).normal() for c in cluster_objects] labels, costs, r_est = REFORM_cuda(target, cluster_objects, n_samples, objects_per_sample, iterations, mutation_probability=None) try: assert np.sum(labels == range(n_objects_per_cluster)) == n_objects_per_cluster except: print(disturbance_rotor) print(r_est) error_count += 1 print('Correct fraction: ', 1.0 - error_count / n_runs)
def test_sequential_rotor_estimation_kernel(self): n_mvs = 1000 query_model = [random_line() for i in range(n_mvs)] r = random_rotation_translation_rotor() reference_model = [(r*a*~r).normal() for a in query_model] query_model_array = np.array(query_model) reference_model_array = np.array(reference_model) n_samples = 100 n_objects_per_sample = 100 output = np.zeros((n_samples, 32)) mv_d_array = np.zeros(output.shape) print('Starting kernel') t = time.time() # blockdim = 64 # griddim = int(math.ceil(n_mvs / blockdim)) # sequential_rotor_estimation_kernel[griddim, blockdim](reference_model_array, query_model_array, output) output, cost_array = sequential_rotor_estimation_cuda(reference_model_array, query_model_array, n_samples, n_objects_per_sample) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(output.shape[0]): mv_d_array[i, :] = sequential_object_rotor_estimation_convergence_detection(reference_model, query_model)[0].value print(time.time() - t) print(cost_array) np.testing.assert_almost_equal(output, mv_d_array, 5)
def test_ip(self): n_mvs = 500 mv_a_array = np.array([self.layout.randomMV().value for i in range(n_mvs)]) mv_b_array = np.array([self.layout.randomMV().value for i in range(n_mvs)]) mv_c_array = np.zeros(mv_b_array.shape) mv_d_array = np.zeros(mv_b_array.shape) print('Starting kernel') t = time.time() blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) ip_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(mv_a_array.shape[0]): mv_d_array[i, :] = self.layout.imt_func(mv_a_array[i, :], mv_b_array[i, :]) print(time.time() - t) np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_sparse_multiply(self): algebras = [Cl(i) for i in [4]] + [conformalize(Cl(3)[0])] # For all the algebras we are interested in for alg in algebras: layout = alg[0] # Make two random multivectors a = layout.randomMV() b = layout.randomMV() # Project the multivectors to the grades required grades_possibilities = [] for r in range(1, len(layout.sig)): possible_grades = [ list(m) for m in list( itertools.combinations(range(len(layout.sig)), r)) ] grades_possibilities += possible_grades for i, grades_a in enumerate(grades_possibilities): sparse_mv_a = sum([a(k) for k in grades_a]) for j, grades_b in enumerate(grades_possibilities): sparse_mv_b = sum([b(k) for k in grades_b]) # Compute results gp = layout.gmt_func_generator(grades_a=grades_a, grades_b=grades_b) result_sparse = gp(sparse_mv_a.value, sparse_mv_b.value) result_dense = (sparse_mv_a * sparse_mv_b).value # Check they are the same testing.assert_almost_equal(result_sparse, result_dense) print(j + i * len(grades_possibilities), len(grades_possibilities)**2)
def test_rotor_between_objects(self): # Make a big array of data n_mvs = 1000 generator_list = [random_point_pair, random_line, random_circle, \ random_sphere, random_plane] for generator in generator_list: mv_a_array = np.array([generator() for i in range(n_mvs)], dtype=np.double) mv_b_array = np.array([generator() for i in range(n_mvs)], dtype=np.double) mv_c_array = np.zeros(mv_b_array.shape, dtype=np.double) mv_d_array = np.zeros(mv_b_array.shape, dtype=np.double) print('Starting kernel') t = time.time() blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) rotor_between_objects_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(mv_a_array.shape[0]): mv_a = cf.MultiVector(self.layout, mv_a_array[i, :]) mv_b = cf.MultiVector(self.layout, mv_b_array[i, :]) mv_d_array[i, :] = rotor_between_objects(mv_a, mv_b).value print(time.time() - t) print(generator.__name__) np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_dorst_norm_val(self): # Make a big array of data n_mvs = 500 mv_a_list = [random_line() for i in range(n_mvs)] mv_b_list = [random_line() for i in range(n_mvs)] c_list = [1 + b*a for a,b in zip(mv_a_list, mv_b_list)] sigma_list = [c*~c for c in c_list] mv_sigma_array = np.array(sigma_list, dtype=np.double) mv_c_array = np.zeros(mv_sigma_array.shape[0], dtype=np.double) mv_d_array = np.zeros(mv_sigma_array.shape[0], dtype=np.double) print('Starting kernel') t = time.time() blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) dorst_norm_val_kernel[griddim, blockdim](mv_sigma_array, mv_c_array) end_time = time.time() - t print('Kernel finished') print(end_time) # Now do the non cuda kernel t = time.time() for i in range(len(mv_a_list)): sigma = sigma_list[i] k_value = dorst_norm_val(sigma.value) mv_d_array[i] = k_value print(time.time() - t) np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_object_set_cost(self): n_mvs = 100 generator_list = [random_point_pair, random_line, random_circle, \ random_sphere, random_plane] for generator in generator_list: mv_a_array = [generator() for i in range(n_mvs)] mv_b_array = [generator() for i in range(n_mvs)] print(mv_a_array) print('Starting kernel') t = time.time() mv_c_array = object_set_cost_cuda_mvs(mv_a_array, mv_b_array) end_time = time.time() - t print('Kernel finished') print(end_time) t = time.time() mv_d_array = object_set_cost_matrix(mv_a_array, mv_b_array, object_type='generic') print(time.time() - t) for i in range(n_mvs): for j in range(n_mvs): try: assert abs(mv_d_array[i,j]-mv_c_array[i,j])/abs(mv_d_array[i,j]) < 10**(-6) except: print(generator.__name__) if generator.__name__ == 'random_line': print(val_object_cost_function(mv_a_array[i].value, mv_b_array[j].value)) print(mv_d_array[i,j]) print(mv_c_array[i,j]) print(abs(mv_d_array[i,j]-mv_c_array[i,j])/abs(mv_d_array[i,j])) assert abs(mv_d_array[i,j]-mv_c_array[i,j])/abs(mv_d_array[i,j]) < 10**(-6)
def pencil_mark(row, col): """ Marks the value of grid[i][j] element in it's row, column and sub-grid. Parameters: - i (int): grid row. - j (int): grid column. Returns: The more completed version of the grid. """ if self.values[row][col] != 0: helper[row][col] = [self.values[row][col]] # Remove from same row for j in range(grid_size): if j != col: if self.values[row][col] in helper[row][j]: helper[row][j].remove(self.values[row][col]) # Remove from same column for i in range(grid_size): if i != row: if self.values[row][col] in helper[i][col]: helper[i][col].remove(self.values[row][col]) # Remove from same subgrid h = self.make_index(row) k = self.make_index(col) for i in range(h, h + 3): for j in range(k, k + 3): if i != row and j != col: if self.values[row][col] in helper[i][j]: helper[i][j].remove(self.values[row][col])
def test_object_set_cost(self): n_mvs = 100 generator_list = [random_point_pair, random_line, random_circle, \ random_sphere, random_plane] for generator in generator_list: mv_a_array = [generator() for i in range(n_mvs)] mv_b_array = [generator() for i in range(n_mvs)] print(mv_a_array) print('Starting kernel') t = time.time() mv_c_array = object_set_cost_cuda_mvs(mv_a_array, mv_b_array) end_time = time.time() - t print('Kernel finished') print(end_time) t = time.time() mv_d_array = object_set_cost_matrix(mv_a_array, mv_b_array, object_type='generic') print(time.time() - t) try: np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3) except: print(mv_c_array[0, :]) print(mv_d_array[0, :]) np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
def test_file_with_custom_encoder(self): """Should allow a custom encoder""" col_count = 3 row_count = 100 header = ['col1,col2,col3'] data = [[float(cnum + rnum) for cnum in range(col_count)] for rnum in range(row_count)] rows = map(lambda x: ','.join(map(str, x)), data) test_data = '\n'.join(header + rows) with patch('bcipy.acquisition.datastream.generator.open', mock_open(read_data=test_data), create=True): gen = file_data_generator(filename='foo', header_row=1, encoder=CustomEncoder()) generated_data = [next(gen) for _ in range(row_count)] for _count, record in generated_data: self.assertEqual(len(record), col_count) self.assertEqual(generated_data[0][0], 1) self.assertEqual(generated_data[99][0], 100)
def draw_circles(self): self.clabpt = self.fit_text('00000', self.cellw / 7, padding=0) y = self.margin[2] + self.cellh / float(2) for xi in range(2, len(self.xcoo)): x = self.margin[0] + sum(self.xcoo[:xi]) + self.cellw / float(2) for i, sz in enumerate(self.xcircsize[xi - 2]): self.circle(x, y, sz, self.rgb1(self.xcols[xi - 2][i])) self.label(str(self.xsizes[xi - 2][i]), *tuple([ a + b * sz for a, b in zip([x, y], list(self.clabels[i])) ]), pt=self.clabpt, c=self.colors['embl_gray875'], center=True, vcenter=True, rot=-45) x = self.margin[0] + self.cellw / float(2) for yi in range(2, len(self.ycoo)): y = self.margin[2] + sum(self.ycoo[:yi]) + self.cellh / float(2) for i, sz in enumerate(self.ycircsize[yi - 2]): self.circle(x, y, sz, self.rgb1(self.ycols[yi - 2][i])) self.label(str(self.ysizes[yi - 2][i]), *tuple([ a + b * sz for a, b in zip([x, y], list(self.clabels[i])) ]), pt=self.clabpt, c=self.colors['embl_gray875'], center=True, vcenter=True, rot=-45) # intersections for yi in range(2, len(self.ycoo)): y = self.margin[2] + sum(self.ycoo[:yi]) + self.cellh / float(2) for xi in range(2, len(self.xcoo)): x = self.margin[0] + \ sum(self.xcoo[:xi]) + self.cellw / float(2) for i, sz in enumerate( self.intersects[(self.xlabs[xi - 2], self.ylabs[yi - 2])]['ssize']): self.circle( x, y, sz, self.rgb1( self.intersects[(self.xlabs[xi - 2], self.ylabs[yi - 2])]['color'][i])) self.label( str(self.intersects[(self.xlabs[xi - 2], self.ylabs[yi - 2])]['size'][i]), *tuple([ a + b * sz for a, b in zip([x, y], list(self.clabels[i])) ]), pt=self.clabpt, c=self.colors['embl_gray875'], center=True, vcenter=True, rot=-45) '''
def svm_loss_naive(W, X, y, reg): """ Structured SVM loss function, naive implementation (with loops). Inputs have dimension D, there are C classes, and we operate on minibatches of N examples. Inputs: - W: A numpy array of shape (D, C) containing weights. - X: A numpy array of shape (N, D) containing a minibatch of data. - y: A numpy array of shape (N,) containing training labels; y[i] = c means that X[i] has label c, where 0 <= c < C. - reg: (float) regularization strength Returns a tuple of: - loss as single float - gradient with respect to weights W; an array of same shape as W """ dW = np.zeros(W.shape) # initialize the gradient as zero # compute the loss and the gradient num_classes = W.shape[1] num_train = X.shape[0] loss = 0.0 for i in range(num_train): scores = X[i].dot(W) correct_class_score = scores[y[i]] for j in range(num_classes): if j == y[i]: continue margin = scores[j] - correct_class_score + 1 # note delta = 1 if margin > 0: loss += margin #compute gradient dW[:,j] += X[i,:].T dW[:,y[i]] -= X[i,:].T # Right now the loss is a sum over all training examples, but we want it # to be an average instead so we divide by num_train. loss /= num_train dW /= num_train # Add regularization to the loss. loss += reg * np.sum(W * W) ############################################################################# # TODO: # # Compute the gradient of the loss function and store it dW. # # Rather that first computing the loss and then computing the derivative, # # it may be simpler to compute the derivative at the same time that the # # loss is being computed. As a result you may need to modify some of the # # code above to compute the gradient. # ############################################################################# dW += reg * W return loss, dW
def svm_loss_naive(W, X, y, reg): """ Structured SVM loss function, naive implementation (with loops). Inputs have dimension D, there are C classes, and we operate on minibatches of N examples. Inputs: - W: A numpy array of shape (D, C) containing weights. - X: A numpy array of shape (N, D) containing a minibatch of data. - y: A numpy array of shape (N,) containing training labels; y[i] = c means that X[i] has label c, where 0 <= c < C. - reg: (float) regularization strength Returns a tuple of: - loss as single float - gradient with respect to weights W; an array of same shape as W """ dW = np.zeros(W.shape) # initialize the gradient as zero # compute the loss and the gradient num_classes = W.shape[1] num_train = X.shape[0] loss = 0.0 for i in range(num_train): scores = X[i].dot(W) correct_class_score = scores[y[i]] for j in range(num_classes): if j == y[i]: continue margin = scores[j] - correct_class_score + 1 # note delta = 1 if margin > 0: loss += margin #compute gradient dW[:, j] += X[i, :].T dW[:, y[i]] -= X[i, :].T # Right now the loss is a sum over all training examples, but we want it # to be an average instead so we divide by num_train. loss /= num_train dW /= num_train # Add regularization to the loss. loss += reg * np.sum(W * W) ############################################################################# # TODO: # # Compute the gradient of the loss function and store it dW. # # Rather that first computing the loss and then computing the derivative, # # it may be simpler to compute the derivative at the same time that the # # loss is being computed. As a result you may need to modify some of the # # code above to compute the gradient. # ############################################################################# dW += reg * W return loss, dW
def maxN(a, n): if not isinstance(a, list) or not isinstance(a, ndarray): return False if n > len(a): n = len(a) b = a[:] for i in range(len(a)): b[i] = (b[i], i) b.sort(key=lambda x: x[0], reverse=True) return array([b[i][0] for i in range(n) ]), array(map(int, [b[i][1] for i in range(n)]))
def update_position(self): for i in range(Nd): for j in range(Nd): self.position[i][j] = self.position[i][j] + self.velocity[i][j] if self.position[i][j] >= 9.5: self.position[i][j] = round(self.position[i][j]) - 9 if self.position[i][j] < .5: self.position[i][j] = round(self.position[i][j]) + 9 self.mutate(0.5)
def is_subgrid_duplicate(self, row, column, value): """ Check duplicate in a subgrid. """ h = self.make_index(row) k = self.make_index(column) for i in range(h, h + 3): for j in range(k, k + 3): if self.values[i][j] == value: return True return False
def seed(self, Nc, given): self.candidates = [] # Determine the valid values. helper = Candidate() # The helper is a 3d list, two of them determine the position and the other one is a list of valid numbers helper.values = [[[] for _ in range(0, Nd)] for _ in range(0, Nd)] for row in range(0, Nd): for column in range(0, Nd): for value in range(1, 10): # Check if it is allowed to put a number into the position # And check if the new number will cause duplicate in this row/column/block if (given.values[row][column] == 0) and not ( given.is_column_duplicate(column, value) or given.is_block_duplicate(row, column, value) or given.is_row_duplicate(row, value)): helper.values[row][column].append(value) elif given.values[row][column] != 0: helper.values[row][column].append( given.values[row][column]) break # Seed a new population. for _ in range(0, Nc): g = Candidate() for i in range(0, Nd): # New row in candidate. row = np.zeros(Nd) # Fill in the givens. for j in range(0, Nd): # New column j value in row i. # We can only fill the position where the number in it is 0 in given puzzle if given.values[i][j] != 0: row[j] = given.values[i][j] elif given.values[i][j] == 0: row[j] = helper.values[i][j][random.randint( 0, len(helper.values[i][j]) - 1)] k = 0 while len(list(set(row))) != Nd: k += 1 # Set a threshold to stop if k > 500000: return 0 for j in range(0, Nd): if given.values[i][j] == 0: row[j] = helper.values[i][j][random.randint( 0, len(helper.values[i][j]) - 1)] g.values[i] = row self.candidates.append(g) # Compute the fitness of all candidates in the population. self.update_fitness() return 1
def crossover_rows(self, row1, row2): size = grid_size child1_row = [0 for i in range(size)] child2_row = [0 for i in range(size)] index1 = 0 # Availabel index of child1 index2 = 0 # Availabel index of child2 current = 0 # Current index to traverse begin = True while (0 in child1_row) or (0 in child2_row): # i, ii if begin == True: child1_row[index1] = row1[current] index1 += 1 begin = False # iii next = row2[current] # iv if next not in child1_row: # v child1_row[index1] = next index1 += 1 else: # vi child2_row[index2] = next index2 += 1 # vii if index2 == size: # xii # print("Child 1: ", child1_row) # print("Child 2: ", child2_row) break else: # viii current += 1 next = row1[current] # ix if next in child1_row: # xi child2_row[index2] = next index2 += 1 else: # x child1_row[index1] = next index1 += 1 return child1_row, child2_row
def softmax_loss_naive(W, X, y, reg): """ Softmax loss function, naive implementation (with loops) Inputs have dimension D, there are C classes, and we operate on minibatches of N examples. Inputs: - W: A numpy array of shape (D, C) containing weights. - X: A numpy array of shape (N, D) containing a minibatch of data. - y: A numpy array of shape (N,) containing training labels; y[i] = c means that X[i] has label c, where 0 <= c < C. - reg: (float) regularization strength Returns a tuple of: - loss as single float - gradient with respect to weights W; an array of same shape as W """ # Initialize the loss and gradient to zero. loss = 0.0 dW = np.zeros_like(W) num_train, _ = X.shape _, num_classes = W.shape ############################################################################# # TODO: Compute the softmax loss and its gradient using explicit loops. # # Store the loss in loss and the gradient in dW. If you are not careful # # here, it is easy to run into numeric instability. Don't forget the # # regularization! # ############################################################################# for i in range(num_train): margin = X[i].dot(W) shifted_margin = margin - np.max(margin) loss += (-shifted_margin[y[i]] + np.log(np.sum(np.exp(shifted_margin)))) for j in range(num_classes): prob = np.exp(shifted_margin[j]) / np.sum(np.exp(shifted_margin)) if j == y[i]: dW[:, j] -= X[i] dW[:, j] += prob * X[i] ############################################################################# # END OF YOUR CODE # ############################################################################# loss /= num_train dW /= num_train loss += 0.5 * reg * np.sum(W * W) dW += reg * W return loss, dW
def test_adjoint(self): n_mvs = 1000 mv_a_array = np.array([self.layout.randomMV().value for i in range(n_mvs)]) mv_d_array = np.zeros(mv_a_array.shape) mv_c_array = np.zeros(mv_a_array.shape) blockdim = 64 griddim = int(math.ceil(n_mvs / blockdim)) adjoint_kernel[griddim, blockdim](mv_a_array, mv_c_array) for i in range(mv_a_array.shape[0]): mv_d_array[i, :] = self.layout.adjoint_func(mv_a_array[i, :]) np.testing.assert_almost_equal(mv_c_array, mv_d_array, 5)
def func(self, threadpool): t = lambda:None nq = self.nqueues for i in range(self.batchsize * nq): threadpool.apply_async(t, queue=i % nq) evs = [] for q in range(nq): ev = threading.Event() evs.append(ev) threadpool.apply_async(ev.set, queue=q) for ev in evs: if not ev.isSet(): ev.wait(1)
def update_velocity(self): for i in range(Nd): for j in range(Nd): t1 = self.weights[0] * self.velocity[i][j] t2 = self.weights[1] * random.uniform(0, 1) * ( self.personal_best_position[i][j] - self.given[i][j]) t3 = self.weights[2] * random.uniform(0, 1) * ( self.global_best_position[i][j] - self.given[i][j]) self.velocity[i][j] = t1 + t2 + t3 if self.velocity[i][j] > 3: self.velocity[i][j] = -3 if self.velocity[i][j] < -3: self.velocity[i][j] = 3
def main(): seed = 19260817 print(cmd_args) random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) if cmd_args.ae_type == 'vae': ae = MolVAE() elif cmd_args.ae_type == 'autoenc': ae = MolAutoEncoder() else: raise Exception('unknown ae type %s' % cmd_args.ae_type) if cmd_args.mode == 'gpu': ae = ae.cuda() if cmd_args.saved_model is not None and cmd_args.saved_model != '': if os.path.isfile(cmd_args.saved_model): print('loading model from %s' % cmd_args.saved_model) ae.load_state_dict(torch.load(cmd_args.saved_model)) assert cmd_args.encoder_type == 'cnn' optimizer = optim.Adam(ae.parameters(), lr=cmd_args.learning_rate) lr_scheduler = ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=3, verbose=True, min_lr=0.0001) train_binary, train_masks, valid_binary, valid_masks = load_data() print('num_train: %d\tnum_valid: %d' % (train_binary.shape[0], valid_binary.shape[0])) sample_idxes = list(range(train_binary.shape[0])) best_valid_loss = None for epoch in range(cmd_args.num_epochs): random.shuffle(sample_idxes) avg_loss = loop_dataset('train', ae, sample_idxes, train_binary, train_masks, optimizer) print('>>>>average \033[92mtraining\033[0m of epoch %d: loss %.5f perp %.5f kl %.5f' % (epoch, avg_loss[0], avg_loss[1], avg_loss[2])) if epoch % 1 == 0: valid_loss = loop_dataset('valid', ae, list(range(valid_binary.shape[0])), valid_binary, valid_masks) print(' average \033[93mvalid\033[0m of epoch %d: loss %.5f perp %.5f kl %.5f' % (epoch, valid_loss[0], valid_loss[1], valid_loss[2])) valid_loss = valid_loss[0] lr_scheduler.step(valid_loss) torch.save(ae.state_dict(), cmd_args.save_dir + '/epoch-%d.model' % epoch) if best_valid_loss is None or valid_loss < best_valid_loss: best_valid_loss = valid_loss print('----saving to best model since this is the best valid loss so far.----') torch.save(ae.state_dict(), cmd_args.save_dir + '/epoch-best.model')
def test_noniterators_produce_lists(self): l = range(10) self.assertTrue(isinstance(l, list)) l2 = zip(l, list('ABCDE')*2) self.assertTrue(isinstance(l2, list)) double = lambda x: x*2 l3 = map(double, l) self.assertTrue(isinstance(l3, list)) is_odd = lambda x: x % 2 == 1 l4 = filter(is_odd, range(10)) self.assertEqual(l4, [1, 3, 5, 7, 9]) self.assertTrue(isinstance(l4, list))
def run_job(L): chunk_size = 5000 list_binary = Parallel(n_jobs=cmd_args.data_gen_threads, verbose=50)( delayed(process_chunk)(L[start: start + chunk_size]) for start in range(0, len(L), chunk_size) ) all_onehot = np.zeros((len(L), cmd_args.max_decode_steps, DECISION_DIM), dtype=np.byte) all_masks = np.zeros((len(L), cmd_args.max_decode_steps, DECISION_DIM), dtype=np.byte) for start, b_pair in zip( range(0, len(L), chunk_size), list_binary ): all_onehot[start: start + chunk_size, :, :] = b_pair[0] all_masks[start: start + chunk_size, :, :] = b_pair[1] return all_onehot, all_masks
def test_grade_obj(self): algebras = [Cl(i) for i in [3, 4]] + [conformalize(Cl(3)[0])] for alg in algebras: layout = alg[0] for i in range(len(layout.sig)+1): mv = layout.randomMV()(i) assert i == grade_obj(mv)
def test_find_rotor_aligning_vectors(self): """ Currently fails, needs to be dug into """ from clifford.g3c import layout e1 = layout.blades['e1'] e2 = layout.blades['e2'] from clifford.tools.g3 import random_euc_mv, random_rotation_rotor, rotor_align_vecs u_list = [random_euc_mv() for i in range(50)] for i in range(100): r = random_rotation_rotor() v_list = [r*u*~r for u in u_list] r_2 = rotor_align_vecs(u_list, v_list) print(r_2) print(r) testing.assert_almost_equal(r.value, r_2.value)
def test_convex_hull_conformal_flats(self): from clifford.tools.g3c import random_conformal_point from clifford.tools.point_processing import GAConvexHull point_list = [random_conformal_point() for i in range(100)] hull = GAConvexHull(point_list, hull_dims=3) flats = hull.conformal_flats()
def decode(self, chunk, use_random=True): ''' Args: chunk: A numpy array of dtype np.float32, of shape (n, latent_dim) Return: a list of `n` strings, each being a SMILES. ''' raw_logits = self.pred_raw_logits(chunk) result_list = [] for i in range(raw_logits.shape[1]): pred_logits = raw_logits[:, i, :] walker = ConditionalProgramDecoder(np.squeeze(pred_logits), use_random) new_t = Node('program') try: self.tree_decoder.decode(new_t, walker) sampled = get_program_from_tree(new_t) except Exception as ex: print('Warning, decoder failed with', ex) # failed. output a random junk. import random, string sampled = 'JUNK' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(256)) result_list.append(sampled) return result_list
def test_speed(self): algebras = range(2,9) print() # So that the first number is on a new line for i in algebras: t_start = time.time() Cl(i) t_end = time.time() print(i, t_end - t_start)
def test_left_multiplication_matrix(self): algebras = [Cl(i) for i in [3, 4]] + [conformalize(Cl(3)[0])] for alg in algebras: layout = alg[0] for i in range(1000): mv = layout.randomMV() mv2 = layout.randomMV() np.testing.assert_almost_equal(np.matmul(layout.get_left_gmt_matrix(mv),mv2.value), (mv*mv2).value)
def test_rotor_mask(self): for alg in self.algebras: layout, blades = alg rotor_m = layout.rotor_mask rotor_m_t = np.zeros(layout.gaDims) for _ in range(10): rotor_m_t += 100*np.abs(layout.randomRotor().value) np.testing.assert_almost_equal(rotor_m_t > 0, rotor_m)
def run(self, N=10, popsize=50, maxgens=100,nbits=1): from easydev import progressbar pb = progressbar.progress_bar(N, interval=1) for i in range(0,N): s = SteadyCont(self.pknmodel, self.data) s.optimise(maxgens=maxgens, popsize=popsize, dimension_bits=nbits) self.mr.add_scores(s.scores) pb.animate(i+1) self.mr.interval = popsize
def _handle_set_of_flexi_range_fields( self, range_based_flexi_fields, bit_field_space, routing_keys_and_masks, application_keys_and_masks, inputs, position): """ Take a set of flexible fields which are range based and deduce\ the routing keys and masks for the set. :param range_based_flexi_fields: the set of range based flexible fields :param bit_field_space: the bit field space :param routing_keys_and_masks: set of routing keys and masks built\ from previous iterations :param application_keys_and_masks: the application keys and masks\ from previous iterations :param inputs: parameters used by the bit field to get keys :param position: the position within the set (used for exit condition) :return: routing keys and application keys for the flexible field set """ for value in range(0, range_based_flexi_fields[position].instance_n_keys): inputs[range_based_flexi_fields[position].name] = value if position < len(range_based_flexi_fields): # routing keys and masks routing_key = bit_field_space(**inputs).get_value( tag=SUPPORTED_TAGS.ROUTING.name) routing_mask = bit_field_space(**inputs).get_mask( tag=SUPPORTED_TAGS.ROUTING.name) routing_keys_and_masks.append(BaseKeyAndMask(routing_key, routing_mask)) # application keys and masks application_key = bit_field_space(**inputs).get_value( tag=SUPPORTED_TAGS.APPLICATION.name) application_mask = bit_field_space(**inputs).get_mask( tag=SUPPORTED_TAGS.APPLICATION.name) application_keys_and_masks.append(BaseKeyAndMask( application_key, application_mask)) else: # not at the end, keep iterating but add the results from # the iterations before going back upwards position += 1 other_routing_keys_and_masks, \ other_application_keys_and_masks = \ self._handle_set_of_flexi_range_fields( range_based_flexi_fields, bit_field_space, routing_keys_and_masks, application_keys_and_masks, inputs, position) # add keys before going upwards routing_keys_and_masks.extend(other_routing_keys_and_masks) application_keys_and_masks.extend( other_application_keys_and_masks) # exit this iteration return routing_keys_and_masks, application_keys_and_masks
def test_GADelaunay_facets(self): from clifford.g3c import up, blades, layout e1 = blades['e1'] e2 = blades['e2'] einf = layout.einf from clifford.tools.g3c import random_conformal_point, project_points_to_plane from clifford.tools.point_processing import GADelaunay point_list = [random_conformal_point() for i in range(100)] point_list_flat = project_points_to_plane(point_list, (up(0)^up(e1)^up(e2)^einf).normal()) hull = GADelaunay(point_list_flat, hull_dims=2) facets = hull.conformal_facets()
def test_line_set_cost(self): n_mvs = 50 mv_a_array = [random_line() for i in range(n_mvs)] mv_b_array = [random_line() for i in range(n_mvs)] print(mv_a_array) print('Starting kernel') t = time.time() mv_c_array = line_set_cost_cuda_mvs(mv_a_array, mv_b_array) end_time = time.time() - t print('Kernel finished') print(end_time) t = time.time() mv_d_array = object_set_cost_matrix(mv_a_array, mv_b_array, object_type='generic') print(time.time() - t) try: np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3) except: print(mv_c_array[0,:]) print(mv_d_array[0,:]) np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
def batch_decode(raw_logits, use_random, decode_times): size = (raw_logits.shape[1] + 7) / 8 logit_lists = [] for i in range(0, raw_logits.shape[1], size): if i + size < raw_logits.shape[1]: logit_lists.append(raw_logits[:, i : i + size, :]) else: logit_lists.append(raw_logits[:, i : , :]) result_list = Parallel(n_jobs=-1)(delayed(decode_chunk)(logit_lists[i], use_random, decode_times) for i in range(len(logit_lists))) return [_1 for _0 in result_list for _1 in _0]
def test_quaternion_conversions(self): """ Bidirectional rotor - quaternion test. This needs work but is a reasonable start """ from clifford.g3c import layout from clifford.tools.g3 import rotor_to_quaternion, quaternion_to_rotor from clifford.tools.g3c import random_rotation_rotor for i in range(1000): rotor = random_rotation_rotor() quaternion = rotor_to_quaternion(rotor) rotor_return = quaternion_to_rotor(quaternion) testing.assert_almost_equal(rotor.value, rotor_return.value)
def test_inverse(self): for layout, blades in self.algebras: a = 1. + blades['e1'] self.assertRaises(ValueError, lambda x: 1/x, a) for i in range(10): a = randomMV(layout, grades=[0, 1]) denominator = float(a(1)**2-a(0)**2) if abs(denominator) > 1.e-5: a_inv = (-a(0)/denominator) + ((1./denominator) * a(1)) self.assert_(abs((a * a_inv)-1.) < 1.e-11) self.assert_(abs((a_inv * a)-1.) < 1.e-11) self.assert_(abs(a_inv - 1./a) < 1.e-11)
def test_right_multiplication_matrix(self): algebras = [Cl(i) for i in [3, 4]] + [conformalize(Cl(3)[0])] for alg in algebras: layout = alg[0] for i in range(1000): a = layout.randomMV() b = layout.randomMV() b_right = val_get_right_gmt_matrix(b.value, layout.k_list, layout.l_list, layout.m_list, layout.mult_table_vals, layout.gaDims) res = a*b res2 = layout.MultiVector([email protected]) testing.assert_almost_equal(res.value, res2.value)