/
test.py
317 lines (276 loc) · 9.11 KB
/
test.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
import os
import sys
import time
import traceback
import numpy as np
import linear_regression
import svm
import softmax
import features
import kernel
sys.path.append("..")
import utils
verbose = False
epsilon = 1e-6
def green(s):
return '\033[1;32m%s\033[m' % s
def yellow(s):
return '\033[1;33m%s\033[m' % s
def red(s):
return '\033[1;31m%s\033[m' % s
def log(*m):
print(" ".join(map(str, m)))
def log_exit(*m):
log(red("ERROR:"), *m)
exit(1)
def check_real(ex_name, f, exp_res, *args):
try:
res = f(*args)
except NotImplementedError:
log(red("FAIL"), ex_name, ": not implemented")
return True
if not np.isreal(res):
log(red("FAIL"), ex_name, ": does not return a real number, type: ", type(res))
return True
if not -epsilon < res - exp_res < epsilon:
log(red("FAIL"), ex_name, ": incorrect answer. Expected", exp_res, ", got: ", res)
return True
def equals(x, y):
if type(y) == np.ndarray:
return (np.abs(x - y) < epsilon).all()
return -epsilon < x - y < epsilon
def check_tuple(ex_name, f, exp_res, *args, **kwargs):
try:
res = f(*args, **kwargs)
except NotImplementedError:
log(red("FAIL"), ex_name, ": not implemented")
return True
if not type(res) == tuple:
log(red("FAIL"), ex_name, ": does not return a tuple, type: ", type(res))
return True
if not len(res) == len(exp_res):
log(red("FAIL"), ex_name, ": expected a tuple of size ", len(exp_res), " but got tuple of size", len(res))
return True
if not all(equals(x, y) for x, y in zip(res, exp_res)):
log(red("FAIL"), ex_name, ": incorrect answer. Expected", exp_res, ", got: ", res)
return True
def check_array(ex_name, f, exp_res, *args):
try:
res = f(*args)
except NotImplementedError:
log(red("FAIL"), ex_name, ": not implemented")
return True
if not type(res) == np.ndarray:
log(red("FAIL"), ex_name, ": does not return a numpy array, type: ", type(res))
return True
if not len(res) == len(exp_res):
log(red("FAIL"), ex_name, ": expected an array of shape ", exp_res.shape, " but got array of shape", res.shape)
return True
if not equals(res, exp_res):
log(red("FAIL"), ex_name, ": incorrect answer. Expected", exp_res, ", got: ", res)
return True
def check_list(ex_name, f, exp_res, *args):
try:
res = f(*args)
except NotImplementedError:
log(red("FAIL"), ex_name, ": not implemented")
return True
if not type(res) == list:
log(red("FAIL"), ex_name, ": does not return a list, type: ", type(res))
return True
if not len(res) == len(exp_res):
log(red("FAIL"), ex_name, ": expected a list of size ", len(exp_res), " but got list of size", len(res))
return True
if not all(equals(x, y) for x, y in zip(res, exp_res)):
log(red("FAIL"), ex_name, ": incorrect answer. Expected", exp_res, ", got: ", res)
return True
def check_get_mnist():
ex_name = "Get MNIST data"
train_x, train_y, test_x, test_y = utils.get_MNIST_data()
log(green("PASS"), ex_name, "")
def check_closed_form():
ex_name = "Closed form"
X = np.arange(1, 16).reshape(3, 5)
Y = np.arange(1, 4)
lambda_factor = 0.5
exp_res = np.array([-0.03411225, 0.00320187, 0.04051599, 0.07783012, 0.11514424])
if check_array(
ex_name, linear_regression.closed_form,
exp_res, X, Y, lambda_factor):
return
log(green("PASS"), ex_name, "")
def check_svm():
ex_name = "One vs rest SVM"
n, m, d = 5, 3, 7
train_x = np.random.random((n, d))
test_x = train_x[:m]
train_y = np.zeros(n)
train_y[-1] = 1
exp_res = np.zeros(m)
if check_array(
ex_name, svm.one_vs_rest_svm,
exp_res, train_x, train_y, test_x):
return
train_y = np.ones(n)
train_y[-1] = 0
exp_res = np.ones(m)
if check_array(
ex_name, svm.one_vs_rest_svm,
exp_res, train_x, train_y, test_x):
return
log(green("PASS"), ex_name, "")
def check_compute_probabilities():
ex_name = "Compute probabilities"
n, d, k = 3, 5, 7
X = np.arange(0, n * d).reshape(n, d)
zeros = np.zeros((k, d))
temp = 0.2
exp_res = np.ones((k, n)) / k
if check_array(
ex_name, softmax.compute_probabilities,
exp_res, X, zeros, temp):
return
theta = np.arange(0, k * d).reshape(k, d)
softmax.compute_probabilities(X, theta, temp)
exp_res = np.zeros((k, n))
exp_res[-1] = 1
if check_array(
ex_name, softmax.compute_probabilities,
exp_res, X, theta, temp):
return
log(green("PASS"), ex_name, "")
def check_compute_cost_function():
ex_name = "Compute cost function"
n, d, k = 3, 5, 7
X = np.arange(0, n * d).reshape(n, d)
Y = np.arange(0, n)
zeros = np.zeros((k, d))
temp = 0.2
lambda_factor = 0.5
exp_res = 1.9459101490553135
if check_real(
ex_name, softmax.compute_cost_function,
exp_res, X, Y, zeros, lambda_factor, temp):
return
log(green("PASS"), ex_name, "")
def check_run_gradient_descent_iteration():
ex_name = "Run gradient descent iteration"
n, d, k = 3, 5, 7
X = np.arange(0, n * d).reshape(n, d)
Y = np.arange(0, n)
zeros = np.zeros((k, d))
alpha = 2
temp = 0.2
lambda_factor = 0.5
exp_res = np.zeros((k, d))
exp_res = np.array([
[ -7.14285714, -5.23809524, -3.33333333, -1.42857143, 0.47619048],
[ 9.52380952, 11.42857143, 13.33333333, 15.23809524, 17.14285714],
[ 26.19047619, 28.0952381 , 30. , 31.9047619 , 33.80952381],
[ -7.14285714, -8.57142857, -10. , -11.42857143, -12.85714286],
[ -7.14285714, -8.57142857, -10. , -11.42857143, -12.85714286],
[ -7.14285714, -8.57142857, -10. , -11.42857143, -12.85714286],
[ -7.14285714, -8.57142857, -10. , -11.42857143, -12.85714286]
])
if check_array(
ex_name, softmax.run_gradient_descent_iteration,
exp_res, X, Y, zeros, alpha, lambda_factor, temp):
return
softmax.run_gradient_descent_iteration(X, Y, zeros, alpha, lambda_factor, temp)
log(green("PASS"), ex_name, "")
def check_update_y():
ex_name = "Update y"
train_y = np.arange(0, 10)
test_y = np.arange(9, -1, -1)
exp_res = (
np.array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0]),
np.array([0, 2, 1, 0, 2, 1, 0, 2, 1, 0])
)
if check_tuple(
ex_name, softmax.update_y,
exp_res, train_y, test_y):
return
log(green("PASS"), ex_name, "")
def check_project_onto_PC():
ex_name = "Project onto PC"
X = np.array([
[1, 2, 3],
[2, 4, 6],
[3, 6, 9],
[4, 8, 12],
]);
pcs = features.principal_components(X)
exp_res = np.array([
[5.61248608, 0],
[1.87082869, 0],
[-1.87082869, 0],
[-5.61248608, 0],
])
n_components = 2
if check_array(
ex_name, features.project_onto_PC,
exp_res, X, pcs, n_components):
return
log(green("PASS"), ex_name, "")
def check_polynomial_kernel():
ex_name = "Polynomial kernel"
n, m, d = 3, 5, 7
c = 1
p = 2
X = np.random.random((n, d))
Y = np.random.random((m, d))
try:
K = kernel.polynomial_kernel(X, Y, c, d)
except NotImplementedError:
log(red("FAIL"), ex_name, ": not implemented")
return True
for i in range(n):
for j in range(m):
exp = (X[i] @ Y[j] + c) ** d
got = K[i][j]
if (not equals(exp, got)):
log(
red("FAIL"), ex_name,
": values at ({}, {}) do not match. Expected {}, got {}"
.format(i, j, exp, got)
)
log(green("PASS"), ex_name, "")
def check_rbf_kernel():
ex_name = "RBF kernel"
n, m, d = 3, 5, 7
gamma = 0.5
X = np.random.random((n, d))
Y = np.random.random((m, d))
try:
K = kernel.rbf_kernel(X, Y, gamma)
except NotImplementedError:
log(red("FAIL"), ex_name, ": not implemented")
return True
for i in range(n):
for j in range(m):
exp = np.exp(-gamma * (np.linalg.norm(X[i] - Y[j]) ** 2))
got = K[i][j]
if (not equals(exp, got)):
log(
red("FAIL"), ex_name,
": values at ({}, {}) do not match. Expected {}, got {}"
.format(i, j, exp, got)
)
log(green("PASS"), ex_name, "")
def main():
log(green("PASS"), "Import mnist project")
try:
check_get_mnist()
check_closed_form()
check_svm()
check_compute_probabilities()
check_compute_cost_function()
check_run_gradient_descent_iteration()
check_update_y()
check_project_onto_PC()
check_polynomial_kernel()
check_rbf_kernel()
except Exception:
log_exit(traceback.format_exc())
if __name__ == "__main__":
main()