-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_mlp.py
327 lines (254 loc) · 11.3 KB
/
post_mlp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import numpy
import theano
from theano import tensor as T
from hidden import HiddenLayer
from log_reg import *
import pickle as pkl
DEBUGGING = False
class Costs:
Crossentropy = "crossentropy"
NegativeLikelihood = "negativelikelihood"
class PosttrainMLP:
"""Post training:- Second phase MLP.
A multilayer perceptron is a feedforward artificial neural network model
that has one layer or more of hidden units and nonlinear activations.
Intermediate layers usually have as activation function thanh or the
sigmoid function (defined here by a ``SigmoidalLayer`` class) while the
top layer is a softamx layer (defined here by a ``LogisticRegression``
class).
"""
def __init__(self,
input,
n_in,
n_hidden,
n_out,
rng=None,
is_binary=False,
params_first_phase=None):
"""
Initialize the parameters for the multilayer perceptron
:type rng: numpy.random.RandomState
:param rng: a random number generator used to initialize weights
:type input: theano.tensor.TensorType
:param input: symbolic variable that describes the input of the
architecture (one minibatch)
:type n_in: int
:param n_in: number of input units, the dimension of the space in
which the datapoints lie
:type n_hidden: int
:param n_hidden: number of hidden units
:type n_out: int
:param n_out: number of output units, the dimension of the space in which
the labels lie.
"""
self.input = input
if rng == None:
rng = numpy.random.RandomState(1234)
if DEBUGGING:
theano.config.compute_test_value = 'raise'
self.input.tag.test_value = numpy.random.rand(1800, n_in)
# Since we are dealing with a one hidden layer MLP, this will
# translate into a TanhLayer connected to the LogisticRegression
# layer; this can be replaced by a SigmoidalLayer, or a layer
# implementing any other nonlinearity
self.hiddenLayer = HiddenLayer(rng=rng, input=self.input,
n_in=n_in, n_out=n_hidden,
activation=T.tanh)
self.state = "train"
self.is_binary = is_binary
self.out_dir = "out/"
# The logistic regression layer gets as input the hidden units
# of the hidden layer
self.logRegressionLayer = LogisticRegressionLayer(
input=self.hiddenLayer.output,
n_in=n_hidden,
n_out=n_out,
is_binary=is_binary,
rng=rng)
# L1 norm ; one regularization option is to enforce L1 norm to
# be small
self.L1 = abs(self.hiddenLayer.W).sum() \
+ abs(self.logRegressionLayer.W).sum()
# square of L2 norm ; one regularization option is to enforce
# square of L2 norm to be small
self.L2_sqr = (self.hiddenLayer.W ** 2).sum() \
+ (self.logRegressionLayer.W ** 2).sum()
# negative log likelihood of the MLP is given by the
# crossentropy of the output of the model, computed in the
# logistic regression layer
if is_binary:
self.crossentropy = self.logRegressionLayer.crossentropy
else:
self.crossentropy = self.logRegressionLayer.crossentropy_categorical
# negative log likelihood of the MLP is given by the negative
# log likelihood of the output of the model, computed in the
# logistic regression layer
self.negative_log_likelihood = self.logRegressionLayer.negative_log_likelihood
# same holds for the function computing the number of errors
self.errors = self.logRegressionLayer.errors
# Class memberships
self.class_memberships = self.logRegressionLayer.get_class_memberships(self.hiddenLayer.get_outputs(self.input))
self.train_pkl_file = "post_train_n_hidden_" + str(n_hidden) + "n_out_" + str(n_out)
self.test_pkl_file = "post_test_n_hidden_" + str(n_hidden) + "n_out_" + str(n_out)
if params_first_phase is not None:
self.params = params_first_phase
# the parameters of the model are the parameters of the two layer it is
# made out of
self.params = self.hiddenLayer.params + self.logRegressionLayer.params
self.data_dict = { 'Ws':[],
'costs':[],
'test_probs':[],
'train_probs':[],
'test_scores':[]}
def _shared_dataset(self, data_x, name="x"):
shared_x = theano.shared(numpy.asarray(list(data_x), dtype=theano.config.floatX))
shared_x.name = name
return shared_x
def save_data(self):
output = None
if self.state == "train":
output = open(self.out_dir + self.train_pkl_file + ".pkl", 'wb')
else:
output = open(self.out_dir + self.test_pkl_file + ".pkl", 'wb')
pkl.dump(self.data_dict, output)
self.data_dict['Ws'] = []
self.data_dict['costs'] = []
self.data_dict['test_scores'] = []
def posttrain(self,
learning_rate=0.1,
L1_reg=0.00,
L2_reg=0.0001,
n_epochs=80,
data=None,
labels=None,
cost_type=Costs.Crossentropy,
save_exp_data=False,
batch_size=20):
if data is None:
raise Exception("Post-training can't start without pretraining class membership probabilities.")
if labels is None:
raise Exception("Post-training can not start without posttraining class labels.")
self.state = "train"
self.learning_rate = learning_rate
train_set_x = self._shared_dataset(data, name="training_set")
train_set_y = labels
# compute number of minibatches for training
n_examples = train_set_x.get_value(borrow=True).shape[0]
n_train_batches = n_examples / batch_size
######################
# BUILD ACTUAL MODEL #
######################
print '...postraining the model'
# allocate symbolic variables for the data
index = T.lscalar('index') # index to a [mini]batch
y = T.ivector('y') # the labels are presented as 1D vector of int32
mode = "FAST_COMPILE" #DEBUG_MODE"
if DEBUGGING:
self.input.tag.test_value = numpy.random.rand(self)
index.tag.test_value = 0
y.tag.test_value = numpy.ones(n_examples)
mode = "DEBUG_MODE"
# the cost we minimize during training is the negative log likelihood of
# the model plus the regularization terms (L1 and L2); cost is expressed
# here symbolically.
cost = None
if cost_type == Costs.NegativeLikelihood:
cost = self.negative_log_likelihood(y) \
+ L1_reg * self.L1 \
+ L2_reg * self.L2_sqr
elif cost_type == Costs.Crossentropy:
cost = self.crossentropy(y) \
+ L1_reg * self.L1 \
+ L2_reg * self.L2_sqr
gparams = []
for param in self.params:
gparam = T.grad(cost, param)
gparams.append(gparam)
# specify how to update the parameters of the model as selfa dictionary
updates = {}
# given two list the zip A = [a1, a2, a3, a4] and B = [b1, b2, b3, b4] of
# same length, zip generates a list C of same size, where each element
# is a pair formed from the two lists :
# C = [(a1, b1), (a2, b2), (a3, b3) , (a4, b4)]
for param, gparam in zip(self.params, gparams):
updates[param] = param - learning_rate * gparam
# compiling a Theano function `train_model` that returns the cost, butx
# in the same time updates the parameter of the model based on the rules
# defined in `updates`
# p_y_given_x = self.class_memberships
train_model = theano.function(inputs=[index],
outputs=cost,
updates = updates,
givens = {
self.input: train_set_x[index * batch_size:(index + 1) * batch_size],
y: train_set_y[index * batch_size: (index + 1) * batch_size]
},
mode=mode)
if DEBUGGING:
theano.printing.debugprint(train_model)
epoch = 0
costs = []
Ws = []
while (epoch < n_epochs):
for minibatch_index in xrange(n_train_batches):
print "Postraining in Minibatch %i " % (minibatch_index)
minibatch_avg_cost = train_model(minibatch_index)
costs.append(float(minibatch_avg_cost))
Ws.append(self.params[2])
epoch +=1
if save_exp_data:
self.data_dict['Ws'].append(Ws)
self.data_dict['costs'].append([costs])
self.save_data()
return costs
def posttest(self,
data=None,
labels=None,
save_exp_data = False,
batch_size=20):
if data is None:
raise Exception("Post-training can't start without pretraining class membership probabilities.")
if labels is None:
raise Exception("Post-training can not start without posttraining class-membership probabilities.")
test_set_x = shared_dataset(data)
test_set_y = labels
self.state = "test"
# compute number of minibatches for training, validation and testing
n_examples = test_set_x.get_value(borrow=True).shape[0]
n_test_batches = n_examples / batch_size
print '...post-testing the model'
# allocate symbolic variables for the data
index = T.lscalar() # index to a [mini]batch
y = T.ivector('y') # the labels are presented as 1D vector of
# [int] labels
mode = "FAST_RUN"
if DEBUGGING:
theano.config.compute_test_value = 'raise'
index.tag.test_value = 0
y.tag.test_value = numpy.ones(n_examples)
mode = "DEBUG_MODE"
# the cost we minimize during training is the negative log likelihood of
# the model plus the regularization terms (L1 and L2); cost is expressed
# here symbolically
# compiling a Theano function `test_model` that returns the cost, but
# in the same time updates the parameter of the model based on the rules
# defined in `updates`
test_model = theano.function(inputs=[index],
outputs=self.errors(y),
givens={
self.input: test_set_x[index * batch_size:(index + 1) * batch_size],
y: test_set_y[index * batch_size: (index + 1) * batch_size]},
mode=mode)
###############
# TEST MODEL #
###############
test_losses = []
for minibatch_index in xrange(n_test_batches):
test_losses.append(float(test_model(minibatch_index)))
test_score = numpy.mean(test_losses)
print("Minibatch %i, mean test error %f" % (minibatch_index, test_losses[minibatch_index] * 100))
print "In the end test score is %f" %(test_score * 100)
if save_exp_data:
self.data_dict['test_scores'].append(test_losses)
self.save_data()
return test_score, test_losses