-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogisticRegression.py
308 lines (247 loc) · 11.1 KB
/
LogisticRegression.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: ritesh
# @Date: 2015-09-05 09:50:19
# @Last Modified by: ritesh
# @Last Modified time: 2015-09-08 15:06:29
import os
import sys
import timeit
import cPickle
import gzip
import theano
from theano import tensor as T
import numpy
class LogisticRegression:
""" Logistic Regression for multiple class Classification
W: Weight matrix
b: bias vector
"""
def __init__(self, input, n_in, n_out):
""" Initialize the parameters
:type input: theano.tensor.TensorType
:param input: input of one minibatch
:type n_in: int
:param n_in: number of input units, datapoints
:type n_out: int
:param n_out: number of input units, labels
"""
self.W = theano.shared( value=numpy.zeros((n_in, n_out), dtype=theano.config.floatX),
name="W",
borrow=True
)
self.b = theano.shared( value=numpy.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)
self.params = [self.W, self.b]
self.input = input
def negative_log_likelihood(self, y):
""" Returns the mean of negative log-likelihood of prediction
:type y: theano.tensor
:param y: vector giving correct label to each examples
"""
return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
def errors(self, y):
""" Returns a float representing the number of errors
"""
if y.ndim != self.y_pred.ndim:
raise TypeError('y must be same size of y_pred', 'y', y.type, 'y_pred', y_pred.type)
if y.dtype.startswith('int'):
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError
def load_data(dataset):
""" Loads the dataset
:type dataset: string
:param dataset: the path to the dataset (here MNIST)
"""
# Download the MNIST dataset if it is not present
data_dir, data_file = os.path.split(dataset)
if data_dir == "" and not os.path.isfile(dataset):
# Check if dataset is in the data directory.
new_path = os.path.join(
os.path.split(__file__)[0],
"..",
"data",
dataset
)
if os.path.isfile(new_path) or data_file == 'mnist.pkl.gz':
dataset = new_path
if (not os.path.isfile(dataset)) and data_file == 'mnist.pkl.gz':
import urllib
origin = (
'http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz'
)
print 'Downloading data from %s' % origin
urllib.urlretrieve(origin, dataset)
print '... loading data'
# Load the dataset
f = gzip.open(dataset, 'rb')
train_set, valid_set, test_set = cPickle.load(f)
f.close()
def shared_dataset(data_xy, borrow=True):
""" Loads dataset in shared variables
"""
data_x, data_y = data_xy
shared_x = theano.shared(numpy.asarray(data_x, dtype=theano.config.floatX), borrow=borrow)
shared_y = theano.shared(numpy.asarray(data_y, dtype=theano.config.floatX), borrow=borrow)
return shared_x, T.cast(shared_y, 'int32')
train_set_x, train_set_y = shared_dataset(train_set)
valid_set_x, valid_set_y = shared_dataset(valid_set)
test_set_x, test_set_y = shared_dataset(test_set)
rval = [(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]
return rval
#optimization train
def sgd_optimization(dataset, learning_rate, n_epochs, batch_size):
"""
Stochastic gradient descent optimization with minibatches
:type dataset: string
:param dataset: MNIST dataset path
:type learning_rate: float
:param learning_rate: learning rate used
:type n_epochs: int
:param n_epochs: max epochs to run
"""
datasets = load_data(dataset)
train_set_x, train_set_y = datasets[0]
valid_set_x, valid_set_y = datasets[1]
test_set_x, test_set_y = datasets[2]
#number of minibatches
n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size
#build the model
print "... building the model"
index = T.lscalar()
x = T.matrix('x') #data for the rasterized images
y = T.ivector('y') # labels (int)
# logistic regression Class
classifierLR = LogisticRegression(input=x, n_in=28*28, n_out=10)
cost = classifierLR.negative_log_likelihood(y)
# test model (no updates)
test_model = theano.function(
inputs=[index],
outputs=classifierLR.errors(y),
givens={
x: test_set_x[index * batch_size: (index + 1) * batch_size],
y: test_set_y[index * batch_size: (index + 1) * batch_size]
}
)
#validate model (no updates)
validate_model = theano.function(
inputs=[index],
outputs=classifierLR.errors(y),
givens={
x: valid_set_x[index * batch_size: (index + 1) * batch_size],
y: valid_set_y[index * batch_size: (index + 1) * batch_size]
}
)
#compute the gradient of cost wrt W, b
g_W = T.grad(cost=cost, wrt=classifierLR.W)
g_b = T.grad(cost=cost, wrt=classifierLR.b)
#updating expression
updates = [(classifierLR.W, classifierLR.W - learning_rate * g_W),
(classifierLR.b, classifierLR.b - learning_rate * g_b)]
# Train model (theano function); updates
train_model = theano.function(
inputs=[index],
outputs=cost,
updates=updates,
givens={
x: train_set_x[index * batch_size: (index + 1) * batch_size],
y: train_set_y[index * batch_size: (index + 1) * batch_size]
}
)
# Training model (early stopping with validation examples)
print "... training the model"
patience = 5000
patience_inc = 2 # wait this much
improved_threshold = 0.995 # relative improvement (significant)
validation_frequency = min(n_train_batches, patience / 2)
best_validation_loss = numpy.inf
test_score = 0.
start_time = timeit.default_timer()
done_looping = False
epoch = 0
while (epoch < n_epochs) and (not done_looping):
epoch += 1
for minibatch_index in xrange(n_train_batches):
minibatch_avg_cost = train_model(minibatch_index)
iter = (epoch - 1) * n_train_batches + minibatch_index
if (iter + 1) % validation_frequency == 0:
# compute loss on validation set
validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]
this_validation_loss = numpy.mean(validation_losses)
print(
"Epoch: %i, minibatch: %i/%i, validation_error: %f %%" %
(
epoch,
minibatch_index + 1,
n_train_batches,
this_validation_loss * 100.
)
)
if this_validation_loss < best_validation_loss:
#improve patience if good improvement
if this_validation_loss < best_validation_loss * improved_threshold:
patience = max(patience, iter * patience_inc)
best_validation_loss = this_validation_loss
#testing on test_set
test_losses = [test_model(i) for i in xrange(n_test_batches)]
test_score = numpy.mean(test_losses)
print(
(
"Epoch : %i, minibatch %i/%i,"
" test error of best model %f %%"
) % (
epoch,
minibatch_index,
n_train_batches,
test_score * 100.
)
)
#save the best model
print "New best model found; saving ..."
with open('best_model.pkl', "w") as f:
cPickle.dump(classifierLR, f)
if patience <= iter:
done_looping = True
break
end_time = timeit.default_timer()
print(
(
"Optimization Complete: best validation score : %f %%,"
" test performance : %f %%"
)
% (best_validation_loss * 100., test_score * 100.)
)
print "The code run for %d epochs, with %f epochs/sec" %(epoch, 1. * epoch / (end_time - start_time))
print >> sys.stderr, ("The code for file " + os.path.split(__file__)[1] + " ran for %.1fs" % ((end_time - start_time)))
#predict
def predict():
"""Example to load the trained model and predict labels
"""
bestClassifierLR = cPickle.load(open("best_model.pkl"))
predict_model = theano.function(
inputs =[bestClassifierLR.input],
outputs=bestClassifierLR.y_pred)
dataset = "mnist.pkl.gz"
datasets = load_data(dataset)
test_set_x, test_set_y = datasets[2]
test_set_x = test_set_x.get_value()
predicted_values = predict_model(test_set_x[:10])
print ("Predicted values for the first 10 examples in the test set : ")
print predicted_values
#run
def main():
dataset = 'mnist.pkl.gz'
learning_rate = 0.1
n_epochs = 1000
batch_size = 600
sgd_optimization(dataset, learning_rate, n_epochs, batch_size)
if __name__ == '__main__':
# main() main()
predict()