-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp_unit_test.py
386 lines (346 loc) · 11.5 KB
/
mlp_unit_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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import numpy as np
import matplotlib.pyplot as plt
from typing import List, Tuple, Dict, Any
from sklearn.model_selection import train_test_split
from nndiy import Sequential
from nndiy.layer import Linear
from nndiy.utils import one_hot
YELLOW = "\033[93m"
GREEN = "\033[92m"
RED = "\033[91m"
ENDC = "\033[0m"
class DataGeneration:
def __init__(self, nb_points, eps):
self.x, self.y = None, None
self.nb_points = nb_points
self.eps = eps
def display_data(self):
if self.x is None or self.y is None:
raise ValueError("Data is not generated. Nothing to display")
if self.x.shape[1] > 1:
plt.scatter(self.x[:, 0], self.x[:, 1], marker='.', c=self.y, cmap="brg")
plt.show()
else:
plt.scatter(self.x, self.y, marker='.')
plt.show()
def get_data(self):
return self.x, self.y
class ContinuousGen(DataGeneration):
def __init__(self, nb_points=1000, eps=0.1, sigma=0.1):
super().__init__(nb_points=nb_points, eps=eps)
self.sigma = sigma
def make_sinus(self, freq=2, ampli=1, affine=0.9):
self.x = np.linspace(0, np.pi, self.nb_points).reshape(-1, 1) * freq
self.y = np.sin(self.x + affine) * ampli
self._mix_data()
def make_regression(self, slope=1, affine=0):
self.x = np.linspace(-2, 2, self.nb_points).reshape(-1, 1)
self.y = self.x * slope + affine
self._mix_data()
def _mix_data(self):
self.y += np.random.normal(0, self.sigma, self.x.shape)
idx = np.random.permutation((range(self.y.size)))
self.x = self.x[idx,:]
self.y = self.y[idx,:]
class MultiClassGen(DataGeneration):
def __init__(self, nb_classes, nb_points=1000, eps=0.1):
super().__init__(nb_points=nb_points, eps=eps)
self.nb_classes = nb_classes
def make_vertical(self):
class_size = self.nb_points // self.nb_classes
self.x = np.zeros((class_size * self.nb_classes, 2))
self.y = np.zeros(class_size * self.nb_classes, dtype=np.uint8)
for cl in range(self.nb_classes):
ix = range(class_size * cl, class_size * (cl+1))
self.x[ix] = np.c_[np.random.randn(class_size)/10 + cl/3, np.random.randn(class_size)/10 + 0.5]
self.y[ix] = cl
def make_spiral(self):
class_size = self.nb_points // self.nb_classes
self.x = np.zeros((class_size * self.nb_classes, 2))
self.y = np.zeros(class_size * self.nb_classes, dtype=np.uint8)
for cl in range(self.nb_classes):
ix = range(class_size * cl, class_size * (cl+1))
r = np.linspace(0, 1, class_size)
t = np.linspace(cl * 4, (cl+1) * 4, class_size) + np.random.randn(class_size)*0.2
self.x[ix] = np.c_[r * np.sin(t*2.5), r * np.cos(t*2.5)]
self.y[ix] = cl
class TwoClassGen(MultiClassGen):
def __init__(self, nb_points=1000, eps=0.1):
super().__init__(nb_classes=2, nb_points=nb_points, eps=eps)
def make_2_gaussians(self, center_x=1, sigma=0.1):
x_one = np.random.multivariate_normal(
[center_x, center_x],
np.diag([sigma, sigma]),
self.nb_points // 2)
x_zero = np.random.multivariate_normal(
[-center_x, -center_x],
np.diag([sigma, sigma]),
self.nb_points // 2)
self.x = np.vstack((x_one, x_zero))
self.y = np.hstack(
(np.ones(self.nb_points // 2, dtype=np.uint8),
np.zeros(self.nb_points // 2, dtype=np.uint8)))
self._mix_data()
def make_4_gaussians(self, center_x=1, sigma=0.1):
x_one = np.vstack(
(np.random.multivariate_normal(
[center_x, center_x],
np.diag([sigma,sigma]),
self.nb_points // 4),
np.random.multivariate_normal(
[-center_x, -center_x],
np.diag([sigma, sigma]),
self.nb_points // 4)))
x_zero = np.vstack(
(np.random.multivariate_normal(
[-center_x, center_x],
np.diag([sigma, sigma]),
self.nb_points // 4),
np.random.multivariate_normal(
[center_x, -center_x],
np.diag([sigma, sigma]),
self.nb_points // 4)))
self.x = np.vstack((x_one,x_zero))
self.y = np.hstack(
(np.ones(self.nb_points // 2, dtype=np.uint8),
np.zeros(self.nb_points // 2, dtype=np.uint8)))
self._mix_data()
def make_checker_board(self):
self.x = np.random.uniform(-4 , 4, 2*self.nb_points).reshape((self.nb_points, 2))
y = np.ceil(self.x[:,0]) + np.ceil(self.x[:,1])
self.y = np.array(y % 2, dtype=np.uint8)
self._mix_data()
def _mix_data(self):
self.x[:,0] += np.random.normal(0, self.eps, self.nb_points)
self.x[:,1] += np.random.normal(0, self.eps, self.nb_points)
idx = np.random.permutation((range(self.y.size)))
self.x = self.x[idx, :]
self.y = self.y[idx].reshape(-1, 1)
def binary_classif_score(
Y_hat: np.ndarray,
Y: np.ndarray
):
predictions = np.argmax(Y_hat, axis=1).reshape(-1, 1)
return np.sum(predictions == Y) / Y.shape[0]
def multi_classif_score(
Y_hat: np.ndarray,
Y: np.ndarray
):
oh = not (len(Y.shape) == 1 or Y.shape[1] == 1) # labels are one-hot encoded?
Y = np.argmax(Y, axis=1) if oh else Y.reshape(-1)
predictions = np.argmax(Y_hat, axis=1)
return np.sum(predictions == Y) / Y.shape[0]
def mse_score(
Y_hat: np.ndarray,
Y: np.ndarray
):
return np.mean((Y_hat - Y) ** 2)
def run_test(
test_name: str, # Name of test for displaying purpose
X: np.ndarray, Y: np.ndarray, # Data
layers: List[Tuple[Linear, str]], # NN's layers in a list of tuple (Linear, activation_function_name)
model_kwargs: Dict[str, Any], # Keyword arguments to pass into NN's constructor
compile_kwargs: Dict[str, Any], # Keyword arguments to pass into NN's compile function
fit_kwargs: Dict[str, Any], # Keyword arguments to pass into NN's fit function
train_valid_test=(0.6,0.2,0.2), # Size of train, validation and test set, must sum to 1
target_score=0.85, # Desired NN's score for pass/fail assertion. Can be set to None
scoring_func=mse_score, # Function to calculate prediction's score
scoring_method="lt" # Comparator to NN's target score to decide whether test passes or fails, "lt" or "gt"
):
print(f"Testing {YELLOW}{test_name}{ENDC}:")
r_train, r_valid, r_test = train_valid_test
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=r_test)
X_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size=(r_valid/(r_valid+r_train)))
model = Sequential() if model_kwargs is None else Sequential(**model_kwargs)
for l, a in layers:
model.add(layer=l, activation=a)
model.compile(**compile_kwargs)
model.fit(X_train, Y_train, X_valid, Y_valid, **fit_kwargs)
score = scoring_func(model.predict(X_test), Y_test)
print(" Score: %.4f " % score, end='')
# model.plot_stats()
if target_score is None \
or (scoring_method == "lt" and score <= target_score) \
or (scoring_method == "gt" and score >= target_score):
print(f"{GREEN}OK{ENDC}")
return True
print(f"{RED}KO{ENDC}")
return False
if __name__ == '__main__':
np.random.seed(42)
#############################################################################
print("===== SIMPLE CLASSIFICATION PROBLEM WITH 2 CLASSES =====")
gen2C = TwoClassGen()
gen2C.make_2_gaussians(sigma=0.5)
# gen2C.display_data()
test_params = {
"2 Gaussians, BCE loss, GD": ("binary_crossentropy", "gd"),
"2 Gaussians, BCE loss, SGD": ("binary_crossentropy", "sgd"),
"2 Gaussians, BCE loss, MGD": ("binary_crossentropy", "mgd"),
"2 Gaussians, BCE loss, ADAM": ("binary_crossentropy", "adam"),
"2 Gaussians, Sparse BCE loss, GD": ("sparse_binary_crossentropy", "gd"),
"2 Gaussians, Sparse BCE loss, SGD": ("sparse_binary_crossentropy", "sgd"),
"2 Gaussians, Sparse BCE loss, MGD": ("sparse_binary_crossentropy", "mgd"),
"2 Gaussians, Sparse BCE loss, ADAM": ("sparse_binary_crossentropy", "adam"),
}
for name in test_params:
loss, optim = test_params[name]
if "sparse" not in loss:
Y = one_hot(gen2C.y, 2)
else:
Y = gen2C.y
run_test(
test_name=name,
X=gen2C.x, Y=Y,
layers=[
(Linear(2, 4), "tanh"),
(Linear(4, 2), "sigmoid")
],
model_kwargs=None,
compile_kwargs=dict(
loss=loss,
optimizer=optim,
learning_rate=1e-4,
metric="accuracy"
),
fit_kwargs=dict(
n_epochs=50,
verbose=False
),
target_score=0.85,
scoring_func=binary_classif_score,
scoring_method="gt"
)
gen2C.make_4_gaussians(sigma=0.2)
# gen2C.display_data()
test_params = {
"4 Gaussians, BCE loss, GD optim": ("binary_crossentropy", "gd"),
"4 Gaussians, BCE loss, SGD optim": ("binary_crossentropy", "sgd"),
"4 Gaussians, BCE loss, MGD optim": ("binary_crossentropy", "mgd"),
"4 Gaussians, BCE loss, ADAM optim": ("binary_crossentropy", "adam"),
"4 Gaussians, Sparse BCE loss, GD optim": ("sparse_binary_crossentropy", "gd"),
"4 Gaussians, Sparse BCE loss, SGD optim": ("sparse_binary_crossentropy", "sgd"),
"4 Gaussians, Sparse BCE loss, MGD optim": ("sparse_binary_crossentropy", "mgd"),
"4 Gaussians, Sparse BCE loss, ADAM optim": ("sparse_binary_crossentropy", "adam")
}
for name in test_params:
loss, optim = test_params[name]
if "sparse" not in loss:
Y = one_hot(gen2C.y, 2)
else:
Y = gen2C.y
run_test(
test_name=name,
X=gen2C.x, Y=Y,
layers=[
(Linear(2, 4), "tanh"),
(Linear(4, 2), "sigmoid")
],
model_kwargs=None,
compile_kwargs=dict(
loss=loss,
optimizer=optim,
learning_rate=1e-2,
n_batch = 20,
metric="accuracy"
),
fit_kwargs=dict(
n_epochs=50,
verbose=False
),
target_score=0.85,
scoring_func=binary_classif_score,
scoring_method="gt"
)
del gen2C
#############################################################################
print(end='\n')
nb_class = 4
print(f"===== CLASSIFICATION WITH {nb_class} CLASSES =====")
gen4C = MultiClassGen(nb_class)
gen4C.make_vertical()
# gen4C.display_data()
test_params = {
"Vertical data, CCE, GD optim": ("categorical_crossentropy", "gd"),
"Vertical data, CCE, SGD optim": ("categorical_crossentropy", "sgd"),
"Vertical data, CCpE, MGD optim": ("categorical_crossentropy", "mgd"),
"Vertical data, CCE, ADAM optim": ("categorical_crossentropy", "adam"),
"Vertical data, Sparse CCE, GD optim": ("sparse_categorical_crossentropy", "gd"),
"Vertical data, Sparse CCE, SGD optim": ("sparse_categorical_crossentropy", "sgd"),
"Vertical data, Sparse CCE, MGD optim": ("sparse_categorical_crossentropy", "mgd"),
"Vertical data, Sparse CCE, ADAM optim": ("sparse_categorical_crossentropy", "adam")
}
for name in test_params:
loss, optim = test_params[name]
if "sparse" not in loss:
Y = one_hot(gen4C.y, 4)
else:
Y = gen4C.y
run_test(
test_name=name,
X=gen4C.x, Y=Y,
layers=[
(Linear(2, nb_class * 4), "tanh"),
(Linear(nb_class * 4, nb_class), "sigmoid")
],
model_kwargs=None,
compile_kwargs=dict(
loss=loss,
optimizer=optim,
learning_rate=0.01,
metric="accuracy"
),
fit_kwargs=dict(
n_epochs=150,
verbose=False
),
target_score=0.85,
scoring_func=multi_classif_score,
scoring_method="gt"
)
del gen4C
#############################################################################
print(end='\n')
nb_class = 4
print("===== REGRESSION PROBLEM =====")
genCont = ContinuousGen()
genCont.make_regression()
# genCont.display_data()
params_optim = [
"gd",
"sgd",
"mgd",
"adam",
]
params_loss = [
"mse",
"mae",
"rmse",
]
for optim in params_optim:
for loss in params_loss:
name = f"Optimizer {optim}\tLoss function {loss}"
run_test(
test_name=name,
X=genCont.x, Y=genCont.y,
layers=[
(Linear(1, 4), "relu"),
(Linear(4, 1), "identity")
],
model_kwargs=None,
compile_kwargs=dict(
loss=loss,
optimizer=optim,
#learning_rate=8e-4
learning_rate=1e-4 if optim != "mgd" else 1e-5,
decay=(1e-4 * 5)
),
fit_kwargs=dict(
n_epochs=150,
verbose=False
),
target_score=0.1,
scoring_func=mse_score,
scoring_method="lt"
)
del genCont