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.
- pyerualjetwork/__init__.py +1 -1
- pyerualjetwork/activation_functions.py +2 -2
- pyerualjetwork/activation_functions_cuda.py +63 -114
- pyerualjetwork/data_operations_cuda.py +1 -1
- pyerualjetwork/model_operations.py +14 -14
- pyerualjetwork/model_operations_cuda.py +16 -17
- pyerualjetwork/parallel.py +118 -0
- pyerualjetwork/plan.py +61 -256
- pyerualjetwork/plan_cuda.py +60 -267
- pyerualjetwork/planeat.py +12 -44
- pyerualjetwork/planeat_cuda.py +9 -45
- pyerualjetwork/visualizations.py +29 -26
- pyerualjetwork/visualizations_cuda.py +20 -22
- {pyerualjetwork-4.3.7.dev1.dist-info → pyerualjetwork-4.3.8.dist-info}/METADATA +2 -19
- pyerualjetwork-4.3.8.dist-info/RECORD +25 -0
- pyerualjetwork-4.3.8.dist-info/top_level.txt +1 -0
- pyerualjetwork-4.3.7.dev1.dist-info/RECORD +0 -44
- pyerualjetwork-4.3.7.dev1.dist-info/top_level.txt +0 -2
- pyerualjetwork_afterburner/__init__.py +0 -11
- pyerualjetwork_afterburner/activation_functions.py +0 -290
- pyerualjetwork_afterburner/activation_functions_cuda.py +0 -289
- pyerualjetwork_afterburner/data_operations.py +0 -406
- pyerualjetwork_afterburner/data_operations_cuda.py +0 -461
- pyerualjetwork_afterburner/help.py +0 -17
- pyerualjetwork_afterburner/loss_functions.py +0 -21
- pyerualjetwork_afterburner/loss_functions_cuda.py +0 -21
- pyerualjetwork_afterburner/memory_operations.py +0 -298
- pyerualjetwork_afterburner/metrics.py +0 -190
- pyerualjetwork_afterburner/metrics_cuda.py +0 -163
- pyerualjetwork_afterburner/model_operations.py +0 -408
- pyerualjetwork_afterburner/model_operations_cuda.py +0 -420
- pyerualjetwork_afterburner/plan.py +0 -425
- pyerualjetwork_afterburner/plan_cuda.py +0 -436
- pyerualjetwork_afterburner/planeat.py +0 -793
- pyerualjetwork_afterburner/planeat_cuda.py +0 -797
- pyerualjetwork_afterburner/ui.py +0 -22
- pyerualjetwork_afterburner/visualizations.py +0 -823
- pyerualjetwork_afterburner/visualizations_cuda.py +0 -825
- {pyerualjetwork-4.3.7.dev1.dist-info → pyerualjetwork-4.3.8.dist-info}/WHEEL +0 -0
pyerualjetwork/plan_cuda.py
CHANGED
@@ -16,31 +16,21 @@ 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
|
22
|
+
from .data_operations_cuda import normalization
|
24
23
|
from .loss_functions_cuda import binary_crossentropy, categorical_crossentropy
|
25
|
-
from .activation_functions_cuda import apply_activation,
|
24
|
+
from .activation_functions_cuda import apply_activation, all_activations
|
26
25
|
from .metrics_cuda import metrics
|
27
26
|
from .model_operations_cuda import get_acc, get_preds, get_preds_softmax
|
28
27
|
from .memory_operations import transfer_to_gpu, transfer_to_cpu, optimize_labels
|
29
28
|
from .visualizations_cuda import (
|
30
29
|
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
30
|
display_visualizations_for_learner,
|
40
31
|
update_history_plots_for_learner,
|
41
32
|
initialize_visualization_for_learner,
|
42
|
-
update_neuron_history_for_learner
|
43
|
-
show
|
33
|
+
update_neuron_history_for_learner
|
44
34
|
)
|
45
35
|
|
46
36
|
### GLOBAL VARIABLES ###
|
@@ -48,147 +38,46 @@ bar_format_normal = loading_bars()[0]
|
|
48
38
|
bar_format_learner = loading_bars()[1]
|
49
39
|
|
50
40
|
# BUILD -----
|
41
|
+
|
51
42
|
def fit(
|
52
43
|
x_train,
|
53
44
|
y_train,
|
54
|
-
val=False,
|
55
|
-
val_count=None,
|
56
45
|
activation_potentiation=['linear'],
|
57
|
-
|
58
|
-
|
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'
|
46
|
+
W=None,
|
47
|
+
dtype=cp.float32
|
68
48
|
):
|
69
49
|
"""
|
70
|
-
Creates a model to fitting data
|
50
|
+
Creates a model to fitting data.,
|
71
51
|
|
72
52
|
fit Args:
|
73
53
|
|
74
|
-
x_train (
|
75
|
-
|
76
|
-
y_train (list[num]): List or numarray of target labels. (one hot encoded)
|
77
|
-
|
78
|
-
val (None or True): validation in training process ? None or True default: None (optional)
|
54
|
+
x_train (aray-like[cupy]): List or cupy array of input data.
|
79
55
|
|
80
|
-
|
56
|
+
y_train (aray-like[cupy]): List or cupy array of target labels. (one hot encoded)
|
81
57
|
|
82
58
|
activation_potentiation (list): For deeper PLAN networks, activation function parameters. For more information please run this code: plan.activations_list() default: [None] (optional)
|
83
59
|
|
84
|
-
|
85
|
-
|
86
|
-
|
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)
|
103
|
-
|
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'.
|
60
|
+
W (cupy.ndarray): If you want to re-continue or update model
|
61
|
+
|
62
|
+
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)
|
105
63
|
|
106
64
|
Returns:
|
107
|
-
|
65
|
+
cupyarray: (Weight matrix).
|
108
66
|
"""
|
109
|
-
# Pre-
|
67
|
+
# Pre-check
|
110
68
|
|
111
|
-
if
|
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)
|
115
|
-
|
116
|
-
if len(x_train) != len(y_train):
|
117
|
-
raise ValueError("x_train and y_train must have the same length.")
|
118
|
-
|
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)
|
69
|
+
if len(x_train) != len(y_train): raise ValueError("x_train and y_train must have the same length.")
|
125
70
|
|
126
|
-
|
127
|
-
x_val = transfer_to_gpu(x_val, dtype=dtype)
|
128
|
-
y_val = transfer_to_gpu(y_val, dtype=y_train.dtype)
|
71
|
+
LTPW = cp.zeros((len(y_train[0]), len(x_train[0].ravel()))).astype(dtype, copy=False) if W is None else W
|
129
72
|
|
130
|
-
|
131
|
-
|
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()
|
182
|
-
|
183
|
-
if neurons_history:
|
184
|
-
ani5 = display_visualization_for_fit(vis_objects['fig1'], vis_objects['artist5'], interval)
|
185
|
-
show()
|
73
|
+
x_train = apply_activation(x_train, activation_potentiation)
|
74
|
+
LTPW += y_train.T @ x_train
|
186
75
|
|
187
76
|
return normalization(LTPW, dtype=dtype)
|
188
77
|
|
189
78
|
|
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,
|
79
|
+
def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=None, batch_size=1, pop_size=None,
|
80
|
+
neural_web_history=False, show_current_activations=False,
|
192
81
|
neurons_history=False, early_stop=False, loss='categorical_crossentropy', show_history=False,
|
193
82
|
interval=33.33, target_acc=None, target_loss=None,
|
194
83
|
start_this_act=None, start_this_W=None, dtype=cp.float32, memory='gpu'):
|
@@ -231,9 +120,9 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
231
120
|
|
232
121
|
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
122
|
|
234
|
-
|
123
|
+
pop_size (int, optional): Population size of each generation. Default: count of activation functions
|
235
124
|
|
236
|
-
|
125
|
+
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
126
|
|
238
127
|
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
128
|
|
@@ -242,7 +131,7 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
242
131
|
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
132
|
|
244
133
|
interval (int, optional): The interval at which evaluations are conducted during training. (33.33 = 30 FPS, 16.67 = 60 FPS) Default is 100.
|
245
|
-
|
134
|
+
|
246
135
|
target_acc (int, optional): The target accuracy to stop training early when achieved. Default is None.
|
247
136
|
|
248
137
|
target_loss (float, optional): The target loss to stop training early when achieved. Default is None.
|
@@ -268,11 +157,15 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
268
157
|
|
269
158
|
data = 'Train'
|
270
159
|
|
271
|
-
|
160
|
+
except_this = ['spiral', 'circular']
|
161
|
+
activation_potentiation = [item for item in all_activations() if item not in except_this]
|
272
162
|
activation_potentiation_len = len(activation_potentiation)
|
273
163
|
|
164
|
+
if pop_size is None: pop_size = activation_potentiation_len
|
274
165
|
y_train = optimize_labels(y_train, cuda=True)
|
275
166
|
|
167
|
+
if pop_size < activation_potentiation_len: raise ValueError(f"pop_size must be higher or equal to {activation_potentiation_len}")
|
168
|
+
|
276
169
|
if memory == 'gpu':
|
277
170
|
x_train = transfer_to_gpu(x_train, dtype=dtype)
|
278
171
|
y_train = transfer_to_gpu(y_train, dtype=y_train.dtype)
|
@@ -313,16 +206,16 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
313
206
|
|
314
207
|
progress = initialize_loading_bar(total=activation_potentiation_len, desc="", ncols=ncols, bar_format=bar_format_learner)
|
315
208
|
|
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=
|
209
|
+
if fit_start is False or pop_size > activation_potentiation_len:
|
210
|
+
weight_pop, act_pop = define_genomes(input_shape=len(x_train[0]), output_shape=len(y_train[0]), population_size=pop_size, dtype=dtype)
|
318
211
|
|
319
212
|
if start_this_act is not None and start_this_W is not None:
|
320
213
|
weight_pop[0] = start_this_W
|
321
214
|
act_pop[0] = start_this_act
|
322
215
|
|
323
216
|
else:
|
324
|
-
weight_pop = []
|
325
|
-
act_pop = []
|
217
|
+
weight_pop = [0] * pop_size
|
218
|
+
act_pop = [0] * pop_size
|
326
219
|
|
327
220
|
for i in range(gen):
|
328
221
|
postfix_dict["Gen"] = str(i+1) + '/' + str(gen)
|
@@ -332,15 +225,19 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
332
225
|
progress.last_print_n = 0
|
333
226
|
progress.update(0)
|
334
227
|
|
335
|
-
for j in range(
|
228
|
+
for j in range(pop_size):
|
336
229
|
|
337
230
|
x_train_batch, y_train_batch = batcher(x_train, y_train, batch_size=batch_size)
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
231
|
+
|
232
|
+
x_train_batch = cp.array(x_train_batch, dtype=dtype, copy=False)
|
233
|
+
y_train_batch = cp.array(y_train_batch, dtype=dtype, copy=False)
|
234
|
+
|
235
|
+
if fit_start is True and i == 0 and j < activation_potentiation_len:
|
236
|
+
act_pop[j] = activation_potentiation[j]
|
237
|
+
W = fit(x_train_batch, y_train_batch, activation_potentiation=act_pop[-1], dtype=dtype)
|
238
|
+
weight_pop[j] = W
|
239
|
+
|
240
|
+
model = evaluate(x_train_batch, y_train_batch, W=weight_pop[j], activation_potentiation=act_pop[j])
|
344
241
|
acc = model[get_acc()]
|
345
242
|
|
346
243
|
if strategy == 'accuracy': target_pop.append(acc)
|
@@ -374,6 +271,7 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
374
271
|
best_acc = acc
|
375
272
|
best_weights = cp.copy(weight_pop[j])
|
376
273
|
final_activations = act_pop[j].copy() if isinstance(act_pop[j], list) else act_pop[j]
|
274
|
+
|
377
275
|
best_model = model
|
378
276
|
|
379
277
|
final_activations = [final_activations[0]] if len(set(final_activations)) == 1 else final_activations # removing if all same
|
@@ -425,7 +323,7 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
425
323
|
if target_acc is not None and best_acc >= target_acc:
|
426
324
|
progress.close()
|
427
325
|
train_model = evaluate(x_train, y_train, W=best_weights, loading_bar_status=False,
|
428
|
-
activation_potentiation=final_activations
|
326
|
+
activation_potentiation=final_activations)
|
429
327
|
|
430
328
|
if loss == 'categorical_crossentropy':
|
431
329
|
train_loss = categorical_crossentropy(y_true_batch=y_train,
|
@@ -446,8 +344,8 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
446
344
|
# Check target loss
|
447
345
|
if target_loss is not None and best_loss <= target_loss:
|
448
346
|
progress.close()
|
449
|
-
train_model = evaluate(x_train, y_train, W=best_weights,
|
450
|
-
activation_potentiation=final_activations
|
347
|
+
train_model = evaluate(x_train, y_train, W=best_weights,
|
348
|
+
activation_potentiation=final_activations)
|
451
349
|
|
452
350
|
if loss == 'categorical_crossentropy':
|
453
351
|
train_loss = categorical_crossentropy(y_true_batch=y_train,
|
@@ -457,7 +355,7 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
457
355
|
y_pred_batch=train_model[get_preds_softmax()])
|
458
356
|
|
459
357
|
print('\nActivations: ', final_activations)
|
460
|
-
print(f'Train Accuracy:', train_model[get_acc()])
|
358
|
+
print(f'Train Accuracy: ', train_model[get_acc()])
|
461
359
|
print(f'Train Loss: ', train_loss, '\n')
|
462
360
|
|
463
361
|
# Display final visualizations
|
@@ -477,8 +375,8 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
477
375
|
if early_stop == True and i > 0:
|
478
376
|
if best_acc_per_gen_list[i] == best_acc_per_gen_list[i-1]:
|
479
377
|
progress.close()
|
480
|
-
train_model = evaluate(x_train, y_train, W=best_weights,
|
481
|
-
activation_potentiation=final_activations
|
378
|
+
train_model = evaluate(x_train, y_train, W=best_weights,
|
379
|
+
activation_potentiation=final_activations)
|
482
380
|
|
483
381
|
if loss == 'categorical_crossentropy':
|
484
382
|
train_loss = categorical_crossentropy(y_true_batch=y_train,
|
@@ -498,8 +396,8 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
498
396
|
|
499
397
|
# Final evaluation
|
500
398
|
progress.close()
|
501
|
-
train_model = evaluate(x_train, y_train, W=best_weights,
|
502
|
-
activation_potentiation=final_activations
|
399
|
+
train_model = evaluate(x_train, y_train, W=best_weights,
|
400
|
+
activation_potentiation=final_activations)
|
503
401
|
|
504
402
|
if loss == 'categorical_crossentropy':
|
505
403
|
train_loss = categorical_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
|
@@ -507,69 +405,18 @@ def learner(x_train, y_train, optimizer, fit_start, strategy='accuracy', gen=Non
|
|
507
405
|
train_loss = binary_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
|
508
406
|
|
509
407
|
print('\nActivations: ', final_activations)
|
510
|
-
print(f'Train Accuracy:', train_model[get_acc()])
|
511
|
-
print(f'Train Loss: ', train_loss, '\n')
|
408
|
+
print(f'Train Accuracy: ', train_model[get_acc()])
|
409
|
+
print(f'Train Loss : ', train_loss, '\n')
|
512
410
|
|
513
411
|
# Display final visualizations
|
514
412
|
display_visualizations_for_learner(viz_objects, best_weights, data, best_acc, train_loss, y_train, interval)
|
515
413
|
return best_weights, best_model[get_preds()], best_acc, final_activations
|
516
414
|
|
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
|
563
|
-
|
564
415
|
def evaluate(
|
565
416
|
x_test,
|
566
417
|
y_test,
|
567
418
|
W,
|
568
|
-
activation_potentiation=['linear']
|
569
|
-
loading_bar_status=True,
|
570
|
-
show_metrics=False,
|
571
|
-
dtype=cp.float32,
|
572
|
-
memory='gpu'
|
419
|
+
activation_potentiation=['linear']
|
573
420
|
) -> tuple:
|
574
421
|
"""
|
575
422
|
Evaluates the neural network model using the given test data.
|
@@ -579,70 +426,16 @@ def evaluate(
|
|
579
426
|
|
580
427
|
y_test (cp.ndarray): Test labels (one-hot encoded).
|
581
428
|
|
582
|
-
W (
|
429
|
+
W (cp.ndarray): Neural net weight matrix.
|
583
430
|
|
584
431
|
activation_potentiation (list): Activation list. Default = ['linear'].
|
585
432
|
|
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
433
|
Returns:
|
595
434
|
tuple: Model (list).
|
596
435
|
"""
|
597
436
|
|
598
|
-
|
599
|
-
|
600
|
-
|
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)
|
437
|
+
x_test = apply_activation(x_test, activation_potentiation)
|
438
|
+
result = x_test @ W.T
|
439
|
+
softmax_preds = cp.exp(result) / cp.sum(cp.exp(result), axis=1, keepdims=True); accuracy = (cp.argmax(result, axis=1) == cp.argmax(y_test, axis=1)).mean()
|
647
440
|
|
648
|
-
return W,
|
441
|
+
return W, None, accuracy, None, None, softmax_preds
|
pyerualjetwork/planeat.py
CHANGED
@@ -17,10 +17,9 @@ import random
|
|
17
17
|
import math
|
18
18
|
|
19
19
|
### LIBRARY IMPORTS ###
|
20
|
-
from .plan import feed_forward
|
21
20
|
from .data_operations import normalization
|
22
21
|
from .ui import loading_bars, initialize_loading_bar
|
23
|
-
from.
|
22
|
+
from .activation_functions import apply_activation, all_activations
|
24
23
|
|
25
24
|
def define_genomes(input_shape, output_shape, population_size, dtype=np.float32):
|
26
25
|
"""
|
@@ -297,6 +296,7 @@ def evolver(weights,
|
|
297
296
|
mutated_W = np.copy(bad_weights)
|
298
297
|
mutated_act = bad_activations.copy()
|
299
298
|
|
299
|
+
|
300
300
|
for i in range(len(bad_weights)):
|
301
301
|
|
302
302
|
if policy == 'aggressive':
|
@@ -399,7 +399,7 @@ def evolver(weights,
|
|
399
399
|
return weights, activation_potentiations
|
400
400
|
|
401
401
|
|
402
|
-
def evaluate(x_population, weights, activation_potentiations
|
402
|
+
def evaluate(x_population, weights, activation_potentiations):
|
403
403
|
"""
|
404
404
|
Evaluates the performance of a population of genomes, applying different activation functions
|
405
405
|
and weights depending on whether reinforcement learning mode is enabled or not.
|
@@ -412,62 +412,30 @@ def evaluate(x_population, weights, activation_potentiations, rl_mode=False, dty
|
|
412
412
|
activation_potentiations (list or str): A list where each entry represents an activation function
|
413
413
|
or a potentiation strategy applied to each genome. If only one
|
414
414
|
activation function is used, this can be a single string.
|
415
|
-
rl_mode (bool, optional): If True, reinforcement learning mode is activated, this accepts x_population is a single genome. (Also weights and activation_potentations a single genomes part.)
|
416
|
-
Default is False.
|
417
|
-
|
418
|
-
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)
|
419
|
-
|
420
415
|
Returns:
|
421
416
|
list: A list of outputs corresponding to each genome in the population after applying the respective
|
422
417
|
activation function and weights.
|
423
418
|
|
424
|
-
Notes:
|
425
|
-
- If `rl_mode` is True:
|
426
|
-
- Accepts x_population is a single genom
|
427
|
-
- The inputs are flattened, and the activation function is applied across the single genom.
|
428
|
-
|
429
|
-
- If `rl_mode` is False:
|
430
|
-
- Accepts x_population is a list of genomes
|
431
|
-
- Each genome is processed individually, and the results are stored in the `outputs` list.
|
432
|
-
|
433
|
-
- `feed_forward()` function is the core function that processes the input with the given weights and activation function.
|
434
|
-
|
435
419
|
Example:
|
436
420
|
```python
|
437
|
-
outputs = evaluate(x_population, weights, activation_potentiations
|
421
|
+
outputs = evaluate(x_population, weights, activation_potentiations)
|
438
422
|
```
|
439
423
|
|
440
424
|
- The function returns a list of outputs after processing the population, where each element corresponds to
|
441
425
|
the output for each genome in `x_population`.
|
442
|
-
"""
|
443
|
-
|
444
|
-
### IF RL_MODE IS TRUE, A SINGLE GENOME IS ASSUMED AS INPUT, A FEEDFORWARD PREDICTION IS MADE, AND THE OUTPUT(NPARRAY) IS RETURNED:
|
445
|
-
|
446
|
-
### IF RL_MODE IS FALSE, PREDICTIONS ARE MADE FOR ALL GENOMES IN THE GROUP USING THEIR CORRESPONDING INDEXED INPUTS AND DATA.
|
426
|
+
"""
|
447
427
|
### THE OUTPUTS ARE RETURNED AS A PYTHON LIST, WHERE EACH GENOME'S OUTPUT MATCHES ITS INDEX:
|
448
428
|
|
449
|
-
if rl_mode == True:
|
450
|
-
Input = np.array(x_population, copy=False, dtype=dtype)
|
451
|
-
Input = Input.ravel()
|
452
|
-
|
453
|
-
if isinstance(activation_potentiations, str):
|
454
|
-
activation_potentiations = [activation_potentiations]
|
455
|
-
|
456
|
-
outputs = feed_forward(Input=Input, is_training=False, activation_potentiation=activation_potentiations, w=weights)
|
457
|
-
|
458
|
-
else:
|
459
|
-
outputs = [0] * len(x_population)
|
460
|
-
for i, genome in enumerate(x_population):
|
461
|
-
|
462
|
-
Input = np.array(genome, copy=False)
|
463
|
-
Input = Input.ravel()
|
464
429
|
|
465
|
-
|
466
|
-
|
430
|
+
if isinstance(activation_potentiations, str):
|
431
|
+
activation_potentiations = [activation_potentiations]
|
432
|
+
else:
|
433
|
+
activation_potentiations = [item if isinstance(item, list) else [item] for item in activation_potentiations]
|
467
434
|
|
468
|
-
|
435
|
+
x_population = apply_activation(x_population, activation_potentiations)
|
436
|
+
result = x_population @ weights.T
|
469
437
|
|
470
|
-
return
|
438
|
+
return result
|
471
439
|
|
472
440
|
|
473
441
|
def cross_over(first_parent_W,
|