pyerualjetwork 2.7.8__py3-none-any.whl → 4.1.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyerualjetwork/__init__.py +61 -0
- pyerualjetwork/activation_functions.py +341 -0
- pyerualjetwork/activation_functions_cuda.py +341 -0
- pyerualjetwork/data_operations.py +403 -0
- pyerualjetwork/data_operations_cuda.py +461 -0
- pyerualjetwork/help.py +16 -0
- pyerualjetwork/loss_functions.py +21 -0
- pyerualjetwork/loss_functions_cuda.py +21 -0
- pyerualjetwork/memory_operations.py +298 -0
- pyerualjetwork/metrics.py +190 -0
- pyerualjetwork/metrics_cuda.py +163 -0
- pyerualjetwork/model_operations.py +408 -0
- pyerualjetwork/model_operations_cuda.py +421 -0
- pyerualjetwork/plan.py +664 -0
- pyerualjetwork/plan_cuda.py +696 -0
- pyerualjetwork/planeat.py +743 -0
- pyerualjetwork/planeat_cuda.py +744 -0
- pyerualjetwork/ui.py +22 -0
- pyerualjetwork/visualizations.py +823 -0
- pyerualjetwork/visualizations_cuda.py +826 -0
- pyerualjetwork-4.1.9.dist-info/METADATA +120 -0
- pyerualjetwork-4.1.9.dist-info/RECORD +24 -0
- {pyerualjetwork-2.7.8.dist-info → pyerualjetwork-4.1.9.dist-info}/WHEEL +1 -1
- pyerualjetwork-4.1.9.dist-info/top_level.txt +1 -0
- plan/__init__.py +0 -5
- plan/plan.py +0 -1602
- pyerualjetwork-2.7.8.dist-info/METADATA +0 -8
- pyerualjetwork-2.7.8.dist-info/RECORD +0 -6
- pyerualjetwork-2.7.8.dist-info/top_level.txt +0 -1
plan/plan.py
DELETED
@@ -1,1602 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
"""
|
3
|
-
Created on Tue Jun 18 23:32:16 2024
|
4
|
-
|
5
|
-
@author: hasan
|
6
|
-
"""
|
7
|
-
|
8
|
-
import pandas as pd
|
9
|
-
import numpy as np
|
10
|
-
import time
|
11
|
-
from colorama import Fore, Style
|
12
|
-
from typing import List, Union
|
13
|
-
from scipy.special import expit, softmax
|
14
|
-
import matplotlib.pyplot as plt
|
15
|
-
import seaborn as sns
|
16
|
-
from tqdm import tqdm
|
17
|
-
|
18
|
-
# BUILD -----
|
19
|
-
|
20
|
-
|
21
|
-
def fit(
|
22
|
-
x_train: List[Union[int, float]],
|
23
|
-
y_train: List[Union[int, float]], # At least two.. and one hot encoded
|
24
|
-
val= None,
|
25
|
-
val_count = None,
|
26
|
-
activation_potentiation=None, # (float): Input activation_potentiation (optional)
|
27
|
-
x_val= None,
|
28
|
-
y_val= None,
|
29
|
-
show_training = None,
|
30
|
-
show_count= None
|
31
|
-
) -> str:
|
32
|
-
|
33
|
-
infoPLAN = """
|
34
|
-
Creates and configures a PLAN model.
|
35
|
-
|
36
|
-
Args:
|
37
|
-
x_train (list[num]): List of input data.
|
38
|
-
y_train (list[num]): List of target labels. (one hot encoded)
|
39
|
-
val (None, True or 'final'): validation in training process ? None, True or 'final' Default: None (optional)
|
40
|
-
val_count (None, int): After how many examples learned will an accuracy test be performed? Default: 0.1 (%10) (optional)
|
41
|
-
activation_potentiation (float): Input activation potentiation (for binary injection) (optional) in range: -1, 1
|
42
|
-
x_val (list[num]): List of validation data. (optional) Default: 1% of x_train (auto_balanced) it means every %1 of train progress starts validation
|
43
|
-
y_val (list[num]): (list[num]): List of target labels. (one hot encoded) (optional) Default: 1% of y_train (auto_balanced) it means every %1 of train progress starts validation
|
44
|
-
show_training (bool, str): True, None or'final'
|
45
|
-
show_count (None, int): How many learning steps in total will be displayed in a single figure? (Adjust according to your hardware) Default: 10 (optional)
|
46
|
-
Returns:
|
47
|
-
list([num]): (Weight matrices list, train_predictions list, Train_acc).
|
48
|
-
error handled ?: Process status ('e')
|
49
|
-
"""
|
50
|
-
|
51
|
-
if len(x_train) != len(y_train):
|
52
|
-
|
53
|
-
print(Fore.RED + "ERROR301: x_train list and y_train list must be same length. from: fit", infoPLAN + Style.RESET_ALL)
|
54
|
-
return 'e'
|
55
|
-
|
56
|
-
if val == True or val == 'final':
|
57
|
-
|
58
|
-
try:
|
59
|
-
|
60
|
-
if x_val == None and y_val == None:
|
61
|
-
|
62
|
-
x_train, x_val, y_train, y_val = split(x_train, y_train, test_size=0.1, random_state=42)
|
63
|
-
|
64
|
-
x_train, y_train = auto_balancer(x_train, y_train)
|
65
|
-
x_val, y_val = auto_balancer(x_val, y_val)
|
66
|
-
|
67
|
-
except:
|
68
|
-
pass
|
69
|
-
|
70
|
-
if val == True:
|
71
|
-
|
72
|
-
if val_count == None:
|
73
|
-
|
74
|
-
val_count = 0.01
|
75
|
-
|
76
|
-
v_iter = 0
|
77
|
-
|
78
|
-
if val == 'final':
|
79
|
-
|
80
|
-
val_count = 0.99
|
81
|
-
|
82
|
-
val_count = int(len(x_train) * val_count)
|
83
|
-
val_count_copy = val_count
|
84
|
-
val_bar = tqdm(total=1, desc="Validating Accuracy", ncols=120)
|
85
|
-
val_list = [] * val_count
|
86
|
-
|
87
|
-
if show_count == None:
|
88
|
-
|
89
|
-
show_count = 10
|
90
|
-
|
91
|
-
if show_training == True or show_training == 'final':
|
92
|
-
|
93
|
-
row, col = shape_control(x_train)
|
94
|
-
|
95
|
-
class_count = set()
|
96
|
-
|
97
|
-
for sublist in y_train:
|
98
|
-
|
99
|
-
class_count.add(tuple(sublist))
|
100
|
-
|
101
|
-
class_count = list(class_count)
|
102
|
-
|
103
|
-
y_train = [tuple(sublist) for sublist in y_train]
|
104
|
-
|
105
|
-
neurons = [len(class_count), len(class_count)]
|
106
|
-
layers = ['fex']
|
107
|
-
|
108
|
-
x_train[0] = np.array(x_train[0])
|
109
|
-
x_train[0] = x_train[0].ravel()
|
110
|
-
x_train_size = len(x_train[0])
|
111
|
-
|
112
|
-
STPW = weight_identification(
|
113
|
-
len(layers) - 1, len(class_count), neurons, x_train_size) # STPW = SHORT TIME POTENTIATION WEIGHT
|
114
|
-
|
115
|
-
LTPW = [0] * len(STPW) # LTPW = LONG TIME POTENTIATION WEIGHT
|
116
|
-
|
117
|
-
y = decode_one_hot(y_train)
|
118
|
-
|
119
|
-
train_progress = tqdm(total=len(x_train),leave=False, desc="Training",ncols= 120)
|
120
|
-
|
121
|
-
for index, inp in enumerate(x_train):
|
122
|
-
|
123
|
-
progress = index / len(x_train) * 100
|
124
|
-
|
125
|
-
inp = np.array(inp)
|
126
|
-
inp = inp.ravel()
|
127
|
-
|
128
|
-
if x_train_size != len(inp):
|
129
|
-
print(Fore.RED + "ERROR304: All input matrices or vectors in x_train list, must be same size. from: fit",
|
130
|
-
infoPLAN + Style.RESET_ALL)
|
131
|
-
return 'e'
|
132
|
-
|
133
|
-
neural_layer = inp
|
134
|
-
|
135
|
-
for Lindex, Layer in enumerate(layers):
|
136
|
-
|
137
|
-
if Layer == 'fex':
|
138
|
-
STPW[Lindex] = fex(neural_layer, STPW[Lindex], True, y[index], activation_potentiation)
|
139
|
-
|
140
|
-
for i, w in enumerate(STPW):
|
141
|
-
LTPW[i] = LTPW[i] + w
|
142
|
-
|
143
|
-
|
144
|
-
if val == True and index == val_count:
|
145
|
-
|
146
|
-
|
147
|
-
val_count += val_count_copy
|
148
|
-
|
149
|
-
validation_model = evaluate(x_val, y_val, LTPW, activation_potentiation, None)
|
150
|
-
|
151
|
-
val_acc = validation_model[get_acc()]
|
152
|
-
|
153
|
-
val_list.append(val_acc)
|
154
|
-
|
155
|
-
if v_iter == 0:
|
156
|
-
|
157
|
-
val_bar.update(val_acc)
|
158
|
-
|
159
|
-
|
160
|
-
if v_iter != 0:
|
161
|
-
|
162
|
-
val_acc = val_acc - val_list[v_iter - 1]
|
163
|
-
val_bar.update(val_acc)
|
164
|
-
|
165
|
-
v_iter += 1
|
166
|
-
|
167
|
-
if show_training == True:
|
168
|
-
|
169
|
-
if index %show_count == 0:
|
170
|
-
|
171
|
-
|
172
|
-
if index != 0:
|
173
|
-
plt.close(fig)
|
174
|
-
|
175
|
-
if row != 0:
|
176
|
-
|
177
|
-
fig, ax = plt.subplots(1, len(class_count), figsize=(18, 14))
|
178
|
-
|
179
|
-
else:
|
180
|
-
|
181
|
-
fig, ax = plt.subplots(1, 1, figsize=(18, 14))
|
182
|
-
|
183
|
-
for j in range(len(class_count)):
|
184
|
-
|
185
|
-
|
186
|
-
if row != 0:
|
187
|
-
|
188
|
-
mat = LTPW[0][j,:].reshape(row, col)
|
189
|
-
suptitle_info = 'Neurons Learning Progress: % '
|
190
|
-
title_info = f'{j+1}. Neuron'
|
191
|
-
|
192
|
-
mat = LTPW[0][j,:].reshape(row, col)
|
193
|
-
|
194
|
-
ax[j].imshow(mat, interpolation='sinc', cmap='viridis')
|
195
|
-
|
196
|
-
ax[j].set_aspect('equal')
|
197
|
-
|
198
|
-
ax[j].set_xticks([])
|
199
|
-
ax[j].set_yticks([])
|
200
|
-
ax[j].set_title(title_info)
|
201
|
-
|
202
|
-
else:
|
203
|
-
|
204
|
-
mat = LTPW[0]
|
205
|
-
ax.imshow(mat, interpolation='sinc', cmap='viridis')
|
206
|
-
suptitle_info = 'Weight Learning Progress: % '
|
207
|
-
title_info = 'Weight Matrix Of Fex Layer'
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
progress_status = f"{progress:.1f}"
|
213
|
-
fig.suptitle(suptitle_info + progress_status)
|
214
|
-
plt.draw()
|
215
|
-
plt.pause(0.1)
|
216
|
-
|
217
|
-
|
218
|
-
STPW = weight_identification(
|
219
|
-
len(layers) - 1, len(class_count), neurons, x_train_size)
|
220
|
-
|
221
|
-
train_progress.update(1)
|
222
|
-
|
223
|
-
if show_training == 'final':
|
224
|
-
|
225
|
-
fig, ax = plt.subplots(1, len(class_count), figsize=(18, 14))
|
226
|
-
|
227
|
-
for j in range(len(class_count)):
|
228
|
-
|
229
|
-
mat = LTPW[0][j,:].reshape(row, col)
|
230
|
-
|
231
|
-
ax[j].imshow(mat, interpolation='sinc', cmap='viridis')
|
232
|
-
ax[j].set_aspect('equal')
|
233
|
-
|
234
|
-
ax[j].set_xticks([])
|
235
|
-
ax[j].set_yticks([])
|
236
|
-
ax[j].set_title(f'{j+1}. Neuron')
|
237
|
-
|
238
|
-
progress_status = f"{progress:.1f}"
|
239
|
-
fig.suptitle('Neurons Learning Progress: % ' + progress_status)
|
240
|
-
plt.draw()
|
241
|
-
plt.pause(0.1)
|
242
|
-
|
243
|
-
|
244
|
-
if val == 'final':
|
245
|
-
|
246
|
-
validation_model = evaluate(x_val, y_val, LTPW, activation_potentiation, bar_status=None, show_metrices=None)
|
247
|
-
|
248
|
-
val_acc = validation_model[get_acc()]
|
249
|
-
|
250
|
-
val_list.append(val_acc)
|
251
|
-
|
252
|
-
val_bar.update(val_acc)
|
253
|
-
|
254
|
-
LTPW = normalization(LTPW)
|
255
|
-
|
256
|
-
return LTPW
|
257
|
-
|
258
|
-
# FUNCTIONS -----
|
259
|
-
|
260
|
-
def shape_control(x_train):
|
261
|
-
|
262
|
-
try:
|
263
|
-
row = x_train[1].shape[0]
|
264
|
-
col = x_train[1].shape[1]
|
265
|
-
|
266
|
-
except:
|
267
|
-
|
268
|
-
print(Fore.MAGENTA + 'WARNING: You trying show_training but inputs is raveled. x_train inputs should be reshaped for show_training.' + Style.RESET_ALL)
|
269
|
-
|
270
|
-
try:
|
271
|
-
row, col = find_numbers(len(x_train[0]))
|
272
|
-
|
273
|
-
except:
|
274
|
-
|
275
|
-
print(Fore.MAGENTA + 'WARNING: Input length cannot be reshaped. Neurons learning progression cannot be draw, weight learning progress drwaing started.' + Style.RESET_ALL)
|
276
|
-
return [0, 0]
|
277
|
-
|
278
|
-
return row, col
|
279
|
-
|
280
|
-
def find_numbers(n):
|
281
|
-
if n <= 1:
|
282
|
-
raise ValueError("Parameter 'n' must be greater than 1.")
|
283
|
-
|
284
|
-
for i in range(2, int(n**0.5) + 1):
|
285
|
-
if n % i == 0:
|
286
|
-
factor1 = i
|
287
|
-
factor2 = n // i
|
288
|
-
if factor1 == factor2:
|
289
|
-
return factor1, factor2
|
290
|
-
|
291
|
-
return None
|
292
|
-
|
293
|
-
def weight_normalization(
|
294
|
-
W,
|
295
|
-
class_count
|
296
|
-
) -> str:
|
297
|
-
"""
|
298
|
-
Row(Neuron) based normalization. For unbalanced models.
|
299
|
-
|
300
|
-
Args:
|
301
|
-
W (list(num)): Trained weight matrix list.
|
302
|
-
class_count (int): Class count of model.
|
303
|
-
|
304
|
-
Returns:
|
305
|
-
list([numpy_arrays],[...]): posttrained weight matices of the model. .
|
306
|
-
"""
|
307
|
-
|
308
|
-
for i in range(class_count):
|
309
|
-
|
310
|
-
W[0][i,:] = normalization(W[0][i,:])
|
311
|
-
|
312
|
-
return W
|
313
|
-
|
314
|
-
def weight_identification(
|
315
|
-
layer_count, # int: Number of layers in the neural network.
|
316
|
-
class_count, # int: Number of classes in the classification task.
|
317
|
-
neurons, # list[num]: List of neuron counts for each layer.
|
318
|
-
x_train_size # int: Size of the input data.
|
319
|
-
) -> str:
|
320
|
-
"""
|
321
|
-
Identifies the weights for a neural network model.
|
322
|
-
|
323
|
-
Args:
|
324
|
-
layer_count (int): Number of layers in the neural network.
|
325
|
-
class_count (int): Number of classes in the classification task.
|
326
|
-
neurons (list[num]): List of neuron counts for each layer.
|
327
|
-
x_train_size (int): Size of the input data.
|
328
|
-
|
329
|
-
Returns:
|
330
|
-
list([numpy_arrays],[...]): pretrained weight matices of the model. .
|
331
|
-
"""
|
332
|
-
|
333
|
-
Wlen = layer_count + 1
|
334
|
-
W = [None] * Wlen
|
335
|
-
W[0] = np.ones((neurons[0], x_train_size))
|
336
|
-
ws = layer_count - 1
|
337
|
-
for w in range(ws):
|
338
|
-
W[w + 1] = np.ones((neurons[w + 1], neurons[w]))
|
339
|
-
|
340
|
-
return W
|
341
|
-
|
342
|
-
|
343
|
-
def fex(
|
344
|
-
Input, # list[num]: Input data.
|
345
|
-
w, # num: Weight matrix of the neural network.
|
346
|
-
is_training, # bool: Flag indicating if the function is called during training (True or False).
|
347
|
-
Class, # int: Which class is, if training.
|
348
|
-
activation_potentiation # float or None: Input activation potentiation (optional)
|
349
|
-
) -> tuple:
|
350
|
-
"""
|
351
|
-
Applies feature extraction process to the input data using synaptic potentiation.
|
352
|
-
|
353
|
-
Args:
|
354
|
-
Input (num): Input data.
|
355
|
-
w (num): Weight matrix of the neural network.
|
356
|
-
is_training (bool): Flag indicating if the function is called during training (True or False).
|
357
|
-
Class (int): if is during training then which class(label) ? is isnt then put None.
|
358
|
-
activation_potentiation (float or None): Threshold value for comparison. (optional)
|
359
|
-
|
360
|
-
Returns:
|
361
|
-
tuple: A tuple (vector) containing the neural layer result and the updated weight matrix.
|
362
|
-
"""
|
363
|
-
|
364
|
-
if is_training == True and activation_potentiation == None:
|
365
|
-
|
366
|
-
w[Class, :] = Input
|
367
|
-
|
368
|
-
return w
|
369
|
-
|
370
|
-
elif is_training == True and activation_potentiation != None:
|
371
|
-
|
372
|
-
|
373
|
-
Input[Input < activation_potentiation] = 0
|
374
|
-
Input[Input > activation_potentiation] = 1
|
375
|
-
|
376
|
-
w[Class,:] = Input
|
377
|
-
|
378
|
-
return w
|
379
|
-
|
380
|
-
elif is_training == False and activation_potentiation == None:
|
381
|
-
|
382
|
-
neural_layer = np.dot(w, Input)
|
383
|
-
|
384
|
-
return neural_layer
|
385
|
-
|
386
|
-
elif is_training == False and activation_potentiation != None:
|
387
|
-
|
388
|
-
Input[Input < activation_potentiation] = 0
|
389
|
-
Input[Input > activation_potentiation] = 1
|
390
|
-
|
391
|
-
neural_layer = np.dot(w, Input)
|
392
|
-
|
393
|
-
return neural_layer
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
def normalization(
|
398
|
-
Input # num: Input data to be normalized.
|
399
|
-
):
|
400
|
-
"""
|
401
|
-
Normalizes the input data using maximum absolute scaling.
|
402
|
-
|
403
|
-
Args:
|
404
|
-
Input (num): Input data to be normalized.
|
405
|
-
|
406
|
-
Returns:
|
407
|
-
(num) Scaled input data after normalization.
|
408
|
-
"""
|
409
|
-
|
410
|
-
AbsVector = np.abs(Input)
|
411
|
-
|
412
|
-
MaxAbs = np.max(AbsVector)
|
413
|
-
|
414
|
-
ScaledInput = Input / MaxAbs
|
415
|
-
|
416
|
-
return ScaledInput
|
417
|
-
|
418
|
-
|
419
|
-
def Softmax(
|
420
|
-
x # num: Input data to be transformed using softmax function.
|
421
|
-
):
|
422
|
-
"""
|
423
|
-
Applies the softmax function to the input data.
|
424
|
-
|
425
|
-
Args:
|
426
|
-
(num): Input data to be transformed using softmax function.
|
427
|
-
|
428
|
-
Returns:
|
429
|
-
(num): Transformed data after applying softmax function.
|
430
|
-
"""
|
431
|
-
|
432
|
-
return softmax(x)
|
433
|
-
|
434
|
-
|
435
|
-
def Sigmoid(
|
436
|
-
x # num: Input data to be transformed using sigmoid function.
|
437
|
-
):
|
438
|
-
"""
|
439
|
-
Applies the sigmoid function to the input data.
|
440
|
-
|
441
|
-
Args:
|
442
|
-
(num): Input data to be transformed using sigmoid function.
|
443
|
-
|
444
|
-
Returns:
|
445
|
-
(num): Transformed data after applying sigmoid function.
|
446
|
-
"""
|
447
|
-
return expit(x)
|
448
|
-
|
449
|
-
|
450
|
-
def Relu(
|
451
|
-
x # num: Input data to be transformed using ReLU function.
|
452
|
-
):
|
453
|
-
"""
|
454
|
-
Applies the Rectified Linear Unit (ReLU) function to the input data.
|
455
|
-
|
456
|
-
Args:
|
457
|
-
(num): Input data to be transformed using ReLU function.
|
458
|
-
|
459
|
-
Returns:
|
460
|
-
(num): Transformed data after applying ReLU function.
|
461
|
-
"""
|
462
|
-
|
463
|
-
return np.maximum(0, x)
|
464
|
-
|
465
|
-
|
466
|
-
def evaluate(
|
467
|
-
x_test, # list[num]: Test input data.
|
468
|
-
y_test, # list[num]: Test labels.
|
469
|
-
W, # list[num]: Weight matrix list of the neural network.
|
470
|
-
activation_potentiation=None, # activation_potentiation (float or None): Threshold value for comparison. (optional) Default: None
|
471
|
-
bar_status=True, # bar_status (bool): Loading bar for accuracy (True or None) (optional) Default: True
|
472
|
-
show_metrices=None # show_metrices (bool): (True or None) (optional) Default: None
|
473
|
-
) -> tuple:
|
474
|
-
infoTestModel = """
|
475
|
-
Tests the neural network model with the given test data.
|
476
|
-
|
477
|
-
Args:
|
478
|
-
x_test (list[num]): Test input data.
|
479
|
-
y_test (list[num]): Test labels.
|
480
|
-
W (list[num]): Weight matrix list of the neural network.
|
481
|
-
activation_potentiation (float or None): Threshold value for comparison. (optional) Default: None
|
482
|
-
bar_status (bool): Loading bar for accuracy (True or None) (optional) Default: True
|
483
|
-
show_metrices (bool): (True or None) (optional) Default: None
|
484
|
-
|
485
|
-
Returns:
|
486
|
-
tuple: A tuple containing the predicted labels and the accuracy of the model.
|
487
|
-
"""
|
488
|
-
try:
|
489
|
-
layers = ['fex']
|
490
|
-
|
491
|
-
Wc = [0] * len(W) # Wc = Weight copy
|
492
|
-
true = 0
|
493
|
-
y_preds = [-1] * len(y_test)
|
494
|
-
acc_list = []
|
495
|
-
|
496
|
-
for i, w in enumerate(W):
|
497
|
-
Wc[i] = np.copy(w)
|
498
|
-
|
499
|
-
|
500
|
-
if bar_status == True:
|
501
|
-
|
502
|
-
test_progress = tqdm(total=len(x_test),leave=False, desc='Testing',ncols=120)
|
503
|
-
acc_bar = tqdm(total=1, desc="Test Accuracy", ncols=120)
|
504
|
-
|
505
|
-
|
506
|
-
for inpIndex, Input in enumerate(x_test):
|
507
|
-
Input = np.array(Input)
|
508
|
-
Input = Input.ravel()
|
509
|
-
neural_layer = Input
|
510
|
-
|
511
|
-
for index, Layer in enumerate(layers):
|
512
|
-
|
513
|
-
if Layer == 'fex':
|
514
|
-
neural_layer = fex(neural_layer, W[index], False, None, activation_potentiation)
|
515
|
-
|
516
|
-
|
517
|
-
for i, w in enumerate(Wc):
|
518
|
-
W[i] = np.copy(w)
|
519
|
-
RealOutput = np.argmax(y_test[inpIndex])
|
520
|
-
PredictedOutput = np.argmax(neural_layer)
|
521
|
-
if RealOutput == PredictedOutput:
|
522
|
-
true += 1
|
523
|
-
acc = true / len(y_test)
|
524
|
-
|
525
|
-
|
526
|
-
acc_list.append(acc)
|
527
|
-
y_preds[inpIndex] = PredictedOutput
|
528
|
-
|
529
|
-
if bar_status == True:
|
530
|
-
test_progress.update(1)
|
531
|
-
if inpIndex == 0:
|
532
|
-
acc_bar.update(acc)
|
533
|
-
|
534
|
-
else:
|
535
|
-
acc = acc - acc_list[inpIndex - 1]
|
536
|
-
acc_bar.update(acc)
|
537
|
-
|
538
|
-
if show_metrices == True:
|
539
|
-
plot_evaluate(y_test, y_preds, acc_list)
|
540
|
-
|
541
|
-
|
542
|
-
for i, w in enumerate(Wc):
|
543
|
-
W[i] = np.copy(w)
|
544
|
-
|
545
|
-
except:
|
546
|
-
|
547
|
-
print(Fore.RED + 'ERROR:' + infoTestModel + Style.RESET_ALL)
|
548
|
-
|
549
|
-
return W, y_preds, acc
|
550
|
-
|
551
|
-
|
552
|
-
def multiple_evaluate(
|
553
|
-
x_test, # list[num]: Test input data.
|
554
|
-
y_test, # list[num]: Test labels.
|
555
|
-
show_metrices, # show_metrices (bool): Visualize test progress ? (True or False)
|
556
|
-
MW, # list[list[num]]: Weight matrix of the neural network.
|
557
|
-
activation_potentiation=None # (float or None): Threshold value for comparison. (optional)
|
558
|
-
) -> tuple:
|
559
|
-
infoTestModel = """
|
560
|
-
Tests the neural network model with the given test data.
|
561
|
-
|
562
|
-
Args:
|
563
|
-
x_test (list[num]): Test input data.
|
564
|
-
y_test (list[num]): Test labels.
|
565
|
-
show_metrices (bool): (True or False)
|
566
|
-
MW (list(list[num])): Multiple Weight matrix list of the neural network. (Multiple model testing)
|
567
|
-
|
568
|
-
Returns:
|
569
|
-
tuple: A tuple containing the predicted labels and the accuracy of the model.
|
570
|
-
"""
|
571
|
-
|
572
|
-
layers = ['fex', 'cat']
|
573
|
-
|
574
|
-
try:
|
575
|
-
y_preds = [-1] * len(y_test)
|
576
|
-
acc_list = []
|
577
|
-
print(Fore.GREEN + "\n\nTest Started with 0 ERROR\n" + Style.RESET_ALL)
|
578
|
-
start_time = time.time()
|
579
|
-
true = 0
|
580
|
-
for inpIndex, Input in enumerate(x_test):
|
581
|
-
|
582
|
-
output_layer = 0
|
583
|
-
|
584
|
-
for m, Model in enumerate(MW):
|
585
|
-
|
586
|
-
W = Model
|
587
|
-
|
588
|
-
Wc = [0] * len(W) # Wc = weight copy
|
589
|
-
|
590
|
-
y_preds = [None] * len(y_test)
|
591
|
-
for i, w in enumerate(W):
|
592
|
-
Wc[i] = np.copy(w)
|
593
|
-
|
594
|
-
Input = np.array(Input)
|
595
|
-
Input = Input.ravel()
|
596
|
-
uni_start_time = time.time()
|
597
|
-
neural_layer = Input
|
598
|
-
|
599
|
-
for index, Layer in enumerate(layers):
|
600
|
-
|
601
|
-
neural_layer = normalization(neural_layer)
|
602
|
-
|
603
|
-
if Layer == 'fex':
|
604
|
-
neural_layer = fex(neural_layer, W[index], False, None, activation_potentiation)
|
605
|
-
|
606
|
-
output_layer += neural_layer
|
607
|
-
|
608
|
-
for i, w in enumerate(Wc):
|
609
|
-
W[i] = np.copy(w)
|
610
|
-
for i, w in enumerate(Wc):
|
611
|
-
W[i] = np.copy(w)
|
612
|
-
RealOutput = np.argmax(y_test[inpIndex])
|
613
|
-
PredictedOutput = np.argmax(output_layer)
|
614
|
-
if RealOutput == PredictedOutput:
|
615
|
-
true += 1
|
616
|
-
acc = true / len(y_test)
|
617
|
-
if show_metrices == True:
|
618
|
-
acc_list.append(acc)
|
619
|
-
y_preds[inpIndex] = PredictedOutput
|
620
|
-
|
621
|
-
|
622
|
-
uni_end_time = time.time()
|
623
|
-
|
624
|
-
calculating_est = round(
|
625
|
-
(uni_end_time - uni_start_time) * (len(x_test) - inpIndex), 3)
|
626
|
-
|
627
|
-
if calculating_est < 60:
|
628
|
-
print('\rest......(sec):', calculating_est, '\n', end="")
|
629
|
-
print('\rTest accuracy: ', acc, "\n", end="")
|
630
|
-
|
631
|
-
elif calculating_est > 60 and calculating_est < 3600:
|
632
|
-
print('\rest......(min):', calculating_est/60, '\n', end="")
|
633
|
-
print('\rTest accuracy: ', acc, "\n", end="")
|
634
|
-
|
635
|
-
elif calculating_est > 3600:
|
636
|
-
print('\rest......(h):', calculating_est/3600, '\n', end="")
|
637
|
-
print('\rTest accuracy: ', acc, "\n", end="")
|
638
|
-
if show_metrices == True:
|
639
|
-
plot_evaluate(y_test, y_preds, acc_list)
|
640
|
-
|
641
|
-
EndTime = time.time()
|
642
|
-
for i, w in enumerate(Wc):
|
643
|
-
W[i] = np.copy(w)
|
644
|
-
|
645
|
-
calculating_est = round(EndTime - start_time, 2)
|
646
|
-
|
647
|
-
print(Fore.GREEN + "\nTest Finished with 0 ERROR\n")
|
648
|
-
|
649
|
-
if calculating_est < 60:
|
650
|
-
print('Total testing time(sec): ', calculating_est)
|
651
|
-
|
652
|
-
elif calculating_est > 60 and calculating_est < 3600:
|
653
|
-
print('Total testing time(min): ', calculating_est/60)
|
654
|
-
|
655
|
-
elif calculating_est > 3600:
|
656
|
-
print('Total testing time(h): ', calculating_est/3600)
|
657
|
-
|
658
|
-
if acc >= 0.8:
|
659
|
-
print(Fore.GREEN + '\nTotal Test accuracy: ',
|
660
|
-
acc, '\n' + Style.RESET_ALL)
|
661
|
-
|
662
|
-
elif acc < 0.8 and acc > 0.6:
|
663
|
-
print(Fore.MAGENTA + '\nTotal Test accuracy: ',
|
664
|
-
acc, '\n' + Style.RESET_ALL)
|
665
|
-
|
666
|
-
elif acc <= 0.6:
|
667
|
-
print(Fore.RED + '\nTotal Test accuracy: ',
|
668
|
-
acc, '\n' + Style.RESET_ALL)
|
669
|
-
|
670
|
-
except:
|
671
|
-
|
672
|
-
print(Fore.RED + "ERROR: Testing model parameters like 'activation_potentiation' must be same as trained model. Check parameters. Are you sure weights are loaded ? from: evaluate" + infoTestModel + Style.RESET_ALL)
|
673
|
-
return 'e'
|
674
|
-
|
675
|
-
return W, y_preds, acc
|
676
|
-
|
677
|
-
|
678
|
-
def save_model(model_name,
|
679
|
-
model_type,
|
680
|
-
class_count,
|
681
|
-
test_acc,
|
682
|
-
weights_type,
|
683
|
-
weights_format,
|
684
|
-
model_path,
|
685
|
-
scaler_params,
|
686
|
-
W,
|
687
|
-
activation_potentiation=None
|
688
|
-
):
|
689
|
-
|
690
|
-
infosave_model = """
|
691
|
-
Function to save a potentiation learning model.
|
692
|
-
|
693
|
-
Arguments:
|
694
|
-
model_name (str): Name of the model.
|
695
|
-
model_type (str): Type of the model.(options: PLAN)
|
696
|
-
class_count (int): Number of classes.
|
697
|
-
test_acc (float): Test accuracy of the model.
|
698
|
-
weights_type (str): Type of weights to save (options: 'txt', 'npy', 'mat').
|
699
|
-
WeightFormat (str): Format of the weights (options: 'd', 'f', 'raw').
|
700
|
-
model_path (str): Path where the model will be saved. For example: C:/Users/beydili/Desktop/denemePLAN/
|
701
|
-
scaler_params (int, float): standard scaler params list: mean,std. If not used standard scaler then be: None.
|
702
|
-
W: Weights of the model.
|
703
|
-
activation_potentiation (float or None): Threshold value for comparison. (optional)
|
704
|
-
|
705
|
-
Returns:
|
706
|
-
str: Message indicating if the model was saved successfully or encountered an error.
|
707
|
-
"""
|
708
|
-
|
709
|
-
# Operations to be performed by the function will be written here
|
710
|
-
pass
|
711
|
-
|
712
|
-
layers = ['fex']
|
713
|
-
|
714
|
-
if weights_type != 'txt' and weights_type != 'npy' and weights_type != 'mat':
|
715
|
-
print(Fore.RED + "ERROR110: Save Weight type (File Extension) Type must be 'txt' or 'npy' or 'mat' from: save_model" +
|
716
|
-
infosave_model + Style.RESET_ALL)
|
717
|
-
return 'e'
|
718
|
-
|
719
|
-
if weights_format != 'd' and weights_format != 'f' and weights_format != 'raw':
|
720
|
-
print(Fore.RED + "ERROR111: Weight Format Type must be 'd' or 'f' or 'raw' from: save_model" +
|
721
|
-
infosave_model + Style.RESET_ALL)
|
722
|
-
return 'e'
|
723
|
-
|
724
|
-
NeuronCount = 0
|
725
|
-
SynapseCount = 0
|
726
|
-
|
727
|
-
try:
|
728
|
-
for w in W:
|
729
|
-
NeuronCount += np.shape(w)[0]
|
730
|
-
SynapseCount += np.shape(w)[0] * np.shape(w)[1]
|
731
|
-
except:
|
732
|
-
|
733
|
-
print(Fore.RED + "ERROR: Weight matrices has a problem from: save_model" +
|
734
|
-
infosave_model + Style.RESET_ALL)
|
735
|
-
return 'e'
|
736
|
-
import pandas as pd
|
737
|
-
from datetime import datetime
|
738
|
-
from scipy import io
|
739
|
-
|
740
|
-
data = {'MODEL NAME': model_name,
|
741
|
-
'MODEL TYPE': model_type,
|
742
|
-
'LAYERS': layers,
|
743
|
-
'LAYER COUNT': len(layers),
|
744
|
-
'CLASS COUNT': class_count,
|
745
|
-
'NEURON COUNT': NeuronCount,
|
746
|
-
'SYNAPSE COUNT': SynapseCount,
|
747
|
-
'TEST ACCURACY': test_acc,
|
748
|
-
'SAVE DATE': datetime.now(),
|
749
|
-
'WEIGHTS TYPE': weights_type,
|
750
|
-
'WEIGHTS FORMAT': weights_format,
|
751
|
-
'MODEL PATH': model_path,
|
752
|
-
'STANDARD SCALER': scaler_params,
|
753
|
-
'ACTIVATION POTENTIATION': activation_potentiation
|
754
|
-
}
|
755
|
-
try:
|
756
|
-
|
757
|
-
df = pd.DataFrame(data)
|
758
|
-
|
759
|
-
df.to_csv(model_path + model_name + '.txt', sep='\t', index=False)
|
760
|
-
|
761
|
-
except:
|
762
|
-
|
763
|
-
print(Fore.RED + "ERROR: Model log not saved probably model_path incorrect. Check the log parameters from: save_model" +
|
764
|
-
infosave_model + Style.RESET_ALL)
|
765
|
-
return 'e'
|
766
|
-
try:
|
767
|
-
|
768
|
-
if weights_type == 'txt' and weights_format == 'd':
|
769
|
-
|
770
|
-
for i, w in enumerate(W):
|
771
|
-
np.savetxt(model_path + model_name +
|
772
|
-
str(i+1) + 'w.txt', w, fmt='%d')
|
773
|
-
|
774
|
-
if weights_type == 'txt' and weights_format == 'f':
|
775
|
-
|
776
|
-
for i, w in enumerate(W):
|
777
|
-
np.savetxt(model_path + model_name +
|
778
|
-
str(i+1) + 'w.txt', w, fmt='%f')
|
779
|
-
|
780
|
-
if weights_type == 'txt' and weights_format == 'raw':
|
781
|
-
|
782
|
-
for i, w in enumerate(W):
|
783
|
-
np.savetxt(model_path + model_name + str(i+1) + 'w.txt', w)
|
784
|
-
|
785
|
-
###
|
786
|
-
|
787
|
-
if weights_type == 'npy' and weights_format == 'd':
|
788
|
-
|
789
|
-
for i, w in enumerate(W):
|
790
|
-
np.save(model_path + model_name +
|
791
|
-
str(i+1) + 'w.npy', w.astype(int))
|
792
|
-
|
793
|
-
if weights_type == 'npy' and weights_format == 'f':
|
794
|
-
|
795
|
-
for i, w in enumerate(W):
|
796
|
-
np.save(model_path + model_name + str(i+1) +
|
797
|
-
'w.npy', w, w.astype(float))
|
798
|
-
|
799
|
-
if weights_type == 'npy' and weights_format == 'raw':
|
800
|
-
|
801
|
-
for i, w in enumerate(W):
|
802
|
-
np.save(model_path + model_name + str(i+1) + 'w.npy', w)
|
803
|
-
|
804
|
-
###
|
805
|
-
|
806
|
-
if weights_type == 'mat' and weights_format == 'd':
|
807
|
-
|
808
|
-
for i, w in enumerate(W):
|
809
|
-
w = {'w': w.astype(int)}
|
810
|
-
io.savemat(model_path + model_name + str(i+1) + 'w.mat', w)
|
811
|
-
|
812
|
-
if weights_type == 'mat' and weights_format == 'f':
|
813
|
-
|
814
|
-
for i, w in enumerate(W):
|
815
|
-
w = {'w': w.astype(float)}
|
816
|
-
io.savemat(model_path + model_name + str(i+1) + 'w.mat', w)
|
817
|
-
|
818
|
-
if weights_type == 'mat' and weights_format == 'raw':
|
819
|
-
|
820
|
-
for i, w in enumerate(W):
|
821
|
-
w = {'w': w}
|
822
|
-
io.savemat(model_path + model_name + str(i+1) + 'w.mat', w)
|
823
|
-
|
824
|
-
except:
|
825
|
-
|
826
|
-
print(Fore.RED + "ERROR: Model Weights not saved. Check the Weight parameters. SaveFilePath expl: 'C:/Users/hasancanbeydili/Desktop/denemePLAN/' from: save_model" + infosave_model + Style.RESET_ALL)
|
827
|
-
return 'e'
|
828
|
-
print(df)
|
829
|
-
message = (
|
830
|
-
Fore.GREEN + "Model Saved Successfully\n" +
|
831
|
-
Fore.MAGENTA + "Don't forget, if you want to load model: model log file and weight files must be in the same directory." +
|
832
|
-
Style.RESET_ALL
|
833
|
-
)
|
834
|
-
|
835
|
-
return print(message)
|
836
|
-
|
837
|
-
|
838
|
-
def load_model(model_name,
|
839
|
-
model_path,
|
840
|
-
):
|
841
|
-
infoload_model = """
|
842
|
-
Function to load a potentiation learning model.
|
843
|
-
|
844
|
-
Arguments:
|
845
|
-
model_name (str): Name of the model.
|
846
|
-
model_path (str): Path where the model is saved.
|
847
|
-
|
848
|
-
Returns:
|
849
|
-
lists: W(list[num]), activation_potentiation, DataFrame of the model
|
850
|
-
"""
|
851
|
-
pass
|
852
|
-
|
853
|
-
import scipy.io as sio
|
854
|
-
|
855
|
-
try:
|
856
|
-
|
857
|
-
df = pd.read_csv(model_path + model_name + '.' + 'txt', delimiter='\t')
|
858
|
-
|
859
|
-
except:
|
860
|
-
|
861
|
-
print(Fore.RED + "ERROR: Model Path error. accaptable form: 'C:/Users/hasancanbeydili/Desktop/denemePLAN/' from: load_model" +
|
862
|
-
infoload_model + Style.RESET_ALL)
|
863
|
-
|
864
|
-
model_name = str(df['MODEL NAME'].iloc[0])
|
865
|
-
layer_count = int(df['LAYER COUNT'].iloc[0])
|
866
|
-
WeightType = str(df['WEIGHTS TYPE'].iloc[0])
|
867
|
-
|
868
|
-
W = [0] * layer_count
|
869
|
-
|
870
|
-
if WeightType == 'txt':
|
871
|
-
for i in range(layer_count):
|
872
|
-
W[i] = np.loadtxt(model_path + model_name + str(i+1) + 'w.txt')
|
873
|
-
elif WeightType == 'npy':
|
874
|
-
for i in range(layer_count):
|
875
|
-
W[i] = np.load(model_path + model_name + str(i+1) + 'w.npy')
|
876
|
-
elif WeightType == 'mat':
|
877
|
-
for i in range(layer_count):
|
878
|
-
W[i] = sio.loadmat(model_path + model_name + str(i+1) + 'w.mat')
|
879
|
-
else:
|
880
|
-
raise ValueError(
|
881
|
-
Fore.RED + "Incorrect weight type value. Value must be 'txt', 'npy' or 'mat' from: load_model." + infoload_model + Style.RESET_ALL)
|
882
|
-
print(Fore.GREEN + "Model loaded succesfully" + Style.RESET_ALL)
|
883
|
-
return W, df
|
884
|
-
|
885
|
-
|
886
|
-
def predict_model_ssd(Input, model_name, model_path):
|
887
|
-
|
888
|
-
infopredict_model_ssd = """
|
889
|
-
Function to make a prediction using a divided potentiation learning artificial neural network (PLAN).
|
890
|
-
|
891
|
-
Arguments:
|
892
|
-
Input (list or ndarray): Input data for the model (single vector or single matrix).
|
893
|
-
model_name (str): Name of the model.
|
894
|
-
Returns:
|
895
|
-
ndarray: Output from the model.
|
896
|
-
"""
|
897
|
-
W, df = load_model(model_name, model_path)
|
898
|
-
|
899
|
-
activation_potentiation = str(df['ACTIVATION POTENTIATION'].iloc[0])
|
900
|
-
|
901
|
-
if activation_potentiation != 'nan':
|
902
|
-
|
903
|
-
activation_potentiation = float(activation_potentiation)
|
904
|
-
|
905
|
-
else:
|
906
|
-
|
907
|
-
activation_potentiation = None
|
908
|
-
|
909
|
-
try:
|
910
|
-
|
911
|
-
scaler_params = df['STANDARD SCALER'].tolist()
|
912
|
-
|
913
|
-
|
914
|
-
scaler_params = [np.fromstring(arr.strip('[]'), sep=' ') for arr in scaler_params]
|
915
|
-
|
916
|
-
Input = standard_scaler(None, Input, scaler_params)
|
917
|
-
|
918
|
-
except:
|
919
|
-
|
920
|
-
pass
|
921
|
-
|
922
|
-
|
923
|
-
layers = ['fex']
|
924
|
-
|
925
|
-
Wc = [0] * len(W)
|
926
|
-
for i, w in enumerate(W):
|
927
|
-
Wc[i] = np.copy(w)
|
928
|
-
try:
|
929
|
-
neural_layer = Input
|
930
|
-
neural_layer = np.array(neural_layer)
|
931
|
-
neural_layer = neural_layer.ravel()
|
932
|
-
for index, Layer in enumerate(layers):
|
933
|
-
|
934
|
-
if Layer == 'fex':
|
935
|
-
neural_layer = fex(neural_layer, W[index], False, None, activation_potentiation)
|
936
|
-
elif Layer == 'cat':
|
937
|
-
neural_layer = np.dot(W[index], neural_layer)
|
938
|
-
except:
|
939
|
-
print(Fore.RED + "ERROR: The input was probably entered incorrectly. from: predict_model_ssd" +
|
940
|
-
infopredict_model_ssd + Style.RESET_ALL)
|
941
|
-
return 'e'
|
942
|
-
for i, w in enumerate(Wc):
|
943
|
-
W[i] = np.copy(w)
|
944
|
-
return neural_layer
|
945
|
-
|
946
|
-
|
947
|
-
def predict_model_ram(Input, W, scaler_params=None, activation_potentiation=None):
|
948
|
-
|
949
|
-
infopredict_model_ram = """
|
950
|
-
Function to make a prediction using a divided potentiation learning artificial neural network (PLAN).
|
951
|
-
from weights and parameters stored in memory.
|
952
|
-
|
953
|
-
Arguments:
|
954
|
-
Input (list or ndarray): Input data for the model (single vector or single matrix).
|
955
|
-
W (list of ndarrays): Weights of the model.
|
956
|
-
scaler_params (int, float): standard scaler params list: mean,std. (optional) Default: None.
|
957
|
-
activation_potentiation (float or None): Threshold value for comparison. (optional) Default: None
|
958
|
-
|
959
|
-
Returns:
|
960
|
-
ndarray: Output from the model.
|
961
|
-
"""
|
962
|
-
try:
|
963
|
-
if scaler_params != None:
|
964
|
-
|
965
|
-
Input = standard_scaler(None, Input, scaler_params)
|
966
|
-
except:
|
967
|
-
Input = standard_scaler(None, Input, scaler_params)
|
968
|
-
|
969
|
-
layers = ['fex']
|
970
|
-
|
971
|
-
Wc = [0] * len(W)
|
972
|
-
for i, w in enumerate(W):
|
973
|
-
Wc[i] = np.copy(w)
|
974
|
-
try:
|
975
|
-
neural_layer = Input
|
976
|
-
neural_layer = np.array(neural_layer)
|
977
|
-
neural_layer = neural_layer.ravel()
|
978
|
-
for index, Layer in enumerate(layers):
|
979
|
-
|
980
|
-
if Layer == 'fex':
|
981
|
-
neural_layer = fex(neural_layer, W[index], False, None, activation_potentiation)
|
982
|
-
elif Layer == 'cat':
|
983
|
-
neural_layer = np.dot(W[index], neural_layer)
|
984
|
-
|
985
|
-
except:
|
986
|
-
print(Fore.RED + "ERROR: Unexpected input or wrong model parameters from: predict_model_ram." +
|
987
|
-
infopredict_model_ram + Style.RESET_ALL)
|
988
|
-
return 'e'
|
989
|
-
for i, w in enumerate(Wc):
|
990
|
-
W[i] = np.copy(w)
|
991
|
-
return neural_layer
|
992
|
-
|
993
|
-
|
994
|
-
def auto_balancer(x_train, y_train):
|
995
|
-
|
996
|
-
infoauto_balancer = """
|
997
|
-
Function to balance the training data across different classes.
|
998
|
-
|
999
|
-
Arguments:
|
1000
|
-
x_train (list): Input data for training.
|
1001
|
-
y_train (list): Labels corresponding to the input data.
|
1002
|
-
|
1003
|
-
Returns:
|
1004
|
-
tuple: A tuple containing balanced input data and labels.
|
1005
|
-
"""
|
1006
|
-
classes = np.arange(y_train.shape[1])
|
1007
|
-
class_count = len(classes)
|
1008
|
-
|
1009
|
-
try:
|
1010
|
-
ClassIndices = {i: np.where(np.array(y_train)[:, i] == 1)[
|
1011
|
-
0] for i in range(class_count)}
|
1012
|
-
classes = [len(ClassIndices[i]) for i in range(class_count)]
|
1013
|
-
|
1014
|
-
if len(set(classes)) == 1:
|
1015
|
-
print(Fore.WHITE + "INFO: All training data have already balanced. from: auto_balancer" + Style.RESET_ALL)
|
1016
|
-
return x_train, y_train
|
1017
|
-
|
1018
|
-
MinCount = min(classes)
|
1019
|
-
|
1020
|
-
BalancedIndices = []
|
1021
|
-
for i in tqdm(range(class_count),leave=False,desc='Balancing Data',ncols=120):
|
1022
|
-
if len(ClassIndices[i]) > MinCount:
|
1023
|
-
SelectedIndices = np.random.choice(
|
1024
|
-
ClassIndices[i], MinCount, replace=False)
|
1025
|
-
else:
|
1026
|
-
SelectedIndices = ClassIndices[i]
|
1027
|
-
BalancedIndices.extend(SelectedIndices)
|
1028
|
-
|
1029
|
-
BalancedInputs = [x_train[idx] for idx in BalancedIndices]
|
1030
|
-
BalancedLabels = [y_train[idx] for idx in BalancedIndices]
|
1031
|
-
|
1032
|
-
print(Fore.GREEN + "All Training Data Succesfully Balanced from: " + str(len(x_train)
|
1033
|
-
) + " to: " + str(len(BalancedInputs)) + ". from: auto_balancer " + Style.RESET_ALL)
|
1034
|
-
except:
|
1035
|
-
print(Fore.RED + "ERROR: Inputs and labels must be same length check parameters" + infoauto_balancer)
|
1036
|
-
return 'e'
|
1037
|
-
|
1038
|
-
return np.array(BalancedInputs), np.array(BalancedLabels)
|
1039
|
-
|
1040
|
-
|
1041
|
-
def synthetic_augmentation(x_train, y_train):
|
1042
|
-
"""
|
1043
|
-
Generates synthetic examples to balance classes with fewer examples.
|
1044
|
-
|
1045
|
-
Arguments:
|
1046
|
-
x -- Input dataset (examples) - list format
|
1047
|
-
y -- Class labels (one-hot encoded) - list format
|
1048
|
-
|
1049
|
-
Returns:
|
1050
|
-
x_balanced -- Balanced input dataset (list format)
|
1051
|
-
y_balanced -- Balanced class labels (one-hot encoded, list format)
|
1052
|
-
"""
|
1053
|
-
x = x_train
|
1054
|
-
y = y_train
|
1055
|
-
classes = np.arange(y_train.shape[1])
|
1056
|
-
class_count = len(classes)
|
1057
|
-
|
1058
|
-
# Calculate class distribution
|
1059
|
-
class_distribution = {i: 0 for i in range(class_count)}
|
1060
|
-
for label in y:
|
1061
|
-
class_distribution[np.argmax(label)] += 1
|
1062
|
-
|
1063
|
-
max_class_count = max(class_distribution.values())
|
1064
|
-
|
1065
|
-
x_balanced = list(x)
|
1066
|
-
y_balanced = list(y)
|
1067
|
-
|
1068
|
-
for class_label in tqdm(range(class_count), leave=False, desc='Augmenting Data',ncols= 120):
|
1069
|
-
class_indices = [i for i, label in enumerate(
|
1070
|
-
y) if np.argmax(label) == class_label]
|
1071
|
-
num_samples = len(class_indices)
|
1072
|
-
|
1073
|
-
if num_samples < max_class_count:
|
1074
|
-
while num_samples < max_class_count:
|
1075
|
-
|
1076
|
-
random_indices = np.random.choice(
|
1077
|
-
class_indices, 2, replace=False)
|
1078
|
-
sample1 = x[random_indices[0]]
|
1079
|
-
sample2 = x[random_indices[1]]
|
1080
|
-
|
1081
|
-
synthetic_sample = sample1 + \
|
1082
|
-
(np.array(sample2) - np.array(sample1)) * np.random.rand()
|
1083
|
-
|
1084
|
-
x_balanced.append(synthetic_sample.tolist())
|
1085
|
-
y_balanced.append(y[class_indices[0]])
|
1086
|
-
|
1087
|
-
num_samples += 1
|
1088
|
-
|
1089
|
-
return np.array(x_balanced), np.array(y_balanced)
|
1090
|
-
|
1091
|
-
|
1092
|
-
def standard_scaler(x_train, x_test=None, scaler_params=None):
|
1093
|
-
info_standard_scaler = """
|
1094
|
-
Standardizes training and test datasets. x_test may be None.
|
1095
|
-
|
1096
|
-
Args:
|
1097
|
-
train_data: numpy.ndarray
|
1098
|
-
Training data
|
1099
|
-
test_data: numpy.ndarray
|
1100
|
-
Test data (optional)
|
1101
|
-
|
1102
|
-
Returns:
|
1103
|
-
list:
|
1104
|
-
Scaler parameters: mean and std
|
1105
|
-
tuple
|
1106
|
-
Standardized training and test datasets
|
1107
|
-
"""
|
1108
|
-
try:
|
1109
|
-
|
1110
|
-
x_train = x_train.tolist()
|
1111
|
-
x_test = x_test.tolist()
|
1112
|
-
|
1113
|
-
except:
|
1114
|
-
|
1115
|
-
pass
|
1116
|
-
|
1117
|
-
try:
|
1118
|
-
|
1119
|
-
if scaler_params == None and x_test != None:
|
1120
|
-
|
1121
|
-
mean = np.mean(x_train, axis=0)
|
1122
|
-
std = np.std(x_train, axis=0)
|
1123
|
-
train_data_scaled = (x_train - mean) / std
|
1124
|
-
test_data_scaled = (x_test - mean) / std
|
1125
|
-
|
1126
|
-
train_data_scaled = np.nan_to_num(train_data_scaled, nan=0)
|
1127
|
-
test_data_scaled = np.nan_to_num(test_data_scaled, nan=0)
|
1128
|
-
|
1129
|
-
scaler_params = [mean, std]
|
1130
|
-
|
1131
|
-
return scaler_params, train_data_scaled, test_data_scaled
|
1132
|
-
|
1133
|
-
if scaler_params == None and x_test == None:
|
1134
|
-
|
1135
|
-
mean = np.mean(x_train, axis=0)
|
1136
|
-
std = np.std(x_train, axis=0)
|
1137
|
-
train_data_scaled = (x_train - mean) / std
|
1138
|
-
|
1139
|
-
train_data_scaled = np.nan_to_num(train_data_scaled, nan=0)
|
1140
|
-
|
1141
|
-
scaler_params = [mean, std]
|
1142
|
-
|
1143
|
-
return scaler_params, train_data_scaled
|
1144
|
-
|
1145
|
-
if scaler_params != None:
|
1146
|
-
|
1147
|
-
test_data_scaled = (x_test - scaler_params[0]) / scaler_params[1]
|
1148
|
-
test_data_scaled = np.nan_to_num(test_data_scaled, nan=0)
|
1149
|
-
|
1150
|
-
return test_data_scaled
|
1151
|
-
|
1152
|
-
except:
|
1153
|
-
print(
|
1154
|
-
Fore.RED + "ERROR: x_train and x_test must be list[numpyarray] from standard_scaler" + info_standard_scaler + Style.RESET_ALL)
|
1155
|
-
|
1156
|
-
|
1157
|
-
def encode_one_hot(y_train, y_test):
|
1158
|
-
info_one_hot_encode = """
|
1159
|
-
Performs one-hot encoding on y_train and y_test data..
|
1160
|
-
|
1161
|
-
Args:
|
1162
|
-
y_train (numpy.ndarray): Eğitim etiketi verisi.
|
1163
|
-
y_test (numpy.ndarray): Test etiketi verisi.
|
1164
|
-
|
1165
|
-
Returns:
|
1166
|
-
tuple: One-hot encoded y_train ve y_test verileri.
|
1167
|
-
"""
|
1168
|
-
classes = np.unique(y_train)
|
1169
|
-
class_count = len(classes)
|
1170
|
-
|
1171
|
-
class_to_index = {cls: idx for idx, cls in enumerate(classes)}
|
1172
|
-
|
1173
|
-
y_train_encoded = np.zeros((y_train.shape[0], class_count))
|
1174
|
-
for i, label in enumerate(y_train):
|
1175
|
-
y_train_encoded[i, class_to_index[label]] = 1
|
1176
|
-
|
1177
|
-
y_test_encoded = np.zeros((y_test.shape[0], class_count))
|
1178
|
-
for i, label in enumerate(y_test):
|
1179
|
-
y_test_encoded[i, class_to_index[label]] = 1
|
1180
|
-
|
1181
|
-
return y_train_encoded, y_test_encoded
|
1182
|
-
|
1183
|
-
|
1184
|
-
def split(X, y, test_size, random_state):
|
1185
|
-
"""
|
1186
|
-
Splits the given X (features) and y (labels) data into training and testing subsets.
|
1187
|
-
|
1188
|
-
Args:
|
1189
|
-
X (numpy.ndarray): Features data.
|
1190
|
-
y (numpy.ndarray): Labels data.
|
1191
|
-
test_size (float or int): Proportion or number of samples for the test subset.
|
1192
|
-
random_state (int or None): Seed for random state.
|
1193
|
-
|
1194
|
-
Returns:
|
1195
|
-
tuple: x_train, x_test, y_train, y_test as ordered training and testing data subsets.
|
1196
|
-
"""
|
1197
|
-
num_samples = X.shape[0]
|
1198
|
-
|
1199
|
-
if isinstance(test_size, float):
|
1200
|
-
test_size = int(test_size * num_samples)
|
1201
|
-
elif isinstance(test_size, int):
|
1202
|
-
if test_size > num_samples:
|
1203
|
-
raise ValueError(
|
1204
|
-
"test_size cannot be larger than the number of samples.")
|
1205
|
-
else:
|
1206
|
-
raise ValueError("test_size should be float or int.")
|
1207
|
-
|
1208
|
-
if random_state is not None:
|
1209
|
-
np.random.seed(random_state)
|
1210
|
-
|
1211
|
-
indices = np.arange(num_samples)
|
1212
|
-
np.random.shuffle(indices)
|
1213
|
-
|
1214
|
-
test_indices = indices[:test_size]
|
1215
|
-
train_indices = indices[test_size:]
|
1216
|
-
|
1217
|
-
x_train, x_test = X[train_indices], X[test_indices]
|
1218
|
-
y_train, y_test = y[train_indices], y[test_indices]
|
1219
|
-
|
1220
|
-
return x_train, x_test, y_train, y_test
|
1221
|
-
|
1222
|
-
|
1223
|
-
def metrics(y_ts, test_preds, average='weighted'):
|
1224
|
-
"""
|
1225
|
-
Calculates precision, recall and F1 score for a classification task.
|
1226
|
-
|
1227
|
-
Args:
|
1228
|
-
y_ts (list or numpy.ndarray): True labels.
|
1229
|
-
test_preds (list or numpy.ndarray): Predicted labels.
|
1230
|
-
average (str): Type of averaging ('micro', 'macro', 'weighted').
|
1231
|
-
|
1232
|
-
Returns:
|
1233
|
-
tuple: Precision, recall, F1 score.
|
1234
|
-
"""
|
1235
|
-
y_test_d = decode_one_hot(y_ts)
|
1236
|
-
y_test_d = np.array(y_test_d)
|
1237
|
-
y_pred = np.array(test_preds)
|
1238
|
-
|
1239
|
-
if y_test_d.ndim > 1:
|
1240
|
-
y_test_d = y_test_d.reshape(-1)
|
1241
|
-
if y_pred.ndim > 1:
|
1242
|
-
y_pred = y_pred.reshape(-1)
|
1243
|
-
|
1244
|
-
tp = {}
|
1245
|
-
fp = {}
|
1246
|
-
fn = {}
|
1247
|
-
|
1248
|
-
classes = np.unique(np.concatenate((y_test_d, y_pred)))
|
1249
|
-
|
1250
|
-
for c in classes:
|
1251
|
-
tp[c] = 0
|
1252
|
-
fp[c] = 0
|
1253
|
-
fn[c] = 0
|
1254
|
-
|
1255
|
-
for c in classes:
|
1256
|
-
for true, pred in zip(y_test_d, y_pred):
|
1257
|
-
if true == c and pred == c:
|
1258
|
-
tp[c] += 1
|
1259
|
-
elif true != c and pred == c:
|
1260
|
-
fp[c] += 1
|
1261
|
-
elif true == c and pred != c:
|
1262
|
-
fn[c] += 1
|
1263
|
-
|
1264
|
-
precision = {}
|
1265
|
-
recall = {}
|
1266
|
-
f1 = {}
|
1267
|
-
|
1268
|
-
for c in classes:
|
1269
|
-
precision[c] = tp[c] / (tp[c] + fp[c]) if (tp[c] + fp[c]) > 0 else 0
|
1270
|
-
recall[c] = tp[c] / (tp[c] + fn[c]) if (tp[c] + fn[c]) > 0 else 0
|
1271
|
-
f1[c] = 2 * (precision[c] * recall[c]) / (precision[c] + recall[c]) if (precision[c] + recall[c]) > 0 else 0
|
1272
|
-
|
1273
|
-
if average == 'micro':
|
1274
|
-
precision_val = np.sum(list(tp.values())) / (np.sum(list(tp.values())) + np.sum(list(fp.values()))) if (np.sum(list(tp.values())) + np.sum(list(fp.values()))) > 0 else 0
|
1275
|
-
recall_val = np.sum(list(tp.values())) / (np.sum(list(tp.values())) + np.sum(list(fn.values()))) if (np.sum(list(tp.values())) + np.sum(list(fn.values()))) > 0 else 0
|
1276
|
-
f1_val = 2 * (precision_val * recall_val) / (precision_val + recall_val) if (precision_val + recall_val) > 0 else 0
|
1277
|
-
|
1278
|
-
elif average == 'macro':
|
1279
|
-
precision_val = np.mean(list(precision.values()))
|
1280
|
-
recall_val = np.mean(list(recall.values()))
|
1281
|
-
f1_val = np.mean(list(f1.values()))
|
1282
|
-
|
1283
|
-
elif average == 'weighted':
|
1284
|
-
weights = np.array([np.sum(y_test_d == c) for c in classes])
|
1285
|
-
weights = weights / np.sum(weights)
|
1286
|
-
precision_val = np.sum([weights[i] * precision[classes[i]] for i in range(len(classes))])
|
1287
|
-
recall_val = np.sum([weights[i] * recall[classes[i]] for i in range(len(classes))])
|
1288
|
-
f1_val = np.sum([weights[i] * f1[classes[i]] for i in range(len(classes))])
|
1289
|
-
|
1290
|
-
else:
|
1291
|
-
raise ValueError("Invalid value for 'average'. Choose from 'micro', 'macro', 'weighted'.")
|
1292
|
-
|
1293
|
-
return precision_val, recall_val, f1_val
|
1294
|
-
|
1295
|
-
|
1296
|
-
def decode_one_hot(encoded_data):
|
1297
|
-
"""
|
1298
|
-
Decodes one-hot encoded data to original categorical labels.
|
1299
|
-
|
1300
|
-
Args:
|
1301
|
-
encoded_data (numpy.ndarray): One-hot encoded data with shape (n_samples, n_classes).
|
1302
|
-
|
1303
|
-
Returns:
|
1304
|
-
numpy.ndarray: Decoded categorical labels with shape (n_samples,).
|
1305
|
-
"""
|
1306
|
-
|
1307
|
-
decoded_labels = np.argmax(encoded_data, axis=1)
|
1308
|
-
|
1309
|
-
return decoded_labels
|
1310
|
-
|
1311
|
-
|
1312
|
-
def roc_curve(y_true, y_score):
|
1313
|
-
"""
|
1314
|
-
Compute Receiver Operating Characteristic (ROC) curve.
|
1315
|
-
|
1316
|
-
Parameters:
|
1317
|
-
y_true : array, shape = [n_samples]
|
1318
|
-
True binary labels in range {0, 1} or {-1, 1}.
|
1319
|
-
y_score : array, shape = [n_samples]
|
1320
|
-
Target scores, can either be probability estimates of the positive class,
|
1321
|
-
confidence values, or non-thresholded measure of decisions (as returned
|
1322
|
-
by decision_function on some classifiers).
|
1323
|
-
|
1324
|
-
Returns:
|
1325
|
-
fpr : array, shape = [n]
|
1326
|
-
Increasing false positive rates such that element i is the false positive rate
|
1327
|
-
of predictions with score >= thresholds[i].
|
1328
|
-
tpr : array, shape = [n]
|
1329
|
-
Increasing true positive rates such that element i is the true positive rate
|
1330
|
-
of predictions with score >= thresholds[i].
|
1331
|
-
thresholds : array, shape = [n]
|
1332
|
-
Decreasing thresholds on the decision function used to compute fpr and tpr.
|
1333
|
-
"""
|
1334
|
-
|
1335
|
-
y_true = np.asarray(y_true)
|
1336
|
-
y_score = np.asarray(y_score)
|
1337
|
-
|
1338
|
-
if len(np.unique(y_true)) != 2:
|
1339
|
-
raise ValueError("Only binary classification is supported.")
|
1340
|
-
|
1341
|
-
|
1342
|
-
desc_score_indices = np.argsort(y_score, kind="mergesort")[::-1]
|
1343
|
-
y_score = y_score[desc_score_indices]
|
1344
|
-
y_true = y_true[desc_score_indices]
|
1345
|
-
|
1346
|
-
|
1347
|
-
fpr = []
|
1348
|
-
tpr = []
|
1349
|
-
thresholds = []
|
1350
|
-
n_pos = np.sum(y_true)
|
1351
|
-
n_neg = len(y_true) - n_pos
|
1352
|
-
|
1353
|
-
tp = 0
|
1354
|
-
fp = 0
|
1355
|
-
prev_score = None
|
1356
|
-
|
1357
|
-
|
1358
|
-
for i, score in enumerate(y_score):
|
1359
|
-
if score != prev_score:
|
1360
|
-
fpr.append(fp / n_neg)
|
1361
|
-
tpr.append(tp / n_pos)
|
1362
|
-
thresholds.append(score)
|
1363
|
-
prev_score = score
|
1364
|
-
|
1365
|
-
if y_true[i] == 1:
|
1366
|
-
tp += 1
|
1367
|
-
else:
|
1368
|
-
fp += 1
|
1369
|
-
|
1370
|
-
fpr.append(fp / n_neg)
|
1371
|
-
tpr.append(tp / n_pos)
|
1372
|
-
thresholds.append(score)
|
1373
|
-
|
1374
|
-
return np.array(fpr), np.array(tpr), np.array(thresholds)
|
1375
|
-
|
1376
|
-
|
1377
|
-
def confusion_matrix(y_true, y_pred, class_count):
|
1378
|
-
"""
|
1379
|
-
Computes confusion matrix.
|
1380
|
-
|
1381
|
-
Args:
|
1382
|
-
y_true (numpy.ndarray): True class labels (1D array).
|
1383
|
-
y_pred (numpy.ndarray): Predicted class labels (1D array).
|
1384
|
-
num_classes (int): Number of classes.
|
1385
|
-
|
1386
|
-
Returns:
|
1387
|
-
numpy.ndarray: Confusion matrix of shape (num_classes, num_classes).
|
1388
|
-
"""
|
1389
|
-
confusion = np.zeros((class_count, class_count), dtype=int)
|
1390
|
-
|
1391
|
-
for i in range(len(y_true)):
|
1392
|
-
true_label = y_true[i]
|
1393
|
-
pred_label = y_pred[i]
|
1394
|
-
confusion[true_label, pred_label] += 1
|
1395
|
-
|
1396
|
-
return confusion
|
1397
|
-
|
1398
|
-
|
1399
|
-
def plot_evaluate(y_test, y_preds, acc_list):
|
1400
|
-
|
1401
|
-
acc = acc_list[len(acc_list) - 1]
|
1402
|
-
y_true = decode_one_hot(y_test)
|
1403
|
-
|
1404
|
-
y_true = np.array(y_true)
|
1405
|
-
y_preds = np.array(y_preds)
|
1406
|
-
Class = np.unique(decode_one_hot(y_test))
|
1407
|
-
|
1408
|
-
precision, recall, f1 = metrics(y_test, y_preds)
|
1409
|
-
|
1410
|
-
|
1411
|
-
# Confusion matrix
|
1412
|
-
cm = confusion_matrix(y_true, y_preds, len(Class))
|
1413
|
-
fig, axs = plt.subplots(2, 2, figsize=(16, 12))
|
1414
|
-
|
1415
|
-
# Confusion Matrix
|
1416
|
-
sns.heatmap(cm, annot=True, fmt='d', ax=axs[0, 0])
|
1417
|
-
axs[0, 0].set_title("Confusion Matrix")
|
1418
|
-
axs[0, 0].set_xlabel("Predicted Class")
|
1419
|
-
axs[0, 0].set_ylabel("Actual Class")
|
1420
|
-
|
1421
|
-
if len(Class) == 2:
|
1422
|
-
fpr, tpr, thresholds = roc_curve(y_true, y_preds)
|
1423
|
-
# ROC Curve
|
1424
|
-
roc_auc = np.trapz(tpr, fpr)
|
1425
|
-
axs[1, 0].plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
|
1426
|
-
axs[1, 0].plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
|
1427
|
-
axs[1, 0].set_xlim([0.0, 1.0])
|
1428
|
-
axs[1, 0].set_ylim([0.0, 1.05])
|
1429
|
-
axs[1, 0].set_xlabel('False Positive Rate')
|
1430
|
-
axs[1, 0].set_ylabel('True Positive Rate')
|
1431
|
-
axs[1, 0].set_title('Receiver Operating Characteristic (ROC) Curve')
|
1432
|
-
axs[1, 0].legend(loc="lower right")
|
1433
|
-
axs[1, 0].legend(loc="lower right")
|
1434
|
-
else:
|
1435
|
-
|
1436
|
-
for i in range(len(Class)):
|
1437
|
-
|
1438
|
-
y_true_copy = np.copy(y_true)
|
1439
|
-
y_preds_copy = np.copy(y_preds)
|
1440
|
-
|
1441
|
-
y_true_copy[y_true_copy == i] = 0
|
1442
|
-
y_true_copy[y_true_copy != 0] = 1
|
1443
|
-
|
1444
|
-
y_preds_copy[y_preds_copy == i] = 0
|
1445
|
-
y_preds_copy[y_preds_copy != 0] = 1
|
1446
|
-
|
1447
|
-
|
1448
|
-
fpr, tpr, thresholds = roc_curve(y_true_copy, y_preds_copy)
|
1449
|
-
|
1450
|
-
roc_auc = np.trapz(tpr, fpr)
|
1451
|
-
axs[1, 0].plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
|
1452
|
-
axs[1, 0].plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
|
1453
|
-
axs[1, 0].set_xlim([0.0, 1.0])
|
1454
|
-
axs[1, 0].set_ylim([0.0, 1.05])
|
1455
|
-
axs[1, 0].set_xlabel('False Positive Rate')
|
1456
|
-
axs[1, 0].set_ylabel('True Positive Rate')
|
1457
|
-
axs[1, 0].set_title('Receiver Operating Characteristic (ROC) Curve')
|
1458
|
-
axs[1, 0].legend(loc="lower right")
|
1459
|
-
axs[1, 0].legend(loc="lower right")
|
1460
|
-
|
1461
|
-
|
1462
|
-
"""
|
1463
|
-
accuracy_per_class = []
|
1464
|
-
|
1465
|
-
for cls in Class:
|
1466
|
-
correct = np.sum((y_true == cls) & (y_preds == cls))
|
1467
|
-
total = np.sum(y_true == cls)
|
1468
|
-
accuracy_cls = correct / total if total > 0 else 0.0
|
1469
|
-
accuracy_per_class.append(accuracy_cls)
|
1470
|
-
|
1471
|
-
axs[2, 0].bar(Class, accuracy_per_class, color='b', alpha=0.7)
|
1472
|
-
axs[2, 0].set_xlabel('Class')
|
1473
|
-
axs[2, 0].set_ylabel('Accuracy')
|
1474
|
-
axs[2, 0].set_title('Class-wise Accuracy')
|
1475
|
-
axs[2, 0].set_xticks(Class)
|
1476
|
-
axs[2, 0].grid(True)
|
1477
|
-
"""
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
# Precision, Recall, F1 Score, Accuracy
|
1483
|
-
metric = ['Precision', 'Recall', 'F1 Score', 'Accuracy']
|
1484
|
-
values = [precision, recall, f1, acc]
|
1485
|
-
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728']
|
1486
|
-
|
1487
|
-
#
|
1488
|
-
bars = axs[0, 1].bar(metric, values, color=colors)
|
1489
|
-
|
1490
|
-
|
1491
|
-
for bar, value in zip(bars, values):
|
1492
|
-
axs[0, 1].text(bar.get_x() + bar.get_width() / 2, bar.get_height() - 0.05, f'{value:.2f}',
|
1493
|
-
ha='center', va='bottom', fontsize=12, color='white', weight='bold')
|
1494
|
-
|
1495
|
-
axs[0, 1].set_ylim(0, 1) # Y eksenini 0 ile 1 arasında sınırla
|
1496
|
-
axs[0, 1].set_xlabel('Metrics')
|
1497
|
-
axs[0, 1].set_ylabel('Score')
|
1498
|
-
axs[0, 1].set_title('Precision, Recall, F1 Score, and Accuracy (Weighted)')
|
1499
|
-
axs[0, 1].grid(True, axis='y', linestyle='--', alpha=0.7)
|
1500
|
-
|
1501
|
-
# Accuracy
|
1502
|
-
plt.plot(acc_list, marker='o', linestyle='-',
|
1503
|
-
color='r', label='Accuracy')
|
1504
|
-
|
1505
|
-
|
1506
|
-
plt.axhline(y=1, color='g', linestyle='--', label='Maximum Accuracy')
|
1507
|
-
|
1508
|
-
|
1509
|
-
plt.xlabel('Samples')
|
1510
|
-
plt.ylabel('Accuracy')
|
1511
|
-
plt.title('Accuracy History')
|
1512
|
-
plt.legend()
|
1513
|
-
|
1514
|
-
|
1515
|
-
plt.tight_layout()
|
1516
|
-
plt.show()
|
1517
|
-
|
1518
|
-
def manuel_balancer(x_train, y_train, target_samples_per_class):
|
1519
|
-
"""
|
1520
|
-
Generates synthetic examples to balance classes to the specified number of examples per class.
|
1521
|
-
|
1522
|
-
Arguments:
|
1523
|
-
x_train -- Input dataset (examples) - NumPy array format
|
1524
|
-
y_train -- Class labels (one-hot encoded) - NumPy array format
|
1525
|
-
target_samples_per_class -- Desired number of samples per class
|
1526
|
-
|
1527
|
-
Returns:
|
1528
|
-
x_balanced -- Balanced input dataset (NumPy array format)
|
1529
|
-
y_balanced -- Balanced class labels (one-hot encoded, NumPy array format)
|
1530
|
-
"""
|
1531
|
-
try:
|
1532
|
-
x_train = np.array(x_train)
|
1533
|
-
y_train = np.array(y_train)
|
1534
|
-
except:
|
1535
|
-
print(Fore.GREEN + "x_tarin and y_train already numpyarray." + Style.RESET_ALL)
|
1536
|
-
pass
|
1537
|
-
classes = np.arange(y_train.shape[1])
|
1538
|
-
class_count = len(classes)
|
1539
|
-
|
1540
|
-
x_balanced = []
|
1541
|
-
y_balanced = []
|
1542
|
-
|
1543
|
-
for class_label in tqdm(range(class_count),leave=False, desc='Augmenting Data',ncols= 120):
|
1544
|
-
class_indices = np.where(np.argmax(y_train, axis=1) == class_label)[0]
|
1545
|
-
num_samples = len(class_indices)
|
1546
|
-
|
1547
|
-
if num_samples > target_samples_per_class:
|
1548
|
-
|
1549
|
-
selected_indices = np.random.choice(class_indices, target_samples_per_class, replace=False)
|
1550
|
-
x_balanced.append(x_train[selected_indices])
|
1551
|
-
y_balanced.append(y_train[selected_indices])
|
1552
|
-
|
1553
|
-
else:
|
1554
|
-
|
1555
|
-
x_balanced.append(x_train[class_indices])
|
1556
|
-
y_balanced.append(y_train[class_indices])
|
1557
|
-
|
1558
|
-
if num_samples < target_samples_per_class:
|
1559
|
-
|
1560
|
-
samples_to_add = target_samples_per_class - num_samples
|
1561
|
-
additional_samples = np.zeros((samples_to_add, x_train.shape[1]))
|
1562
|
-
additional_labels = np.zeros((samples_to_add, y_train.shape[1]))
|
1563
|
-
|
1564
|
-
for i in range(samples_to_add):
|
1565
|
-
|
1566
|
-
random_indices = np.random.choice(class_indices, 2, replace=False)
|
1567
|
-
sample1 = x_train[random_indices[0]]
|
1568
|
-
sample2 = x_train[random_indices[1]]
|
1569
|
-
|
1570
|
-
|
1571
|
-
synthetic_sample = sample1 + (sample2 - sample1) * np.random.rand()
|
1572
|
-
|
1573
|
-
additional_samples[i] = synthetic_sample
|
1574
|
-
additional_labels[i] = y_train[class_indices[0]]
|
1575
|
-
|
1576
|
-
|
1577
|
-
x_balanced.append(additional_samples)
|
1578
|
-
y_balanced.append(additional_labels)
|
1579
|
-
|
1580
|
-
x_balanced = np.vstack(x_balanced)
|
1581
|
-
y_balanced = np.vstack(y_balanced)
|
1582
|
-
|
1583
|
-
return x_balanced, y_balanced
|
1584
|
-
|
1585
|
-
def get_weights():
|
1586
|
-
|
1587
|
-
return 0
|
1588
|
-
|
1589
|
-
|
1590
|
-
def get_df():
|
1591
|
-
|
1592
|
-
return 2
|
1593
|
-
|
1594
|
-
|
1595
|
-
def get_preds():
|
1596
|
-
|
1597
|
-
return 1
|
1598
|
-
|
1599
|
-
|
1600
|
-
def get_acc():
|
1601
|
-
|
1602
|
-
return 2
|