pyerualjetwork 4.3.7.dev1__py3-none-any.whl → 4.3.8__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 (39) 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/model_operations.py +14 -14
  6. pyerualjetwork/model_operations_cuda.py +16 -17
  7. pyerualjetwork/parallel.py +118 -0
  8. pyerualjetwork/plan.py +61 -256
  9. pyerualjetwork/plan_cuda.py +60 -267
  10. pyerualjetwork/planeat.py +12 -44
  11. pyerualjetwork/planeat_cuda.py +9 -45
  12. pyerualjetwork/visualizations.py +29 -26
  13. pyerualjetwork/visualizations_cuda.py +20 -22
  14. {pyerualjetwork-4.3.7.dev1.dist-info → pyerualjetwork-4.3.8.dist-info}/METADATA +2 -19
  15. pyerualjetwork-4.3.8.dist-info/RECORD +25 -0
  16. pyerualjetwork-4.3.8.dist-info/top_level.txt +1 -0
  17. pyerualjetwork-4.3.7.dev1.dist-info/RECORD +0 -44
  18. pyerualjetwork-4.3.7.dev1.dist-info/top_level.txt +0 -2
  19. pyerualjetwork_afterburner/__init__.py +0 -11
  20. pyerualjetwork_afterburner/activation_functions.py +0 -290
  21. pyerualjetwork_afterburner/activation_functions_cuda.py +0 -289
  22. pyerualjetwork_afterburner/data_operations.py +0 -406
  23. pyerualjetwork_afterburner/data_operations_cuda.py +0 -461
  24. pyerualjetwork_afterburner/help.py +0 -17
  25. pyerualjetwork_afterburner/loss_functions.py +0 -21
  26. pyerualjetwork_afterburner/loss_functions_cuda.py +0 -21
  27. pyerualjetwork_afterburner/memory_operations.py +0 -298
  28. pyerualjetwork_afterburner/metrics.py +0 -190
  29. pyerualjetwork_afterburner/metrics_cuda.py +0 -163
  30. pyerualjetwork_afterburner/model_operations.py +0 -408
  31. pyerualjetwork_afterburner/model_operations_cuda.py +0 -420
  32. pyerualjetwork_afterburner/plan.py +0 -425
  33. pyerualjetwork_afterburner/plan_cuda.py +0 -436
  34. pyerualjetwork_afterburner/planeat.py +0 -793
  35. pyerualjetwork_afterburner/planeat_cuda.py +0 -797
  36. pyerualjetwork_afterburner/ui.py +0 -22
  37. pyerualjetwork_afterburner/visualizations.py +0 -823
  38. pyerualjetwork_afterburner/visualizations_cuda.py +0 -825
  39. {pyerualjetwork-4.3.7.dev1.dist-info → pyerualjetwork-4.3.8.dist-info}/WHEEL +0 -0
@@ -1,425 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
-
4
- MAIN MODULE FOR PLAN
5
-
6
- Examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes
7
-
8
- PLAN document: https://github.com/HCB06/PyerualJetwork/blob/main/Welcome_to_PLAN/PLAN.pdf
9
- PYERUALJETWORK document: https://github.com/HCB06/PyerualJetwork/blob/main/Welcome_to_PyerualJetwork/PYERUALJETWORK_USER_MANUEL_AND_LEGAL_INFORMATION(EN).pdf
10
-
11
- @author: Hasan Can Beydili
12
- @YouTube: https://www.youtube.com/@HasanCanBeydili
13
- @Linkedin: https://www.linkedin.com/in/hasan-can-beydili-77a1b9270/
14
- @Instagram: https://www.instagram.com/canbeydilj/
15
- @contact: tchasancan@gmail.com
16
- """
17
-
18
- import numpy as np
19
-
20
- ### LIBRARY IMPORTS ###
21
- from .ui import loading_bars, initialize_loading_bar
22
- from .data_operations import normalization, batcher
23
- from .loss_functions import binary_crossentropy, categorical_crossentropy
24
- from .activation_functions import apply_activation, all_activations
25
- from .metrics import metrics
26
- from .model_operations import get_acc, get_preds, get_preds_softmax
27
- from .memory_operations import optimize_labels
28
- from .visualizations import (
29
- draw_neural_web,
30
- display_visualizations_for_learner,
31
- update_history_plots_for_learner,
32
- initialize_visualization_for_learner,
33
- update_neuron_history_for_learner
34
- )
35
-
36
- ### GLOBAL VARIABLES ###
37
- bar_format_normal = loading_bars()[0]
38
- bar_format_learner = loading_bars()[1]
39
-
40
- # BUILD -----
41
-
42
- def fit(
43
- x_train,
44
- y_train,
45
- activation_potentiation=['linear'],
46
- W=None,
47
- dtype=np.float32
48
- ):
49
- """
50
- Creates a model to fitting data.
51
-
52
- fit Args:
53
-
54
- x_train (aray-like[num]): List or numarray of input data.
55
-
56
- y_train (aray-like[num]): List or numarray of target labels. (one hot encoded)
57
-
58
- activation_potentiation (list): For deeper PLAN networks, activation function parameters. For more information please run this code: plan.activations_list() default: [None] (optional)
59
-
60
- W (numpy.ndarray): If you want to re-continue or update model
61
-
62
- dtype (numpy.dtype): Data type for the arrays. np.float32 by default. Example: np.float64 or np.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
63
-
64
- Returns:
65
- numpyarray: (Weight matrix).
66
- """
67
-
68
- # Pre-check
69
-
70
- if len(x_train) != len(y_train):
71
- raise ValueError("x_train and y_train must have the same length.")
72
-
73
- LTPW = np.zeros((len(y_train[0]), len(x_train[0].ravel()))).astype(dtype, copy=False) if W is None else W
74
-
75
- x_train = apply_activation(x_train, activation_potentiation)
76
- LTPW += np.array(y_train, dtype=optimize_labels(y_train, cuda=False).dtype).T @ x_train
77
-
78
- return normalization(LTPW, dtype=dtype)
79
-
80
-
81
- def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=None, batch_size=1,
82
- neural_web_history=False, show_current_activations=False,
83
- neurons_history=False, early_stop=False, loss='categorical_crossentropy', show_history=False,
84
- interval=33.33, target_acc=None, target_loss=None,
85
- start_this_act=None, start_this_W=None, dtype=np.float32):
86
- """
87
- Optimizes the activation functions for a neural network by leveraging train data to find
88
- the most accurate combination of activation potentiation for the given dataset using genetic algorithm NEAT (Neuroevolution of Augmenting Topologies). But modifided for PLAN version. Created by me: PLANEAT.
89
-
90
- Why genetic optimization and not backpropagation?
91
- Because PLAN is different from other neural network architectures. In PLAN, the learnable parameters are not the weights; instead, the learnable parameters are the activation functions.
92
- Since activation functions are not differentiable, we cannot use gradient descent or backpropagation. However, I developed a more powerful genetic optimization algorithm: PLANEAT.
93
-
94
- Args:
95
-
96
- x_train (array-like): Training input data.
97
-
98
- y_train (array-like): Labels for training data. one-hot encoded.
99
-
100
- optimizer (function): PLAN optimization technique with hyperparameters. (PLAN using NEAT(PLANEAT) for optimization.) Please use this: from pyerualjetwork import planeat (and) optimizer = lambda *args, **kwargs: planeat.evolve(*args, 'here give your neat hyperparameters for example: activation_add_prob=0.85', **kwargs) Example:
101
- ```python
102
- genetic_optimizer = lambda *args, **kwargs: planeat.evolver(*args,
103
- activation_add_prob=0.85,
104
- strategy='aggressive',
105
- **kwargs)
106
-
107
- model = plan.learner(x_train,
108
- y_train,
109
- optimizer=genetic_optimizer,
110
- fit_start=True,
111
- strategy='accuracy',
112
- show_history=True,
113
- gen=15,
114
- batch_size=0.05,
115
- interval=16.67)
116
- ```
117
-
118
- 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.
119
-
120
- 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'.
121
-
122
- gen (int, optional): The generation count for genetic optimization.
123
-
124
- batch_size (float, optional): Batch size is used in the prediction process to receive train feedback by dividing the test 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)
125
-
126
- early_stop (bool, optional): If True, implements early stopping during training.(If accuracy not improves in two gen stops learning.) Default is False.
127
-
128
- 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
129
-
130
- show_history (bool, optional): If True, displays the training history after optimization. Default is False.
131
-
132
- 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'.
133
-
134
- interval (int, optional): The interval at which evaluations are conducted during training. (33.33 = 30 FPS, 16.67 = 60 FPS) Default is 100.
135
-
136
- target_acc (int, optional): The target accuracy to stop training early when achieved. Default is None.
137
-
138
- target_loss (float, optional): The target loss to stop training early when achieved. Default is None.
139
-
140
- 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
141
-
142
- start_this_W (numpy.array, 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 weight matrix of this genome. Default is None
143
-
144
- neurons_history (bool, optional): Shows the history of changes that neurons undergo during the TFL (Test or Train Feedback Learning) stages. True or False. Default is False.
145
-
146
- neural_web_history (bool, optional): Draws history of neural web. Default is False.
147
-
148
- dtype (numpy.dtype): Data type for the arrays. np.float32 by default. Example: np.float64 or np.float16. [fp32 for balanced devices, fp64 for strong devices, fp16 for weak devices: not reccomended!] (optional)
149
-
150
- Returns:
151
- tuple: A list for model parameters: [Weight matrix, Test loss, Test Accuracy, [Activations functions]].
152
-
153
- """
154
-
155
- from .planeat import define_genomes
156
-
157
- data = 'Train'
158
-
159
- activation_potentiation = all_activations()
160
- activation_potentiation_len = len(activation_potentiation)
161
-
162
- # Pre-checks
163
-
164
- x_train = x_train.astype(dtype, copy=False)
165
- y_train = optimize_labels(y_train, cuda=False)
166
-
167
- if gen is None:
168
- gen = activation_potentiation_len
169
-
170
- 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'.")
171
- 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')
172
- 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')
173
-
174
- # Initialize visualization components
175
- viz_objects = initialize_visualization_for_learner(show_history, neurons_history, neural_web_history, x_train, y_train)
176
-
177
- # Initialize progress bar
178
- if batch_size == 1:
179
- ncols = 76
180
- else:
181
- ncols = 89
182
-
183
- # Initialize variables
184
- best_acc = 0
185
- best_f1 = 0
186
- best_recall = 0
187
- best_precision = 0
188
- best_acc_per_gen_list = []
189
- postfix_dict = {}
190
- loss_list = []
191
- target_pop = []
192
-
193
- progress = initialize_loading_bar(total=activation_potentiation_len, desc="", ncols=ncols, bar_format=bar_format_learner)
194
-
195
- if fit_start is False:
196
- 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)
197
-
198
- if start_this_act is not None and start_this_W is not None:
199
- weight_pop[0] = start_this_W
200
- act_pop[0] = start_this_act
201
-
202
- else:
203
- weight_pop = []
204
- act_pop = []
205
-
206
- for i in range(gen):
207
- postfix_dict["Gen"] = str(i+1) + '/' + str(gen)
208
- progress.set_postfix(postfix_dict)
209
-
210
- progress.n = 0
211
- progress.last_print_n = 0
212
- progress.update(0)
213
-
214
- for j in range(activation_potentiation_len):
215
-
216
- x_train_batch, y_train_batch = batcher(x_train, y_train, batch_size=batch_size)
217
-
218
- if fit_start is True and i == 0:
219
- act_pop.append(activation_potentiation[j])
220
- W = fit(x_train_batch, y_train_batch, activation_potentiation=act_pop[-1], dtype=dtype)
221
- weight_pop.append(W)
222
-
223
- model = evaluate(x_train_batch, y_train_batch, W=weight_pop[j], activation_potentiation=act_pop[j])
224
- acc = model[get_acc()]
225
-
226
- if strategy == 'accuracy': target_pop.append(acc)
227
-
228
- elif strategy == 'f1' or strategy == 'precision' or strategy == 'recall':
229
- precision_score, recall_score, f1_score = metrics(y_train_batch, model[get_preds()])
230
-
231
- if strategy == 'precision':
232
- target_pop.append(precision_score)
233
-
234
- if i == 0 and j == 0:
235
- best_precision = precision_score
236
-
237
- if strategy == 'recall':
238
- target_pop.append(recall_score)
239
-
240
- if i == 0 and j == 0:
241
- best_recall = recall_score
242
-
243
- if strategy == 'f1':
244
- target_pop.append(f1_score)
245
-
246
- if i == 0 and j == 0:
247
- best_f1 = f1_score
248
-
249
- if ((strategy == 'accuracy' and acc >= best_acc) or
250
- (strategy == 'f1' and f1_score >= best_f1) or
251
- (strategy == 'precision' and precision_score >= best_precision) or
252
- (strategy == 'recall' and recall_score >= best_recall)):
253
-
254
- best_acc = acc
255
- best_weights = np.copy(weight_pop[j])
256
- final_activations = act_pop[j].copy() if isinstance(act_pop[j], list) else act_pop[j]
257
-
258
- best_model = model
259
-
260
- final_activations = [final_activations[0]] if len(set(final_activations)) == 1 else final_activations # removing if all same
261
-
262
- if batch_size == 1:
263
- postfix_dict[f"{data} Accuracy"] = best_acc
264
- else:
265
- postfix_dict[f"{data} Batch Accuracy"] = acc
266
- progress.set_postfix(postfix_dict)
267
-
268
- if show_current_activations:
269
- print(f", Current Activations={final_activations}", end='')
270
-
271
- if loss == 'categorical_crossentropy':
272
- train_loss = categorical_crossentropy(y_true_batch=y_train_batch, y_pred_batch=model[get_preds_softmax()])
273
- else:
274
- train_loss = binary_crossentropy(y_true_batch=y_train_batch, y_pred_batch=model[get_preds_softmax()])
275
-
276
- if batch_size == 1:
277
- postfix_dict[f"{data} Loss"] = train_loss
278
- best_loss = train_loss
279
- else:
280
- postfix_dict[f"{data} Batch Loss"] = train_loss
281
- progress.set_postfix(postfix_dict)
282
- best_loss = train_loss
283
-
284
- # Update visualizations during training
285
- if show_history:
286
- gen_list = range(1, len(best_acc_per_gen_list) + 2)
287
- update_history_plots_for_learner(viz_objects, gen_list, loss_list + [train_loss],
288
- best_acc_per_gen_list + [best_acc], x_train, final_activations)
289
-
290
- if neurons_history:
291
- viz_objects['neurons']['artists'] = (
292
- update_neuron_history_for_learner(np.copy(best_weights), viz_objects['neurons']['ax'],
293
- viz_objects['neurons']['row'], viz_objects['neurons']['col'],
294
- y_train[0], viz_objects['neurons']['artists'],
295
- data=data, fig1=viz_objects['neurons']['fig'],
296
- acc=best_acc, loss=train_loss)
297
- )
298
-
299
- if neural_web_history:
300
- art5_1, art5_2, art5_3 = draw_neural_web(W=best_weights, ax=viz_objects['web']['ax'],
301
- G=viz_objects['web']['G'], return_objs=True)
302
- art5_list = [art5_1] + [art5_2] + list(art5_3.values())
303
- viz_objects['web']['artists'].append(art5_list)
304
-
305
- # Check target accuracy
306
- if target_acc is not None and best_acc >= target_acc:
307
- progress.close()
308
- train_model = evaluate(x_train, y_train, W=best_weights,
309
- activation_potentiation=final_activations, dtype=dtype)
310
-
311
- if loss == 'categorical_crossentropy':
312
- train_loss = categorical_crossentropy(y_true_batch=y_train,
313
- y_pred_batch=train_model[get_preds_softmax()])
314
- else:
315
- train_loss = binary_crossentropy(y_true_batch=y_train,
316
- y_pred_batch=train_model[get_preds_softmax()])
317
-
318
- print('\nActivations: ', final_activations)
319
- print(f'Train Accuracy:', train_model[get_acc()])
320
- print(f'Train Loss: ', train_loss, '\n')
321
-
322
- # Display final visualizations
323
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
324
- train_loss, y_train, interval)
325
- return best_weights, best_model[get_preds()], best_acc, final_activations
326
-
327
- # Check target loss
328
- if target_loss is not None and best_loss <= target_loss:
329
- progress.close()
330
- train_model = evaluate(x_train, y_train, W=best_weights,
331
- activation_potentiation=final_activations, dtype=dtype)
332
-
333
- if loss == 'categorical_crossentropy':
334
- train_loss = categorical_crossentropy(y_true_batch=y_train,
335
- y_pred_batch=train_model[get_preds_softmax()])
336
- else:
337
- train_loss = binary_crossentropy(y_true_batch=y_train,
338
- y_pred_batch=train_model[get_preds_softmax()])
339
-
340
- print('\nActivations: ', final_activations)
341
- print(f'Train Accuracy:', train_model[get_acc()])
342
- print(f'Train Loss: ', train_loss, '\n')
343
-
344
- # Display final visualizations
345
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
346
- train_loss, y_train, interval)
347
- return best_weights, best_model[get_preds()], best_acc, final_activations
348
-
349
- progress.update(1)
350
-
351
- best_acc_per_gen_list.append(best_acc)
352
- loss_list.append(best_loss)
353
-
354
- weight_pop, act_pop = optimizer(np.array(weight_pop, copy=False, dtype=dtype), act_pop, i, np.array(target_pop, dtype=dtype, copy=False), bar_status=False)
355
- target_pop = []
356
-
357
- # Early stopping check
358
- if early_stop == True and i > 0:
359
- if best_acc_per_gen_list[i] == best_acc_per_gen_list[i-1]:
360
- progress.close()
361
- train_model = evaluate(x_train, y_train, W=best_weights,
362
- activation_potentiation=final_activations, dtype=dtype)
363
-
364
- if loss == 'categorical_crossentropy':
365
- train_loss = categorical_crossentropy(y_true_batch=y_train,
366
- y_pred_batch=train_model[get_preds_softmax()])
367
- else:
368
- train_loss = binary_crossentropy(y_true_batch=y_train,
369
- y_pred_batch=train_model[get_preds_softmax()])
370
-
371
- print('\nActivations: ', final_activations)
372
- print(f'Train Accuracy:', train_model[get_acc()])
373
- print(f'Train Loss: ', train_loss, '\n')
374
-
375
- # Display final visualizations
376
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
377
- train_loss, y_train, interval)
378
- return best_weights, best_model[get_preds()], best_acc, final_activations
379
-
380
- # Final evaluation
381
- progress.close()
382
- train_model = evaluate(x_train, y_train, W=best_weights,
383
- activation_potentiation=final_activations, dtype=dtype)
384
-
385
- if loss == 'categorical_crossentropy':
386
- train_loss = categorical_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
387
- else:
388
- train_loss = binary_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
389
-
390
- print('\nActivations: ', final_activations)
391
- print(f'Train Accuracy:', train_model[get_acc()])
392
- print(f'Train Loss: ', train_loss, '\n')
393
-
394
- # Display final visualizations
395
- display_visualizations_for_learner(viz_objects, best_weights, data, best_acc, train_loss, y_train, interval)
396
- return best_weights, best_model[get_preds()], best_acc, final_activations
397
-
398
-
399
- def evaluate(
400
- x_test,
401
- y_test,
402
- W,
403
- activation_potentiation=['linear']
404
- ) -> tuple:
405
- """
406
- Evaluates the neural network model using the given test data.
407
-
408
- Args:
409
- x_test (np.ndarray): Test data.
410
-
411
- y_test (np.ndarray): Test labels (one-hot encoded).
412
-
413
- W (np.ndarray): Neural net weight matrix.
414
-
415
- activation_potentiation (list): Activation list. Default = ['linear'].
416
-
417
- Returns:
418
- tuple: Model (list).
419
- """
420
-
421
- x_test = apply_activation(x_test, activation_potentiation)
422
- result = x_test @ W.T
423
- softmax_preds = np.exp(result) / np.sum(np.exp(result), axis=1, keepdims=True); accuracy = (np.argmax(result, axis=1) == np.argmax(y_test, axis=1)).mean()
424
-
425
- return W, None, accuracy, None, None, softmax_preds