comparison deep/stacked_dae/old/stacked_dae.py @ 265:c8fe09a65039

Déplacer le nouveau code de stacked_dae de v2 vers le répertoire de base 'stacked_dae', et bougé le vieux code vers le répertoire 'old'
author fsavard
date Fri, 19 Mar 2010 10:54:39 -0400
parents deep/stacked_dae/stacked_dae.py@acb942530923
children
comparison
equal deleted inserted replaced
243:3c54cb3713ef 265:c8fe09a65039
1 #!/usr/bin/python
2 # coding: utf-8
3
4 import numpy
5 import theano
6 import time
7 import theano.tensor as T
8 from theano.tensor.shared_randomstreams import RandomStreams
9 import copy
10
11 from utils import update_locals
12
13 # taken from LeDeepNet/daa.py
14 # has a special case when taking log(0) (defined =0)
15 # modified to not take the mean anymore
16 from theano.tensor.xlogx import xlogx, xlogy0
17 # it's target*log(output)
18 def binary_cross_entropy(target, output, sum_axis=1):
19 XE = xlogy0(target, output) + xlogy0((1 - target), (1 - output))
20 return -T.sum(XE, axis=sum_axis)
21
22 class LogisticRegression(object):
23 def __init__(self, input, n_in, n_out):
24 # initialize with 0 the weights W as a matrix of shape (n_in, n_out)
25 self.W = theano.shared( value=numpy.zeros((n_in,n_out),
26 dtype = theano.config.floatX) )
27 # initialize the baises b as a vector of n_out 0s
28 self.b = theano.shared( value=numpy.zeros((n_out,),
29 dtype = theano.config.floatX) )
30 # compute vector of class-membership probabilities in symbolic form
31 self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)+self.b)
32
33 # compute prediction as class whose probability is maximal in
34 # symbolic form
35 self.y_pred=T.argmax(self.p_y_given_x, axis=1)
36
37 # list of parameters for this layer
38 self.params = [self.W, self.b]
39
40 def negative_log_likelihood(self, y):
41 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y])
42
43 def errors(self, y):
44 # check if y has same dimension of y_pred
45 if y.ndim != self.y_pred.ndim:
46 raise TypeError('y should have the same shape as self.y_pred',
47 ('y', target.type, 'y_pred', self.y_pred.type))
48
49 # check if y is of the correct datatype
50 if y.dtype.startswith('int'):
51 # the T.neq operator returns a vector of 0s and 1s, where 1
52 # represents a mistake in prediction
53 return T.mean(T.neq(self.y_pred, y))
54 else:
55 raise NotImplementedError()
56
57
58 class SigmoidalLayer(object):
59 def __init__(self, rng, input, n_in, n_out):
60 self.input = input
61
62 W_values = numpy.asarray( rng.uniform( \
63 low = -numpy.sqrt(6./(n_in+n_out)), \
64 high = numpy.sqrt(6./(n_in+n_out)), \
65 size = (n_in, n_out)), dtype = theano.config.floatX)
66 self.W = theano.shared(value = W_values)
67
68 b_values = numpy.zeros((n_out,), dtype= theano.config.floatX)
69 self.b = theano.shared(value= b_values)
70
71 self.output = T.nnet.sigmoid(T.dot(input, self.W) + self.b)
72 self.params = [self.W, self.b]
73
74
75
76 class dA(object):
77 def __init__(self, n_visible= 784, n_hidden= 500, corruption_level = 0.1,\
78 input = None, shared_W = None, shared_b = None):
79 self.n_visible = n_visible
80 self.n_hidden = n_hidden
81
82 # create a Theano random generator that gives symbolic random values
83 theano_rng = RandomStreams()
84
85 if shared_W != None and shared_b != None :
86 self.W = shared_W
87 self.b = shared_b
88 else:
89 # initial values for weights and biases
90 # note : W' was written as `W_prime` and b' as `b_prime`
91
92 # W is initialized with `initial_W` which is uniformely sampled
93 # from -6./sqrt(n_visible+n_hidden) and 6./sqrt(n_hidden+n_visible)
94 # the output of uniform if converted using asarray to dtype
95 # theano.config.floatX so that the code is runable on GPU
96 initial_W = numpy.asarray( numpy.random.uniform( \
97 low = -numpy.sqrt(6./(n_hidden+n_visible)), \
98 high = numpy.sqrt(6./(n_hidden+n_visible)), \
99 size = (n_visible, n_hidden)), dtype = theano.config.floatX)
100 initial_b = numpy.zeros(n_hidden, dtype = theano.config.floatX)
101
102
103 # theano shared variables for weights and biases
104 self.W = theano.shared(value = initial_W, name = "W")
105 self.b = theano.shared(value = initial_b, name = "b")
106
107
108 initial_b_prime= numpy.zeros(n_visible)
109 # tied weights, therefore W_prime is W transpose
110 self.W_prime = self.W.T
111 self.b_prime = theano.shared(value = initial_b_prime, name = "b'")
112
113 # if no input is given, generate a variable representing the input
114 if input == None :
115 # we use a matrix because we expect a minibatch of several examples,
116 # each example being a row
117 self.x = T.dmatrix(name = 'input')
118 else:
119 self.x = input
120 # Equation (1)
121 # keep 90% of the inputs the same and zero-out randomly selected subset of 10% of the inputs
122 # note : first argument of theano.rng.binomial is the shape(size) of
123 # random numbers that it should produce
124 # second argument is the number of trials
125 # third argument is the probability of success of any trial
126 #
127 # this will produce an array of 0s and 1s where 1 has a
128 # probability of 1 - ``corruption_level`` and 0 with
129 # ``corruption_level``
130 self.tilde_x = theano_rng.binomial( self.x.shape, 1, 1 - corruption_level) * self.x
131 # Equation (2)
132 # note : y is stored as an attribute of the class so that it can be
133 # used later when stacking dAs.
134 self.y = T.nnet.sigmoid(T.dot(self.tilde_x, self.W ) + self.b)
135 # Equation (3)
136 self.z = T.nnet.sigmoid(T.dot(self.y, self.W_prime) + self.b_prime)
137 # Equation (4)
138 # note : we sum over the size of a datapoint; if we are using minibatches,
139 # L will be a vector, with one entry per example in minibatch
140 #self.L = - T.sum( self.x*T.log(self.z) + (1-self.x)*T.log(1-self.z), axis=1 )
141 #self.L = binary_cross_entropy(target=self.x, output=self.z, sum_axis=1)
142
143 # bypassing z to avoid running to log(0)
144 #self.z_a = T.dot(self.y, self.W_prime) + self.b_prime)
145 #self.L = -T.sum( self.x * (T.log(1)-T.log(1+T.exp(-self.z_a))) \
146 # + (1.0-self.x) * (T.log(1)-T.log(1+T.exp(-self.z_a))), axis=1 )
147
148 # I added this epsilon to avoid getting log(0) and 1/0 in grad
149 # This means conceptually that there'd be no probability of 0, but that
150 # doesn't seem to me as important (maybe I'm wrong?).
151 eps = 0.00000001
152 eps_1 = 1-eps
153 self.L = - T.sum( self.x * T.log(eps + eps_1*self.z) \
154 + (1-self.x)*T.log(eps + eps_1*(1-self.z)), axis=1 )
155 # note : L is now a vector, where each element is the cross-entropy cost
156 # of the reconstruction of the corresponding example of the
157 # minibatch. We need to compute the average of all these to get
158 # the cost of the minibatch
159 self.cost = T.mean(self.L)
160
161 self.params = [ self.W, self.b, self.b_prime ]
162
163
164 class SdA(object):
165 def __init__(self, train_set_x, train_set_y, batch_size, n_ins,
166 hidden_layers_sizes, n_outs,
167 corruption_levels, rng, pretrain_lr, finetune_lr, input_divider=1.0):
168 # Just to make sure those are not modified somewhere else afterwards
169 hidden_layers_sizes = copy.deepcopy(hidden_layers_sizes)
170 corruption_levels = copy.deepcopy(corruption_levels)
171
172 update_locals(self, locals())
173
174 self.layers = []
175 self.pretrain_functions = []
176 self.params = []
177 # MODIF: added this so we also get the b_primes
178 # (not used for finetuning... still using ".params")
179 self.all_params = []
180 self.n_layers = len(hidden_layers_sizes)
181
182 print "Creating SdA with params:"
183 print "batch_size", batch_size
184 print "hidden_layers_sizes", hidden_layers_sizes
185 print "corruption_levels", corruption_levels
186 print "n_ins", n_ins
187 print "n_outs", n_outs
188 print "pretrain_lr", pretrain_lr
189 print "finetune_lr", finetune_lr
190 print "input_divider", input_divider
191 print "----"
192
193 self.shared_divider = theano.shared(numpy.asarray(input_divider, dtype=theano.config.floatX))
194
195 if len(hidden_layers_sizes) < 1 :
196 raiseException (' You must have at least one hidden layer ')
197
198
199 # allocate symbolic variables for the data
200 index = T.lscalar() # index to a [mini]batch
201 self.x = T.matrix('x') # the data is presented as rasterized images
202 self.y = T.ivector('y') # the labels are presented as 1D vector of
203 # [int] labels
204
205 for i in xrange( self.n_layers ):
206 # construct the sigmoidal layer
207
208 # the size of the input is either the number of hidden units of
209 # the layer below or the input size if we are on the first layer
210 if i == 0 :
211 input_size = n_ins
212 else:
213 input_size = hidden_layers_sizes[i-1]
214
215 # the input to this layer is either the activation of the hidden
216 # layer below or the input of the SdA if you are on the first
217 # layer
218 if i == 0 :
219 layer_input = self.x
220 else:
221 layer_input = self.layers[-1].output
222
223 layer = SigmoidalLayer(rng, layer_input, input_size,
224 hidden_layers_sizes[i] )
225 # add the layer to the
226 self.layers += [layer]
227 self.params += layer.params
228
229 # Construct a denoising autoencoder that shared weights with this
230 # layer
231 dA_layer = dA(input_size, hidden_layers_sizes[i], \
232 corruption_level = corruption_levels[0],\
233 input = layer_input, \
234 shared_W = layer.W, shared_b = layer.b)
235
236 self.all_params += dA_layer.params
237
238 # Construct a function that trains this dA
239 # compute gradients of layer parameters
240 gparams = T.grad(dA_layer.cost, dA_layer.params)
241 # compute the list of updates
242 updates = {}
243 for param, gparam in zip(dA_layer.params, gparams):
244 updates[param] = param - gparam * pretrain_lr
245
246 # create a function that trains the dA
247 update_fn = theano.function([index], dA_layer.cost, \
248 updates = updates,
249 givens = {
250 self.x : train_set_x[index*batch_size:(index+1)*batch_size] / self.shared_divider})
251 # collect this function into a list
252 self.pretrain_functions += [update_fn]
253
254
255 # We now need to add a logistic layer on top of the MLP
256 self.logLayer = LogisticRegression(\
257 input = self.layers[-1].output,\
258 n_in = hidden_layers_sizes[-1], n_out = n_outs)
259
260 self.params += self.logLayer.params
261 self.all_params += self.logLayer.params
262 # construct a function that implements one step of finetunining
263
264 # compute the cost, defined as the negative log likelihood
265 cost = self.logLayer.negative_log_likelihood(self.y)
266 # compute the gradients with respect to the model parameters
267 gparams = T.grad(cost, self.params)
268 # compute list of updates
269 updates = {}
270 for param,gparam in zip(self.params, gparams):
271 updates[param] = param - gparam*finetune_lr
272
273 self.finetune = theano.function([index], cost,
274 updates = updates,
275 givens = {
276 self.x : train_set_x[index*batch_size:(index+1)*batch_size]/self.shared_divider,
277 self.y : train_set_y[index*batch_size:(index+1)*batch_size]} )
278
279 # symbolic variable that points to the number of errors made on the
280 # minibatch given by self.x and self.y
281
282 self.errors = self.logLayer.errors(self.y)
283
284 if __name__ == '__main__':
285 import sys
286 args = sys.argv[1:]
287