annotate deep/crbm/mnist_crbm.py @ 595:da46a62ce402

submitted JMLR pdf
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Tue, 05 Oct 2010 15:07:33 -0400
parents ed1c0830681e
children
rev   line source
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
1 #!/usr/bin/python
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
2
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
3 import sys
344
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
4 import os, os.path
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
5
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
6 # do this before importing custom modules
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
7 from mnist_config import *
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
8
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
9 if not (len(sys.argv) > 1 and sys.argv[1] in \
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
10 ('test_jobman_entrypoint', 'run_local')):
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
11 # in those cases don't use isolated code, use dev code
344
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
12 print "Running experiment isolation code"
342
b25ad1670ff7 Slight bug
fsavard
parents: 340
diff changeset
13 isolate_experiment()
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
14
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
15 import numpy as N
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
16
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
17 import theano
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
18 import theano.tensor as T
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
19
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
20 from crbm import CRBM, ConvolutionParams
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
21
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
22 import pylearn, pylearn.version
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
23 from pylearn.datasets import MNIST
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
24 from pylearn.io.image_tiling import tile_raster_images
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
25
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
26 import Image
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
27
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
28 from pylearn.io.seriestables import *
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
29 import tables
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
30
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
31 import ift6266
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
32
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
33 import utils
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
34
344
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
35 def setup_workdir():
343
82dae7c46046 Last few bugfixes before launching
fsavard
parents: 342
diff changeset
36 if not os.path.exists(IMAGE_OUTPUT_DIR):
82dae7c46046 Last few bugfixes before launching
fsavard
parents: 342
diff changeset
37 os.mkdir(IMAGE_OUTPUT_DIR)
344
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
38 if not os.path.exists(IMAGE_OUTPUT_DIR):
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
39 print "For some reason mkdir(IMAGE_OUTPUT_DIR) failed!"
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
40 sys.exit(1)
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
41 print "Created image output dir"
343
82dae7c46046 Last few bugfixes before launching
fsavard
parents: 342
diff changeset
42 elif os.path.isfile(IMAGE_OUTPUT_DIR):
82dae7c46046 Last few bugfixes before launching
fsavard
parents: 342
diff changeset
43 print "IMAGE_OUTPUT_DIR is not a directory!"
82dae7c46046 Last few bugfixes before launching
fsavard
parents: 342
diff changeset
44 sys.exit(1)
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
45
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
46 #def filename_from_time(suffix):
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
47 # import datetime
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
48 # return str(datetime.datetime.now()) + suffix + ".png"
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
49
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
50 def jobman_entrypoint(state, channel):
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
51 # record mercurial versions of each package
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
52 pylearn.version.record_versions(state,[theano,ift6266,pylearn])
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
53 channel.save()
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
54
344
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
55 setup_workdir()
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
56
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
57 crbm = MnistCrbm(state)
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
58 crbm.train()
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
59
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
60 return channel.COMPLETE
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
61
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
62 class MnistCrbm(object):
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
63 def __init__(self, state):
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
64 self.state = state
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
65
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
66 if TEST_CONFIG:
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
67 self.mnist = MNIST.first_1k()
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
68 print "Test config, so loaded MNIST first 1000"
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
69 else:
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
70 self.mnist = MNIST.full()#first_10k()
340
523e7b87c521 Corrected a few bugs, no new features. Supposedly ready to run on cluster.
fsavard
parents: 339
diff changeset
71 print "Loaded MNIST full"
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
72
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
73 self.cp = ConvolutionParams( \
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
74 num_filters=state.num_filters,
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
75 num_input_planes=1,
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
76 height_filters=state.filter_size,
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
77 width_filters=state.filter_size)
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
78
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
79 self.image_size = (28,28)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
80
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
81 self.minibatch_size = state.minibatch_size
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
82
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
83 self.lr = state.learning_rate
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
84 self.sparsity_lambda = state.sparsity_lambda
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
85 # about 1/num_filters, so only one filter active at a time
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
86 # 40 * 0.05 = ~2 filters active for any given pixel
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
87 self.sparsity_p = state.sparsity_p
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
88
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
89 self.crbm = CRBM( \
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
90 minibatch_size=self.minibatch_size,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
91 image_size=self.image_size,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
92 conv_params=self.cp,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
93 learning_rate=self.lr,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
94 sparsity_lambda=self.sparsity_lambda,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
95 sparsity_p=self.sparsity_p)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
96
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
97 self.num_epochs = state.num_epochs
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
98
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
99 self.init_series()
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
100
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
101 def init_series(self):
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
102 series = {}
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
103
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
104 basedir = os.getcwd()
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
105
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
106 h5f = tables.openFile(os.path.join(basedir, "series.h5"), "w")
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
107
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
108 cd_series_names = self.crbm.cd_return_desc
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
109 series['cd'] = \
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
110 utils.get_accumulator_series_array( \
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
111 h5f, 'cd', cd_series_names,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
112 REDUCE_EVERY,
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
113 stdout_too=SERIES_STDOUT_TOO)
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
114
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
115 sparsity_series_names = self.crbm.sparsity_return_desc
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
116 series['sparsity'] = \
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
117 utils.get_accumulator_series_array( \
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
118 h5f, 'sparsity', sparsity_series_names,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
119 REDUCE_EVERY,
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
120 stdout_too=SERIES_STDOUT_TOO)
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
121
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
122 # so first we create the names for each table, based on
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
123 # position of each param in the array
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
124 params_stdout = []
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
125 if SERIES_STDOUT_TOO:
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
126 params_stdout = [StdoutAppendTarget()]
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
127 series['params'] = SharedParamsStatisticsWrapper(
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
128 new_group_name="params",
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
129 base_group="/",
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
130 arrays_names=['W','b_h','b_x'],
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
131 hdf5_file=h5f,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
132 index_names=('epoch','minibatch'),
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
133 other_targets=params_stdout)
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
134
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
135 self.series = series
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
136
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
137 def train(self):
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
138 num_minibatches = len(self.mnist.train.x) / self.minibatch_size
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
139
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
140 for epoch in xrange(self.num_epochs):
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
141 for mb_index in xrange(num_minibatches):
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
142 mb_x = self.mnist.train.x \
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
143 [mb_index : mb_index+self.minibatch_size]
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
144 mb_x = mb_x.reshape((self.minibatch_size, 1, 28, 28))
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
145
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
146 #E_h = crbm.E_h_given_x_func(mb_x)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
147 #print "Shape of E_h", E_h.shape
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
148
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
149 cd_return = self.crbm.CD_step(mb_x)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
150 sp_return = self.crbm.sparsity_step(mb_x)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
151
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
152 self.series['cd'].append( \
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
153 (epoch, mb_index), cd_return)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
154 self.series['sparsity'].append( \
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
155 (epoch, mb_index), sp_return)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
156
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
157 total_idx = epoch*num_minibatches + mb_index
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
158
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
159 if (total_idx+1) % REDUCE_EVERY == 0:
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
160 self.series['params'].append( \
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
161 (epoch, mb_index), self.crbm.params)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
162
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
163 if total_idx % VISUALIZE_EVERY == 0:
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
164 self.visualize_gibbs_result(\
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
165 mb_x, GIBBS_STEPS_IN_VIZ_CHAIN,
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
166 "gibbs_chain_"+str(epoch)+"_"+str(mb_index))
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
167 self.visualize_gibbs_result(mb_x, 1,
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
168 "gibbs_1_"+str(epoch)+"_"+str(mb_index))
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
169 self.visualize_filters(
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
170 "filters_"+str(epoch)+"_"+str(mb_index))
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
171 if TEST_CONFIG:
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
172 # do a single epoch for cluster tests config
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
173 break
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
174
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
175 if SAVE_PARAMS:
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
176 utils.save_params(self.crbm.params, "params.pkl")
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
177
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
178 def visualize_gibbs_result(self, start_x, gibbs_steps, filename):
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
179 # Run minibatch_size chains for gibbs_steps
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
180 x_samples = None
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
181 if not start_x is None:
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
182 x_samples = self.crbm.gibbs_samples_from(start_x, gibbs_steps)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
183 else:
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
184 x_samples = self.crbm.random_gibbs_samples(gibbs_steps)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
185 x_samples = x_samples.reshape((self.minibatch_size, 28*28))
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
186
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
187 tile = tile_raster_images(x_samples, self.image_size,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
188 (1, self.minibatch_size), output_pixel_vals=True)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
189
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
190 filepath = os.path.join(IMAGE_OUTPUT_DIR, filename+".png")
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
191 img = Image.fromarray(tile)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
192 img.save(filepath)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
193
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
194 print "Result of running Gibbs", \
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
195 gibbs_steps, "times outputed to", filepath
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
196
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
197 def visualize_filters(self, filename):
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
198 cp = self.cp
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
199
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
200 # filter size
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
201 fsz = (cp.height_filters, cp.width_filters)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
202 tile_shape = (cp.num_filters, cp.num_input_planes)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
203
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
204 filters_flattened = self.crbm.W.value.reshape(
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
205 (tile_shape[0]*tile_shape[1],
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
206 fsz[0]*fsz[1]))
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
207
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
208 tile = tile_raster_images(filters_flattened, fsz,
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
209 tile_shape, output_pixel_vals=True)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
210
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
211 filepath = os.path.join(IMAGE_OUTPUT_DIR, filename+".png")
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
212 img = Image.fromarray(tile)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
213 img.save(filepath)
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
214
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
215 print "Filters (as images) outputed to", filepath
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
216
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
217
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
218
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
219 if __name__ == '__main__':
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
220 args = sys.argv[1:]
337
8d116d4a7593 Added convolutional RBM (ala Lee09) code, imported from my working dir elsewhere. Seems to work for one layer. No subsampling yet.
fsavard
parents:
diff changeset
221
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
222 if len(args) == 0:
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
223 print "Bad usage"
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
224 elif args[0] == 'jobman_insert':
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
225 utils.jobman_insert_job_vals(JOBDB, EXPERIMENT_PATH, JOB_VALS)
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
226 elif args[0] == 'test_jobman_entrypoint':
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
227 chanmock = DD({'COMPLETE':0,'save':(lambda:None)})
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
228 jobman_entrypoint(DEFAULT_STATE, chanmock)
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
229 elif args[0] == 'run_default':
344
ed1c0830681e Correcting bugs after trying to launch on cluster. Image dir must be created after workdir is changed.
fsavard
parents: 343
diff changeset
230 setup_workdir()
339
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
231 mc = MnistCrbm(DEFAULT_STATE)
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
232 mc.train()
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
233
ffbf0e41bcee Aded code to run experiment on cluster, separate configuration from other machinery. Not tested yet.
fsavard
parents: 337
diff changeset
234