annotate deep/autoencoder/DA_training.py @ 195:92c9a6c48ce9

Add option for test.py to test modules specified on the command-line.
author Arnaud Bergeron <abergeron@gmail.com>
date Tue, 02 Mar 2010 18:01:22 -0500
parents 70a9df1cd20e
children e12702b88a2d
rev   line source
190
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
1 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
2 This tutorial introduces stacked denoising auto-encoders (SdA) using Theano.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
3
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
4 Denoising autoencoders are the building blocks for SDAE.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
5 They are based on auto-encoders as the ones used in Bengio et al. 2007.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
6 An autoencoder takes an input x and first maps it to a hidden representation
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
7 y = f_{\theta}(x) = s(Wx+b), parameterized by \theta={W,b}. The resulting
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
8 latent representation y is then mapped back to a "reconstructed" vector
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
9 z \in [0,1]^d in input space z = g_{\theta'}(y) = s(W'y + b'). The weight
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
10 matrix W' can optionally be constrained such that W' = W^T, in which case
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
11 the autoencoder is said to have tied weights. The network is trained such
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
12 that to minimize the reconstruction error (the error between x and z).
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
13
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
14 For the denosing autoencoder, during training, first x is corrupted into
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
15 \tilde{x}, where \tilde{x} is a partially destroyed version of x by means
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
16 of a stochastic mapping. Afterwards y is computed as before (using
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
17 \tilde{x}), y = s(W\tilde{x} + b) and z as s(W'y + b'). The reconstruction
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
18 error is now measured between z and the uncorrupted input x, which is
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
19 computed as the cross-entropy :
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
20 - \sum_{k=1}^d[ x_k \log z_k + (1-x_k) \log( 1-z_k)]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
21
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
22 For X iteration of the main program loop it takes *** minutes on an
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
23 Intel Core i7 and *** minutes on GPU (NVIDIA GTX 285 graphics processor).
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
24
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
25
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
26 References :
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
27 - P. Vincent, H. Larochelle, Y. Bengio, P.A. Manzagol: Extracting and
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
28 Composing Robust Features with Denoising Autoencoders, ICML'08, 1096-1103,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
29 2008
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
30 - Y. Bengio, P. Lamblin, D. Popovici, H. Larochelle: Greedy Layer-Wise
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
31 Training of Deep Networks, Advances in Neural Information Processing
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
32 Systems 19, 2007
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
33
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
34 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
35
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
36 import numpy
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
37 import theano
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
38 import time
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
39 import theano.tensor as T
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
40 from theano.tensor.shared_randomstreams import RandomStreams
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
41
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
42 import gzip
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
43 import cPickle
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
44
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
45 from pylearn.io import filetensor as ft
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
46
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
47 class dA():
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
48 """Denoising Auto-Encoder class (dA)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
49
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
50 A denoising autoencoders tries to reconstruct the input from a corrupted
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
51 version of it by projecting it first in a latent space and reprojecting
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
52 it afterwards back in the input space. Please refer to Vincent et al.,2008
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
53 for more details. If x is the input then equation (1) computes a partially
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
54 destroyed version of x by means of a stochastic mapping q_D. Equation (2)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
55 computes the projection of the input into the latent space. Equation (3)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
56 computes the reconstruction of the input, while equation (4) computes the
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
57 reconstruction error.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
58
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
59 .. math::
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
60
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
61 \tilde{x} ~ q_D(\tilde{x}|x) (1)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
62
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
63 y = s(W \tilde{x} + b) (2)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
64
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
65 z = s(W' y + b') (3)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
66
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
67 L(x,z) = -sum_{k=1}^d [x_k \log z_k + (1-x_k) \log( 1-z_k)] (4)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
68
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
69 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
70
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
71 def __init__(self, n_visible= 784, n_hidden= 500, complexity = 0.1, input= None):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
72 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
73 Initialize the DAE class by specifying the number of visible units (the
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
74 dimension d of the input ), the number of hidden units ( the dimension
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
75 d' of the latent or hidden space ) and by giving a symbolic variable
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
76 for the input. Such a symbolic variable is useful when the input is
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
77 the result of some computations. For example when dealing with SDAEs,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
78 the dA on layer 2 gets as input the output of the DAE on layer 1.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
79 This output can be written as a function of the input to the entire
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
80 model, and as such can be computed by theano whenever needed.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
81
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
82 :param n_visible: number of visible units
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
83
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
84 :param n_hidden: number of hidden units
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
85
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
86 :param input: a symbolic description of the input or None
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
87
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
88 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
89 self.n_visible = n_visible
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
90 self.n_hidden = n_hidden
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
91
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
92 # create a Theano random generator that gives symbolic random values
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
93 theano_rng = RandomStreams()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
94 # create a numpy random generator
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
95 numpy_rng = numpy.random.RandomState()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
96
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
97
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
98 # initial values for weights and biases
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
99 # note : W' was written as `W_prime` and b' as `b_prime`
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
100
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
101 # W is initialized with `initial_W` which is uniformely sampled
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
102 # from -6./sqrt(n_visible+n_hidden) and 6./sqrt(n_hidden+n_visible)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
103 # the output of uniform if converted using asarray to dtype
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
104 # theano.config.floatX so that the code is runable on GPU
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
105 initial_W = numpy.asarray( numpy.random.uniform( \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
106 low = -numpy.sqrt(6./(n_visible+n_hidden)), \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
107 high = numpy.sqrt(6./(n_visible+n_hidden)), \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
108 size = (n_visible, n_hidden)), dtype = theano.config.floatX)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
109 initial_b = numpy.zeros(n_hidden)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
110
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
111 # W' is initialized with `initial_W_prime` which is uniformely sampled
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
112 # from -6./sqrt(n_visible+n_hidden) and 6./sqrt(n_hidden+n_visible)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
113 # the output of uniform if converted using asarray to dtype
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
114 # theano.config.floatX so that the code is runable on GPU
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
115 initial_b_prime= numpy.zeros(n_visible)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
116
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
117
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
118 # theano shared variables for weights and biases
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
119 self.W = theano.shared(value = initial_W, name = "W")
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
120 self.b = theano.shared(value = initial_b, name = "b")
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
121 # tied weights, therefore W_prime is W transpose
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
122 self.W_prime = self.W.T
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
123 self.b_prime = theano.shared(value = initial_b_prime, name = "b'")
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
124
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
125 # if no input is given, generate a variable representing the input
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
126 if input == None :
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
127 # we use a matrix because we expect a minibatch of several examples,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
128 # each example being a row
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
129 x = T.dmatrix(name = 'input')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
130 else:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
131 x = input
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
132 # Equation (1)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
133 # note : first argument of theano.rng.binomial is the shape(size) of
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
134 # random numbers that it should produce
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
135 # second argument is the number of trials
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
136 # third argument is the probability of success of any trial
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
137 #
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
138 # this will produce an array of 0s and 1s where 1 has a
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
139 # probability of 0.9 and 0 of 0.1
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
140
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
141 tilde_x = theano_rng.binomial( x.shape, 1, 1-complexity) * x
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
142 # Equation (2)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
143 # note : y is stored as an attribute of the class so that it can be
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
144 # used later when stacking dAs.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
145 self.y = T.nnet.sigmoid(T.dot(tilde_x, self.W ) + self.b)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
146 # Equation (3)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
147 z = T.nnet.sigmoid(T.dot(self.y, self.W_prime) + self.b_prime)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
148 # Equation (4)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
149 self.L = - T.sum( x*T.log(z) + (1-x)*T.log(1-z), axis=1 )
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
150 # note : L is now a vector, where each element is the cross-entropy cost
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
151 # of the reconstruction of the corresponding example of the
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
152 # minibatch. We need to compute the average of all these to get
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
153 # the cost of the minibatch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
154 self.cost = T.mean(self.L)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
155 # note : y is computed from the corrupted `tilde_x`. Later on,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
156 # we will need the hidden layer obtained from the uncorrupted
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
157 # input when for example we will pass this as input to the layer
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
158 # above
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
159 self.hidden_values = T.nnet.sigmoid( T.dot(x, self.W) + self.b)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
160
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
161
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
162
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
163 def sgd_optimization_nist( learning_rate=0.01, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
164 n_iter = 300, n_code_layer = 400, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
165 complexity = 0.1):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
166 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
167 Demonstrate stochastic gradient descent optimization for a denoising autoencoder
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
168
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
169 This is demonstrated on MNIST.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
170
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
171 :param learning_rate: learning rate used (factor for the stochastic
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
172 gradient
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
173
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
174 :param pretraining_epochs: number of epoch to do pretraining
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
175
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
176 :param pretrain_lr: learning rate to be used during pre-training
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
177
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
178 :param n_iter: maximal number of iterations ot run the optimizer
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
179
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
180 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
181 #open file to save the validation and test curve
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
182 filename = 'lr_' + str(learning_rate) + 'ni_' + str(n_iter) + 'nc_' + str(n_code_layer) + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
183 'c_' + str(complexity) + '.txt'
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
184
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
185 result_file = open(filename, 'w')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
186
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
187
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
188
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
189 data_path = '/data/lisa/data/nist/by_class/'
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
190 f = open(data_path+'all/all_train_data.ft')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
191 g = open(data_path+'all/all_train_labels.ft')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
192 h = open(data_path+'all/all_test_data.ft')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
193 i = open(data_path+'all/all_test_labels.ft')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
194
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
195 train_set_x = ft.read(f)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
196 train_set_y = ft.read(g)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
197 test_set_x = ft.read(h)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
198 test_set_y = ft.read(i)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
199
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
200 f.close()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
201 g.close()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
202 i.close()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
203 h.close()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
204
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
205 # make minibatches of size 20
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
206 batch_size = 20 # sized of the minibatch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
207
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
208 #create a validation set the same size as the test size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
209 #use the end of the training array for this purpose
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
210 #discard the last remaining so we get a %batch_size number
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
211 test_size=len(test_set_y)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
212 test_size = int(test_size/batch_size)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
213 test_size*=batch_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
214 train_size = len(train_set_x)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
215 train_size = int(train_size/batch_size)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
216 train_size*=batch_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
217 validation_size =test_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
218 offset = train_size-test_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
219 if True:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
220 print 'train size = %d' %train_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
221 print 'test size = %d' %test_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
222 print 'valid size = %d' %validation_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
223 print 'offset = %d' %offset
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
224
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
225
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
226 #train_set = (train_set_x,train_set_y)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
227 train_batches = []
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
228 for i in xrange(0, train_size-test_size, batch_size):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
229 train_batches = train_batches + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
230 [(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
231
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
232 test_batches = []
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
233 for i in xrange(0, test_size, batch_size):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
234 test_batches = test_batches + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
235 [(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
236
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
237 valid_batches = []
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
238 for i in xrange(0, test_size, batch_size):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
239 valid_batches = valid_batches + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
240 [(train_set_x[offset+i:offset+i+batch_size], \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
241 train_set_y[offset+i:offset+i+batch_size])]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
242
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
243
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
244 ishape = (32,32) # this is the size of NIST images
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
245
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
246 # allocate symbolic variables for the data
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
247 x = T.fmatrix() # the data is presented as rasterized images
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
248 y = T.lvector() # the labels are presented as 1D vector of
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
249 # [long int] labels
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
250
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
251 # construct the denoising autoencoder class
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
252 n_ins = 32*32
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
253 encoder = dA(n_ins, n_code_layer, input = x.reshape((batch_size,n_ins)))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
254
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
255 # Train autoencoder
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
256
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
257 # compute gradients of the layer parameters
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
258 gW = T.grad(encoder.cost, encoder.W)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
259 gb = T.grad(encoder.cost, encoder.b)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
260 gb_prime = T.grad(encoder.cost, encoder.b_prime)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
261 # compute the updated value of the parameters after one step
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
262 updated_W = encoder.W - gW * learning_rate
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
263 updated_b = encoder.b - gb * learning_rate
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
264 updated_b_prime = encoder.b_prime - gb_prime * learning_rate
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
265
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
266 # defining the function that evaluate the symbolic description of
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
267 # one update step
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
268 train_model = theano.function([x], encoder.cost, updates=\
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
269 { encoder.W : updated_W, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
270 encoder.b : updated_b, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
271 encoder.b_prime : updated_b_prime } )
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
272
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
273
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
274
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
275
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
276 # compiling a theano function that computes the mistakes that are made
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
277 # by the model on a minibatch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
278 test_model = theano.function([x], encoder.cost)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
279
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
280 normalize = numpy.asarray(255, dtype=theano.config.floatX)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
281
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
282
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
283 n_minibatches = len(train_batches)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
284
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
285 # early-stopping parameters
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
286 patience = 10000000 / batch_size # look as this many examples regardless
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
287 patience_increase = 2 # wait this much longer when a new best is
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
288 # found
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
289 improvement_threshold = 0.995 # a relative improvement of this much is
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
290 # considered significant
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
291 validation_frequency = n_minibatches # go through this many
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
292 # minibatche before checking the network
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
293 # on the validation set; in this case we
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
294 # check every epoch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
295
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
296
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
297 best_params = None
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
298 best_validation_loss = float('inf')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
299 best_iter = 0
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
300 test_score = 0.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
301 start_time = time.clock()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
302 # have a maximum of `n_iter` iterations through the entire dataset
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
303 for iter in xrange(n_iter* n_minibatches):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
304
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
305 # get epoch and minibatch index
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
306 epoch = iter / n_minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
307 minibatch_index = iter % n_minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
308
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
309 # get the minibatches corresponding to `iter` modulo
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
310 # `len(train_batches)`
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
311 x,y = train_batches[ minibatch_index ]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
312 '''
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
313 if iter == 0:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
314 b = numpy.asarray(255, dtype=theano.config.floatX)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
315 x = x / b
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
316 print x
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
317 print y
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
318 print x.__class__
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
319 print x.shape
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
320 print x.dtype.name
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
321 print y.dtype.name
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
322 print x.min(), x.max()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
323 '''
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
324
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
325 cost_ij = train_model(x/normalize)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
326
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
327 if (iter+1) % validation_frequency == 0:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
328 # compute zero-one loss on validation set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
329 this_validation_loss = 0.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
330 for x,y in valid_batches:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
331 # sum up the errors for each minibatch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
332 this_validation_loss += test_model(x/normalize)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
333 # get the average by dividing with the number of minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
334 this_validation_loss /= len(valid_batches)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
335
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
336 print('epoch %i, minibatch %i/%i, validation error %f ' % \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
337 (epoch, minibatch_index+1, n_minibatches, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
338 this_validation_loss))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
339
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
340 # save value in file
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
341 result_file.write(str(epoch) + ' ' + str(this_validation_loss)+ '\n')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
342
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
343
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
344 # if we got the best validation score until now
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
345 if this_validation_loss < best_validation_loss:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
346
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
347 #improve patience if loss improvement is good enough
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
348 if this_validation_loss < best_validation_loss * \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
349 improvement_threshold :
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
350 patience = max(patience, iter * patience_increase)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
351
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
352 best_validation_loss = this_validation_loss
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
353 best_iter = iter
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
354 # test it on the test set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
355
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
356 test_score = 0.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
357 for x,y in test_batches:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
358 test_score += test_model(x/normalize)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
359 test_score /= len(test_batches)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
360 print((' epoch %i, minibatch %i/%i, test error of best '
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
361 'model %f ') %
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
362 (epoch, minibatch_index+1, n_minibatches,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
363 test_score))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
364
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
365 if patience <= iter :
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
366 print('iter (%i) is superior than patience(%i). break', iter, patience)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
367 break
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
368
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
369
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
370
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
371 end_time = time.clock()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
372 print(('Optimization complete with best validation score of %f ,'
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
373 'with test performance %f ') %
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
374 (best_validation_loss, test_score))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
375 print ('The code ran for %f minutes' % ((end_time-start_time)/60.))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
376
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
377
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
378 result_file.close()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
379
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
380 return (best_validation_loss, test_score, (end_time-start_time)/60, best_iter)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
381
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
382 def sgd_optimization_mnist( learning_rate=0.01, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
383 n_iter = 1, n_code_layer = 400, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
384 complexity = 0.1):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
385 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
386 Demonstrate stochastic gradient descent optimization for a denoising autoencoder
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
387
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
388 This is demonstrated on MNIST.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
389
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
390 :param learning_rate: learning rate used (factor for the stochastic
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
391 gradient
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
392
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
393 :param pretraining_epochs: number of epoch to do pretraining
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
394
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
395 :param pretrain_lr: learning rate to be used during pre-training
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
396
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
397 :param n_iter: maximal number of iterations ot run the optimizer
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
398
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
399 """
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
400 #open file to save the validation and test curve
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
401 filename = 'lr_' + str(learning_rate) + 'ni_' + str(n_iter) + 'nc_' + str(n_code_layer) + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
402 'c_' + str(complexity) + '.txt'
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
403
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
404 result_file = open(filename, 'w')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
405
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
406 # Load the dataset
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
407 f = gzip.open('/u/lisa/HTML/deep/data/mnist/mnist.pkl.gz','rb')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
408 train_set, valid_set, test_set = cPickle.load(f)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
409 f.close()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
410
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
411 # make minibatches of size 20
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
412 batch_size = 20 # sized of the minibatch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
413
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
414 # Dealing with the training set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
415 # get the list of training images (x) and their labels (y)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
416 (train_set_x, train_set_y) = train_set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
417 # initialize the list of training minibatches with empty list
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
418 train_batches = []
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
419 for i in xrange(0, len(train_set_x), batch_size):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
420 # add to the list of minibatches the minibatch starting at
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
421 # position i, ending at position i+batch_size
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
422 # a minibatch is a pair ; the first element of the pair is a list
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
423 # of datapoints, the second element is the list of corresponding
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
424 # labels
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
425 train_batches = train_batches + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
426 [(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
427
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
428 # Dealing with the validation set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
429 (valid_set_x, valid_set_y) = valid_set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
430 # initialize the list of validation minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
431 valid_batches = []
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
432 for i in xrange(0, len(valid_set_x), batch_size):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
433 valid_batches = valid_batches + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
434 [(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
435
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
436 # Dealing with the testing set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
437 (test_set_x, test_set_y) = test_set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
438 # initialize the list of testing minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
439 test_batches = []
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
440 for i in xrange(0, len(test_set_x), batch_size):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
441 test_batches = test_batches + \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
442 [(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
443
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
444
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
445 ishape = (28,28) # this is the size of MNIST images
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
446
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
447 # allocate symbolic variables for the data
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
448 x = T.fmatrix() # the data is presented as rasterized images
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
449 y = T.lvector() # the labels are presented as 1D vector of
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
450 # [long int] labels
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
451
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
452 # construct the denoising autoencoder class
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
453 n_ins = 28*28
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
454 encoder = dA(n_ins, n_code_layer, input = x.reshape((batch_size,n_ins)))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
455
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
456 # Train autoencoder
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
457
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
458 # compute gradients of the layer parameters
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
459 gW = T.grad(encoder.cost, encoder.W)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
460 gb = T.grad(encoder.cost, encoder.b)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
461 gb_prime = T.grad(encoder.cost, encoder.b_prime)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
462 # compute the updated value of the parameters after one step
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
463 updated_W = encoder.W - gW * learning_rate
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
464 updated_b = encoder.b - gb * learning_rate
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
465 updated_b_prime = encoder.b_prime - gb_prime * learning_rate
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
466
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
467 # defining the function that evaluate the symbolic description of
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
468 # one update step
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
469 train_model = theano.function([x], encoder.cost, updates=\
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
470 { encoder.W : updated_W, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
471 encoder.b : updated_b, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
472 encoder.b_prime : updated_b_prime } )
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
473
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
474
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
475
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
476
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
477 # compiling a theano function that computes the mistakes that are made
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
478 # by the model on a minibatch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
479 test_model = theano.function([x], encoder.cost)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
480
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
481
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
482
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
483
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
484 n_minibatches = len(train_batches)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
485
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
486 # early-stopping parameters
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
487 patience = 10000# look as this many examples regardless
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
488 patience_increase = 2 # wait this much longer when a new best is
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
489 # found
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
490 improvement_threshold = 0.995 # a relative improvement of this much is
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
491 # considered significant
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
492 validation_frequency = n_minibatches # go through this many
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
493 # minibatche before checking the network
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
494 # on the validation set; in this case we
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
495 # check every epoch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
496
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
497
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
498 best_params = None
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
499 best_validation_loss = float('inf')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
500 best_iter = 0
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
501 test_score = 0.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
502 start_time = time.clock()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
503 # have a maximum of `n_iter` iterations through the entire dataset
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
504 for iter in xrange(n_iter* n_minibatches):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
505
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
506 # get epoch and minibatch index
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
507 epoch = iter / n_minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
508 minibatch_index = iter % n_minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
509
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
510 # get the minibatches corresponding to `iter` modulo
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
511 # `len(train_batches)`
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
512 x,y = train_batches[ minibatch_index ]
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
513 cost_ij = train_model(x)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
514
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
515 if (iter+1) % validation_frequency == 0:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
516 # compute zero-one loss on validation set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
517 this_validation_loss = 0.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
518 for x,y in valid_batches:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
519 # sum up the errors for each minibatch
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
520 this_validation_loss += test_model(x)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
521 # get the average by dividing with the number of minibatches
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
522 this_validation_loss /= len(valid_batches)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
523
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
524 print('epoch %i, minibatch %i/%i, validation error %f ' % \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
525 (epoch, minibatch_index+1, n_minibatches, \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
526 this_validation_loss))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
527
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
528 # save value in file
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
529 result_file.write(str(epoch) + ' ' + str(this_validation_loss)+ '\n')
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
530
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
531
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
532 # if we got the best validation score until now
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
533 if this_validation_loss < best_validation_loss:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
534
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
535 #improve patience if loss improvement is good enough
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
536 if this_validation_loss < best_validation_loss * \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
537 improvement_threshold :
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
538 patience = max(patience, iter * patience_increase)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
539
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
540 best_validation_loss = this_validation_loss
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
541 best_iter = iter
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
542 # test it on the test set
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
543
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
544 test_score = 0.
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
545 for x,y in test_batches:
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
546 test_score += test_model(x)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
547 test_score /= len(test_batches)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
548 print((' epoch %i, minibatch %i/%i, test error of best '
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
549 'model %f ') %
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
550 (epoch, minibatch_index+1, n_minibatches,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
551 test_score))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
552
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
553 if patience <= iter :
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
554 print('iter (%i) is superior than patience(%i). break', iter, patience)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
555 break
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
556
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
557
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
558 end_time = time.clock()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
559 print(('Optimization complete with best validation score of %f ,'
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
560 'with test performance %f ') %
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
561 (best_validation_loss, test_score))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
562 print ('The code ran for %f minutes' % ((end_time-start_time)/60.))
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
563
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
564
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
565 result_file.close()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
566
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
567 return (best_validation_loss, test_score, (end_time-start_time)/60, best_iter)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
568
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
569
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
570 def experiment(state,channel):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
571
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
572 (best_validation_loss, test_score, minutes_trained, iter) = \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
573 sgd_optimization_mnist(state.learning_rate, state.n_iter, state.n_code_layer,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
574 state.complexity)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
575
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
576 state.best_validation_loss = best_validation_loss
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
577 state.test_score = test_score
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
578 state.minutes_trained = minutes_trained
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
579 state.iter = iter
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
580
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
581 return channel.COMPLETE
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
582
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
583 def experiment_nist(state,channel):
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
584
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
585 (best_validation_loss, test_score, minutes_trained, iter) = \
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
586 sgd_optimization_nist(state.learning_rate, state.n_iter, state.n_code_layer,
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
587 state.complexity)
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
588
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
589 state.best_validation_loss = best_validation_loss
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
590 state.test_score = test_score
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
591 state.minutes_trained = minutes_trained
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
592 state.iter = iter
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
593
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
594 return channel.COMPLETE
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
595
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
596
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
597 if __name__ == '__main__':
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
598
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
599 sgd_optimization_nist()
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
600
70a9df1cd20e initial commit for autoencoder training
youssouf
parents:
diff changeset
601