pyerualjetwork 5.1__py3-none-any.whl → 5.5__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 (29) hide show
  1. pyerualjetwork/__init__.py +15 -14
  2. pyerualjetwork/cpu/__init__.py +24 -0
  3. pyerualjetwork/{activation_functions_cpu.py → cpu/activation_functions.py} +40 -4
  4. pyerualjetwork/{data_operations_cpu.py → cpu/data_ops.py} +17 -19
  5. pyerualjetwork/{metrics_cpu.py → cpu/metrics.py} +3 -1
  6. pyerualjetwork/{visualizations_cpu.py → cpu/visualizations.py} +96 -139
  7. pyerualjetwork/cuda/__init__.py +24 -0
  8. pyerualjetwork/{activation_functions_cuda.py → cuda/activation_functions.py} +54 -5
  9. pyerualjetwork/{data_operations_cuda.py → cuda/data_ops.py} +16 -16
  10. pyerualjetwork/{metrics_cuda.py → cuda/metrics.py} +1 -1
  11. pyerualjetwork/{visualizations_cuda.py → cuda/visualizations.py} +8 -244
  12. pyerualjetwork/{ene_cpu.py → ene.py} +29 -95
  13. pyerualjetwork/fitness_functions.py +0 -1
  14. pyerualjetwork/help.py +5 -5
  15. pyerualjetwork/issue_solver.py +39 -11
  16. pyerualjetwork/{memory_operations.py → memory_ops.py} +1 -1
  17. pyerualjetwork/model_ops.py +734 -0
  18. pyerualjetwork/{neu_cpu.py → nn.py} +199 -91
  19. pyerualjetwork/{model_operations_cpu.py → old_cpu_model_ops.py} +62 -59
  20. pyerualjetwork/{model_operations_cuda.py → old_cuda_model_ops.py} +99 -86
  21. {pyerualjetwork-5.1.dist-info → pyerualjetwork-5.5.dist-info}/METADATA +16 -18
  22. pyerualjetwork-5.5.dist-info/RECORD +27 -0
  23. pyerualjetwork/ene_cuda.py +0 -962
  24. pyerualjetwork/neu_cuda.py +0 -588
  25. pyerualjetwork-5.1.dist-info/RECORD +0 -26
  26. /pyerualjetwork/{loss_functions_cpu.py → cpu/loss_functions.py} +0 -0
  27. /pyerualjetwork/{loss_functions_cuda.py → cuda/loss_functions.py} +0 -0
  28. {pyerualjetwork-5.1.dist-info → pyerualjetwork-5.5.dist-info}/WHEEL +0 -0
  29. {pyerualjetwork-5.1.dist-info → pyerualjetwork-5.5.dist-info}/top_level.txt +0 -0
@@ -1,588 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
-
4
-
5
- NEU (Neural Networks) on CUDA
6
- =============================
7
- This module hosts functions for training and evaluating artificial neural networks on CUDA GPU for labeled classification tasks (for now).
8
-
9
- Currently, 3 types of models can be trained:
10
-
11
- PLAN (Potentiation Learning Artificial Neural Network)
12
- * Training Time for Small Projects: fast
13
- * Training Time for Big Projects: fast
14
- * Explainability: high
15
- * Learning Capacity: medium (compared to single perceptrons)
16
-
17
- MLP (Multi-Layer Perceptron → Deep Learning) -- With non-bias
18
- * Training Time for Small Projects: fast
19
- * Training Time for Big Projects: slow
20
- * Explainability: low
21
- * Learning Capacity: high
22
-
23
- PTNN (Potentiation Transfer Neural Network) -- With non-bias
24
- * Training Time for Small Projects: fast
25
- * Training Time for Big Projects: fast
26
- * Explainability: low
27
- * Learning Capacity: high
28
-
29
- Read learn function docstring for know how to use of these model architectures.
30
-
31
-
32
- For more information about PLAN: https://github.com/HCB06/PyerualJetwork/blob/main/Welcome_to_PLAN/PLAN.pdf
33
-
34
- Module functions:
35
- -----------------
36
- - plan_fit()
37
- - learn()
38
- - evaluate()
39
-
40
- Examples: https://github.com/HCB06/PyerualJetwork/tree/main/Welcome_to_PyerualJetwork/ExampleCodes
41
-
42
- PyerualJetwork document: https://github.com/HCB06/PyerualJetwork/blob/main/Welcome_to_PyerualJetwork/PYERUALJETWORK_USER_MANUEL_AND_LEGAL_INFORMATION(EN).pdf
43
-
44
- - Author: Hasan Can Beydili
45
- - YouTube: https://www.youtube.com/@HasanCanBeydili
46
- - Linkedin: https://www.linkedin.com/in/hasan-can-beydili-77a1b9270/
47
- - Instagram: https://www.instagram.com/canbeydilj
48
- - Contact: tchasancan@gmail.com
49
- """
50
-
51
- import cupy as cp
52
- import numpy as np
53
- import copy
54
- import random
55
-
56
- ### LIBRARY IMPORTS ###
57
- from .ui import loading_bars, initialize_loading_bar
58
- from .data_operations_cuda import normalization
59
- from .activation_functions_cuda import apply_activation, all_activations
60
- from .model_operations_cuda import get_acc, get_preds_softmax
61
- from .memory_operations import transfer_to_gpu, transfer_to_cpu, optimize_labels
62
- from .loss_functions_cuda import categorical_crossentropy, binary_crossentropy
63
- from .fitness_functions import wals
64
- from .visualizations_cuda import (
65
- draw_neural_web,
66
- display_visualizations_for_learner,
67
- update_history_plots_for_learner,
68
- initialize_visualization_for_learner,
69
- update_neuron_history_for_learner
70
- )
71
-
72
- ### GLOBAL VARIABLES ###
73
- bar_format_normal = loading_bars()[0]
74
- bar_format_learner = loading_bars()[1]
75
-
76
- # BUILD -----
77
-
78
- def plan_fit(
79
- x_train,
80
- y_train,
81
- activations=['linear'],
82
- W=None,
83
- auto_normalization=False,
84
- dtype=cp.float32
85
- ):
86
- """
87
- Creates a PLAN model to fitting data.,
88
-
89
- plan_fit Args:
90
- :param (aray-like[cupy]) x_train: (aray-like[cupy]): List or cupy array of input data.
91
- :param (aray-like[cupy]) y_train: List or cupy array of target labels. (one hot encoded)
92
- :param (list) activations: For deeper PLAN networks, activation function parameters. For more information please run this code: neu_cuda.activations_list() default: [None] (optional)
93
- W (cupy.ndarray, optional): If you want to re-continue or update model
94
- auto_normalization (bool, optional): Normalization may solves overflow problem. Default: False
95
- dtype (cupy.dtype, optional): Data type for the arrays. cp.float32 by default. Example: cp.float64 or cp.float16.
96
-
97
- Returns:
98
- cupyarray: (Weight matrix).
99
- """
100
- # Pre-check
101
-
102
- if len(x_train) != len(y_train): raise ValueError("x_train and y_train must have the same length.")
103
-
104
- weight = cp.zeros((len(y_train[0]), len(x_train[0].ravel()))).astype(dtype, copy=False) if W is None else W
105
-
106
- if auto_normalization is True: x_train = normalization(apply_activation(x_train, activations))
107
- elif auto_normalization is False: x_train = apply_activation(x_train, activations)
108
- else: raise ValueError('normalization parameter only be True or False')
109
-
110
- weight += y_train.T @ x_train
111
-
112
- return normalization(weight, dtype=dtype)
113
-
114
- def learn(x_train, y_train, optimizer, gen, pop_size, fit_start=True, batch_size=1,
115
- weight_evolve=True, neural_web_history=False, show_current_activations=False, auto_normalization=False, target_acc=None,
116
- neurons_history=False, early_stop=False, show_history=False, loss='categorical_crossentropy',
117
- interval=33.33, target_loss=None, loss_impact=0.1, acc_impact=0.9,
118
- start_this_act=None, start_this_W=None, neurons=[], activation_functions=[], dtype=cp.float32, memory='gpu'):
119
- """
120
- Optimizes the activation functions for a neural network by leveraging train data to find
121
- the most accurate combination of activation potentiation(or activation function) & weight values for the given labeled classificaiton dataset.
122
-
123
- Why genetic optimization ENE(Eugenic NeuroEvolution) and not backpropagation?
124
- 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.
125
- Since activation functions are not differentiable, we cannot use gradient descent or backpropagation. However, I developed a more powerful genetic optimization algorithm: ENE.
126
-
127
- * This function also able to train classic MLP model architectures.
128
- * And my newest innovative architecture: PTNN (Potentiation Transfer Neural Network).
129
-
130
- :Args:
131
- :param x_train: (array-like): Training input data.
132
- :param y_train: (array-like): Labels for training data.
133
- :param optimizer: (function): Optimization technique with hyperparameters. (PLAN, MLP & PTNN (all) using ENE for optimization. Gradient based technique's will added in the future.) Please use this: from pyerualjetwork.ene_cuda import evolver (and) optimizer = lambda *args, **kwargs: evolver(*args, 'here give your hyperparameters for example: activation_add_prob=0.85', **kwargs) Example:
134
- ```python
135
-
136
- optimizer = lambda *args, **kwargs: ene_cuda.evolver(*args,
137
- activation_add_prob=0.05,
138
- strategy='aggressive',
139
- policy='more_selective',
140
- **kwargs)
141
-
142
- model = neu_cuda.learn(x_train,
143
- y_train,
144
- optimizer,
145
- fit_start=True,
146
- show_history=True,
147
- gen=15,
148
- batch_size=0.05,
149
- interval=16.67)
150
- ```
151
- :param 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. Additonaly if you want to train PTNN model you must be give True. Options: True or False. Default: True
152
- :param gen: (int or list): The generation count for genetic optimization. If you want to train PTNN model you must give a list of two number. First number for PLAN model training second number for MLP.
153
- :param 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 train 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)
154
- :param pop_size: (int): Population size of each generation.
155
- :param weight_evolve: (bool, optional): Activation combinations already optimizes by PLANEAT genetic search algorithm. Should the weight parameters also evolve or should the weights be determined according to the aggregating learning principle of the PLAN algorithm? Default: True (Evolves Weights)
156
- :param neural_web_history: (bool, optional): Draws history of neural web. Default is False. [ONLY FOR PLAN MODELS]
157
- :param 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
158
- :param auto_normalization: (bool, optional): Normalization may solves overflow problem. Default: False
159
- :param target_acc: (float, optional): The target accuracy to stop training early when achieved. Default is None.
160
- :param neurons_history: (bool, optional): Shows the history of changes that neurons undergo during the ENE process. True or False. Default is False. [ONLY FOR PLAN MODELS]
161
- :param early_stop: (bool, optional): If True, implements early stopping during training.(If train accuracy not improves in two gen stops learning.) Default is False.
162
- :param show_history: (bool, optional): If True, displays the training history after optimization. Default is False.
163
- :param 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'.
164
- :param interval: (int, optional): The interval at which evaluations are conducted during training. (33.33 = 30 FPS, 16.67 = 60 FPS) Default is 33.33.
165
- :param target_loss: (float, optional): The target loss to stop training early when achieved. Default is None.
166
- :param loss_impact: (float, optional): Impact of loss for optimization [0-1]. Default: 0.1
167
- :param acc_impact: (float, optional): Impact of accuracy for optimization [0-1]. Default: 0.9
168
- :param 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
169
- :param start_this_W: (cupy.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
170
- :param neurons: (list[int], optional): If you dont want train PLAN model this parameter represents neuron count of each hidden layer for MLP or PTNN. Number of elements --> Layer count. Default: [] (No hidden layer) --> architecture setted to PLAN, if not --> architecture setted to MLP.
171
- :param activation_functions: (list[str], optional): If you dont want train PLAN model this parameter represents activation function of each hidden layer for MLP or PTNN. if neurons is not [] --> uses default: ['linear'] * len(neurons). if neurons is [] --> uses [].
172
- :param dtype: (cupy.dtype): Data type for the Weight matrices. np.float32 by default. Example: cp.float64 or cp.float16.
173
- :param 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'.
174
-
175
- Returns:
176
- tuple: A list for model parameters: [Weight matrix, Train Preds, Train Accuracy, [Activations functions]]. You can acces this parameters in model_operations module. For example: model_operations.get_weights() for Weight matrix.
177
- """
178
-
179
- from .ene_cuda import define_genomes
180
-
181
- data = 'Train'
182
-
183
- except_this = ['spiral', 'circular']
184
- activations = [item for item in all_activations() if item not in except_this]
185
- activations_len = len(activations)
186
-
187
- if pop_size > activations_len and fit_start is True:
188
- for _ in range(pop_size - len(activations)):
189
- random_index_all_act = random.randint(0, len(activations)-1)
190
- activations.append(activations[random_index_all_act])
191
-
192
- y_train = optimize_labels(y_train, cuda=True)
193
-
194
- if pop_size < activations_len: raise ValueError(f"pop_size must be higher or equal to {activations_len}")
195
-
196
- if memory == 'gpu':
197
- x_train = transfer_to_gpu(x_train, dtype=x_train.dtype)
198
- y_train = transfer_to_gpu(y_train, dtype=y_train.dtype)
199
-
200
- from .data_operations_cuda import batcher
201
-
202
- elif memory == 'cpu':
203
- x_train = transfer_to_cpu(x_train, dtype=x_train.dtype)
204
- y_train = transfer_to_cpu(y_train, dtype=y_train.dtype)
205
-
206
- from .data_operations_cpu import batcher
207
-
208
- else:
209
- raise ValueError("memory parameter must be 'cpu' or 'gpu'.")
210
-
211
- 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')
212
- 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')
213
-
214
- if neurons != []:
215
- weight_evolve = True
216
-
217
- if activation_functions == []: activation_functions = ['linear'] * len(neurons)
218
-
219
- if fit_start is False:
220
- # MLP
221
- activations = activation_functions
222
- model_type = 'MLP'
223
- activation_potentiations = [0] * pop_size
224
- activation_potentiation = None
225
- is_mlp = True
226
- transfer_learning = False
227
- else:
228
- # PTNN
229
- model_type = 'PLAN' # First generation index gen[0] is PLAN, other index gen[1] it will change to PTNN (PLAN Connects to MLP and will transfer the learned information)
230
- transfer_learning = True
231
-
232
- neurons_copy = neurons.copy()
233
- neurons = []
234
- gen_copy = gen.copy()
235
- gen = gen[0] + gen[1]
236
- activation_potentiations = [0] * pop_size
237
- activation_potentiation = None
238
- is_mlp = False # it will change
239
-
240
- else:
241
- # PLAN
242
- model_type = 'PLAN'
243
- transfer_learning = False
244
-
245
- activation_potentiations = [0] * pop_size # NOTE: For PLAN models, activation_potentiations is needed BUT activations variable already mirros activation_potentiation values.
246
- # So, we don't need to use activation_potentiations variable. activation_potentiations variable is only for PTNN models.
247
- activation_potentiation = None
248
- is_mlp = False
249
-
250
- # Initialize visualization components
251
- viz_objects = initialize_visualization_for_learner(show_history, neurons_history, neural_web_history, x_train, y_train)
252
-
253
- # Initialize variables
254
- best_acc = 0
255
- best_loss = float('inf')
256
- best_fitness = float('-inf')
257
- best_acc_per_gen_list = []
258
- postfix_dict = {}
259
- loss_list = []
260
- target_pop = []
261
-
262
- progress = initialize_loading_bar(total=activations_len, desc="", ncols=79, bar_format=bar_format_learner)
263
-
264
- if fit_start is False:
265
- weight_pop, act_pop = define_genomes(input_shape=len(x_train[0]), output_shape=len(y_train[0]), neurons=neurons, activation_functions=activations, population_size=pop_size, dtype=dtype)
266
-
267
- else:
268
- weight_pop = [0] * len(activations)
269
- act_pop = [0] * len(activations)
270
-
271
- if start_this_act is not None and start_this_W is not None:
272
- weight_pop[0] = start_this_W
273
- act_pop[0] = start_this_act
274
-
275
- # LEARNING STARTED
276
- for i in range(gen):
277
-
278
- # TRANSFORMATION PLAN TO MLP FOR PTNN (in later generations)
279
- if model_type == 'PLAN' and transfer_learning:
280
- if i == gen_copy[0]:
281
-
282
- model_type = 'PTNN'
283
- neurons = neurons_copy
284
-
285
- for individual in range(len(weight_pop)):
286
- weight_pop[individual] = cp.copy(best_weight)
287
- activation_potentiations[individual] = final_activations.copy() if isinstance(final_activations, list) else final_activations
288
-
289
- activation_potentiation = activation_potentiations[0]
290
-
291
- neurons_copy = [len(y_train[0])] + neurons_copy
292
- activation_functions = ['linear'] + activation_functions
293
-
294
- weight_pop, act_pop = define_genomes(input_shape=len(x_train[0]), output_shape=len(y_train[0]), neurons=neurons_copy, activation_functions=activation_functions, population_size=pop_size, dtype=dtype)
295
-
296
- # 0 indexed individual will keep PLAN's learned informations and in later generations it will share other individuals.
297
- for l in range(1, len(weight_pop[0])):
298
- original_shape = weight_pop[0][l].shape
299
-
300
- identity_matrix = cp.eye(original_shape[0], original_shape[1], dtype=weight_pop[0][l].dtype)
301
- weight_pop[0][l] = identity_matrix
302
-
303
- for l in range(len(weight_pop)):
304
- weight_pop[l][0] = cp.copy(best_weight)
305
-
306
- best_weight = list(weight_pop[0])
307
- final_activations = act_pop[0]
308
- is_mlp = True
309
- fit_start = False
310
-
311
-
312
- postfix_dict["Gen"] = str(i+1) + '/' + str(gen)
313
- progress.set_postfix(postfix_dict)
314
-
315
- progress.n = 0
316
- progress.last_print_n = 0
317
- progress.update(0)
318
-
319
- for j in range(pop_size):
320
-
321
- x_train_batch, y_train_batch = batcher(x_train, y_train, batch_size=batch_size)
322
-
323
- x_train_batch = cp.array(x_train_batch, dtype=x_train_batch.dtype, copy=False)
324
- y_train_batch = cp.array(y_train_batch, dtype=y_train.dtype)
325
-
326
- if fit_start is True and i == 0:
327
- if start_this_act is not None and j == 0:
328
- pass
329
- else:
330
-
331
- act_pop[j] = activations[j]
332
- W = plan_fit(x_train_batch, y_train_batch, activations=act_pop[j], auto_normalization=auto_normalization, dtype=dtype)
333
- weight_pop[j] = W
334
-
335
- if weight_evolve is False:
336
- weight_pop[j] = plan_fit(x_train_batch, y_train_batch, activations=act_pop[j], auto_normalization=auto_normalization, dtype=dtype)
337
-
338
- model = evaluate(x_train_batch, y_train_batch, W=weight_pop[j], activations=act_pop[j], activation_potentiations=activation_potentiations[j], auto_normalization=auto_normalization, model_type=model_type)
339
- acc = model[get_acc()]
340
-
341
- if loss == 'categorical_crossentropy':
342
- train_loss = categorical_crossentropy(y_true_batch=y_train_batch,
343
- y_pred_batch=model[get_preds_softmax()])
344
- else:
345
- train_loss = binary_crossentropy(y_true_batch=y_train_batch,
346
- y_pred_batch=model[get_preds_softmax()])
347
-
348
-
349
- fitness = wals(acc, train_loss, acc_impact, loss_impact)
350
- target_pop.append(fitness.get())
351
-
352
- if fitness >= best_fitness:
353
-
354
- best_fitness = fitness
355
- best_acc = acc
356
- best_loss = train_loss
357
- best_weight = cp.copy(weight_pop[j]) if model_type == 'PLAN' else copy.deepcopy(weight_pop[j])
358
- best_model = model
359
-
360
- final_activations = act_pop[j].copy() if isinstance(act_pop[j], list) else act_pop[j]
361
- if model_type == 'PLAN': final_activations = [final_activations[0]] if len(set(final_activations)) == 1 else final_activations # removing if all same
362
-
363
- if batch_size == 1:
364
- postfix_dict[f"{data} Accuracy"] = cp.round(best_acc, 4)
365
- postfix_dict[f"{data} Loss"] = cp.round(train_loss, 4)
366
- progress.set_postfix(postfix_dict)
367
-
368
- if show_current_activations:
369
- print(f", Current Activations={final_activations}", end='')
370
-
371
- # Update visualizations during training
372
- if show_history:
373
- gen_list = range(1, len(best_acc_per_gen_list) + 2)
374
- update_history_plots_for_learner(viz_objects, gen_list, loss_list + [train_loss],
375
- best_acc_per_gen_list + [best_acc], x_train, final_activations)
376
-
377
- if neurons_history:
378
- viz_objects['neurons']['artists'] = (
379
- update_neuron_history_for_learner(cp.copy(best_weight), viz_objects['neurons']['ax'],
380
- viz_objects['neurons']['row'], viz_objects['neurons']['col'],
381
- y_train[0], viz_objects['neurons']['artists'],
382
- data=data, fig1=viz_objects['neurons']['fig'],
383
- acc=best_acc, loss=train_loss)
384
- )
385
-
386
- if neural_web_history:
387
- art5_1, art5_2, art5_3 = draw_neural_web(W=best_weight, ax=viz_objects['web']['ax'],
388
- G=viz_objects['web']['G'], return_objs=True)
389
- art5_list = [art5_1] + [art5_2] + list(art5_3.values())
390
- viz_objects['web']['artists'].append(art5_list)
391
-
392
- # Check target accuracy
393
- if target_acc is not None and best_acc >= target_acc:
394
- progress.close()
395
- train_model = evaluate(x_train, y_train, W=best_weight,
396
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
397
- if loss == 'categorical_crossentropy':
398
- train_loss = categorical_crossentropy(y_true_batch=y_train,
399
- y_pred_batch=train_model[get_preds_softmax()])
400
- else:
401
- train_loss = binary_crossentropy(y_true_batch=y_train,
402
- y_pred_batch=train_model[get_preds_softmax()])
403
- print('\nActivations: ', final_activations)
404
- print('Activation Potentiation: ', activation_potentiation)
405
- print('Train Accuracy:', train_model[get_acc()])
406
- print('Train Loss: ', train_loss, '\n')
407
- print('Model Type:', model_type)
408
-
409
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
410
- best_loss, y_train, interval)
411
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, None, None, None, None, None, activation_potentiation
412
-
413
- # Check target loss
414
- if target_loss is not None and best_loss <= target_loss:
415
- progress.close()
416
- train_model = evaluate(x_train, y_train, W=best_weight,
417
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
418
-
419
- if loss == 'categorical_crossentropy':
420
- train_loss = categorical_crossentropy(y_true_batch=y_train,
421
- y_pred_batch=train_model[get_preds_softmax()])
422
- else:
423
- train_loss = binary_crossentropy(y_true_batch=y_train,
424
- y_pred_batch=train_model[get_preds_softmax()])
425
-
426
- print('\nActivations: ', final_activations)
427
- print('Activation Potentiation: ', activation_potentiation)
428
- print('Train Accuracy:', train_model[get_acc()])
429
- print('Train Loss: ', train_loss, '\n')
430
- print('Model Type:', model_type)
431
-
432
- # Display final visualizations
433
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
434
- train_loss, y_train, interval)
435
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, None, None, None, None, None, activation_potentiation, None, None, None, None, None, None, None, activation_potentiation
436
-
437
-
438
- progress.update(1)
439
-
440
- if batch_size != 1:
441
- train_model = evaluate(x_train, y_train, W=best_weight,
442
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
443
-
444
- if loss == 'categorical_crossentropy':
445
- train_loss = categorical_crossentropy(y_true_batch=y_train,
446
- y_pred_batch=train_model[get_preds_softmax()])
447
- else:
448
- train_loss = binary_crossentropy(y_true_batch=y_train,
449
- y_pred_batch=train_model[get_preds_softmax()])
450
-
451
- postfix_dict[f"{data} Accuracy"] = cp.round(train_model[get_acc()], 4)
452
- postfix_dict[f"{data} Loss"] = cp.round(train_loss, 4)
453
- progress.set_postfix(postfix_dict)
454
-
455
- best_acc_per_gen_list.append(train_model[get_acc()])
456
- loss_list.append(train_loss)
457
-
458
- else:
459
- best_acc_per_gen_list.append(best_acc)
460
- loss_list.append(best_loss)
461
-
462
- if model_type == 'PLAN': weight_pop = cp.array(weight_pop, copy=False, dtype=dtype)
463
-
464
- weight_pop, act_pop = optimizer(weight_pop, act_pop, i, np.array(target_pop), weight_evolve=weight_evolve, is_mlp=is_mlp, bar_status=False)
465
- target_pop = []
466
-
467
- # Early stopping check
468
- if early_stop == True and i > 0:
469
- if best_acc_per_gen_list[i] == best_acc_per_gen_list[i-1]:
470
- progress.close()
471
- train_model = evaluate(x_train, y_train, W=best_weight,
472
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
473
-
474
- if loss == 'categorical_crossentropy':
475
- train_loss = categorical_crossentropy(y_true_batch=y_train,
476
- y_pred_batch=train_model[get_preds_softmax()])
477
- else:
478
- train_loss = binary_crossentropy(y_true_batch=y_train,
479
- y_pred_batch=train_model[get_preds_softmax()])
480
-
481
- print('\nActivations: ', final_activations)
482
- print('Activation Potentiation: ', activation_potentiation)
483
- print('Train Accuracy:', train_model[get_acc()])
484
- print('Train Loss: ', train_loss, '\n')
485
- print('Model Type:', model_type)
486
-
487
- # Display final visualizations
488
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
489
- train_loss, y_train, interval)
490
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, None, None, None, None, None, activation_potentiation
491
-
492
- # Final evaluation
493
- progress.close()
494
- train_model = evaluate(x_train, y_train, W=best_weight,
495
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
496
-
497
- if loss == 'categorical_crossentropy':
498
- train_loss = categorical_crossentropy(y_true_batch=y_train,
499
- y_pred_batch=train_model[get_preds_softmax()])
500
- else:
501
- train_loss = binary_crossentropy(y_true_batch=y_train,
502
- y_pred_batch=train_model[get_preds_softmax()])
503
-
504
- print('\nActivations: ', final_activations)
505
- print('Activation Potentiation: ', activation_potentiation)
506
- print('Train Accuracy:', train_model[get_acc()])
507
- print('Train Loss: ', train_loss, '\n')
508
- print('Model Type:', model_type)
509
-
510
- # Display final visualizations
511
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc, train_loss, y_train, interval)
512
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, None, None, None, None, None, activation_potentiation
513
-
514
- def evaluate(
515
- x_test,
516
- y_test,
517
- model_type,
518
- W,
519
- activations=['linear'],
520
- activation_potentiations=[],
521
- auto_normalization=False
522
- ) -> tuple:
523
- """
524
- Evaluates the neural network model using the given test data.
525
-
526
- Args:
527
- x_test (cp.ndarray): Test data.
528
-
529
- y_test (cp.ndarray): Test labels (one-hot encoded).
530
-
531
- model_type: (str): Type of the model. Options: 'PLAN', 'MLP', 'PTNN'.
532
-
533
- W (cp.ndarray): Neural net weight matrix.
534
-
535
- activations (list, optional): Activation list for PLAN or MLP models (MLP layers activations if it PTNN model). Default = ['linear'].
536
-
537
- activation_potentiations (list, optional): Extra activation potentiation list (PLAN layers activations) for PTNN models. Default = [].
538
-
539
- auto_normalization (bool, optional): Normalization for x_test ? Default = False.
540
-
541
- Returns:
542
- tuple: Model (list).
543
- """
544
-
545
- if auto_normalization: x_test = normalization(x_test, dtype=x_test.dtype)
546
-
547
- if isinstance(activations, str):
548
- activations = [activations]
549
- elif isinstance(activations, list):
550
- activations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activations]
551
-
552
- if model_type == 'MLP':
553
- layer = x_test
554
- for i in range(len(W)):
555
- if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
556
-
557
- layer = layer @ W[i].T
558
-
559
- result = layer
560
-
561
- if model_type == 'PLAN':
562
-
563
- x_test = apply_activation(x_test, activations)
564
- result = x_test @ W.T
565
-
566
- if model_type == 'PTNN':
567
-
568
- if isinstance(activation_potentiations, str):
569
- activation_potentiations = [activation_potentiations]
570
- elif isinstance(activation_potentiations, list):
571
- activation_potentiations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activation_potentiations]
572
-
573
- x_test = apply_activation(x_test, activation_potentiations)
574
- layer = x_test @ W[0].T
575
-
576
- for i in range(1, len(W)):
577
- if i != len(W) - 1: layer = apply_activation(layer, activations[i])
578
-
579
- layer = layer @ W[i].T
580
-
581
- result = layer
582
-
583
- max_vals = cp.max(result, axis=1, keepdims=True)
584
-
585
- softmax_preds = cp.exp(result - max_vals) / cp.sum(cp.exp(result - max_vals), axis=1, keepdims=True)
586
- accuracy = (cp.argmax(softmax_preds, axis=1) == cp.argmax(y_test, axis=1)).mean()
587
-
588
- return W, result, accuracy, None, None, softmax_preds
@@ -1,26 +0,0 @@
1
- pyerualjetwork/__init__.py,sha256=IwEIh3pG8VOF-x3j5zczYZoFU0sAPskLx74yLtbhuik,2732
2
- pyerualjetwork/activation_functions_cpu.py,sha256=qP_Ipi2-c5tyJ7Jb9gWJZCj2AgeOIzLBdoEqQOUXD-s,5885
3
- pyerualjetwork/activation_functions_cuda.py,sha256=IJAqlbVdE01MrtOCX8TvxkGmqqhEcr1p9qozy0Sjpjw,5673
4
- pyerualjetwork/data_operations_cpu.py,sha256=HemqiYfSdlQKTTYNzpCh_9lTtS3AimMI4DvqJBAGjGw,16186
5
- pyerualjetwork/data_operations_cuda.py,sha256=5zgyJGPjQuHyx6IHNkRwMguYhm-GcI6Hal49WNvw-bM,18536
6
- pyerualjetwork/ene_cpu.py,sha256=2y5__d-vx7t5Ajs4IPuNnQe8ULR39Km_KQFNIUnalGA,45167
7
- pyerualjetwork/ene_cuda.py,sha256=dWavZydKL9b5BAGL430SuWs1TenMelbFtltoEAXKkJY,45673
8
- pyerualjetwork/fitness_functions.py,sha256=pUmAbUy5ex1Vpu6n_RRac_df-FR52gYIV8uxYS5H3tw,1252
9
- pyerualjetwork/help.py,sha256=FcX8mxo1_mvoqONVXY0Kn7S09CDkhi0jwNmn8g9mYZc,804
10
- pyerualjetwork/issue_solver.py,sha256=iY6hSsBxYI5l82RwnXQp2DrRUJyksk_7U9GUSnt2YfU,3117
11
- pyerualjetwork/loss_functions_cpu.py,sha256=6PyBI232SQRGuFnG3LDGvnv_PUdWzT2_2mUODJiejGI,618
12
- pyerualjetwork/loss_functions_cuda.py,sha256=C93IZJcrOpT6HMK9x1O4AHJWXYTkN5WZiqdssPbvAPk,617
13
- pyerualjetwork/memory_operations.py,sha256=g24d-cDuUFc0fOEtk3AJe-z_EBctYV5S4cY1rQ6VGiE,14279
14
- pyerualjetwork/metrics_cpu.py,sha256=vbfMwS0ay2heMSa0GNo-ydLjQ8cfexbLwaREp4FKAtY,6081
15
- pyerualjetwork/metrics_cuda.py,sha256=PWyJyexeqlPKb09LAcF55JvhZVeXLCu3P_siYq5m2gg,5065
16
- pyerualjetwork/model_operations_cpu.py,sha256=Y0uPkLVbdodP7lC-fOPdja3RWi2J9z2rwWIS2pxzotU,20523
17
- pyerualjetwork/model_operations_cuda.py,sha256=9KfaO3NwvA-bwZqshYQwfc5e-4AYPp53EwFHMvDHS3I,21537
18
- pyerualjetwork/neu_cpu.py,sha256=C9nNzMaan8-QyUK6gXTM_PQiSb4cBEpbM-WMLGbuDDY,31202
19
- pyerualjetwork/neu_cuda.py,sha256=nK74JgfyJDbaPmSR1CmtZc_PaSKk0FeFmNcWHe5-8U0,32354
20
- pyerualjetwork/ui.py,sha256=JBTFYz5R24XwNKhA3GSW-oYAoiIBxAE3kFGXkvm5gqw,656
21
- pyerualjetwork/visualizations_cpu.py,sha256=StyD1Hl1Gt55EMqR6tO3yVJZdPyGkOgCnQ75Zn8K6J8,28252
22
- pyerualjetwork/visualizations_cuda.py,sha256=7lYrkOdrjwQGB3T4k_vI8UDxsm_TRjzaSSg9GhlNczs,28667
23
- pyerualjetwork-5.1.dist-info/METADATA,sha256=aYILBPyOZMu50jTsmu22UVYh6DMToxX_XdhPF720RQw,8132
24
- pyerualjetwork-5.1.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
25
- pyerualjetwork-5.1.dist-info/top_level.txt,sha256=BRyt62U_r3ZmJpj-wXNOoA345Bzamrj6RbaWsyW4tRg,15
26
- pyerualjetwork-5.1.dist-info/RECORD,,