Skip to content

Commit

Permalink
function coarsen shared in coarsening module
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Oct 13, 2016
1 parent 7baf90f commit 3a7261a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 78 deletions.
28 changes: 2 additions & 26 deletions 20news.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -223,37 +223,13 @@
},
"outputs": [],
"source": [
"def coarsen(A, levels):\n",
" graphs, parents = coarsening.metis(A, levels)\n",
" perms = coarsening.compute_perm(parents)\n",
"\n",
" laplacians = []\n",
" for i,A in enumerate(graphs):\n",
" M, M = A.shape\n",
"\n",
" # No self-connections.\n",
" if True:\n",
" A = A.tocoo()\n",
" A.setdiag(0)\n",
"\n",
" if i < levels:\n",
" A = coarsening.perm_adjacency(A, perms[i])\n",
"\n",
" A = A.tocsr()\n",
" A.eliminate_zeros()\n",
" Mnew, Mnew = A.shape\n",
" print('Layer {0}: M_{0} = |V| = {1} nodes ({2} added), |E| = {3} edges'.format(i, Mnew, Mnew-M, A.nnz//2))\n",
"\n",
" L = graph.laplacian(A, normalized=FLAGS.normalized_laplacian)\n",
" laplacians.append(L)\n",
" return laplacians, perms[0] if len(perms) > 0 else None\n",
"\n",
"t_start = time.process_time()\n",
"dist, idx = graph.distance_sklearn_metrics(graph_data, k=FLAGS.number_edges, metric=FLAGS.metric)\n",
"A = graph.adjacency(dist, idx)\n",
"print(\"{} > {} edges\".format(A.nnz//2, FLAGS.number_edges*graph_data.shape[0]//2))\n",
"A = graph.replace_random_edges(A, 0)\n",
"L, perm = coarsen(A, FLAGS.coarsening_levels)\n",
"graphs, perm = coarsening.coarsen(A, levels=FLAGS.coarsening_levels, self_connections=False)\n",
"L = [graph.laplacian(A, normalized=True) for A in graphs]\n",
"print('Execution time: {:.2f}s'.format(time.process_time() - t_start))\n",
"#graph.plot_spectrum(L)\n",
"#del graph_data, A, dist, idx"
Expand Down
29 changes: 29 additions & 0 deletions coarsening.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@
import scipy.sparse


def coarsen(A, levels, self_connections=False):
"""
Coarsen a graph, represented by its adjacency matrix A, at multiple
levels.
"""
graphs, parents = metis(A, levels)
perms = compute_perm(parents)

for i, A in enumerate(graphs):
M, M = A.shape

if not self_connections:
A = A.tocoo()
A.setdiag(0)

if i < levels:
A = perm_adjacency(A, perms[i])

A = A.tocsr()
A.eliminate_zeros()
graphs[i] = A

Mnew, Mnew = A.shape
print('Layer {0}: M_{0} = |V| = {1} nodes ({2} added),'
'|E| = {3} edges'.format(i, Mnew, Mnew-M, A.nnz//2))

return graphs, perms[0] if levels > 0 else None


def metis(W, levels, rid=None):
"""
Coarsen a graph multiple times using the METIS algorithm.
Expand Down
28 changes: 2 additions & 26 deletions mnist.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,11 @@
" print(\"{} > {} edges\".format(A.nnz//2, FLAGS.number_edges*m**2//2))\n",
" return A\n",
"\n",
"def coarsen(A, levels):\n",
" graphs, parents = coarsening.metis(A, levels)\n",
" perms = coarsening.compute_perm(parents)\n",
"\n",
" laplacians = []\n",
" for i,A in enumerate(graphs):\n",
" M, M = A.shape\n",
"\n",
" # No self-connections.\n",
" if True:\n",
" A = A.tocoo()\n",
" A.setdiag(0)\n",
"\n",
" if i < levels:\n",
" A = coarsening.perm_adjacency(A, perms[i])\n",
"\n",
" A = A.tocsr()\n",
" A.eliminate_zeros()\n",
" Mnew, Mnew = A.shape\n",
" print('Layer {0}: M_{0} = |V| = {1} nodes ({2} added), |E| = {3} edges'.format(i, Mnew, Mnew-M, A.nnz//2))\n",
"\n",
" L = graph.laplacian(A, normalized=FLAGS.normalized_laplacian)\n",
" laplacians.append(L)\n",
" return laplacians, perms[0] if len(perms) > 0 else None\n",
"\n",
"t_start = time.process_time()\n",
"A = grid_graph(28, corners=False)\n",
"A = graph.replace_random_edges(A, 0)\n",
"L, perm = coarsen(A, FLAGS.coarsening_levels)\n",
"graphs, perm = coarsening.coarsen(A, levels=FLAGS.coarsening_levels, self_connections=False)\n",
"L = [graph.laplacian(A, normalized=True) for A in graphs]\n",
"print('Execution time: {:.2f}s'.format(time.process_time() - t_start))\n",
"graph.plot_spectrum(L)\n",
"del A"
Expand Down
28 changes: 2 additions & 26 deletions rcv1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -227,37 +227,13 @@
},
"outputs": [],
"source": [
"def coarsen(A, levels):\n",
" graphs, parents = coarsening.metis(A, levels)\n",
" perms = coarsening.compute_perm(parents)\n",
"\n",
" laplacians = []\n",
" for i,A in enumerate(graphs):\n",
" M, M = A.shape\n",
"\n",
" # No self-connections.\n",
" if True:\n",
" A = A.tocoo()\n",
" A.setdiag(0)\n",
"\n",
" if i < levels:\n",
" A = coarsening.perm_adjacency(A, perms[i])\n",
"\n",
" A = A.tocsr()\n",
" A.eliminate_zeros()\n",
" Mnew, Mnew = A.shape\n",
" print('Layer {0}: M_{0} = |V| = {1} nodes ({2} added), |E| = {3} edges'.format(i, Mnew, Mnew-M, A.nnz//2))\n",
"\n",
" L = graph.laplacian(A, normalized=FLAGS.normalized_laplacian)\n",
" laplacians.append(L)\n",
" return laplacians, perms[0] if len(perms) > 0 else None\n",
"\n",
"t_start = time.process_time()\n",
"dist, idx = graph.distance_lshforest(graph_data.astype(np.float64), k=FLAGS.number_edges, metric=FLAGS.metric)\n",
"A = graph.adjacency(dist.astype(np.float32), idx)\n",
"print(\"{} > {} edges\".format(A.nnz//2, FLAGS.number_edges*graph_data.shape[0]//2))\n",
"A = graph.replace_random_edges(A, 0)\n",
"L, perm = coarsen(A, FLAGS.coarsening_levels)\n",
"graphs, perm = coarsening.coarsen(A, levels=FLAGS.coarsening_levels, self_connections=False)\n",
"L = [graph.laplacian(A, normalized=True) for A in graphs]\n",
"print('Execution time: {:.2f}s'.format(time.process_time() - t_start))\n",
"#graph.plot_spectrum(L)\n",
"#del graph_data, A, dist, idx"
Expand Down

0 comments on commit 3a7261a

Please sign in to comment.