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