pyerualjetwork 4.3.8.dev15__py3-none-any.whl → 4.3.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.
Files changed (43) hide show
  1. pyerualjetwork/__init__.py +1 -1
  2. pyerualjetwork/activation_functions.py +2 -2
  3. pyerualjetwork/activation_functions_cuda.py +63 -114
  4. pyerualjetwork/data_operations_cuda.py +1 -1
  5. pyerualjetwork/fitness_functions.py +72 -0
  6. pyerualjetwork/fitness_functions_cuda.py +85 -0
  7. pyerualjetwork/model_operations.py +14 -14
  8. pyerualjetwork/model_operations_cuda.py +16 -17
  9. pyerualjetwork/plan.py +159 -382
  10. pyerualjetwork/plan_cuda.py +149 -387
  11. pyerualjetwork/planeat.py +24 -54
  12. pyerualjetwork/planeat_cuda.py +11 -47
  13. pyerualjetwork/visualizations.py +33 -30
  14. pyerualjetwork/visualizations_cuda.py +22 -24
  15. {pyerualjetwork-4.3.8.dev15.dist-info → pyerualjetwork-4.3.9.dist-info}/METADATA +3 -19
  16. pyerualjetwork-4.3.9.dist-info/RECORD +24 -0
  17. pyerualjetwork-4.3.9.dist-info/top_level.txt +1 -0
  18. pyerualjetwork/loss_functions.py +0 -21
  19. pyerualjetwork/loss_functions_cuda.py +0 -21
  20. pyerualjetwork-4.3.8.dev15.dist-info/RECORD +0 -45
  21. pyerualjetwork-4.3.8.dev15.dist-info/top_level.txt +0 -2
  22. pyerualjetwork_afterburner/__init__.py +0 -11
  23. pyerualjetwork_afterburner/activation_functions.py +0 -290
  24. pyerualjetwork_afterburner/activation_functions_cuda.py +0 -289
  25. pyerualjetwork_afterburner/data_operations.py +0 -406
  26. pyerualjetwork_afterburner/data_operations_cuda.py +0 -461
  27. pyerualjetwork_afterburner/help.py +0 -17
  28. pyerualjetwork_afterburner/loss_functions.py +0 -21
  29. pyerualjetwork_afterburner/loss_functions_cuda.py +0 -21
  30. pyerualjetwork_afterburner/memory_operations.py +0 -298
  31. pyerualjetwork_afterburner/metrics.py +0 -190
  32. pyerualjetwork_afterburner/metrics_cuda.py +0 -163
  33. pyerualjetwork_afterburner/model_operations.py +0 -408
  34. pyerualjetwork_afterburner/model_operations_cuda.py +0 -420
  35. pyerualjetwork_afterburner/parallel.py +0 -118
  36. pyerualjetwork_afterburner/plan.py +0 -432
  37. pyerualjetwork_afterburner/plan_cuda.py +0 -441
  38. pyerualjetwork_afterburner/planeat.py +0 -793
  39. pyerualjetwork_afterburner/planeat_cuda.py +0 -752
  40. pyerualjetwork_afterburner/ui.py +0 -22
  41. pyerualjetwork_afterburner/visualizations.py +0 -823
  42. pyerualjetwork_afterburner/visualizations_cuda.py +0 -825
  43. {pyerualjetwork-4.3.8.dev15.dist-info → pyerualjetwork-4.3.9.dist-info}/WHEEL +0 -0
@@ -16,31 +16,19 @@ PYERUALJETWORK document: https://github.com/HCB06/PyerualJetwork/blob/main/Welco
16
16
  """
17
17
 
18
18
  import cupy as cp
19
- import math
20
19
 
21
20
  ### LIBRARY IMPORTS ###
22
21
  from .ui import loading_bars, initialize_loading_bar
23
- from .data_operations_cuda import normalization, decode_one_hot, batcher
24
- from .loss_functions_cuda import binary_crossentropy, categorical_crossentropy
25
- from .activation_functions_cuda import apply_activation, Softmax, all_activations
26
- from .metrics_cuda import metrics
22
+ from .data_operations_cuda import normalization
23
+ from .activation_functions_cuda import apply_activation, all_activations
27
24
  from .model_operations_cuda import get_acc, get_preds, get_preds_softmax
28
25
  from .memory_operations import transfer_to_gpu, transfer_to_cpu, optimize_labels
29
26
  from .visualizations_cuda import (
30
27
  draw_neural_web,
31
- update_neural_web_for_fit,
32
- plot_evaluate,
33
- update_neuron_history,
34
- initialize_visualization_for_fit,
35
- update_weight_visualization_for_fit,
36
- update_decision_boundary_for_fit,
37
- update_validation_history_for_fit,
38
- display_visualization_for_fit,
39
28
  display_visualizations_for_learner,
40
29
  update_history_plots_for_learner,
41
30
  initialize_visualization_for_learner,
42
- update_neuron_history_for_learner,
43
- show
31
+ update_neuron_history_for_learner
44
32
  )
45
33
 
46
34
  ### GLOBAL VARIABLES ###
@@ -48,149 +36,54 @@ bar_format_normal = loading_bars()[0]
48
36
  bar_format_learner = loading_bars()[1]
49
37
 
50
38
  # BUILD -----
39
+
51
40
  def fit(
52
41
  x_train,
53
42
  y_train,
54
- val=False,
55
- val_count=None,
56
43
  activation_potentiation=['linear'],
57
- x_val=None,
58
- y_val=None,
59
- show_training=None,
60
- interval=100,
61
- LTD=0,
62
- decision_boundary_status=True,
63
- train_bar=True,
64
- auto_normalization=True,
65
- neurons_history=False,
66
- dtype=cp.float32,
67
- memory='gpu'
44
+ W=None,
45
+ auto_normalization=False,
46
+ dtype=cp.float32
68
47
  ):
69
48
  """
70
- Creates a model to fitting data.
49
+ Creates a model to fitting data.,
71
50
 
72
51
  fit Args:
73
52
 
74
- x_train (list[num]): List or numarray of input data.
75
-
76
- y_train (list[num]): List or numarray of target labels. (one hot encoded)
53
+ x_train (aray-like[cupy]): List or cupy array of input data.
77
54
 
78
- val (None or True): validation in training process ? None or True default: None (optional)
79
-
80
- val_count (None or int): After how many examples learned will an accuracy test be performed? default: 10=(%10) it means every approximately 10 step (optional)
55
+ y_train (aray-like[cupy]): List or cupy array of target labels. (one hot encoded)
81
56
 
82
57
  activation_potentiation (list): For deeper PLAN networks, activation function parameters. For more information please run this code: plan.activations_list() default: [None] (optional)
83
58
 
84
- x_val (list[num]): List of validation data. default: x_train (optional)
85
-
86
- y_val (list[num]): (list[num]): List of target labels. (one hot encoded) default: y_train (optional)
87
-
88
- show_training (bool, str): True or None default: None (optional)
89
-
90
- LTD (int): Long Term Depression Hyperparameter for train PLAN neural network default: 0 (optional)
91
-
92
- interval (float, int): frame delay (milisecond) parameter for Training Report (show_training=True) This parameter effects to your Training Report performance. Lower value is more diffucult for Low end PC's (33.33 = 30 FPS, 16.67 = 60 FPS) default: 100 (optional)
93
-
94
- decision_boundary_status (bool): If the visualization of validation and training history is enabled during training, should the decision boundaries also be visualized? True or False. Default is True. (optional)
95
-
96
- train_bar (bool): Training loading bar? True or False. Default is True. (optional)
97
-
98
- auto_normalization(bool): Normalization process during training. May effect training time and model quality. True or False. Default is True. (optional)
99
-
100
- neurons_history (bool, optional): Shows the history of changes that neurons undergo during the CL (Cumulative Learning) stages. True or False. Default is False. (optional)
101
-
102
- dtype (cupy.dtype): Data type for the arrays. np.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
59
+ W (cupy.ndarray, optional): If you want to re-continue or update model
60
+
61
+ auto_normalization (bool, optional): Normalization may solves overflow problem. Default: False
103
62
 
104
- memory (str): The memory parameter determines whether the dataset to be processed on the GPU will be stored in the CPU's RAM or the GPU's RAM. Options: 'gpu', 'cpu'. Default: 'gpu'.
63
+ dtype (cupy.dtype, optional): Data type for the arrays. cp.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
105
64
 
106
65
  Returns:
107
- numpyarray([num]): (Weight matrix).
66
+ cupyarray: (Weight matrix).
108
67
  """
109
- # Pre-checks
68
+ # Pre-check
110
69
 
111
- if train_bar and val:
112
- train_progress = initialize_loading_bar(total=len(x_train), ncols=71, desc='Fitting', bar_format=bar_format_normal)
113
- elif train_bar and val == False:
114
- train_progress = initialize_loading_bar(total=len(x_train), ncols=44, desc='Fitting', bar_format=bar_format_normal)
70
+ if len(x_train) != len(y_train): raise ValueError("x_train and y_train must have the same length.")
115
71
 
116
- if len(x_train) != len(y_train):
117
- raise ValueError("x_train and y_train must have the same length.")
72
+ weight = cp.zeros((len(y_train[0]), len(x_train[0].ravel()))).astype(dtype, copy=False) if W is None else W
118
73
 
119
- if val and (x_val is None or y_val is None):
120
- x_val, y_val = x_train, y_train
121
-
122
- if memory == 'gpu':
123
- x_train = transfer_to_gpu(x_train, dtype=dtype)
124
- y_train = transfer_to_gpu(y_train, dtype=y_train.dtype)
125
-
126
- if val:
127
- x_val = transfer_to_gpu(x_val, dtype=dtype)
128
- y_val = transfer_to_gpu(y_val, dtype=y_train.dtype)
129
-
130
- elif memory == 'cpu':
131
- x_train = transfer_to_cpu(x_train, dtype=dtype)
132
- y_train = transfer_to_cpu(y_train, dtype=y_train.dtype)
133
-
134
- if val:
135
- x_val = transfer_to_cpu(x_val, dtype=dtype)
136
- y_val = transfer_to_cpu(y_val, dtype=y_train.dtype)
137
-
138
- else:
139
- raise ValueError("memory parameter must be 'cpu' or 'gpu'.")
140
-
141
- val_list = [] if val else None
142
- val_count = val_count or 10
143
- # Defining weights
144
- STPW = cp.ones((len(y_train[0]), len(x_train[0].ravel()))).astype(dtype, copy=False) # STPW = SHORT TERM POTENTIATION WEIGHT
145
- LTPW = cp.zeros((len(y_train[0]), len(x_train[0].ravel()))).astype(dtype, copy=False) # LTPW = LONG TERM POTENTIATION WEIGHT
146
- # Initialize visualization
147
- vis_objects = initialize_visualization_for_fit(val, show_training, neurons_history, x_train, y_train)
148
-
149
- # Training process
150
- for index, inp in enumerate(x_train):
151
- inp = transfer_to_gpu(inp, dtype=dtype).ravel()
152
- y_decoded = decode_one_hot(cp.array(y_train[index], copy=False, dtype=y_train.dtype))
153
- # Weight updates
154
- STPW = feed_forward(inp, STPW, is_training=True, Class=y_decoded, activation_potentiation=activation_potentiation, LTD=LTD)
155
- LTPW += normalization(STPW, dtype=dtype) if auto_normalization else STPW
156
-
157
- if val and index != 0:
158
- if index % math.ceil((val_count / len(x_train)) * 100) == 0:
159
- val_acc = evaluate(x_val, y_val, loading_bar_status=False, activation_potentiation=activation_potentiation, W=LTPW, memory=memory)[get_acc()]
160
- val_list.append(val_acc)
161
-
162
- # Visualization updates
163
- if show_training:
164
- update_weight_visualization_for_fit(vis_objects['ax'][0, 0], LTPW, vis_objects['artist2'])
165
- if decision_boundary_status:
166
- update_decision_boundary_for_fit(vis_objects['ax'][0, 1], x_val, y_val, activation_potentiation, LTPW, vis_objects['artist1'])
167
- update_validation_history_for_fit(vis_objects['ax'][1, 1], val_list, vis_objects['artist3'])
168
- update_neural_web_for_fit(W=LTPW, G=vis_objects['G'], ax=vis_objects['ax'][1, 0], artist=vis_objects['artist4'])
169
- if neurons_history:
170
- update_neuron_history(LTPW, row=vis_objects['row'], col=vis_objects['col'], class_count=len(y_train[0]), fig1=vis_objects['fig1'], ax1=vis_objects['ax1'], artist5=vis_objects['artist5'], acc=val_acc)
171
- if train_bar:
172
- train_progress.update(1)
173
-
174
- STPW = cp.ones((len(y_train[0]), len(x_train[0].ravel()))).astype(dtype, copy=False)
175
-
176
- if show_training:
177
- ani1 = display_visualization_for_fit(vis_objects['fig'], vis_objects['artist1'], interval)
178
- ani2 = display_visualization_for_fit(vis_objects['fig'], vis_objects['artist2'], interval)
179
- ani3 = display_visualization_for_fit(vis_objects['fig'], vis_objects['artist3'], interval)
180
- ani4 = display_visualization_for_fit(vis_objects['fig'], vis_objects['artist4'], interval)
181
- show()
74
+ if auto_normalization is True: x_train = normalization(apply_activation(x_train, activation_potentiation))
75
+ elif auto_normalization is False: x_train = apply_activation(x_train, activation_potentiation)
76
+ else: raise ValueError('normalization parameter only be True or False')
182
77
 
183
- if neurons_history:
184
- ani5 = display_visualization_for_fit(vis_objects['fig1'], vis_objects['artist5'], interval)
185
- show()
78
+ weight += y_train.T @ x_train
186
79
 
187
- return normalization(LTPW, dtype=dtype)
80
+ return normalization(weight, dtype=dtype)
188
81
 
189
82
 
190
- def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=None, batch_size=1,
191
- neural_web_history=False, show_current_activations=False, auto_normalization=True,
192
- neurons_history=False, early_stop=False, loss='categorical_crossentropy', show_history=False,
193
- interval=33.33, target_acc=None, target_loss=None,
83
+ def learner(x_train, y_train, optimizer, fitness, fit_start=True, gen=None, batch_size=1, pop_size=None,
84
+ neural_web_history=False, show_current_activations=False, auto_normalization=False,
85
+ neurons_history=False, early_stop=False, show_history=False,
86
+ interval=33.33, target_acc=None,
194
87
  start_this_act=None, start_this_W=None, dtype=cp.float32, memory='gpu'):
195
88
  """
196
89
  Optimizes the activation functions for a neural network by leveraging train data to find
@@ -223,29 +116,42 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
223
116
  batch_size=0.05,
224
117
  interval=16.67)
225
118
  ```
226
- fit_start (bool): If the fit_start parameter is set to True, the initial generation population undergoes a simple short training process using the PLAN algorithm. This allows for a very robust starting point, especially for large and complex datasets. However, for small or relatively simple datasets, it may result in unnecessary computational overhead. When fit_start is True, completing the first generation may take slightly longer (this increase in computational cost applies only to the first generation and does not affect subsequent generations). If fit_start is set to False, the initial population will be entirely random. Options: True or False. The fit_start parameter is MANDATORY and must be provided.
227
119
 
228
- strategy (str, optional): Learning strategy. (options: 'accuracy', 'f1', 'precision', 'recall'): 'accuracy', Maximizes train (or test if given) accuracy during learning. 'f1', Maximizes train (or test if given) f1 score during learning. 'precision', Maximizes train (or test if given) precision score during learning. 'recall', Maximizes train (or test if given) recall during learning. Default is 'accuracy'.
120
+ fitness (function): Fitness function. We have 'multi_head_fitness' function in the fitness_functions module. Use this: from pyerualjetwork import fitness_functions (and) fitness_function = lambda *args, **kwargs: fitness_functions.multi_head_fitness(*args, 'here give your fitness function hyperparameters for example: alpha=2, beta=1.5, lambda_div=0.05', **kwargs) Example:
121
+ ```python
122
+ fitness = lambda *args, **kwargs: fitness_functions.multi_head_fitness(*args, alpha=2, beta=1.5, lambda_div=0.05, **kwargs)
123
+
124
+ model = plan.learner(x_train,
125
+ y_train,
126
+ optimizer,
127
+ fitness
128
+ fit_start=True,
129
+ strategy='accuracy',
130
+ show_history=True,
131
+ gen=15,
132
+ batch_size=0.05,
133
+ interval=16.67)
134
+ ```
229
135
 
136
+ fit_start (bool, optional): If the fit_start parameter is set to True, the initial generation population undergoes a simple short training process using the PLAN algorithm. This allows for a very robust starting point, especially for large and complex datasets. However, for small or relatively simple datasets, it may result in unnecessary computational overhead. When fit_start is True, completing the first generation may take slightly longer (this increase in computational cost applies only to the first generation and does not affect subsequent generations). If fit_start is set to False, the initial population will be entirely random. Options: True or False. Default: True
137
+
230
138
  gen (int, optional): The generation count for genetic optimization.
231
139
 
232
140
  batch_size (float, optional): Batch size is used in the prediction process to receive train feedback by dividing the train data into chunks and selecting activations based on randomly chosen partitions. This process reduces computational cost and time while still covering the entire test set due to random selection, so it doesn't significantly impact accuracy. For example, a batch size of 0.08 means each train batch represents 8% of the train set. Default is 1. (%100 of train)
233
141
 
234
- early_stop (bool, optional): If True, implements early stopping during training.(If train accuracy not improves in two gen stops learning.) Default is False.
142
+ pop_size (int, optional): Population size of each generation. Default: count of activation functions
235
143
 
236
- auto_normalization (bool, optional): IMPORTANT: auto_nomralization parameter works only if fit_start is True. Do not change this value if fit_start is False, because it doesnt matter.) If auto normalization=False this makes more faster training times and much better accuracy performance for some datasets. Default is True.
144
+ early_stop (bool, optional): If True, implements early stopping during training.(If train accuracy not improves in two gen stops learning.) Default is False.
237
145
 
238
146
  show_current_activations (bool, optional): Should it display the activations selected according to the current strategies during learning, or not? (True or False) This can be very useful if you want to cancel the learning process and resume from where you left off later. After canceling, you will need to view the live training activations in order to choose the activations to be given to the 'start_this' parameter. Default is False
239
147
 
240
148
  show_history (bool, optional): If True, displays the training history after optimization. Default is False.
241
149
 
242
- loss (str, optional): For visualizing and monitoring. PLAN neural networks doesn't need any loss function in training. options: ('categorical_crossentropy' or 'binary_crossentropy') Default is 'categorical_crossentropy'.
243
-
150
+ auto_normalization (bool, optional): Normalization may solves overflow problem. Default: False
151
+
244
152
  interval (int, optional): The interval at which evaluations are conducted during training. (33.33 = 30 FPS, 16.67 = 60 FPS) Default is 100.
245
-
153
+
246
154
  target_acc (int, optional): The target accuracy to stop training early when achieved. Default is None.
247
-
248
- target_loss (float, optional): The target loss to stop training early when achieved. Default is None.
249
155
 
250
156
  start_this_act (list, optional): To resume a previously canceled or interrupted training from where it left off, or to continue from that point with a different strategy, provide the list of activation functions selected up to the learned portion to this parameter. Default is None
251
157
 
@@ -264,15 +170,20 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
264
170
 
265
171
  """
266
172
 
173
+ from .fitness_functions_cuda import diversity_score
267
174
  from .planeat_cuda import define_genomes
268
175
 
269
176
  data = 'Train'
270
177
 
271
- activation_potentiation = all_activations()
178
+ except_this = ['spiral', 'circular']
179
+ activation_potentiation = [item for item in all_activations() if item not in except_this]
272
180
  activation_potentiation_len = len(activation_potentiation)
273
181
 
182
+ if pop_size is None: pop_size = activation_potentiation_len
274
183
  y_train = optimize_labels(y_train, cuda=True)
275
184
 
185
+ if pop_size < activation_potentiation_len: raise ValueError(f"pop_size must be higher or equal to {activation_potentiation_len}")
186
+
276
187
  if memory == 'gpu':
277
188
  x_train = transfer_to_gpu(x_train, dtype=dtype)
278
189
  y_train = transfer_to_gpu(y_train, dtype=y_train.dtype)
@@ -288,41 +199,32 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
288
199
  else:
289
200
  raise ValueError("memory parameter must be 'cpu' or 'gpu'.")
290
201
 
291
- if strategy != 'accuracy' and strategy != 'f1' and strategy != 'recall' and strategy != 'precision': raise ValueError("Strategy parameter only be 'accuracy' or 'f1' or 'recall' or 'precision'.")
292
202
  if target_acc is not None and (target_acc < 0 or target_acc > 1): raise ValueError('target_acc must be in range 0 and 1')
293
203
  if fit_start is not True and fit_start is not False: raise ValueError('fit_start parameter only be True or False. Please read doc-string')
294
204
 
295
205
  # Initialize visualization components
296
206
  viz_objects = initialize_visualization_for_learner(show_history, neurons_history, neural_web_history, x_train, y_train)
297
207
 
298
- # Initialize progress bar
299
- if batch_size == 1:
300
- ncols = 76
301
- else:
302
- ncols = 89
303
-
304
208
  # Initialize variables
305
209
  best_acc = 0
306
- best_f1 = 0
307
- best_recall = 0
308
- best_precision = 0
210
+ best_fit = 0
309
211
  best_acc_per_gen_list = []
310
212
  postfix_dict = {}
311
- loss_list = []
213
+ fit_list = []
312
214
  target_pop = []
313
215
 
314
- progress = initialize_loading_bar(total=activation_potentiation_len, desc="", ncols=ncols, bar_format=bar_format_learner)
216
+ progress = initialize_loading_bar(total=activation_potentiation_len, desc="", ncols=76, bar_format=bar_format_learner)
315
217
 
316
- if fit_start is False:
317
- weight_pop, act_pop = define_genomes(input_shape=len(x_train[0]), output_shape=len(y_train[0]), population_size=activation_potentiation_len, dtype=dtype)
318
-
319
- if start_this_act is not None and start_this_W is not None:
320
- weight_pop[0] = start_this_W
321
- act_pop[0] = start_this_act
218
+ if fit_start is False or pop_size > activation_potentiation_len:
219
+ weight_pop, act_pop = define_genomes(input_shape=len(x_train[0]), output_shape=len(y_train[0]), population_size=pop_size, dtype=dtype)
322
220
 
323
221
  else:
324
- weight_pop = []
325
- act_pop = []
222
+ weight_pop = [0] * pop_size
223
+ act_pop = [0] * pop_size
224
+
225
+ if start_this_act is not None and start_this_W is not None:
226
+ weight_pop[0] = start_this_W
227
+ act_pop[0] = start_this_act
326
228
 
327
229
  for i in range(gen):
328
230
  postfix_dict["Gen"] = str(i+1) + '/' + str(gen)
@@ -332,91 +234,69 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
332
234
  progress.last_print_n = 0
333
235
  progress.update(0)
334
236
 
335
- for j in range(activation_potentiation_len):
237
+ for j in range(pop_size):
336
238
 
337
239
  x_train_batch, y_train_batch = batcher(x_train, y_train, batch_size=batch_size)
338
- if fit_start is True and i == 0:
339
- act_pop.append(activation_potentiation[j])
340
- W = fit(x_train_batch, y_train_batch, activation_potentiation=act_pop[-1], auto_normalization=auto_normalization, train_bar=False, dtype=dtype, memory=memory)
341
- weight_pop.append(W)
342
-
343
- model = evaluate(x_train_batch, y_train_batch, weight_pop[j], act_pop[j], loading_bar_status=False, dtype=dtype, memory=memory)
344
- acc = model[get_acc()]
345
-
346
- if strategy == 'accuracy': target_pop.append(acc)
347
-
348
- elif strategy == 'f1' or strategy == 'precision' or strategy == 'recall':
349
- precision_score, recall_score, f1_score = metrics(y_train_batch, model[get_preds()])
350
240
 
351
- if strategy == 'precision':
352
- target_pop.append(precision_score)
241
+ x_train_batch = cp.array(x_train_batch, dtype=dtype, copy=False)
242
+ y_train_batch = cp.array(y_train_batch, dtype=dtype, copy=False)
353
243
 
354
- if i == 0 and j == 0:
355
- best_precision = precision_score
356
-
357
- if strategy == 'recall':
358
- target_pop.append(recall_score)
359
-
360
- if i == 0 and j == 0:
361
- best_recall = recall_score
362
-
363
- if strategy == 'f1':
364
- target_pop.append(f1_score)
244
+ if fit_start is True and i == 0 and j < activation_potentiation_len:
245
+ if start_this_act is not None and j == 0:
246
+ pass
247
+ else:
248
+ act_pop[j] = activation_potentiation[j]
249
+ W = fit(x_train_batch, y_train_batch, activation_potentiation=act_pop[j], auto_normalization=auto_normalization, dtype=dtype)
250
+ weight_pop[j] = W
251
+
252
+ model = evaluate(x_train_batch, y_train_batch, W=weight_pop[j], activation_potentiation=act_pop[j])
253
+ acc = model[get_acc()]
254
+
255
+ div_score = diversity_score(weight_pop[j])
256
+ fit_score = fitness(y_train_batch, model[get_preds_softmax()], div_score, acc)
365
257
 
366
- if i == 0 and j == 0:
367
- best_f1 = f1_score
258
+ target_pop.append(fit_score)
368
259
 
369
- if ((strategy == 'accuracy' and acc >= best_acc) or
370
- (strategy == 'f1' and f1_score >= best_f1) or
371
- (strategy == 'precision' and precision_score >= best_precision) or
372
- (strategy == 'recall' and recall_score >= best_recall)):
260
+ if fit_score >= best_fit:
373
261
 
374
262
  best_acc = acc
375
- best_weights = cp.copy(weight_pop[j])
263
+ best_fit = fit_score
264
+ best_weight = cp.copy(weight_pop[j])
376
265
  final_activations = act_pop[j].copy() if isinstance(act_pop[j], list) else act_pop[j]
266
+
377
267
  best_model = model
378
268
 
379
269
  final_activations = [final_activations[0]] if len(set(final_activations)) == 1 else final_activations # removing if all same
380
270
 
381
271
  if batch_size == 1:
382
- postfix_dict[f"{data} Accuracy"] = cp.round(best_acc, 3)
383
- else:
384
- postfix_dict[f"{data} Batch Accuracy"] = cp.round(best_acc, 3)
385
- progress.set_postfix(postfix_dict)
272
+ postfix_dict[f"{data} Accuracy"] = cp.round(best_acc, 4)
273
+ progress.set_postfix(postfix_dict)
386
274
 
387
275
  if show_current_activations:
388
276
  print(f", Current Activations={final_activations}", end='')
389
-
390
- if loss == 'categorical_crossentropy':
391
- train_loss = categorical_crossentropy(y_true_batch=transfer_to_gpu(y_train_batch, dtype=y_train_batch.dtype), y_pred_batch=model[get_preds_softmax()])
392
- else:
393
- train_loss = binary_crossentropy(y_true_batch=transfer_to_gpu(y_train_batch, dtype=y_train_batch.dtype), y_pred_batch=model[get_preds_softmax()])
394
277
 
395
278
  if batch_size == 1:
396
- postfix_dict[f"{data} Loss"] = cp.round(train_loss, 3)
397
- best_loss = train_loss
398
- else:
399
- postfix_dict[f"{data} Batch Loss"] = cp.round(train_loss, 3)
400
- progress.set_postfix(postfix_dict)
401
- best_loss = train_loss
279
+ postfix_dict["Fitness"] = cp.round(fit_score, 4)
280
+ progress.set_postfix(postfix_dict)
281
+ best_fit = fit_score
402
282
 
403
283
  # Update visualizations during training
404
284
  if show_history:
405
285
  gen_list = range(1, len(best_acc_per_gen_list) + 2)
406
- update_history_plots_for_learner(viz_objects, gen_list, loss_list + [train_loss],
286
+ update_history_plots_for_learner(viz_objects, gen_list, fit_list + [fit_score],
407
287
  best_acc_per_gen_list + [best_acc], x_train, final_activations)
408
288
 
409
289
  if neurons_history:
410
290
  viz_objects['neurons']['artists'] = (
411
- update_neuron_history_for_learner(cp.copy(best_weights), viz_objects['neurons']['ax'],
291
+ update_neuron_history_for_learner(cp.copy(best_weight), viz_objects['neurons']['ax'],
412
292
  viz_objects['neurons']['row'], viz_objects['neurons']['col'],
413
293
  y_train[0], viz_objects['neurons']['artists'],
414
294
  data=data, fig1=viz_objects['neurons']['fig'],
415
- acc=best_acc, loss=train_loss)
295
+ acc=best_acc, loss=fit_score)
416
296
  )
417
297
 
418
298
  if neural_web_history:
419
- art5_1, art5_2, art5_3 = draw_neural_web(W=best_weights, ax=viz_objects['web']['ax'],
299
+ art5_1, art5_2, art5_3 = draw_neural_web(W=best_weight, ax=viz_objects['web']['ax'],
420
300
  G=viz_objects['web']['G'], return_objs=True)
421
301
  art5_list = [art5_1] + [art5_2] + list(art5_3.values())
422
302
  viz_objects['web']['artists'].append(art5_list)
@@ -424,52 +304,43 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
424
304
  # Check target accuracy
425
305
  if target_acc is not None and best_acc >= target_acc:
426
306
  progress.close()
427
- train_model = evaluate(x_train, y_train, W=best_weights, loading_bar_status=False,
428
- activation_potentiation=final_activations, dtype=dtype)
429
-
430
- if loss == 'categorical_crossentropy':
431
- train_loss = categorical_crossentropy(y_true_batch=y_train,
432
- y_pred_batch=train_model[get_preds_softmax()])
433
- else:
434
- train_loss = binary_crossentropy(y_true_batch=y_train,
435
- y_pred_batch=train_model[get_preds_softmax()])
436
-
437
- print('\nActivations: ', final_activations)
438
- print(f'Train Accuracy:', train_model[get_acc()])
439
- print(f'Train Loss: ', train_loss, '\n')
440
-
441
- # Display final visualizations
442
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
443
- train_loss, y_train, interval)
444
- return best_weights, best_model[get_preds()], best_acc, final_activations
445
-
446
- # Check target loss
447
- if target_loss is not None and best_loss <= target_loss:
448
- progress.close()
449
- train_model = evaluate(x_train, y_train, W=best_weights, loading_bar_status=False,
450
- activation_potentiation=final_activations, dtype=dtype)
307
+ train_model = evaluate(x_train, y_train, W=best_weight,
308
+ activation_potentiation=final_activations)
451
309
 
452
- if loss == 'categorical_crossentropy':
453
- train_loss = categorical_crossentropy(y_true_batch=y_train,
454
- y_pred_batch=train_model[get_preds_softmax()])
455
- else:
456
- train_loss = binary_crossentropy(y_true_batch=y_train,
457
- y_pred_batch=train_model[get_preds_softmax()])
310
+ div_score = diversity_score(best_weight)
311
+ fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
458
312
 
459
313
  print('\nActivations: ', final_activations)
460
314
  print(f'Train Accuracy:', train_model[get_acc()])
461
- print(f'Train Loss: ', train_loss, '\n')
462
-
463
- # Display final visualizations
464
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
465
- train_loss, y_train, interval)
466
- return best_weights, best_model[get_preds()], best_acc, final_activations
315
+ print(f'Fitness Value: ', fit_score, '\n')
316
+
317
+ display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
318
+ fit_score, y_train, interval)
319
+ return best_weight, best_model[get_preds()], best_acc, final_activations
467
320
 
468
321
  progress.update(1)
469
-
470
- best_acc_per_gen_list.append(best_acc)
471
- loss_list.append(best_loss)
472
322
 
323
+ if batch_size != 1:
324
+ train_model = evaluate(x_train, y_train, best_weight, final_activations)
325
+
326
+ div_score = diversity_score(best_weight)
327
+ fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
328
+
329
+ print('\nActivations: ', final_activations)
330
+ print(f'Train Accuracy:', train_model[get_acc()])
331
+ print(f'Fitness Value: ', fit_score, '\n')
332
+
333
+ postfix_dict[f"{data} Accuracy"] = cp.round(train_model[get_acc()], 4)
334
+ postfix_dict["Fitness"] = cp.round(best_fit, 4)
335
+ progress.set_postfix(postfix_dict)
336
+
337
+ best_acc_per_gen_list.append(train_model[get_acc()])
338
+ fit_list.append(fit_score)
339
+
340
+ else:
341
+ best_acc_per_gen_list.append(best_acc)
342
+ fit_list.append(best_fit)
343
+
473
344
  weight_pop, act_pop = optimizer(cp.array(weight_pop, copy=False, dtype=dtype), act_pop, i, cp.array(target_pop, dtype=dtype, copy=False), bar_status=False)
474
345
  target_pop = []
475
346
 
@@ -477,99 +348,42 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
477
348
  if early_stop == True and i > 0:
478
349
  if best_acc_per_gen_list[i] == best_acc_per_gen_list[i-1]:
479
350
  progress.close()
480
- train_model = evaluate(x_train, y_train, W=best_weights, loading_bar_status=False,
481
- activation_potentiation=final_activations, dtype=dtype, memory=memory)
351
+ train_model = evaluate(x_train, y_train, W=best_weight,
352
+ activation_potentiation=final_activations)
482
353
 
483
- if loss == 'categorical_crossentropy':
484
- train_loss = categorical_crossentropy(y_true_batch=y_train,
485
- y_pred_batch=train_model[get_preds_softmax()])
486
- else:
487
- train_loss = binary_crossentropy(y_true_batch=y_train,
488
- y_pred_batch=train_model[get_preds_softmax()])
354
+ div_score = diversity_score(best_weight)
355
+ fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
489
356
 
490
357
  print('\nActivations: ', final_activations)
491
358
  print(f'Train Accuracy:', train_model[get_acc()])
492
- print(f'Train Loss: ', train_loss, '\n')
359
+ print(f'Fitness Value: ', fit_score, '\n')
493
360
 
494
361
  # Display final visualizations
495
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
496
- train_loss, y_train, interval)
497
- return best_weights, best_model[get_preds()], best_acc, final_activations
362
+ display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
363
+ fit_score, y_train, interval)
364
+ return best_weight, best_model[get_preds()], best_acc, final_activations
498
365
 
499
366
  # Final evaluation
500
367
  progress.close()
501
- train_model = evaluate(x_train, y_train, W=best_weights, loading_bar_status=False,
502
- activation_potentiation=final_activations, dtype=dtype, memory=memory)
368
+ train_model = evaluate(x_train, y_train, W=best_weight,
369
+ activation_potentiation=final_activations)
503
370
 
504
- if loss == 'categorical_crossentropy':
505
- train_loss = categorical_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
506
- else:
507
- train_loss = binary_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
371
+ div_score = diversity_score(best_weight)
372
+ fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
508
373
 
509
374
  print('\nActivations: ', final_activations)
510
375
  print(f'Train Accuracy:', train_model[get_acc()])
511
- print(f'Train Loss: ', train_loss, '\n')
376
+ print(f'Fitness Value: ', fit_score, '\n')
512
377
 
513
378
  # Display final visualizations
514
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc, train_loss, y_train, interval)
515
- return best_weights, best_model[get_preds()], best_acc, final_activations
516
-
517
-
518
-
519
- def feed_forward(
520
- Input, # list[num]: Input data.
521
- w, # num: Weight matrix of the neural network.
522
- is_training, # bool: Flag indicating if the function is called during training (True or False).
523
- activation_potentiation,
524
- Class='?', # int: Which class is, if training. # (list): Activation potentiation list for deep PLAN. (optional)
525
- LTD=0
526
- ) -> tuple:
527
- """
528
- Applies feature extraction process to the input data using synaptic potentiation.
529
-
530
- Args:
531
- Input (num): Input data.
532
- w (num): Weight matrix of the neural network.
533
- is_training (bool): Flag indicating if the function is called during training (True or False).
534
- Class (int): if is during training then which class(label) ? is isnt then put None.
535
- # activation_potentiation (list): ac list for deep PLAN. default: [None] ('linear') (optional)
536
-
537
- Returns:
538
- tuple: A tuple (vector) containing the neural layer result and the updated weight matrix.
539
- or
540
- num: neural network output
541
- """
542
-
543
- Output = apply_activation(Input, activation_potentiation)
544
-
545
- Input = Output
546
-
547
- if is_training == True:
548
-
549
- for _ in range(LTD):
550
-
551
- depression_vector = cp.random.rand(*Input.shape)
552
-
553
- Input -= depression_vector
554
-
555
- w[Class, :] = Input
556
- return w
557
-
558
- else:
559
-
560
- neural_layer = cp.dot(w, Input)
561
-
562
- return neural_layer
379
+ display_visualizations_for_learner(viz_objects, best_weight, data, best_acc, fit_score, y_train, interval)
380
+ return best_weight, best_model[get_preds()], best_acc, final_activations
563
381
 
564
382
  def evaluate(
565
383
  x_test,
566
384
  y_test,
567
385
  W,
568
- activation_potentiation=['linear'],
569
- loading_bar_status=True,
570
- show_metrics=False,
571
- dtype=cp.float32,
572
- memory='gpu'
386
+ activation_potentiation=['linear']
573
387
  ) -> tuple:
574
388
  """
575
389
  Evaluates the neural network model using the given test data.
@@ -579,70 +393,18 @@ def evaluate(
579
393
 
580
394
  y_test (cp.ndarray): Test labels (one-hot encoded).
581
395
 
582
- W (list[cp.ndarray]): Neural net weight matrix.
396
+ W (cp.ndarray): Neural net weight matrix.
583
397
 
584
398
  activation_potentiation (list): Activation list. Default = ['linear'].
585
399
 
586
- loading_bar_status (bool): Loading bar (optional). Default = True.
587
-
588
- show_metrics (bool): Visualize metrics ? (optional). Default = False.
589
-
590
- dtype (cupy.dtype): Data type for the arrays. cp.float32 by default. Example: cp.float64 or cp.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
591
-
592
- memory (str): The memory parameter determines whether the dataset to be processed on the GPU will be stored in the CPU's RAM or the GPU's RAM. Options: 'gpu', 'cpu'. Default: 'gpu'.
593
-
594
400
  Returns:
595
401
  tuple: Model (list).
596
402
  """
597
403
 
598
- if memory == 'gpu':
599
- x_test = transfer_to_gpu(x_test, dtype=dtype)
600
- y_test = transfer_to_gpu(y_test, dtype=y_test.dtype)
601
-
602
- elif memory == 'cpu':
603
- x_test = transfer_to_cpu(x_test, dtype=dtype)
604
- y_test = transfer_to_cpu(y_test, dtype=y_test.dtype)
605
-
606
- else:
607
- raise ValueError("memory parameter must be 'cpu' or 'gpu'.")
608
-
609
- predict_probabilitys = cp.empty((len(x_test), W.shape[0]), dtype=dtype)
610
- real_classes = cp.empty(len(x_test), dtype=y_test.dtype)
611
- predict_classes = cp.empty(len(x_test), dtype=y_test.dtype)
612
-
613
- true_predict = 0
614
- acc_list = cp.empty(len(x_test), dtype=dtype)
615
-
616
- if loading_bar_status:
617
- loading_bar = initialize_loading_bar(total=len(x_test), ncols=64, desc='Testing', bar_format=bar_format_normal)
618
-
619
- for inpIndex in range(len(x_test)):
620
- Input = transfer_to_gpu(x_test[inpIndex], dtype=dtype).ravel()
621
- neural_layer = Input
622
-
623
- neural_layer = feed_forward(neural_layer, cp.copy(W), is_training=False, Class='?', activation_potentiation=activation_potentiation)
624
-
625
- predict_probabilitys[inpIndex] = Softmax(neural_layer)
626
-
627
- RealOutput = decode_one_hot(transfer_to_gpu(y_test[inpIndex], dtype=y_test[inpIndex].dtype))
628
- real_classes[inpIndex] = RealOutput
629
- PredictedOutput = cp.argmax(neural_layer)
630
- predict_classes[inpIndex] = PredictedOutput
631
-
632
- if RealOutput == PredictedOutput:
633
- true_predict += 1
634
-
635
- acc = true_predict / (inpIndex + 1)
636
- acc_list[inpIndex] = acc
637
-
638
- if loading_bar_status:
639
- loading_bar.update(1)
640
- loading_bar.set_postfix({"Test Accuracy": acc})
641
-
642
- if loading_bar_status:
643
- loading_bar.close()
644
-
645
- if show_metrics:
646
- plot_evaluate(x_test, y_test, predict_classes, acc_list, W=cp.copy(W), activation_potentiation=activation_potentiation)
404
+ x_test = apply_activation(x_test, activation_potentiation)
405
+ result = x_test @ W.T
406
+ epsilon = 1e-10
407
+ accuracy = (cp.argmax(result, axis=1) == cp.argmax(y_test, axis=1)).mean()
408
+ softmax_preds = cp.exp(result) / (cp.sum(cp.exp(result), axis=1, keepdims=True) + epsilon)
647
409
 
648
- return W, predict_classes, acc_list[-1], None, None, predict_probabilitys
410
+ return W, result, accuracy, None, None, softmax_preds