forked from wangjin0818/Kaggle-MNIST-Competition
/
mlp.py
271 lines (224 loc) · 6.26 KB
/
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
import os
import sys
import logging
import time
import gzip
import cPickle
import numpy as np
import theano
import theano.tensor as T
import util
class LogisticRegression(object):
"""
Multi-class Logitic Regression Class
"""
def __init__(self, input, n_in, n_out):
# assign spacing for W and b
self.W = theano.shared(
value = np.zeros(
(n_in, n_out),
dtype = theano.config.floatX
),
name = 'W',
borrow = True
)
# initialize the baises b as a vector of n_out 0s
self.b = theano.shared(
value = np.zeros(
(n_out, ),
dtype = theano.config.floatX
),
name = 'b',
borrow = True
)
self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)
self.y_pred = T.argmax(self.p_y_given_x, axis=1)
# parameters of the model
self.params = [self.W, self.b]
def negative_log_likelihood(self, y):
"""
Return the mean of the negative log-likelihood of the prediction
of this model under a given target distribution.
"""
return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
def predict(self):
"""
Return the prediction of this model under a given target distribution.
"""
return self.y_pred
def errors(self, y):
"""
Return a float representing the number of errors in the minibatch
over the total number of examples of the minibatch ; zero one
loss over the size of the minibatch
"""
# check if y has same dimension of y_pred
if y.ndim != self.y_pred.ndim:
raise TypeError(
'y should have the same shape as self.y_pred',
('y', y.type, 'y_pred', self.y_pred.type)
)
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
class HiddenLayer(object):
def __init__(self, rng, input, n_in, n_out, W=None, b=None,
activation=T.tanh):
self.input = input
# if W and b is none, assign random value for them
if W is None:
W_values = np.asarray(
# assign uniform distribution for W_value
rng.uniform(
low=-np.sqrt(6. / (n_in + n_out)),
high=np.sqrt(6. / (n_in + n_out)),
size=(n_in, n_out)
),
dtype=theano.config.floatX
)
# if activation is sigmoid function, then low and high boundary
# of W_value will be multiply 4 times
if activation == theano.tensor.nnet.sigmoid:
W_values *= 4
W = theano.shared(value=W_values, name='W', borrow=True)
# as same as b
if b is None:
b_values = np.zeros((n_out, ), dtype=theano.config.floatX)
b = theano.shared(value=b_values, name='b', borrow=True)
self.W = W
self.b = b
# combine the output of the model
# y = activation(W * X + b)
lin_output = T.dot(input, self.W) + self.b
self.output = (
lin_output if activation is None
else activation(lin_output)
)
# parameters of the model
self.params = [self.W, self.b]
class MLP(object):
"""
Multi-Layer Perceptron Class
"""
def __init__(self, rng, input, n_in, n_hidden, n_out):
# hidden layer
self.hiddenLayer = HiddenLayer(
rng = rng,
input = input,
n_in = n_in,
n_out = n_hidden,
activation = T.tanh
)
# The logistic regression layer gets as input the hidden units
# of the hidden layer
self.logRegressionLayer = LogisticRegression(
input = self.hiddenLayer.output,
n_in = n_hidden,
n_out = n_out
)
# L1 regularization
self.L1 = (
abs(self.hiddenLayer.W).sum()
+ abs(self.logRegressionLayer.W).sum()
)
# L2 regularization
self.L2_sqr = (
(self.hiddenLayer.W ** 2).sum()
+ (self.logRegressionLayer.W ** 2).sum()
)
self.negative_log_likelihood = (
self.logRegressionLayer.negative_log_likelihood
)
self.errors = self.logRegressionLayer.errors
self.predict = self.logRegressionLayer.predict
self.params = self.hiddenLayer.params + self.logRegressionLayer.params
if __name__ == "__main__":
program = os.path.basename(sys.argv[0])
logger = logging.getLogger(program)
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')
logging.root.setLevel(level=logging.INFO)
logger.info("running %s" % ''.join(sys.argv))
logging.info('... loading data')
trainData, trainLabel = util.load_total_data()
testData = util.loadTestData()
trainData = util.upToInt(trainData)
## some parameters for training
learning_rate = 0.12
L1_reg = 0.001
L2_reg = 0.0001
n_hidden=500
train_set_x = theano.shared(np.asarray(trainData,
dtype = theano.config.floatX),
borrow = True)
train_set_y = theano.shared(np.asarray(trainLabel,
dtype = theano.config.floatX),
borrow = True)
test_set_x = theano.shared(np.asarray(testData,
dtype = theano.config.floatX),
borrow = True)
train_set_y = T.cast(train_set_y, 'int32')
logging.info('... building the model')
x = T.matrix('x') # the data is presented as tasterized images
y = T.ivector('y') # the labels are presented as 1D vector of
# [int] labels
rng = np.random.RandomState(1234)
classifier = MLP(
rng=rng,
input=x,
n_in=28*28,
n_hidden=n_hidden,
n_out=10
)
cost = (
classifier.negative_log_likelihood(y)
+ L1_reg * classifier.L1
+ L2_reg * classifier.L2_sqr
)
gparams = [T.grad(cost, param) for param in classifier.params]
updates = [
(param, param - learning_rate * gparams)
for param, gparams in zip(classifier.params, gparams)
]
## train model definition
train_model = theano.function(
inputs = [],
outputs = cost,
updates = updates,
givens = {
x: train_set_x,
y: train_set_y
}
)
logging.info('... training')
improvement_threshold = 0.001
epoch = 0
n_epochs = 1000
done_looping = False
prev_cost = np.inf
start_time = time.clock()
while (epoch < n_epochs) and (not done_looping):
epoch = epoch + 1
cost = train_model()
impr = prev_cost - cost
logging.info('epoch %d, cost: %f, impr: %f' % (epoch+1, cost, impr))
if impr < improvement_threshold:
break
prev_cost = cost
end_time = time.clock()
print >> sys.stderr, ('The code for file ' +
os.path.split(__file__)[1] +
' ran for %.2fm' % ((end_time - start_time) / 60.))
# make a prediction
predict_model = theano.function(
inputs=[],
outputs= classifier.predict(),
givens={
x: test_set_x
}
)
testLabel = predict_model()
util.saveResult(testLabel, './result/mlp_result.csv')