comparison deep/convolutional_dae/scdae.py @ 301:be45e7db7cd4

Fix last-minute bugs in the code.
author Arnaud Bergeron <abergeron@gmail.com>
date Mon, 29 Mar 2010 18:14:30 -0400
parents a222af1d0598
children ef28cbb5f464
comparison
equal deleted inserted replaced
300:6eab220a7d70 301:be45e7db7cd4
38 layers.append(SimpleLayer(old_size, size, activation=nlins.tanh, 38 layers.append(SimpleLayer(old_size, size, activation=nlins.tanh,
39 dtype=dtype)) 39 dtype=dtype))
40 old_size = size 40 old_size = size
41 return LayerStack(layers) 41 return LayerStack(layers)
42 42
43 def scdae_net(in_size, num_in, filter_sizes, num_filts, subsamplings, 43 def scdae_net(in_size, filter_sizes, num_filts, subsamplings,
44 corruptions, layer_sizes, out_size, dtype): 44 corruptions, layer_sizes, out_size, dtype):
45 rl1 = ReshapeLayer((None,)+in_size) 45 rl1 = ReshapeLayer((None,)+in_size)
46 ls = scdae(num_in, filter_sizes, num_filts, subsamplings, 46 ls = scdae(filter_sizes, num_filts, subsamplings,
47 corruptions, dtype) 47 corruptions, dtype)
48 x = T.tensor4() 48 x = T.ftensor4()
49 ls.build(x, input_shape=(1,)+in_size) 49 ls.build(x, input_shape=(1,)+in_size)
50 outs = numpy.prod(ls.output_shape) 50 outs = numpy.prod(ls.output_shape)
51 rl2 = ReshapeLayer((None, outs)) 51 rl2 = ReshapeLayer((None, outs))
52 layer_sizes = [outs]+layer_sizes 52 layer_sizes = [outs]+layer_sizes
53 ls2 = mlp(layer_sizes, dtype) 53 ls2 = mlp(layer_sizes, dtype)
55 return NNet([rl1, ls, rl2, ls2, lrl], error=errors.nll) 55 return NNet([rl1, ls, rl2, ls2, lrl], error=errors.nll)
56 56
57 def build_funcs(batch_size, img_size, filter_sizes, num_filters, subs, 57 def build_funcs(batch_size, img_size, filter_sizes, num_filters, subs,
58 noise, mlp_sizes, out_size, dtype, pretrain_lr, train_lr): 58 noise, mlp_sizes, out_size, dtype, pretrain_lr, train_lr):
59 59
60 n = scdae_net((1,)+img_size, batch_size, filter_sizes, num_filters, subs, 60 n = scdae_net((1,)+img_size, filter_sizes, num_filters, subs,
61 noise, mlp_sizes, out_size, dtype) 61 noise, mlp_sizes, out_size, dtype)
62 62
63 n.save('start.net') 63 n.save('start.net')
64 64
65 x = T.fmatrix('x') 65 x = T.fmatrix('x')
71 71
72 def trainfunc(net, alpha): 72 def trainfunc(net, alpha):
73 up = trainers.get_updates(net.params, net.cost, alpha) 73 up = trainers.get_updates(net.params, net.cost, alpha)
74 return theano.function([x, y], net.cost, updates=up) 74 return theano.function([x, y], net.cost, updates=up)
75 75
76 n.build(x, y, input_shape=(bsize, 1)+img_size) 76 n.build(x, y, input_shape=(batch_size, numpy.prod(img_size)))
77 pretrain_funcs_opt = [pretrainfunc(l, pretrain_lr) for l in n.layers[1].layers] 77 pretrain_funcs_opt = [pretrainfunc(l, pretrain_lr) for l in n.layers[1].layers]
78 trainf_opt = trainfunc(n, train_lr) 78 trainf_opt = trainfunc(n, train_lr)
79 evalf_opt = theano.function([x, y], errors.class_error(n.output, y)) 79 evalf_opt = theano.function([x, y], errors.class_error(n.output, y))
80 80
81 n.build(x, y) 81 n.build(x, y)
184 index_names=('iter',), 184 index_names=('iter',),
185 title='Test error (class)') 185 title='Test error (class)')
186 186
187 return series 187 return series
188 188
189 class PrintSeries(object):
190 def append(self, idx, v):
191 print idx, v
192
189 if __name__ == '__main__': 193 if __name__ == '__main__':
190 from ift6266 import datasets 194 from ift6266 import datasets
191 from sgd_opt import sgd_opt 195 from sgd_opt import sgd_opt
192 import sys, time 196 import sys, time
193 197
194 batch_size = 100 198 batch_size = 100
195 dset = datasets.mnist() 199 dset = datasets.nist_digits(1000)
196 200
197 pretrain_funcs, trainf, evalf, net = build_funcs( 201 pretrain_funcs, trainf, evalf, net = build_funcs(
198 img_size = (28, 28), 202 img_size = (32, 32),
199 batch_size=batch_size, filter_sizes=[(5,5), (3,3)], 203 batch_size=batch_size, filter_sizes=[(5,5), (3,3)],
200 num_filters=[4, 4], subs=[(2,2), (2,2)], noise=[0.2, 0.2], 204 num_filters=[4, 4], subs=[(2,2), (2,2)], noise=[0.2, 0.2],
201 mlp_sizes=[500], out_size=10, dtype=numpy.float32, 205 mlp_sizes=[500], out_size=10, dtype=numpy.float32,
202 pretrain_lr=0.01, train_lr=0.1) 206 pretrain_lr=0.01, train_lr=0.1)
203 207
207 pretrain_funcs, trainf, evalf) 211 pretrain_funcs, trainf, evalf)
208 212
209 print "pretraining ...", 213 print "pretraining ...",
210 sys.stdout.flush() 214 sys.stdout.flush()
211 start = time.time() 215 start = time.time()
212 do_pretrain(pretrain_fs, 2500, DummySeries()) 216 do_pretrain(pretrain_fs, 1000, PrintSeries())
213 end = time.time() 217 end = time.time()
214 print "done (in", end-start, "s)" 218 print "done (in", end-start, "s)"
215 219
216 sgd_opt(train, valid, test, training_epochs=10000, patience=1000, 220 sgd_opt(train, valid, test, training_epochs=10000, patience=1000,
217 patience_increase=2., improvement_threshold=0.995, 221 patience_increase=2., improvement_threshold=0.995,