pyerualjetwork 2.6.8__py3-none-any.whl → 2.7.0__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.
plan/plan.py CHANGED
@@ -5,14 +5,6 @@ Created on Tue Jun 18 23:32:16 2024
5
5
  @author: hasan
6
6
  """
7
7
 
8
-
9
- # -*- coding: utf-8 -*-
10
- """
11
- Created on Tue Jun 18 23:32:16 2024
12
-
13
- @author: hasan
14
- """
15
-
16
8
  import pandas as pd
17
9
  import numpy as np
18
10
  import time
@@ -23,7 +15,6 @@ from scipy.special import expit, softmax
23
15
  import matplotlib.pyplot as plt
24
16
  import seaborn as sns
25
17
  from tqdm import tqdm
26
- import progressbar
27
18
 
28
19
  # BUILD -----
29
20
 
@@ -31,12 +22,13 @@ import progressbar
31
22
  def fit(
32
23
  x_train: List[Union[int, float]],
33
24
  y_train: List[Union[int, float]], # At least two.. and one hot encoded
34
- show_training,
35
- show_count= None,
36
25
  val= None,
26
+ val_count = None,
27
+ activation_potentiation=None, # (float): Input activation_potentiation (optional)
37
28
  x_val= None,
38
29
  y_val= None,
39
- activation_potentiation=None # (float): Input activation_potentiation (optional)
30
+ show_training = None,
31
+ show_count= None
40
32
  ) -> str:
41
33
 
42
34
  infoPLAN = """
@@ -45,33 +37,55 @@ def fit(
45
37
  Args:
46
38
  x_train (list[num]): List of input data.
47
39
  y_train (list[num]): List of target labels. (one hot encoded)
48
- show_training (bool, str): True, None or'final'
49
- 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)
50
- val (None, True or 'final'): validation in training process ? None, True or 'final' Default: None
40
+ val (None, True or 'final'): validation in training process ? None, True or 'final' Default: None (optional)
51
41
  val_count (None, int): After how many examples learned will an accuracy test be performed? Default: 0.1 (%10) (optional)
52
- x_val (list[num]): List of validation data. (optional) Default: x_train
53
- y_val (list[num]): (list[num]): List of target labels. (one hot encoded) (optional) Default: y_train
54
42
  activation_potentiation (float): Input activation potentiation (for binary injection) (optional) in range: -1, 1
43
+ x_val (list[num]): List of validation data. (optional) Default: 10% of x_train (auto_balanced)
44
+ y_val (list[num]): (list[num]): List of target labels. (one hot encoded) (optional) Default: 10% of y_train (auto_balanced)
45
+ show_training (bool, str): True, None or'final'
46
+ 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)
55
47
  Returns:
56
48
  list([num]): (Weight matrices list, train_predictions list, Train_acc).
57
49
  error handled ?: Process status ('e')
58
50
  """
59
-
60
51
 
61
52
  if len(x_train) != len(y_train):
62
53
 
63
- print(Fore.RED + "ERROR301: x_train list and y_train list must be same length. from: fit", infoPLAN)
54
+ print(Fore.RED + "ERROR301: x_train list and y_train list must be same length. from: fit", infoPLAN + Style.RESET_ALL)
64
55
  return 'e'
65
-
66
- if x_val == None and y_val == None:
67
-
68
- x_val = x_train
69
- y_val = y_train
70
56
 
71
57
  if val == True or val == 'final':
58
+
59
+ try:
60
+
61
+ if x_val == None and y_val == None:
62
+
63
+ x_train, x_val, y_train, y_val = split(x_train, y_train,test_size=0.1,random_state=42)
64
+
65
+ x_train, y_train = auto_balancer(x_train, y_train)
66
+ x_val, y_val = auto_balancer(x_val, y_val)
67
+
68
+ y_train, y_val = encode_one_hot(y_train, y_val)
69
+
70
+ except:
71
+ pass
72
+
73
+ if val == True:
74
+
75
+ if val_count == None:
76
+
77
+ val_count = 0.1
78
+
79
+ v_iter = 0
80
+
81
+ if val == 'final':
82
+
83
+ val_count = 0.99
72
84
 
73
- val_bar = tqdm(total=1, desc="Training / Validating Accuracy", ncols=120)
74
-
85
+ val_count = int(len(x_train) * val_count)
86
+ val_count_copy = val_count
87
+ val_bar = tqdm(total=1, desc="Validating Accuracy", ncols=120)
88
+ val_list = [] * val_count
75
89
 
76
90
  if show_count == None:
77
91
 
@@ -93,7 +107,6 @@ def fit(
93
107
 
94
108
  neurons = [len(class_count), len(class_count)]
95
109
  layers = ['fex']
96
- val_list = []
97
110
 
98
111
  x_train[0] = np.array(x_train[0])
99
112
  x_train[0] = x_train[0].ravel()
@@ -106,8 +119,10 @@ def fit(
106
119
 
107
120
  y = decode_one_hot(y_train)
108
121
 
122
+ train_progress = tqdm(total=len(x_train),leave=False, desc="Training",ncols= 120)
123
+
109
124
  for index, inp in enumerate(x_train):
110
-
125
+
111
126
  progress = index / len(x_train) * 100
112
127
 
113
128
  inp = np.array(inp)
@@ -131,31 +146,28 @@ def fit(
131
146
  LTPW[i] = LTPW[i] + w
132
147
 
133
148
 
134
- if val == True:
149
+ if val == True and index == val_count:
135
150
 
136
- if show_training == True:
137
- try:
138
- plt.close(fig)
139
151
 
140
- except:
141
- pass
152
+ val_count += val_count_copy
142
153
 
143
- validation_model = evaluate(x_val, y_val, None, LTPW, activation_potentiation, None)
154
+ validation_model = evaluate(x_val, y_val, LTPW, activation_potentiation, None)
144
155
 
145
156
  val_acc = validation_model[get_acc()]
146
157
 
147
158
  val_list.append(val_acc)
148
159
 
149
- if index == 0:
160
+ if v_iter == 0:
150
161
 
151
162
  val_bar.update(val_acc)
152
163
 
153
164
 
154
- if index != 0:
165
+ if v_iter != 0:
155
166
 
156
- val_acc = val_acc - val_list[index - 1]
167
+ val_acc = val_acc - val_list[v_iter - 1]
157
168
  val_bar.update(val_acc)
158
169
 
170
+ v_iter += 1
159
171
 
160
172
  if show_training == True:
161
173
 
@@ -200,17 +212,18 @@ def fit(
200
212
  title_info = 'Weight Matrix Of Fex Layer'
201
213
 
202
214
 
203
-
215
+
204
216
 
205
217
  progress_status = f"{progress:.1f}"
206
218
  fig.suptitle(suptitle_info + progress_status)
207
219
  plt.draw()
208
220
  plt.pause(0.1)
209
221
 
210
-
222
+
211
223
  STPW = weight_identification(
212
224
  len(layers) - 1, len(class_count), neurons, x_train_size)
213
225
 
226
+ train_progress.update(1)
214
227
 
215
228
  if show_training == 'final':
216
229
 
@@ -241,9 +254,8 @@ def fit(
241
254
 
242
255
  val_list.append(val_acc)
243
256
 
244
- val_bar.update(val_acc)
245
-
246
-
257
+ val_bar.update(val_acc)
258
+
247
259
  return LTPW
248
260
 
249
261
  # FUNCTIONS -----
@@ -457,10 +469,10 @@ def Relu(
457
469
  def evaluate(
458
470
  x_test, # list[num]: Test input data.
459
471
  y_test, # list[num]: Test labels.
460
- show_metrices, # show_metrices (bool): (True or False)
461
472
  W, # list[num]: Weight matrix list of the neural network.
462
- activation_potentiation=None, # activation_potentiation (float or None): Threshold value for comparison. (optional)
463
- acc_bar_status = True
473
+ activation_potentiation=None, # activation_potentiation (float or None): Threshold value for comparison. (optional) Default: None
474
+ acc_bar_status=True, # acc_bar_status (bool): Loading bar for accuracy (True or None) (optional) Default: True
475
+ show_metrices=None # show_metrices (bool): (True or None) (optional) Default: None
464
476
  ) -> tuple:
465
477
  infoTestModel = """
466
478
  Tests the neural network model with the given test data.
@@ -468,10 +480,10 @@ def evaluate(
468
480
  Args:
469
481
  x_test (list[num]): Test input data.
470
482
  y_test (list[num]): Test labels.
471
- show_metrices (bool): (True or False)
472
483
  W (list[num]): Weight matrix list of the neural network.
473
- activation_potentiation (float or None): Threshold value for comparison. (optional)
474
- acc_bar_status (bool): Loading bar for accuracy (True or None)
484
+ activation_potentiation (float or None): Threshold value for comparison. (optional) Default: None
485
+ acc_bar_status (bool): Loading bar for accuracy (True or None) (optional) Default: True
486
+ show_metrices (bool): (True or None) (optional) Default: None
475
487
 
476
488
  Returns:
477
489
  tuple: A tuple containing the predicted labels and the accuracy of the model.
@@ -487,25 +499,27 @@ def evaluate(
487
499
 
488
500
  for i, w in enumerate(W):
489
501
  Wc[i] = np.copy(w)
490
- print('\rCopying weights.....', i+1, '/', len(W), end="")
491
502
 
492
- # print(Fore.GREEN + "\n\nTest Started with 0 ERROR\n" + Style.RESET_ALL)
503
+
493
504
  if acc_bar_status == True:
494
- acc_bar = tqdm(total=1, desc="Test Accuracy", ncols=75)
505
+
506
+ test_progress = tqdm(total=len(x_test),leave=False, desc='Testing',ncols=120)
507
+ acc_bar = tqdm(total=1, desc="Test Accuracy", ncols=120)
508
+
509
+
495
510
  for inpIndex, Input in enumerate(x_test):
496
511
  Input = np.array(Input)
497
512
  Input = Input.ravel()
498
- uni_start_time = time.time()
499
513
  neural_layer = Input
500
-
514
+
501
515
  for index, Layer in enumerate(layers):
502
-
516
+
503
517
  neural_layer = normalization(neural_layer)
504
-
518
+
505
519
  if Layer == 'fex':
506
520
  neural_layer = fex(neural_layer, W[index], False, None, activation_potentiation)
507
-
508
-
521
+
522
+
509
523
  for i, w in enumerate(Wc):
510
524
  W[i] = np.copy(w)
511
525
  RealOutput = np.argmax(y_test[inpIndex])
@@ -513,20 +527,21 @@ def evaluate(
513
527
  if RealOutput == PredictedOutput:
514
528
  true += 1
515
529
  acc = true / len(y_test)
516
-
517
-
530
+
531
+
518
532
  acc_list.append(acc)
519
533
  y_preds[inpIndex] = PredictedOutput
520
534
 
521
535
  if acc_bar_status == True:
536
+ test_progress.update(1)
522
537
  if inpIndex == 0:
523
538
  acc_bar.update(acc)
524
539
 
525
540
  else:
526
541
  acc = acc - acc_list[inpIndex - 1]
527
542
  acc_bar.update(acc)
528
-
529
- if show_metrices == True:
543
+
544
+ if show_metrices == True:
530
545
  plot_evaluate(y_test, y_preds, acc_list)
531
546
 
532
547
 
@@ -538,7 +553,7 @@ def evaluate(
538
553
  print(Fore.RED + "ERROR: Are you sure weights are loaded ? from: evaluate" +
539
554
  infoTestModel + Style.RESET_ALL)
540
555
  return 'e'
541
-
556
+
542
557
  return W, y_preds, acc
543
558
 
544
559
 
@@ -910,7 +925,7 @@ def predict_model_ssd(Input, model_name, model_path):
910
925
 
911
926
  except:
912
927
 
913
- non_scaled = True
928
+ pass
914
929
 
915
930
 
916
931
  layers = ['fex']
@@ -939,7 +954,7 @@ def predict_model_ssd(Input, model_name, model_path):
939
954
  return neural_layer
940
955
 
941
956
 
942
- def predict_model_ram(Input, scaler_params, W, activation_potentiation=None):
957
+ def predict_model_ram(Input, W, scaler_params=None, activation_potentiation=None):
943
958
 
944
959
  infopredict_model_ram = """
945
960
  Function to make a prediction using a divided potentiation learning artificial neural network (PLAN).
@@ -947,17 +962,19 @@ def predict_model_ram(Input, scaler_params, W, activation_potentiation=None):
947
962
 
948
963
  Arguments:
949
964
  Input (list or ndarray): Input data for the model (single vector or single matrix).
950
- scaler_params (int, float): standard scaler params list: mean,std. If not used standard scaler then be: None.
951
965
  W (list of ndarrays): Weights of the model.
952
- activation_potentiation (float or None): Threshold value for comparison. (optional)
966
+ scaler_params (int, float): standard scaler params list: mean,std. (optional) Default: None.
967
+ activation_potentiation (float or None): Threshold value for comparison. (optional) Default: None
953
968
 
954
969
  Returns:
955
970
  ndarray: Output from the model.
956
971
  """
957
-
958
- if scaler_params != None:
972
+ try:
973
+ if scaler_params != None:
959
974
 
960
- Input = standard_scaler(None, Input, scaler_params)
975
+ Input = standard_scaler(None, Input, scaler_params)
976
+ except:
977
+ Input = standard_scaler(None, Input, scaler_params)
961
978
 
962
979
  layers = ['fex']
963
980
 
@@ -1013,7 +1030,7 @@ def auto_balancer(x_train, y_train):
1013
1030
  MinCount = min(classes)
1014
1031
 
1015
1032
  BalancedIndices = []
1016
- for i in range(class_count):
1033
+ for i in tqdm(range(class_count),leave=False,desc='Balancing Data',ncols=120):
1017
1034
  if len(ClassIndices[i]) > MinCount:
1018
1035
  SelectedIndices = np.random.choice(
1019
1036
  ClassIndices[i], MinCount, replace=False)
@@ -1030,7 +1047,7 @@ def auto_balancer(x_train, y_train):
1030
1047
  print(Fore.RED + "ERROR: Inputs and labels must be same length check parameters" + infoauto_balancer)
1031
1048
  return 'e'
1032
1049
 
1033
- return BalancedInputs, BalancedLabels
1050
+ return np.array(BalancedInputs), np.array(BalancedLabels)
1034
1051
 
1035
1052
 
1036
1053
  def synthetic_augmentation(x_train, y_train):
@@ -1060,7 +1077,7 @@ def synthetic_augmentation(x_train, y_train):
1060
1077
  x_balanced = list(x)
1061
1078
  y_balanced = list(y)
1062
1079
 
1063
- for class_label in range(class_count):
1080
+ for class_label in tqdm(range(class_count), leave=False, desc='Augmenting Data',ncols= 120):
1064
1081
  class_indices = [i for i, label in enumerate(
1065
1082
  y) if np.argmax(label) == class_label]
1066
1083
  num_samples = len(class_indices)
@@ -1100,6 +1117,15 @@ def standard_scaler(x_train, x_test, scaler_params=None):
1100
1117
  tuple
1101
1118
  Standardized training and test datasets
1102
1119
  """
1120
+ try:
1121
+
1122
+ x_train = x_train.tolist()
1123
+ x_test = x_test.tolist()
1124
+
1125
+ except:
1126
+
1127
+ pass
1128
+
1103
1129
  try:
1104
1130
 
1105
1131
  if scaler_params == None and x_test != None:
@@ -1183,7 +1209,6 @@ def split(X, y, test_size, random_state):
1183
1209
  Returns:
1184
1210
  tuple: x_train, x_test, y_train, y_test as ordered training and testing data subsets.
1185
1211
  """
1186
- # Size of the dataset
1187
1212
  num_samples = X.shape[0]
1188
1213
 
1189
1214
  if isinstance(test_size, float):
@@ -1400,7 +1425,6 @@ def plot_evaluate(y_test, y_preds, acc_list):
1400
1425
 
1401
1426
  # Confusion matrix
1402
1427
  cm = confusion_matrix(y_true, y_preds, len(Class))
1403
- # Subplot içinde düzenleme
1404
1428
  fig, axs = plt.subplots(2, 2, figsize=(16, 12))
1405
1429
 
1406
1430
  # Confusion Matrix
@@ -1519,16 +1543,19 @@ def manuel_balancer(x_train, y_train, target_samples_per_class):
1519
1543
  x_balanced -- Balanced input dataset (NumPy array format)
1520
1544
  y_balanced -- Balanced class labels (one-hot encoded, NumPy array format)
1521
1545
  """
1522
- start_time = time.time()
1523
- x_train = np.array(x_train)
1524
- y_train = np.array(y_train)
1546
+ try:
1547
+ x_train = np.array(x_train)
1548
+ y_train = np.array(y_train)
1549
+ except:
1550
+ print(Fore.GREEN + "x_tarin and y_train already numpyarray." + Style.RESET_ALL)
1551
+ pass
1525
1552
  classes = np.arange(y_train.shape[1])
1526
1553
  class_count = len(classes)
1527
1554
 
1528
1555
  x_balanced = []
1529
1556
  y_balanced = []
1530
1557
 
1531
- for class_label in range(class_count):
1558
+ for class_label in tqdm(range(class_count),leave=False, desc='Augmenting Data',ncols= 120):
1532
1559
  class_indices = np.where(np.argmax(y_train, axis=1) == class_label)[0]
1533
1560
  num_samples = len(class_indices)
1534
1561
 
@@ -1550,8 +1577,7 @@ def manuel_balancer(x_train, y_train, target_samples_per_class):
1550
1577
  additional_labels = np.zeros((samples_to_add, y_train.shape[1]))
1551
1578
 
1552
1579
  for i in range(samples_to_add):
1553
- uni_start_time = time.time()
1554
-
1580
+
1555
1581
  random_indices = np.random.choice(class_indices, 2, replace=False)
1556
1582
  sample1 = x_train[random_indices[0]]
1557
1583
  sample2 = x_train[random_indices[1]]
@@ -1562,41 +1588,10 @@ def manuel_balancer(x_train, y_train, target_samples_per_class):
1562
1588
  additional_samples[i] = synthetic_sample
1563
1589
  additional_labels[i] = y_train[class_indices[0]]
1564
1590
 
1565
- uni_end_time = time.time()
1566
- calculating_est = round(
1567
- (uni_end_time - uni_start_time) * (samples_to_add - i), 3)
1568
-
1569
- if calculating_est < 60:
1570
- print('\rest......(sec):', calculating_est, '\n', end="")
1571
-
1572
- elif calculating_est > 60 and calculating_est < 3600:
1573
- print('\rest......(min):', calculating_est/60, '\n', end="")
1574
-
1575
- elif calculating_est > 3600:
1576
- print('\rest......(h):', calculating_est/3600, '\n', end="")
1577
-
1578
- print('Augmenting: ', class_label, '/', class_count, '...', i, "/", samples_to_add, "\n", end="")
1579
-
1591
+
1580
1592
  x_balanced.append(additional_samples)
1581
1593
  y_balanced.append(additional_labels)
1582
-
1583
-
1584
- EndTime = time.time()
1585
-
1586
- calculating_est = round(EndTime - start_time, 2)
1587
-
1588
- print(Fore.GREEN + " \nBalancing Finished with 0 ERROR\n" + Style.RESET_ALL)
1589
-
1590
- if calculating_est < 60:
1591
- print('Total balancing time(sec): ', calculating_est)
1592
-
1593
- elif calculating_est > 60 and calculating_est < 3600:
1594
- print('Total balancing time(min): ', calculating_est/60)
1595
-
1596
- elif calculating_est > 3600:
1597
- print('Total balancing time(h): ', calculating_est/3600)
1598
1594
 
1599
- # Stack the balanced arrays
1600
1595
  x_balanced = np.vstack(x_balanced)
1601
1596
  y_balanced = np.vstack(y_balanced)
1602
1597
 
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyerualjetwork
3
- Version: 2.6.8
4
- Summary: Visual improvements, new option added for val paramater in fit function: 'final'. Removed: val_count parameter.
3
+ Version: 2.7.0
4
+ Summary: Optimized for Vs Code
5
5
  Author: Hasan Can Beydili
6
6
  Author-email: tchasancan@gmail.com
7
7
  Keywords: model evaluation,classifcation,potentiation learning artficial neural networks
@@ -0,0 +1,6 @@
1
+ plan/__init__.py,sha256=gmaz8lnQfl18MbOQwabBUPmShajK5S99jfyY-hQe8tc,502
2
+ plan/plan.py,sha256=MFMJHqHJVL9leoK_tg96dV9-GDnPPagN36NJHGj7Jd0,53176
3
+ pyerualjetwork-2.7.0.dist-info/METADATA,sha256=xG-j7SZQELeYVeHtCePLvbc_5SbfBJjiAn78qcn0YzY,248
4
+ pyerualjetwork-2.7.0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
5
+ pyerualjetwork-2.7.0.dist-info/top_level.txt,sha256=G0Al3HuNJ88434XneyDtRKAIUaLCizOFYFYNhd7e2OM,5
6
+ pyerualjetwork-2.7.0.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- plan/__init__.py,sha256=gmaz8lnQfl18MbOQwabBUPmShajK5S99jfyY-hQe8tc,502
2
- plan/plan.py,sha256=jg1l3bx1-HxXpnk0XUpn5J2BoXMgbEPFpMZx8Ul4PDA,53416
3
- pyerualjetwork-2.6.8.dist-info/METADATA,sha256=f4xjJ-r0pOfwRU6Yy2BK3o3axIDPxu2Qapj3LofnnxA,338
4
- pyerualjetwork-2.6.8.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
5
- pyerualjetwork-2.6.8.dist-info/top_level.txt,sha256=G0Al3HuNJ88434XneyDtRKAIUaLCizOFYFYNhd7e2OM,5
6
- pyerualjetwork-2.6.8.dist-info/RECORD,,