-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiments.py
363 lines (244 loc) · 11.1 KB
/
experiments.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
import Utils
import numpy as np
from RBF_NN import rbf_model
from SOM import SOM
def experiment_rbf_with_noise():
input_type = 'sin'
np.random.seed(123)
noise = 0.1
lr = 0.1
n_epochs = 200
batch_train = False
cov = 0.2
x_train, y_train, x_test, y_test = Utils.create_dataset(input_type, noise=noise)
predictions_ = []
best_error = 100
best_node = -1
for i in np.arange(1, 49, 1):
# mean = Utils.compute_rbf_centers(i)
mean = Utils.compute_rbf_centers_competitive_learning(x_train, i, eta=0.2, iterations=600,threshold=0.2)
model = rbf_model(x_train, y_train, mean, cov, n_epochs, i, lr, batch_train=batch_train)
model.fit()
predictions = model.forward_pass(x_test, True)
error = model.evaluate(x_test, y_test, transform=True)
if error < best_error:
best_error = error
best_node = i
predictions_ = predictions
print("best error {0}, best node {1}".format(best_error, best_node))
print("best error {0}, best node {1}".format(best_error, best_node))
Utils.plot_pred_actual(predictions_, y_test, 'sequential noisy RBF , sin(2x) lr : {0}, sigma : {1}'.format(lr,cov))
# without noise best nodes = 61
# with noise best nodes = 10
def experiment_competitive_learning():
input_type = 'sin'
np.random.seed(123)
num_hidden_units = 5
noise = 0.1
lr_ = 0.2
n_epochs = 100
batch_train = False
cov = 0.5
x_train, y_train, x_test, y_test = Utils.create_dataset(input_type, noise=noise)
[predictions, error, mean] = run_rbf_expe(units=9, noise=noise, lr=lr_,
epochs=n_epochs,batch=batch_train, cov=cov, competitive=False)
[predictions_competitive, error_competitive, mean_competitive] = run_rbf_expe(units=9, noise=noise, lr=lr_,
epochs=n_epochs,batch=batch_train, cov=cov, competitive=True)
print("number of hidden units: {0} test Error: {1}".format(num_hidden_units, error))
print("competitive number of hidden units: {0} test Error: {1}".format(num_hidden_units, error_competitive))
Utils.plot_data_means_1D(x_train, mean, mean_competitive, 'Plot data along with means')
error = [predictions, predictions_competitive]
legend = ['Manual init', 'Competitive init', 'Actual']
Utils.plot_many_lines(error, y_test, legend, 'RBF network with lr : {1} , epochs : {2}, width : {3}'
.format(num_hidden_units,lr_, n_epochs,cov))
def run_rbf_expe( units = 9, noise = 0, lr = 0 , epochs = 500 , batch = True, cov = 0.3, competitive = True, threshold=0):
input_type = 'sin'
np.random.seed(123)
num_hidden_units = units
n_epochs = epochs
batch_train = batch
cov = cov
x_train, y_train, x_test, y_test = Utils.create_dataset(input_type, noise=noise)
if competitive:
mean = Utils.compute_rbf_centers_competitive_learning(x_train, num_hidden_units, eta=0.2,
iterations=600,threshold=threshold)
else:
mean = Utils.compute_rbf_centers(num_hidden_units)
model_competitive = rbf_model(x_train, y_train, mean, cov, n_epochs, num_hidden_units, lr, batch_train=batch_train)
model_competitive.fit()
predictions_competitive = model_competitive.forward_pass(x_test, True)
error_competitive = model_competitive.evaluate(x_test, y_test, transform=True)
return [predictions_competitive, error_competitive, mean]
def experiment_batch_without_noise():
input_type = 'sin'
np.random.seed(123)
lr = 0.2
n_epochs = 300
batch_train = True
cov = 1
x_train, y_train, x_test, y_test = Utils.create_dataset(input_type)
predictions = []
best_error = 100
best_node = -1
for i in np.arange(1, len(x_train), 1):
mean = Utils.compute_rbf_centers(i)
model = rbf_model(x_train, y_train, mean, cov, n_epochs, i, lr, batch_train=batch_train)
model.fit()
predictions = model.forward_pass(x_test, True)
error = model.evaluate(x_test, y_test, transform=True)
if error < best_error:
best_error = error
best_node = i
predictions = predictions
print("number of hidden units: {0} test Error: {1}".format(i, error))
print("best error {0}, best node {1}".format(best_error, best_node))
Utils.plot_pred_actual(predictions, y_test, '')
def experiment_competitive_learning_vanilla_comparison():
input_type = 'sin'
np.random.seed(123)
num_hidden_units = 5
noise = 0.1
lr_ = 0.2
n_epochs = 100
batch_train = False
cov = 0.5
x_train, y_train, x_test, y_test = Utils.create_dataset(input_type, noise=noise)
[predictions, error] = run_rbf_expe(units=5, noise=noise, lr=lr_,
epochs=n_epochs, batch=batch_train, cov=cov, competitive=True, threshold = 0)
[predictions_competitive, error_competitive] = run_rbf_expe(units=5, noise=noise, lr=lr_,
epochs=n_epochs, batch=batch_train, cov=cov,
competitive=True, threshold = 0.2)
print("number of hidden units: {0} test Error: {1}".format(num_hidden_units, error))
print("competitive number of hidden units: {0} test Error: {1}".format(num_hidden_units, error_competitive))
error = [predictions, predictions_competitive]
legend = ['Vanilla CL', 'Many winners CL', 'Actual']
Utils.plot_many_lines(error, y_test, legend, 'CL Comparison trained with with lr : {1} , epochs : {2}, width : {3}'
.format(num_hidden_units, lr_, n_epochs, cov))
def experiment_plot_error_with_nodes():
input_type = 'sin'
np.random.seed(123)
noise = 0.1
lr = 0.01
n_epochs = 300
batch_train = False
cov = 0.4
x_train, y_train, x_test, y_test = Utils.create_dataset(input_type, noise=noise)
num_nodes = 50
predictions_ = []
best_error = 100
best_node = -1
errors = np.zeros(num_nodes)
for i in np.arange(1, num_nodes, 1):
mean = Utils.compute_rbf_centers(i)
# mean = Utils.compute_rbf_centers_competitive_learning(x_train, i, eta=0.2, iterations=100,threshold=1)
model = rbf_model(x_train, y_train, mean, cov, n_epochs, i, lr, batch_train=batch_train)
model.fit()
predictions = model.forward_pass(x_test, True)
error = model.evaluate(x_test, y_test, transform=True)
errors[i] = error
if error < best_error:
best_error = error
best_node = i
predictions_ = predictions
print("best error {0}, best node {1}".format(best_error, best_node))
print("best error {0}, best node {1}".format(best_error, best_node))
# Utils.plot_pred_actual(predictions_, y_test, '')
Utils.plot_error_nodes(errors, num_nodes, ['error'],'seq noisy RBF , sin(2x) lr : {0}, sigma : {1}'.format(lr,cov))
def run_comp_learning():
input_type = 'sin'
np.random.seed(123)
num_hidden_units = 20
noise = 0.1
lr = 0.2
n_epochs = 100
batch_train = False
cov = 0.5
x_train, y_train, x_test, y_test = Utils.create_dataset(input_type, noise)
mean = Utils.compute_rbf_centers(num_hidden_units)
model = rbf_model(x_train, y_train, mean, cov, n_epochs, num_hidden_units, lr, batch_train=batch_train)
model.fit()
predictions = model.forward_pass(x_test, True)
error = model.evaluate(x_test, y_test, transform=True)
print(error)
def experimen_2d_data():
x_train, y_train, x_test, y_test = Utils.load_ballist_data()
lr = 0.1
n_epochs = 200
batch_train = False
threshold = 0
cov = 0.5
predictions_ = []
best_error = 100
best_node = -1
for i in np.arange(1, 15, 1):
mean = Utils.compute_rbf_centers_competitive_learning(x_train, i, eta=0.2, iterations=600,
threshold=threshold)
model = rbf_model(x_train, y_train, mean, cov, n_epochs, i, lr, batch_train=batch_train)
model.fit()
predictions = model.forward_pass(x_test, True)
error = model.evaluate(x_test, y_test, transform=True)
print(error)
if error < best_error:
best_error = error
best_node = i
predictions_ = predictions
print("best error {0}, best node {1}".format(best_error, best_node))
print("best error {0}, best node {1}".format(best_error, best_node))
best_mean_cl = Utils.compute_rbf_centers_competitive_learning(x_train, best_node, eta=0.2, iterations=200,
threshold=threshold)
best_means = Utils.compute_rbf_centers(best_node, x_train)
# print(best_mean_cl)
Utils.plot_data_means(x_train, best_means, best_mean_cl, 'Plot data along with means')
Utils.plot_pred_actual(predictions_, y_test, 'RBF on 2d data lr : {0}, sigma : {1}'.format(lr, cov))
def run_animals_experiment():
props, names = Utils.load_animals()
# props = shuffle(props)
weight_shape = (100, 84)
epochs = 20
eta = 0.2
som = SOM(shape=weight_shape, n_epochs=epochs, eta=eta, neighbors_num=50)
som.fit(props)
pred = som.predict(props, names)
print(pred)
def run_cities_experiment():
cities_data, cities_labels = Utils.load_cities()
weight_shape = (10, 2)
epochs = 20
eta = 0.2
som = SOM(shape=weight_shape, n_epochs=epochs, eta=eta, neighbors_num=7, neighbohood_function='circular')
som.fit(cities_data)
pred = som.predict(cities_data, cities_labels)
print(pred)
Utils.plot_cities_tour(cities_data, pred)
def run_mp_votes_experiment():
votes, mpnames, mpsex, mpdistrict, mpparty, votes_labels = Utils.load_MPs()
weight_shape = (100, 31)
epochs = 20
eta = 0.2
som = SOM(shape=weight_shape, n_epochs=epochs, eta=eta, neighbors_num=2, neighbohood_function='manhattan')
som.fit(votes)
pred = som.predict_2_D(votes)
# print(pred)
party_names = {0: 'No party', 1: 'M', 2: 'Fp', 3: 'S', 4: 'V', 5: 'MP', 6: 'KD', 7: 'C'}
sex_names = {0: 'Male', 1: 'Female'}
distr_names = {}
for i in range(1, 30):
distr_names[i] = "District: {}".format(i)
party_votes = Utils.generate_dict(pred, party_names, mpparty)
sex_votes = Utils.generate_dict(pred, sex_names, mpsex)
distr_votes = Utils.generate_dict(pred, distr_names, mpdistrict)
Utils.plot_mp_votes(sex_votes, sex_names, type='sex')
Utils.plot_mp_votes(party_votes, party_names, type = 'party')
Utils.plot_mp_votes(distr_votes, distr_names, isDistrict=True)
if __name__ == "__main__":
# run_comp_learning()
# experiment_plot_error_with_nodes()
# experiment_batch_without_noise()
# experiment_rbf_with_noise()
# experiment_competitive_learning()
# experiment_competitive_learning_vanilla_comparison()
# experimen_2d_data()
############# PART 2 ##################
# run_animals_experiment()
# run_cities_experiment()
run_mp_votes_experiment()