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