def build_graph(cls, args): number_edges = args.number_edges metric = args.metric normalized_laplacian = args.normalized_laplacian coarsening_levels = args.coarsening_levels def grid_graph(m, corners=False): z = graph.grid(m) # compute pairwise distance dist, idx = graph.distance_sklearn_metrics(z, k=number_edges, metric=metric) A = graph.adjacency(dist, idx) # build adjacent matrix # Connections are only vertical or horizontal on the grid. # Corner vertices are connected to 2 neightbors only. if corners: A = A.toarray() A[A < A.max()/1.5] = 0 A = scipy.sparse.csr_matrix(A) print('{} edges'.format(A.nnz)) print("{} > {} edges".format(A.nnz//2, number_edges*m**2//2)) return A g = grid_graph(28, corners=False) g = graph.replace_random_edges(g, 0) graphs, perm = coarsening.coarsen(g, levels=coarsening_levels, self_connections=False) laplacians = [graph.laplacian(g, normalized=True) for g in graphs] cls.perm = perm cls.graphs = graphs cls.laplacians = laplacians
def build_graph(cls, args): number_edges = args.number_edges metric = args.metric normalized_laplacian = args.normalized_laplacian coarsening_levels = args.coarsening_levels data_dir = 'data/20news' embed_path = os.path.join(data_dir, 'embeddings.npy') graph_data = np.load(embed_path).astype(np.float32) dist, idx = graph.distance_sklearn_metrics(graph_data, k=number_edges, metric=metric) adj_matrix = graph.adjacency(dist, idx) print("{} > {} edges".format(adj_matrix.nnz // 2, number_edges * graph_data.shape[0] // 2)) adj_matrix = graph.replace_random_edges(adj_matrix, 0) graphs, perm = coarsening.coarsen(adj_matrix, levels=coarsening_levels, self_connections=False) laplacians = [ graph.laplacian(g, normalized=normalized_laplacian) for g in graphs ] cls.perm = perm cls.graphs = graphs cls.laplacians = laplacians
if corners: import scipy.sparse A = A.toarray() A[A < A.max()/1.5] = 0 A = scipy.sparse.csr_matrix(A) print('{} edges'.format(A.nnz)) print("{} > {} edges".format(A.nnz//2, FLAGS.number_edges*m**2//2)) return A t_start = time.process_time() A = grid_graph(28, corners=False) graph.replace_random_edges(A, 0) graphs, perm = coarsening.coarsen(A, levels=FLAGS.coarsening_levels, self_connections=False) L = [graph.laplacian(A, normalized=True,renormalized=True) for A in graphs] #print(L.dtype) #print(L) print('Execution time: {:.2f}s'.format(time.process_time() - t_start)) graph.plot_spectrum(L) print("DONE") del A from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets(FLAGS.dir_data, one_hot=False) train_data = mnist.train.images.astype(np.float32)