forked from aelaguiz/DeepLearningTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mlps.py
215 lines (165 loc) · 8.06 KB
/
test_mlps.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
import numpy
import random
import theano
import theano.tensor as T
from code import HiddenLayer as theano_HiddenLayer, MLP as theano_MLP
from trans_code import HiddenLayer, MLP, train_model, test_model, load_data
rng = numpy.random.RandomState(1234)
num_samples = 1000
num_hidden = 3
num_features = 5
num_outputs = 3
sx = T.matrix('x')
sy = T.ivector('y')
def theano_train_model(tmlp, learning_rate, L1_reg, L2_reg, sx, sy):
# 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 = tmlp.negative_log_likelihood(sy)\
+ L1_reg * tmlp.L1 \
+ L2_reg * tmlp.L2_sqr
#validate_model = theano.function(inputs=[index],
#outputs=classifier.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 with respect to theta (sotred in params)
# the resulting gradients will be stored in a list gparams
gparams = []
for param in tmlp.params:
gparam = T.grad(cost, param)
gparams.append(gparam)
# specify how to update the parameters of the model as a 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(tmlp.params, gparams):
updates[param] = param - learning_rate * gparam
# compiling a Theano function `train_model` that returns the cost, but
# in the same time updates the parameter of the model based on the rules
# defined in `updates`
train_model = theano.function(inputs=[sx, sy], outputs=cost,
updates=updates)
## compiling a Theano function that computes the mistakes that are made
## by the model on a minibatch
test_model = theano.function(inputs=[sx, sy],
outputs=tmlp.errors(sy))
return train_model, test_model
def run_test():
learning_rate = random.random()
L1_reg = random.random()
L2_reg = random.random()
hl = HiddenLayer(
rng, num_features, num_outputs, activation=numpy.tanh)
thl = theano_HiddenLayer(
rng, sx, num_features, num_outputs, W=hl.W, b=hl.b, activation=T.tanh)
mlp = MLP(
rng, n_in=num_features, n_hidden=num_hidden, n_out=num_outputs)
# Ensure that the theano MLP shares the same weights & biases as our MLP
hW = theano.shared(value=numpy.copy(mlp.hiddenLayer.W), name='W', borrow=True)
hb = theano.shared(value=numpy.copy(mlp.hiddenLayer.b), name='b', borrow=True)
tmlp = theano_MLP(
rng, sx, n_in=num_features, n_hidden=num_hidden, n_out=num_outputs, hW=hW, hb=hb)
thl_out = theano.function([sx], thl.output)
tmlp_p_y_given_x = theano.function([sx], tmlp.logRegressionLayer.p_y_given_x)
tmlp_neg_log = theano.function(
inputs=[sx, sy],
outputs=tmlp.negative_log_likelihood(sy))
tmlp_train_model, tmlp_test_model = theano_train_model(tmlp, learning_rate, L1_reg, L2_reg, sx, sy)
X = numpy.array(numpy.random.rand(num_samples, num_features), dtype=theano.config.floatX)
y = numpy.array(numpy.random.random_integers(0, num_outputs-1, num_samples), dtype='int32')
theirs = thl_out(X)
ours = hl.output(X)
assert numpy.allclose(theirs, ours, 0.005)
theirs = tmlp_p_y_given_x(X)
ours = mlp.logRegressionLayer.p_y_given_x(mlp.hiddenLayer.output(X))
assert numpy.allclose(theirs, ours, 0.0000001)
theirs = tmlp_neg_log(X, y)
ours = mlp.negative_log_likelihood(X, y)
assert numpy.allclose(theirs, ours, 0.0000001)
for j in range(10):
X = numpy.array(numpy.random.rand(num_samples, num_features), dtype=theano.config.floatX)
y = numpy.array(numpy.random.random_integers(0, num_outputs-1, num_samples), dtype='int32')
test_X = numpy.array(numpy.random.rand(num_samples, num_features), dtype=theano.config.floatX)
test_y = numpy.array(numpy.random.random_integers(0, num_outputs-1, num_samples), dtype='int32')
train_model(mlp, learning_rate, X, y, 0, num_samples, L1_reg, L2_reg)
tmlp_train_model(X, y)
print "Testing regression layer..."
theirs = tmlp.logRegressionLayer.W.get_value(borrow=True)
ours = mlp.logRegressionLayer.W
assert numpy.allclose(theirs, ours, 0.005)
theirs = tmlp.logRegressionLayer.b.get_value(borrow=True)
ours = mlp.logRegressionLayer.b
assert numpy.allclose(theirs, ours, 0.005)
print "Testing hidden layer..."
theirs = tmlp.hiddenLayer.W.get_value(borrow=True)
ours = mlp.hiddenLayer.W
assert numpy.allclose(theirs, ours, 0.005)
theirs = tmlp.hiddenLayer.b.get_value(borrow=True)
ours = mlp.hiddenLayer.b
assert numpy.allclose(theirs, ours, 0.005)
theirs = tmlp_test_model(test_X, test_y)
ours = test_model(mlp, test_X, test_y, 0, num_samples)
assert numpy.allclose(theirs, ours, 0.005)
def do_mnist():
num_features = 784
num_hidden = 500
num_outputs = 10
L1_reg = 0.00
L2_reg = 0.0001
num_epochs = 1000
learning_rate = 0.01
batch_size = 500
datasets = load_data('data/mnist.pkl.gz')
train_set_x, train_set_y = datasets[0]
valid_set_x, valid_set_y = datasets[1]
test_set_x, test_set_y = datasets[2]
# compute number of minibatches for training, validation and testing
n_train_batches = train_set_x.shape[0] / batch_size
n_valid_batches = valid_set_x.shape[0] / batch_size
n_test_batches = test_set_x.shape[0] / batch_size
mlp = MLP(
rng, n_in=num_features, n_hidden=num_hidden, n_out=num_outputs)
## Ensure that the theano MLP shares the same weights & biases as our MLP
#hW = theano.shared(value=numpy.copy(mlp.hiddenLayer.W), name='W', borrow=True)
#hb = theano.shared(value=numpy.copy(mlp.hiddenLayer.b), name='b', borrow=True)
tmlp = theano_MLP(
rng, sx, n_in=num_features, n_hidden=num_hidden, n_out=num_outputs)
tmlp_train_model, tmlp_test_model = theano_train_model(tmlp, learning_rate, L1_reg, L2_reg, sx, sy)
for epoch in range(num_epochs):
print "Epoch", epoch
for minibatch_index in xrange(n_train_batches):
print "Minibatch", minibatch_index
X = train_set_x[minibatch_index * batch_size:(minibatch_index + 1) * batch_size]
y = train_set_y[minibatch_index * batch_size:(minibatch_index + 1) * batch_size]
train_model(mlp, learning_rate, X, y, 0, len(X), L1_reg, L2_reg)
tmlp_train_model(X, y)
#print "Testing regression layer..."
#theirs = tmlp.logRegressionLayer.W.get_value(borrow=True)
#ours = mlp.logRegressionLayer.W
#print theirs
#print ours
#print format(numpy.sum(abs(theirs - ours)), 'f')
#assert numpy.allclose(theirs, ours, 0.005)
#theirs = tmlp.logRegressionLayer.b.get_value(borrow=True)
#ours = mlp.logRegressionLayer.b
#assert numpy.allclose(theirs, ours, 0.005)
#print "Testing hidden layer..."
#theirs = tmlp.hiddenLayer.W.get_value(borrow=True)
#ours = mlp.hiddenLayer.W
#assert numpy.allclose(theirs, ours, 0.005)
#theirs = tmlp.hiddenLayer.b.get_value(borrow=True)
#ours = mlp.hiddenLayer.b
#assert numpy.allclose(theirs, ours, 0.005)
print "Testing against test set..."
theirs = tmlp_test_model(test_set_x, test_set_y)
ours = test_model(
mlp, test_set_x, test_set_y, 0, test_set_x.shape[0])
print theirs
print ours
#assert numpy.allclose(theirs, ours, 0.005)
for i in range(2):
run_test()
#do_mnist()