Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1269b1b
plot filters
mdeff May 4, 2017
a1aaf9b
gitignore: local configuration
mdeff May 4, 2017
76cebe6
makefile: clean notebook JSON
mdeff May 16, 2017
f3064f7
wikipedia: hyperlink graph
mdeff May 16, 2017
2ca8bc1
wikipedia: pages & page views
mdeff May 16, 2017
38c33e0
requirements: update
mdeff May 16, 2017
4c4f2b5
makefile: some echo implementations interpret \n
mdeff May 16, 2017
a270cda
wikipedia: compute average page views then select pages and load data
mdeff May 17, 2017
995c86b
wikipedia: match graph & activations
mdeff May 17, 2017
9d20460
layers.py: move layers in a class hierarchy
mdeff May 17, 2017
4955b8f
wikipedia: compute graph diameter and don't keep copies in memory
mdeff May 18, 2017
df328f7
trials: clean JSON metadata
mdeff May 18, 2017
b53363a
trials: play with graph-tool
mdeff May 18, 2017
ed98c9e
wikipedia: more and better visualizations
mdeff Jun 20, 2017
68fcb5d
structured sequence trial: TF input pipeline
mdeff Jun 26, 2017
987cf49
structured sequence: model & experiment
mdeff Jun 28, 2017
84c994b
to be finished and merged
mdeff May 18, 2017
f6ea33f
work in progress
mdeff Dec 18, 2017
4bf213d
Merge branch 'wikipedia'
mdeff Feb 21, 2020
aef2edc
keep original parameters
mdeff Feb 21, 2020
416f814
add title and disclaimer to mnist filter viz
mdeff Feb 21, 2020
49431c1
Merge branch 'plot-filters'
mdeff Feb 21, 2020
742ff46
Merge branch 'long-standing'
mdeff Feb 21, 2020
eb245e6
make clean
mdeff Feb 21, 2020
f731393
move all experiments in single folder
mdeff Feb 21, 2020
3265c79
requirements: add version numbers and dependencies
mdeff Feb 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ __pycache__/
# IPython checkpoints
.ipynb_checkpoints/

# Datasets
# Local configuration
.env
.python-version

# Data
data/

# Tensorflow summaries
# Tensorflow summaries & model parameters
summaries/

# Model parameters
checkpoints/
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ setting.

[usage]: http://nbviewer.jupyter.org/github/mdeff/cnn_graph/blob/outputs/usage.ipynb

## Repository organization

* The models (the introduced model and some reference models) are contained in [models.py](models.py).
* Various side functions are implemented in [graph.py](graph.py), [coarsening.py](coarsening.py) and [utils.py](utils.py).
* We did experiments on three datasets: MNIST ([notebook](mnist.ipynb)), 20NEWS ([notebook](20news.ipynb)) and RCV1 ([notebook](rcv1.ipynb)).
* TensorBoard summaries are saved in the `summaries` folder.
* Model parameters are saved in the `checkpoints` folder.
* Data is placed in the `data` folder.
* [MNIST](http://yann.lecun.com/exdb/mnist/) is downloaded automatically.
* [20NEWS](http://qwone.com/~jason/20Newsgroups/) (`20news-bydate.tar.gz`) is downloaded automatically.
* [RCV1](http://trec.nist.gov/data/reuters/reuters.html) should be downloaded manually and placed in TODO.
* [pre-trained word2vec embeddings](https://code.google.com/archive/p/word2vec/) (`GoogleNews-vectors-negative300.bin.gz`).
* Wikipedia graph and activations are available here. Please cite .. if you use it.
* The [trials](trials) folder contains various small experiences in the form of IPython notebooks.
1. [Learning graph filters][trial1]: first experiments on learning
synthesized graph filters through observations of filtered and source
graph signals. The Chebychev and Lanczos methods as well as optimization
methods are compared there.
2. [Classification][trial2]: learning filters who extract good features for
classification.
3. [TensorFlow][trial3]: first experience with TensorFlow.
4. [Coarsening][trial4]: implementation of the Graclus coarsening algorithm
and comparison with a previous matlab implementation.
* A [makefile](makefile) who runs every notebook as a sanity check. It only runs the code, there is no check on the results.

[trial1]: h

## License & co

The code in this repository is released under the terms of the [MIT license](LICENSE.txt).
Expand Down
179 changes: 124 additions & 55 deletions nips2016/mnist.ipynb → experiments/1_mnist.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
Expand All @@ -25,9 +23,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"flags = tf.app.flags\n",
Expand All @@ -54,9 +50,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"def grid_graph(m, corners=False):\n",
Expand Down Expand Up @@ -96,9 +90,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.examples.tutorials.mnist import input_data\n",
Expand All @@ -116,7 +108,7 @@
"val_data = coarsening.perm_data(val_data, perm)\n",
"test_data = coarsening.perm_data(test_data, perm)\n",
"print('Execution time: {:.2f}s'.format(time.process_time() - t_start))\n",
"del perm"
"#del perm"
]
},
{
Expand All @@ -129,9 +121,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"#model = fc1()\n",
Expand All @@ -157,9 +147,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"common = {}\n",
Expand All @@ -178,9 +166,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"if True:\n",
Expand All @@ -203,9 +189,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# Common hyper-parameters for networks with one convolutional layer.\n",
Expand All @@ -223,9 +207,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"if True:\n",
Expand All @@ -234,33 +216,31 @@
" params['dir_name'] += name\n",
" params['filter'] = 'fourier'\n",
" params['K'] = [L[0].shape[0]]\n",
" model_perf.test(models.cgcnn(L, **params), name, params,\n",
" model_f = models.cgcnn(L, **params)\n",
" model_perf.test(model_f, name, params,\n",
" train_data, train_labels, val_data, val_labels, test_data, test_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"if True:\n",
" name = 'sgconv_softmax'\n",
" params = common.copy()\n",
" params['dir_name'] += name\n",
" params['filter'] = 'spline'\n",
" model_perf.test(models.cgcnn(L, **params), name, params,\n",
" model_s = models.cgcnn(L, **params)\n",
" model_perf.test(model_s, name, params,\n",
" train_data, train_labels, val_data, val_labels, test_data, test_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# With 'chebyshev2' and 'b2relu', it corresponds to cgcnn2_2(L[0], F=10, K=20).\n",
Expand All @@ -271,16 +251,15 @@
" params['filter'] = 'chebyshev5'\n",
"# params['filter'] = 'chebyshev2'\n",
"# params['brelu'] = 'b2relu'\n",
" model_perf.test(models.cgcnn(L, **params), name, params,\n",
" model_c = models.cgcnn(L, **params)\n",
" model_perf.test(model_c, name, params,\n",
" train_data, train_labels, val_data, val_labels, test_data, test_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"# Common hyper-parameters for LeNet5-like networks.\n",
Expand All @@ -298,9 +277,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Architecture of TF MNIST conv model (LeNet-5-like).\n",
Expand All @@ -319,9 +296,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"if True:\n",
Expand All @@ -336,9 +311,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"if True:\n",
Expand All @@ -353,9 +326,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"model_perf.show()"
Expand All @@ -364,16 +335,114 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"def plot_filters(coeffs):\n",
" fig = plt.figure(figsize=(15,5))\n",
" ax = fig.add_subplot(1,1,1)\n",
" for coeff in coeffs:\n",
" c = eval(coeff)\n",
" label = '{}: L={:1.2e}, |dL|={:1.2e}'.format(coeff, L(c), np.linalg.norm(dL(X,Y,c)))\n",
" ax.plot(lamb, c, '.-', label=label)\n",
"# np.testing.assert_allclose(np.linalg.norm(c)**2, E, rtol=1e-2)\n",
" ax.set_xlim(lamb[0], lamb[-1])\n",
" ax.set_title('Filter coefficients, M={}, N={}, eps={}'.format(M, N, eps))\n",
" ax.set_xlabel('frequency')\n",
" ax.set_ylabel('amplitude')\n",
" ax.legend(loc='best')\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if False:\n",
" grid_params = {}\n",
" data = (train_data, train_labels, val_data, val_labels, test_data, test_labels)\n",
" utils.grid_search(params, grid_params, *data, model=lambda x: models.cgcnn(L,**x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Filter visualization (experimental)\n",
"\n",
"**Disclaimer**: left as is, not sure if it works. To be checked before usage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1,2,3])\n",
"print(a[[2,0,1]][:2])\n",
"\n",
"print(mnist.train.labels[0])\n",
"plt.imshow(mnist.train.images[0].reshape(28,28))\n",
"\n",
"a = np.random.permutation(range(len(perm)))\n",
"b = a[perm]\n",
"c = b[idx]\n",
"assert np.all(a == c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = model_f\n",
"\n",
"sess = tf.Session(graph=model.graph)\n",
"filename = tf.train.latest_checkpoint(os.path.join('..', 'checkpoints', model.dir_name))\n",
"model.op_saver.restore(sess, filename)\n",
"var = model.graph.get_tensor_by_name('conv1/weights' + ':0')\n",
"val = sess.run(var)\n",
"sess.close()\n",
"\n",
"lamb, U = graph.fourier(L[0])\n",
"\n",
"#filters = model_f.get_var('conv1/weights')\n",
"filters = val\n",
"#filt_fourier = filt\n",
"\n",
"i = 6\n",
"\n",
"print(filters.shape)\n",
"plt.figure(figsize=(15,5))\n",
"plt.plot(lamb, filters[:,i,0])\n",
"\n",
"print(lamb[0], lamb[-1])\n",
"\n",
"filt = U.dot(filters[:,i,0])\n",
"plt.figure(figsize=(15,5))\n",
"plt.plot(filt)\n",
"\n",
"print(len(lamb)-28**2)\n",
"indices = np.array(perm) >= 28**2\n",
"\n",
"print(train_data[0,indices])\n",
"\n",
"idx = np.argsort(perm)\n",
"filt = filt[idx]\n",
"plt.figure(figsize=(15,5))\n",
"plt.plot(train_data[0,perm])\n",
"\n",
"plt.figure(figsize=(15,5))\n",
"img = train_data[0,idx][:28**2].reshape(28,28)\n",
"plt.imshow(train_data[0,idx][:28**2].reshape(28,28))\n",
"assert np.allclose(train_data[0,idx][:28**2].reshape(28,28), mnist.train.images[0].reshape(28,28))"
]
}
],
"metadata": {
Expand Down
Loading