pyerualjetwork 5.37__py3-none-any.whl → 5.40a0__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.
pyerualjetwork/cuda/nn.py DELETED
@@ -1,605 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
-
4
-
5
- NN (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
- - Creator: 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_ops import normalization
59
- from .activation_functions import apply_activation, all_activations
60
- from .model_ops import get_acc, get_preds_softmax
61
- from ..memory_ops import transfer_to_gpu, transfer_to_cpu, optimize_labels
62
- from .loss_functions import categorical_crossentropy, binary_crossentropy
63
- from ..fitness_functions import wals
64
- from .visualizations 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
- Examples:
131
-
132
- This creates a PLAN model:
133
- - ```learn(x_train, y_train, optimizer, pop_size=100, gen=100, fit_start=True) ```
134
-
135
- This creates a MLP model(with 2 hidden layer):
136
- - ```learn(x_train, y_train, optimizer, pop_size=100, gen=100, fit_start=False, neurons=[64, 64], activation_functions=['tanh', 'tanh']) ```
137
-
138
- This creates a PTNN model(with 2 hidden layer & 1 aggregation layer(comes with PLAN)):
139
- - ```learn(x_train, y_train, optimizer, pop_size=100, gen=[10, 100], fit_start=True, neurons=[64, 64], activation_functions=['tanh', 'tanh']) ```
140
-
141
- :Args:
142
- :param x_train: (array-like): Training input data.
143
- :param y_train: (array-like): Labels for training data.
144
- :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.cuda.ene import evolver (and) optimizer = lambda *args, **kwargs: evolver(*args, 'here give your hyperparameters for example: activation_add_prob=0.85', **kwargs) Example:
145
- ```python
146
-
147
- optimizer = lambda *args, **kwargs: ene.evolver(*args,
148
- activation_add_prob=0.05,
149
- strategy='aggressive',
150
- policy='more_selective',
151
- **kwargs)
152
-
153
- model = nn.learn(x_train,
154
- y_train,
155
- optimizer,
156
- fit_start=True,
157
- show_history=True,
158
- gen=15,
159
- batch_size=0.05,
160
- interval=16.67)
161
- ```
162
- :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
163
- :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.
164
- :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)
165
- :param pop_size: (int): Population size of each generation.
166
- :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)
167
- :param neural_web_history: (bool, optional): Draws history of neural web. Default is False. [ONLY FOR PLAN MODELS]
168
- :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
169
- :param auto_normalization: (bool, optional): Normalization may solves overflow problem. Default: False
170
- :param target_acc: (float, optional): The target accuracy to stop training early when achieved. Default is None.
171
- :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]
172
- :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.
173
- :param show_history: (bool, optional): If True, displays the training history after optimization. Default is False.
174
- :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'.
175
- :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.
176
- :param target_loss: (float, optional): The target loss to stop training early when achieved. Default is None.
177
- :param loss_impact: (float, optional): Impact of loss for optimization [0-1]. Default: 0.1
178
- :param acc_impact: (float, optional): Impact of accuracy for optimization [0-1]. Default: 0.9
179
- :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
180
- :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
181
- :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.
182
- :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 [].
183
- :param dtype: (cupy.dtype): Data type for the Weight matrices. np.float32 by default. Example: cp.float64 or cp.float16.
184
- :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'.
185
-
186
- Returns:
187
- 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.
188
- """
189
-
190
- from .ene import define_genomes
191
-
192
- data = 'Train'
193
-
194
- except_this = ['spiral', 'circular']
195
- activations = [item for item in all_activations() if item not in except_this]
196
- activations_len = len(activations)
197
-
198
- if pop_size > activations_len and fit_start is True:
199
- for _ in range(pop_size - len(activations)):
200
- random_index_all_act = random.randint(0, len(activations)-1)
201
- activations.append(activations[random_index_all_act])
202
-
203
- y_train = optimize_labels(y_train, cuda=True)
204
-
205
- if pop_size < activations_len: raise ValueError(f"pop_size must be higher or equal to {activations_len}")
206
-
207
- if memory == 'gpu':
208
- x_train = transfer_to_gpu(x_train, dtype=x_train.dtype)
209
- y_train = transfer_to_gpu(y_train, dtype=y_train.dtype)
210
-
211
- from .data_ops import batcher
212
-
213
- elif memory == 'cpu':
214
- x_train = transfer_to_cpu(x_train, dtype=x_train.dtype)
215
- y_train = transfer_to_cpu(y_train, dtype=y_train.dtype)
216
-
217
- from pyerualjetwork.cpu.data_ops import batcher
218
-
219
- else:
220
- raise ValueError("memory parameter must be 'cpu' or 'gpu'.")
221
-
222
- 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')
223
- 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')
224
-
225
- if neurons != []:
226
- weight_evolve = True
227
-
228
- if activation_functions == []: activation_functions = ['linear'] * len(neurons)
229
-
230
- if fit_start is False:
231
- # MLP
232
- activations = activation_functions
233
- model_type = 'MLP'
234
- activation_potentiations = [0] * pop_size
235
- activation_potentiation = None
236
- is_mlp = True
237
- transfer_learning = False
238
- else:
239
- # PTNN
240
- 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)
241
- transfer_learning = True
242
-
243
- neurons_copy = neurons.copy()
244
- neurons = []
245
- gen_copy = gen.copy()
246
- gen = gen[0] + gen[1]
247
- activation_potentiations = [0] * pop_size
248
- activation_potentiation = None
249
- is_mlp = False # it will change
250
-
251
- else:
252
- # PLAN
253
- model_type = 'PLAN'
254
- transfer_learning = False
255
-
256
- activation_potentiations = [0] * pop_size # NOTE: For PLAN models, activation_potentiations is needed BUT activations variable already mirros activation_potentiation values.
257
- # So, we don't need to use activation_potentiations variable. activation_potentiations variable is only for PTNN models.
258
- activation_potentiation = None
259
- is_mlp = False
260
-
261
- # Initialize visualization components
262
- viz_objects = initialize_visualization_for_learner(show_history, neurons_history, neural_web_history, x_train, y_train)
263
-
264
- # Initialize variables
265
- best_acc = 0
266
- best_loss = float('inf')
267
- best_fitness = float('-inf')
268
- best_acc_per_gen_list = []
269
- postfix_dict = {}
270
- loss_list = []
271
- target_pop = []
272
-
273
- progress = initialize_loading_bar(total=pop_size, desc="", ncols=79, bar_format=bar_format_learner)
274
-
275
- if fit_start is False:
276
- 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)
277
-
278
- else:
279
- weight_pop = [0] * len(activations)
280
- act_pop = [0] * len(activations)
281
-
282
- if start_this_act is not None and start_this_W is not None:
283
- weight_pop[0] = start_this_W
284
- act_pop[0] = start_this_act
285
-
286
- # LEARNING STARTED
287
- for i in range(gen):
288
-
289
- # TRANSFORMATION PLAN TO MLP FOR PTNN (in later generations)
290
- if model_type == 'PLAN' and transfer_learning:
291
- if i == gen_copy[0]:
292
-
293
- model_type = 'PTNN'
294
- neurons = neurons_copy
295
-
296
- for individual in range(len(weight_pop)):
297
- weight_pop[individual] = cp.copy(best_weight)
298
- activation_potentiations[individual] = final_activations.copy() if isinstance(final_activations, list) else final_activations
299
-
300
- activation_potentiation = activation_potentiations[0]
301
-
302
- neurons_copy = [len(y_train[0])] + neurons_copy
303
- activation_functions = ['linear'] + activation_functions
304
-
305
- 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)
306
-
307
- # 0 indexed individual will keep PLAN's learned informations and in later generations it will share other individuals.
308
- for l in range(1, len(weight_pop[0])):
309
- original_shape = weight_pop[0][l].shape
310
-
311
- identity_matrix = cp.eye(original_shape[0], original_shape[1], dtype=weight_pop[0][l].dtype)
312
- weight_pop[0][l] = identity_matrix
313
-
314
- for l in range(len(weight_pop)):
315
- weight_pop[l][0] = cp.copy(best_weight)
316
-
317
- best_weight = list(weight_pop[0])
318
- final_activations = act_pop[0]
319
- is_mlp = True
320
- fit_start = False
321
-
322
-
323
- postfix_dict["Gen"] = str(i+1) + '/' + str(gen)
324
- progress.set_postfix(postfix_dict)
325
-
326
- progress.n = 0
327
- progress.last_print_n = 0
328
- progress.update(0)
329
-
330
- x_train_batch, y_train_batch = batcher(x_train, y_train, batch_size=batch_size)
331
-
332
- for j in range(pop_size):
333
-
334
- x_train_batch = cp.array(x_train_batch, dtype=x_train_batch.dtype, copy=False)
335
- y_train_batch = cp.array(y_train_batch, dtype=y_train.dtype)
336
-
337
- if fit_start is True and i == 0:
338
- if start_this_act is not None and j == 0:
339
- pass
340
- else:
341
-
342
- act_pop[j] = activations[j]
343
- W = plan_fit(x_train_batch, y_train_batch, activations=act_pop[j], auto_normalization=auto_normalization, dtype=dtype)
344
- weight_pop[j] = W
345
-
346
- if weight_evolve is False:
347
- weight_pop[j] = plan_fit(x_train_batch, y_train_batch, activations=act_pop[j], auto_normalization=auto_normalization, dtype=dtype)
348
-
349
- 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)
350
- acc = model[get_acc()]
351
-
352
- if loss == 'categorical_crossentropy':
353
- train_loss = categorical_crossentropy(y_true_batch=y_train_batch,
354
- y_pred_batch=model[get_preds_softmax()])
355
- else:
356
- train_loss = binary_crossentropy(y_true_batch=y_train_batch,
357
- y_pred_batch=model[get_preds_softmax()])
358
-
359
-
360
- fitness = wals(acc, train_loss, acc_impact, loss_impact)
361
- target_pop.append(fitness.get())
362
-
363
- if fitness >= best_fitness:
364
-
365
- best_fitness = fitness
366
- best_acc = acc
367
- best_loss = train_loss
368
- best_weight = cp.copy(weight_pop[j]) if model_type == 'PLAN' else copy.deepcopy(weight_pop[j])
369
- best_model = model
370
-
371
- if isinstance(act_pop[j], list) and model_type == 'PLAN':
372
- final_activations = act_pop[j].copy()
373
- elif isinstance(act_pop[j], str):
374
- final_activations = act_pop[j]
375
- else:
376
- final_activations = copy.deepcopy(act_pop[j])
377
-
378
- if model_type == 'PLAN': final_activations = [final_activations[0]] if len(set(final_activations)) == 1 else final_activations # removing if all same
379
-
380
- if batch_size == 1:
381
- postfix_dict[f"{data} Accuracy"] = cp.round(best_acc, 4)
382
- postfix_dict[f"{data} Loss"] = cp.round(train_loss, 4)
383
- progress.set_postfix(postfix_dict)
384
-
385
- if show_current_activations:
386
- print(f", Current Activations={final_activations}", end='')
387
-
388
- # Update visualizations during training
389
- if show_history:
390
- gen_list = range(1, len(best_acc_per_gen_list) + 2)
391
- update_history_plots_for_learner(viz_objects, gen_list, loss_list + [train_loss],
392
- best_acc_per_gen_list + [best_acc], x_train, final_activations)
393
-
394
- if neurons_history:
395
- viz_objects['neurons']['artists'] = (
396
- update_neuron_history_for_learner(cp.copy(best_weight), viz_objects['neurons']['ax'],
397
- viz_objects['neurons']['row'], viz_objects['neurons']['col'],
398
- y_train[0], viz_objects['neurons']['artists'],
399
- data=data, fig1=viz_objects['neurons']['fig'],
400
- acc=best_acc, loss=train_loss)
401
- )
402
-
403
- if neural_web_history:
404
- art5_1, art5_2, art5_3 = draw_neural_web(W=best_weight, ax=viz_objects['web']['ax'],
405
- G=viz_objects['web']['G'], return_objs=True)
406
- art5_list = [art5_1] + [art5_2] + list(art5_3.values())
407
- viz_objects['web']['artists'].append(art5_list)
408
-
409
- # Check target accuracy
410
- if target_acc is not None and best_acc >= target_acc:
411
- progress.close()
412
- train_model = evaluate(x_train, y_train, W=best_weight,
413
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
414
- if loss == 'categorical_crossentropy':
415
- train_loss = categorical_crossentropy(y_true_batch=y_train,
416
- y_pred_batch=train_model[get_preds_softmax()])
417
- else:
418
- train_loss = binary_crossentropy(y_true_batch=y_train,
419
- y_pred_batch=train_model[get_preds_softmax()])
420
- print('\nActivations: ', final_activations)
421
- print('Activation Potentiation: ', activation_potentiation)
422
- print('Train Accuracy:', train_model[get_acc()])
423
- print('Train Loss: ', train_loss, '\n')
424
- print('Model Type:', model_type)
425
-
426
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
427
- best_loss, y_train, interval)
428
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, model_type, None, None, None, None, activation_potentiation
429
-
430
- # Check target loss
431
- if target_loss is not None and best_loss <= target_loss:
432
- progress.close()
433
- train_model = evaluate(x_train, y_train, W=best_weight,
434
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
435
-
436
- if loss == 'categorical_crossentropy':
437
- train_loss = categorical_crossentropy(y_true_batch=y_train,
438
- y_pred_batch=train_model[get_preds_softmax()])
439
- else:
440
- train_loss = binary_crossentropy(y_true_batch=y_train,
441
- y_pred_batch=train_model[get_preds_softmax()])
442
-
443
- print('\nActivations: ', final_activations)
444
- print('Activation Potentiation: ', activation_potentiation)
445
- print('Train Accuracy:', train_model[get_acc()])
446
- print('Train Loss: ', train_loss, '\n')
447
- print('Model Type:', model_type)
448
-
449
- # Display final visualizations
450
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
451
- train_loss, y_train, interval)
452
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, model_type, None, None, None, None, activation_potentiation, None, None, None, None, None, None, None, activation_potentiation
453
-
454
-
455
- progress.update(1)
456
-
457
- if batch_size != 1:
458
- train_model = evaluate(x_train, y_train, W=best_weight,
459
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
460
-
461
- if loss == 'categorical_crossentropy':
462
- train_loss = categorical_crossentropy(y_true_batch=y_train,
463
- y_pred_batch=train_model[get_preds_softmax()])
464
- else:
465
- train_loss = binary_crossentropy(y_true_batch=y_train,
466
- y_pred_batch=train_model[get_preds_softmax()])
467
-
468
- postfix_dict[f"{data} Accuracy"] = cp.round(train_model[get_acc()], 4)
469
- postfix_dict[f"{data} Loss"] = cp.round(train_loss, 4)
470
- progress.set_postfix(postfix_dict)
471
-
472
- best_acc_per_gen_list.append(train_model[get_acc()])
473
- loss_list.append(train_loss)
474
-
475
- else:
476
- best_acc_per_gen_list.append(best_acc)
477
- loss_list.append(best_loss)
478
-
479
- if model_type == 'PLAN': weight_pop = cp.array(weight_pop, copy=False, dtype=dtype)
480
-
481
- 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)
482
- target_pop = []
483
-
484
- # Early stopping check
485
- if early_stop == True and i > 0:
486
- if best_acc_per_gen_list[i] == best_acc_per_gen_list[i-1]:
487
- progress.close()
488
- train_model = evaluate(x_train, y_train, W=best_weight,
489
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
490
-
491
- if loss == 'categorical_crossentropy':
492
- train_loss = categorical_crossentropy(y_true_batch=y_train,
493
- y_pred_batch=train_model[get_preds_softmax()])
494
- else:
495
- train_loss = binary_crossentropy(y_true_batch=y_train,
496
- y_pred_batch=train_model[get_preds_softmax()])
497
-
498
- print('\nActivations: ', final_activations)
499
- print('Activation Potentiation: ', activation_potentiation)
500
- print('Train Accuracy:', train_model[get_acc()])
501
- print('Train Loss: ', train_loss, '\n')
502
- print('Model Type:', model_type)
503
-
504
- # Display final visualizations
505
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
506
- train_loss, y_train, interval)
507
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, model_type, None, None, None, None, activation_potentiation
508
-
509
- # Final evaluation
510
- progress.close()
511
- train_model = evaluate(x_train, y_train, W=best_weight,
512
- activations=final_activations, activation_potentiations=activation_potentiation, auto_normalization=auto_normalization, model_type=model_type)
513
-
514
- if loss == 'categorical_crossentropy':
515
- train_loss = categorical_crossentropy(y_true_batch=y_train,
516
- y_pred_batch=train_model[get_preds_softmax()])
517
- else:
518
- train_loss = binary_crossentropy(y_true_batch=y_train,
519
- y_pred_batch=train_model[get_preds_softmax()])
520
-
521
- print('\nActivations: ', final_activations)
522
- print('Activation Potentiation: ', activation_potentiation)
523
- print('Train Accuracy:', train_model[get_acc()])
524
- print('Train Loss: ', train_loss, '\n')
525
- print('Model Type:', model_type)
526
-
527
- # Display final visualizations
528
- display_visualizations_for_learner(viz_objects, best_weight, data, best_acc, train_loss, y_train, interval)
529
- return best_weight, best_model[get_preds_softmax()], best_acc, final_activations, None, None, model_type, None, None, None, None, activation_potentiation
530
-
531
- def evaluate(
532
- x_test,
533
- y_test,
534
- model_type,
535
- W,
536
- activations=['linear'],
537
- activation_potentiations=[],
538
- auto_normalization=False
539
- ) -> tuple:
540
- """
541
- Evaluates the neural network model using the given test data.
542
-
543
- Args:
544
- x_test (cp.ndarray): Test data.
545
-
546
- y_test (cp.ndarray): Test labels (one-hot encoded).
547
-
548
- model_type: (str): Type of the model. Options: 'PLAN', 'MLP', 'PTNN'.
549
-
550
- W (cp.ndarray): Neural net weight matrix.
551
-
552
- activations (list, optional): Activation list for PLAN or MLP models (MLP layers activations if it PTNN model). Default = ['linear'].
553
-
554
- activation_potentiations (list, optional): Extra activation potentiation list (PLAN layers activations) for PTNN models. Default = [].
555
-
556
- auto_normalization (bool, optional): Normalization for x_test ? Default = False.
557
-
558
- Returns:
559
- tuple: Model (list).
560
- """
561
-
562
- if auto_normalization: x_test = normalization(x_test, dtype=x_test.dtype)
563
-
564
- if isinstance(activations, str):
565
- activations = [activations]
566
- elif isinstance(activations, list):
567
- activations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activations]
568
-
569
- if model_type == 'MLP':
570
- layer = x_test
571
- for i in range(len(W)):
572
- if i != len(W) - 1 and i != 0: layer = apply_activation(layer, activations[i])
573
-
574
- layer = layer @ W[i].T
575
-
576
- result = layer
577
-
578
- if model_type == 'PLAN':
579
-
580
- x_test = apply_activation(x_test, activations)
581
- result = x_test @ W.T
582
-
583
- if model_type == 'PTNN':
584
-
585
- if isinstance(activation_potentiations, str):
586
- activation_potentiations = [activation_potentiations]
587
- elif isinstance(activation_potentiations, list):
588
- activation_potentiations = [item if isinstance(item, list) or isinstance(item, str) else [item] for item in activation_potentiations]
589
-
590
- x_test = apply_activation(x_test, activation_potentiations)
591
- layer = x_test @ W[0].T
592
-
593
- for i in range(1, len(W)):
594
- if i != len(W) - 1: layer = apply_activation(layer, activations[i])
595
-
596
- layer = layer @ W[i].T
597
-
598
- result = layer
599
-
600
- max_vals = cp.max(result, axis=1, keepdims=True)
601
-
602
- softmax_preds = cp.exp(result - max_vals) / cp.sum(cp.exp(result - max_vals), axis=1, keepdims=True)
603
- accuracy = (cp.argmax(softmax_preds, axis=1) == cp.argmax(y_test, axis=1)).mean()
604
-
605
- return W, result, accuracy, None, None, softmax_preds
@@ -1,28 +0,0 @@
1
- pyerualjetwork/__init__.py,sha256=mq2efXuCnoIrT6GPNSgA0XIN3F-3qITy3fxvKiVYZyg,2704
2
- pyerualjetwork/fitness_functions.py,sha256=D9JVCr9DFid_xXgBD4uCKxdW2k10MVDE5HZRSOK4Igg,1237
3
- pyerualjetwork/help.py,sha256=Nyi0gHAN9ZnO4wgQLeENt0n7tSCZ3hJmjaJ853eGjCE,831
4
- pyerualjetwork/issue_solver.py,sha256=3pZTGotS29sy3pIuGQoJFUePibtSzS-tNoU80T_Usgk,3131
5
- pyerualjetwork/memory_ops.py,sha256=TUFh9SYWCKL6N-vNdWId_EwU313TuZomQCHOrltrD-4,14280
6
- pyerualjetwork/ui.py,sha256=JBTFYz5R24XwNKhA3GSW-oYAoiIBxAE3kFGXkvm5gqw,656
7
- pyerualjetwork/cpu/__init__.py,sha256=0yAYner_-v7SmT3P7JV2itU8xJUQdQpb40dhAMQiZkc,829
8
- pyerualjetwork/cpu/activation_functions.py,sha256=zZSoOQ452Ykp_RsHVxklxesJmmFgufyIB4F3WQjudEQ,6689
9
- pyerualjetwork/cpu/data_ops.py,sha256=SPsIcjU0JPHfsnEmGjD8q-yTlpgYk-KPOPJ44dfp-nU,16143
10
- pyerualjetwork/cpu/ene.py,sha256=7ZPR7NDhuXCFSucH0l-_vUTDILnQOH-Zxv83Yy5gLL8,44451
11
- pyerualjetwork/cpu/loss_functions.py,sha256=6PyBI232SQRGuFnG3LDGvnv_PUdWzT2_2mUODJiejGI,618
12
- pyerualjetwork/cpu/metrics.py,sha256=WhZ8iEqWehaygPRADUlhA5j_Qv3UwqV_eMxpyRVkeVs,6070
13
- pyerualjetwork/cpu/model_ops.py,sha256=1KNgjUeYCO_TsA5RtbNiuIiBJzq8-rL2dE6jxKqCBU0,21481
14
- pyerualjetwork/cpu/nn.py,sha256=AiL1q-pWr-tzTtQlzoKRnqHQVEvgrUPLUl6_RY02T5s,32032
15
- pyerualjetwork/cpu/visualizations.py,sha256=rOQsc-W8b71z7ovXSoF49lx4fmpvlaHLsyj9ejWnhnI,28164
16
- pyerualjetwork/cuda/__init__.py,sha256=NbqvAS4jlMdoFdXa5_hi5ukXQ5zAZR_5BQ4QAqtiKug,879
17
- pyerualjetwork/cuda/activation_functions.py,sha256=FmoSAxDr9SGO4nkE6ZflXK4pmvZ0sL3Epe1Lz-3GOVI,6766
18
- pyerualjetwork/cuda/data_ops.py,sha256=SiNodFNmWyTPY_KnKuAi9biPRdpTAYY3XM01bRSUPCs,18510
19
- pyerualjetwork/cuda/ene.py,sha256=dwH5l7CQqj4kUbcj8vC9gIgEdjFfRN-jw-06ABN-TiU,44976
20
- pyerualjetwork/cuda/loss_functions.py,sha256=C93IZJcrOpT6HMK9x1O4AHJWXYTkN5WZiqdssPbvAPk,617
21
- pyerualjetwork/cuda/metrics.py,sha256=PjDBoRvr6va8vRvDIJJGBO4-I4uumrk3NCM1Vz4NJTo,5054
22
- pyerualjetwork/cuda/model_ops.py,sha256=KJXZ_sxt7JfgEh6jLObhbtmn7zcMn708xeEZthzcNrI,23080
23
- pyerualjetwork/cuda/nn.py,sha256=EDdiWNUdrEHZXQ9K7qM74Q7OpacAgWM5MsQC8YANlY4,33164
24
- pyerualjetwork/cuda/visualizations.py,sha256=9l5BhXqXoeopdhLvVGvjH1TKYZb9JdKOsSE2IYD02zs,28569
25
- pyerualjetwork-5.37.dist-info/METADATA,sha256=DxPEG53B8FA8rX7xYx_WlxA6_R1nKs2XLlgIBjUtRRc,8020
26
- pyerualjetwork-5.37.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
27
- pyerualjetwork-5.37.dist-info/top_level.txt,sha256=BRyt62U_r3ZmJpj-wXNOoA345Bzamrj6RbaWsyW4tRg,15
28
- pyerualjetwork-5.37.dist-info/RECORD,,