annotate deep/stacked_dae/v_youssouf/stacked_dae.py @ 430:5777b5041ac9

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