-
Notifications
You must be signed in to change notification settings - Fork 0
/
EBLS.py
481 lines (323 loc) · 13.8 KB
/
EBLS.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import numpy as np
from sklearn import preprocessing
from numpy import random
from scipy import linalg as LA
import time
class EBLS():
def __init__(self):
self.beta11 = []
self.distMaxAndMin = []
self.minOfEachWindow = []
self.beta = []
self.wh = []
self.beta2 = []
self.parameter = 0
self.correct_count = 0
self.data_counter = 0
self.t2_cal = 0
self.scaler2 = preprocessing.MinMaxScaler(feature_range=(-1, 1))
self.batch_size = 0
self.dot_product = []
self.L2_prev = []
self.weights = []
self.max = []
self.maxOfEachWindow = []
self.t2_trans = []
self.tx2 = []
self.acc = 0
self.acc_eachstep = []
self.batch_acc_ensemble = []
self.batch_prec_ensemble = []
self.batch_recall_ensemble = []
self.batch_subset_acc_ensemble = []
self.s = 0
self.C = 0
self.N1 = 0
self.N2 = 0
self.N3 = 0
def show_accuracy_ensemble_multi_label(self,predictLabel,Label, batch_size):
numerator = np.sum(np.logical_and(Label, predictLabel), axis = 1)
denominator = np.sum(np.logical_or(Label, predictLabel), axis = 1)
instance_accuracy = numerator/denominator
wrong_index = []
avg_accuracy = np.mean(instance_accuracy)
acc_tmp = avg_accuracy
self.data_counter += batch_size
batch_acc = acc_tmp
batch_acc = batch_acc * 100
#self.batch_acc_ensemble = []
self.batch_acc_ensemble.append(batch_acc)
tmp_all_batch = self.batch_acc_ensemble.copy()
all_acc = np.mean(tmp_all_batch)
return batch_acc,all_acc, self.batch_acc_ensemble , wrong_index
def show_f1_ex_ensemble_multi_label(self,prec,recall):
numirator = 2 * prec * recall
denominator = prec + recall
f1 = numirator / denominator
return f1
def show_prec_ex_ensemble_multi_label(self,predictLabel,Label, batch_size):
self.data_counter += batch_size
precision_num = np.sum(np.logical_and(Label, predictLabel), axis = 1)
# Total number of pred true labels
precision_den = np.sum(predictLabel, axis = 1)
# precision averaged over all training examples
avg_precision = np.mean(precision_num/precision_den)
batch_acc = avg_precision
batch_acc = batch_acc * 100
self.batch_prec_ensemble.append(batch_acc)
tmp_all_batch = self.batch_prec_ensemble.copy()
all_acc = np.mean(tmp_all_batch)
return all_acc, batch_acc#self.batch_prec_ensemble
def show_subset_accuracy(self,predictLabel,Label, batch_size):
num = np.sum(np.all(predictLabel == Label ,axis = 1))
den = batch_size
tmp = num/den
tmp = np.all(Label == predictLabel, axis = 1).mean()
avg_subse_acc = tmp
self.data_counter += batch_size
batch_acc = avg_subse_acc
batch_acc = batch_acc * 100
self.batch_subset_acc_ensemble.append(batch_acc)
tmp_all_batch = self.batch_subset_acc_ensemble.copy()
all_acc = np.mean(tmp_all_batch)
return all_acc, batch_acc#self.batch_subset_acc_ensemble
def show_recall_ex_ensemble_multi_label(self,predictLabel,Label, batch_size):
recall_num = np.sum(np.logical_and(Label, predictLabel), axis = 1)
recall_den = np.sum(Label, axis = 1)
tmp = recall_num/recall_den
nans = np.where(np.isnan(tmp))
tmp[nans] = 0
avg_recall = np.mean(tmp)
self.data_counter += batch_size
batch_acc = avg_recall
batch_acc = batch_acc * 100
self.batch_recall_ensemble.append(batch_acc)
tmp_all_batch = self.batch_recall_ensemble.copy()
all_acc = np.mean(tmp_all_batch)
return all_acc, batch_acc#self.batch_recall_ensemble
def show_accuracy_ensemble(self,predictLabel,Label, batch_size):
wrong_index = []
self.data_counter += batch_size
label_1 = np.zeros(Label.shape[0])
label_1 = Label.argmax(axis = 1)
predlabel = predictLabel.argmax(axis = 1)
choices = []
cc = 0
firstclass_c = 0
secondclass_c = 0
indexes = []
for j in list(range(Label.shape[0])):
if(label_1[j] != predlabel[j]):
wrong_index.append(j)
choices.append(0)
indexes.append(j)
elif (label_1[j] == predlabel[j]):
if(label_1[j] == 0 ):
firstclass_c += 1
if(label_1[j] == 1):
secondclass_c += 1
choices.append(1)
self.correct_count += 1
cc += 1
batch_acc = choices.count(1)/batch_size
batch_acc = batch_acc * 100
#print(batch_acc)
self.batch_acc_ensemble = []
self.batch_acc_ensemble.append(batch_acc)
return indexes,(round(self.correct_count/self.data_counter,4)) , self.batch_acc_ensemble , wrong_index
def show_accuracy(self,predictLabel,Label):
label_1 = np.zeros(Label.shape[0])
label_1 = Label.argmax(axis = 1)
predlabel = predictLabel.argmax(axis = 1)
choices = []
cc = 0
firstclass_c = 0
secondclass_c = 0
for j in list(range(Label.shape[0])):
if(label_1[j] != predlabel[j]):
choices.append(0)
if label_1[j] == predlabel[j]:
if(label_1[j] == 0):
firstclass_c += 1
if(label_1[j] == 1):
secondclass_c += 1
choices.append(1)
#self.correct_count += 1
cc += 1
batch_acc = choices.count(1)/self.batch_size
choices = np.array(choices)
return 0 , batch_acc
def tansig(self,x):
return (2/(1+np.exp(-2*x)))-1
def sigmoid(self,data):
return 1.0/(1+np.exp(-data))
def linear(data):
return data
def tanh(self,data):
return (np.exp(data)-np.exp(-data))/(np.exp(data)+np.exp(-data))
def relu(self,data):
return np.maximum(data,0)
def pinv2(self,A,row,reg):
return np.mat(reg*np.eye(A.shape[1])+A).I.dot(row)
def pinv(self,A,reg):
return np.mat(reg*np.eye(A.shape[1])+A.T.dot(A)).I.dot(A.T)
def shrinkage(self,a,b):
z = np.maximum(a - b, 0) - np.maximum( -a - b, 0)
return z
def sparse_bls_2(self,A,b,dot_product,L2_prev):
lam = 0.00001
itrs = 2
AABB = A.T.dot(A)
AABB = AABB + dot_product
m = A.shape[1]
n = b.shape[1]
x1 = np.zeros([m,n])
wk = x1
ok = x1
uk = x1
L1 = np.mat(AABB + np.eye(A.shape[1])).I
L2_1 = A.T.dot(b) + L2_prev
#L2_1 = normalize(L2_1)
L2 = L1.dot(L2_1)
for i in range(itrs):
ck = L2 + np.dot(L1,(ok - uk))
ok = self.shrinkage(ck + uk, lam)
uk = uk + ck - ok
wk = ok
return wk,AABB,L2_1
def BLS_online_init(self ,use_prep, train_x,s,C,N1,N2,N3):
self.s = s
self.C = C
self.N1 = N1
self.N2 = N2
self.N3 = N3
self.batch_size = len(train_x)
if(use_prep):
train_x = preprocessing.scale(train_x,axis = 1)
H1 = np.hstack([train_x, 0.1 * np.ones([train_x.shape[0],1])])
y = np.zeros([train_x.shape[0],N2*N1]);
self.weights = []
i = 0
for i in range(N2):
we = 2 * random.randn(H1.shape[1],N1)-1
self.weights.append(we)
A1 = H1.dot(we)
self.scaler2.partial_fit(A1)
A1 = self.scaler2.transform(A1)
new_dot_product = np.zeros([N1,N1])
new_L2_prev = np.zeros([N1,H1.shape[1]])
self.dot_product.append(new_dot_product)
self.L2_prev.append(new_L2_prev)
beta1,new_dot_product,new_L2_prev = self.sparse_bls_2(A1,H1,new_dot_product,new_L2_prev)
beta1 = beta1.T
self.beta11.append(beta1)
T1 = H1.dot(beta1)
self.minOfEachWindow.append(np.zeros([1,T1.shape[1]]))
self.maxOfEachWindow.append(np.zeros([1,T1.shape[1]]))
self.distMaxAndMin.append( T1.max(axis = 0) - T1.min(axis = 0))
T1 = (T1 - self.minOfEachWindow[i])/self.distMaxAndMin[i]
y[:,N1*i:N1*(i+1)] = T1
H2 = np.hstack([y, 0.1 * np.ones([y.shape[0],1])])
if N1*N2>=N3 :
random.seed(67797325)
wh = LA.orth(2 * random.randn(N2*N1+1,N3)-1)
else:
random.seed(67797325)
wh = LA.orth(2 * random.randn(N2*N1+1,N3).T-1).T
self.wh.append(wh)
T2 = H2.dot(self.wh)
T2 = T2.reshape(T2.shape[0], -1 )
#print(s)
self.parameter = s/np.max(T2)
T2 = self.tanh(T2 * self.parameter);
#T2 = T2 * 0
T3 = np.hstack([y,T2])
return T3
def test_first_phase(self,use_prep, test_x):
self.batch_size = len(test_x)
ymin = 0
if(use_prep):
test_x = preprocessing.scale(test_x,axis = 1)
HH1 = np.hstack([test_x, 0.1 * np.ones([test_x.shape[0],1])])
#HH1 = test_x
yy1=np.zeros([test_x.shape[0],self.N2*self.N1]);
i = 0
for i in range(self.N2):
beta1 = self.beta11[i]
TT1 = HH1.dot(beta1)
TT1 = (1 - ymin)*(TT1 - self.minOfEachWindow[i])/self.distMaxAndMin[i] - ymin
yy1[:,self.N1*i:self.N1*(i+1)]= TT1
HH2 = np.hstack([yy1, 0.1 * np.ones([yy1.shape[0],1])]);
TT2 = self.tanh(HH2.dot(self.wh[0]) * self.parameter)
TT2 = TT2.reshape(TT2.shape[0] , -1)
#TT2 = TT2 * 0
TT3 = np.hstack([yy1,TT2])
return TT3
def test_second_phase(self, TT3,test_y):
self.data_counter = self.data_counter + self.batch_size
self.batch_size = len(test_y)
x = TT3.dot( self.beta2)
x = np.array(x)
TestingAccuracy ,batch_acc = self.show_accuracy(x,test_y)
self.acc = TestingAccuracy * 100
batch_acc = batch_acc * 100
self.acc_eachstep.append(self.acc)
return self.acc_eachstep, batch_acc ,x
def just_test_second_phase(self, TT3):
x = TT3.dot( self.beta2)
x = np.array(x)
return x
def update_first_layer(self,use_prep,train_x):
self.batch_size = len(train_x)
train_xx = train_x
if(use_prep):
train_xx = preprocessing.scale(train_xx,axis = 1)
Hx1 = np.hstack([train_xx, 0.1 * np.ones([train_xx.shape[0],1])])
yx = np.zeros([train_xx.shape[0],self.N1*self.N2])
i = 0
for i in range(self.N2):
A1 = Hx1.dot(self.weights[i])
self.scaler2.partial_fit(A1)
A1 = self.scaler2.transform(A1)
beta1,new_dot_product,new_L2_prev = self.sparse_bls_2(A1,Hx1,self.dot_product[i],self.L2_prev[i])
self.dot_product[i] = new_dot_product
self.L2_prev[i] = new_L2_prev
beta1 = beta1.T
self.beta11[i] = (beta1)
Tx1 = Hx1.dot(beta1)
tmp_min = Tx1.min(axis = 0)
tmp_min_array = []
tmp_min_array.append(tmp_min)
tmp_min_array.append(self.minOfEachWindow[i])
tmp_min_array = np.array(tmp_min_array)
self.minOfEachWindow[i] = (tmp_min_array.min(axis = 0))
tmp_max = Tx1.max(axis = 0)
tmp_max_array = []
tmp_max_array.append(tmp_max)
tmp_max_array.append(self.maxOfEachWindow[i])
tmp_max_array = np.array(tmp_max_array)
self.maxOfEachWindow[i] = (tmp_max_array.max(axis = 0))
self.distMaxAndMin[i] = ( self.maxOfEachWindow[i] - self.minOfEachWindow[i])
Tx1 = (Tx1 - self.minOfEachWindow[i])/self.distMaxAndMin[i]
#if(counter_class == 1):
yx[:,self.N1*i:self.N1*(i+1)] = Tx1
Hx2 = np.hstack([yx, 0.1 * np.ones([yx.shape[0],1])]);
wh = self.wh[0]
self.parameter = self.s/np.max(Hx2)
t2 = self.tanh(Hx2.dot(wh) * self.parameter);
t2 = np.hstack([yx, t2])
return t2
def update_second_layer(self,t2,train_y1,C):
self.C = C
self.batch_size = len(train_y1)
t2 = np.array(t2)
if((self.t2_cal == 0) or ( len(self.t2_trans) == 0)):
self.t2_trans = t2.T.dot(t2)
self.tx2 = t2.T.dot(train_y1)
else:
self.t2_trans = self.t2_trans + (t2.T.dot(t2))
self.tx2 = self.tx2 + (t2.T).dot(train_y1)
betat = self.pinv2(self.t2_trans,self.tx2,self.C)
self.beta2 = betat
self.beta2 = np.array(self.beta2)
self.t2_cal = self.t2_cal + 1