pyerualjetwork 4.3.9b7__py3-none-any.whl → 4.3.9b8__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/fitness_functions.py +72 -0
- pyerualjetwork/fitness_functions_cuda.py +72 -0
- pyerualjetwork/plan.py +74 -122
- pyerualjetwork/plan_cuda.py +71 -118
- pyerualjetwork/visualizations.py +4 -4
- pyerualjetwork/visualizations_cuda.py +3 -4
- {pyerualjetwork-4.3.9b7.dist-info → pyerualjetwork-4.3.9b8.dist-info}/METADATA +2 -1
- {pyerualjetwork-4.3.9b7.dist-info → pyerualjetwork-4.3.9b8.dist-info}/RECORD +11 -11
- pyerualjetwork/loss_functions.py +0 -21
- pyerualjetwork/loss_functions_cuda.py +0 -21
- {pyerualjetwork-4.3.9b7.dist-info → pyerualjetwork-4.3.9b8.dist-info}/WHEEL +0 -0
- {pyerualjetwork-4.3.9b7.dist-info → pyerualjetwork-4.3.9b8.dist-info}/top_level.txt +0 -0
pyerualjetwork/__init__.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
__version__ = "4.3.
|
1
|
+
__version__ = "4.3.9b8"
|
2
2
|
__update__ = "* Changes: https://github.com/HCB06/PyerualJetwork/blob/main/CHANGES\n* PyerualJetwork Homepage: https://github.com/HCB06/PyerualJetwork/tree/main\n* PyerualJetwork document: https://github.com/HCB06/PyerualJetwork/blob/main/Welcome_to_PyerualJetwork/PYERUALJETWORK_USER_MANUEL_AND_LEGAL_INFORMATION(EN).pdf\n* YouTube tutorials: https://www.youtube.com/@HasanCanBeydili"
|
3
3
|
|
4
4
|
def print_version(__version__):
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import numpy as np
|
2
|
+
from scipy.spatial.distance import pdist
|
3
|
+
|
4
|
+
def diversity_score(population):
|
5
|
+
"""
|
6
|
+
Calculates the diversity score of a population based on the
|
7
|
+
Euclidean distances between individuals in the population.
|
8
|
+
|
9
|
+
:param population: That calculates the diversity score of a population based on the Euclidean distances between
|
10
|
+
individuals in the population
|
11
|
+
:return: The function returns the diversity score,
|
12
|
+
which is a measure of how spread out or diverse the population is in terms of their characteristics.
|
13
|
+
"""
|
14
|
+
if len(population) < 2:
|
15
|
+
return 0
|
16
|
+
|
17
|
+
population = np.nan_to_num(population, nan=0.0)
|
18
|
+
|
19
|
+
distances = pdist(population, metric='euclidean')
|
20
|
+
distances = np.maximum(distances, 1e-10)
|
21
|
+
|
22
|
+
avg_distance = np.mean(distances)
|
23
|
+
if population.shape[1] == 0:
|
24
|
+
return 0
|
25
|
+
|
26
|
+
max_possible_distance = np.sqrt(population.shape[1])
|
27
|
+
max_possible_distance = max(max_possible_distance, 1e-10)
|
28
|
+
|
29
|
+
diversity = avg_distance / max_possible_distance
|
30
|
+
return diversity
|
31
|
+
|
32
|
+
def multi_head_fitness(y_true, y_pred, diversity_score, accuracy, alpha=2, beta=1.5, lambda_div=0.05):
|
33
|
+
"""
|
34
|
+
The function calculates a fitness score based on accuracy, margin loss, and diversity score using
|
35
|
+
specified parameters.
|
36
|
+
|
37
|
+
@param y_true The `y_true` parameter represents the true labels of the data points. It is an array
|
38
|
+
or list containing the actual labels of the data points.
|
39
|
+
@param y_pred The `y_pred` parameter in the `multi_head_fitness` function represents the
|
40
|
+
predicted values for a given dataset. It is a NumPy array containing the predicted values for each
|
41
|
+
sample in the dataset.
|
42
|
+
@param diversity_score The `diversity_score` parameter in the `multi_head_fitness` function
|
43
|
+
represents a measure of diversity in the predictions. It is used as a factor in calculating the
|
44
|
+
fitness of the model. The function combines accuracy, margin loss, and diversity score to evaluate
|
45
|
+
the overall performance of the model
|
46
|
+
@param accuracy Accuracy is a measure of the correct predictions made by a model. It is typically
|
47
|
+
calculated as the number of correct predictions divided by the total number of predictions. In the
|
48
|
+
context of the `multi_head_fitness` function, the accuracy parameter represents the accuracy
|
49
|
+
of the model's predictions.
|
50
|
+
@param alpha Alpha is a parameter that controls the impact of accuracy on the overall fitness score
|
51
|
+
in the multi_head_fitness function. It is used as an exponent to raise the accuracy value
|
52
|
+
to, influencing its contribution to the fitness calculation.
|
53
|
+
@param beta The `beta` parameter in the `multi_head_fitness` function is used as an exponent
|
54
|
+
in the fitness calculation formula. It controls the impact of the margin loss term on the overall
|
55
|
+
fitness value. A higher value of `beta` will amplify the effect of the margin loss term, making it
|
56
|
+
@param lambda_div The `lambda_div` parameter in the `multi_head_fitness` function represents
|
57
|
+
the weight given to the diversity score in calculating the fitness value. It is a hyperparameter
|
58
|
+
that controls the impact of diversity score on the overall fitness calculation. A higher value of
|
59
|
+
`lambda_div` will increase the importance
|
60
|
+
@return The function `multi_head_fitness` returns the fitness value calculated based on the
|
61
|
+
input parameters `y_true`, `y_pred`, `diversity_score`, `accuracy`, and optional parameters `alpha`,
|
62
|
+
`beta`, and `lambda_div`. The fitness value is computed using a formula that combines accuracy,
|
63
|
+
margin loss, and diversity score with specified weights and coefficients.
|
64
|
+
"""
|
65
|
+
incorrect = y_true != y_pred
|
66
|
+
margin_loss = np.abs(y_true[incorrect] - y_pred[incorrect]).mean() if np.any(incorrect) else 0
|
67
|
+
|
68
|
+
margin_loss = np.nan_to_num(margin_loss, nan=0.0)
|
69
|
+
accuracy = max(accuracy, 1e-10)
|
70
|
+
|
71
|
+
fitness = (accuracy ** alpha) * ((1 - margin_loss) ** beta) * (1 + lambda_div * diversity_score)
|
72
|
+
return fitness
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import cupy as cp
|
2
|
+
from scipy.spatial.distance import pdist
|
3
|
+
|
4
|
+
def diversity_score(population):
|
5
|
+
"""
|
6
|
+
Calculates the diversity score of a population based on the
|
7
|
+
Euclidean distances between individuals in the population.
|
8
|
+
|
9
|
+
:param population: That calculates the diversity score of a population based on the Euclidean distances between
|
10
|
+
individuals in the population
|
11
|
+
:return: The function returns the diversity score,
|
12
|
+
which is a measure of how spread out or diverse the population is in terms of their characteristics.
|
13
|
+
"""
|
14
|
+
if len(population) < 2:
|
15
|
+
return 0
|
16
|
+
|
17
|
+
population = cp.nan_to_num(population, nan=0.0)
|
18
|
+
|
19
|
+
distances = pdist(population, metric='euclidean')
|
20
|
+
distances = cp.maximum(distances, 1e-10)
|
21
|
+
|
22
|
+
avg_distance = cp.mean(distances)
|
23
|
+
if population.shape[1] == 0:
|
24
|
+
return 0
|
25
|
+
|
26
|
+
max_possible_distance = cp.sqrt(population.shape[1])
|
27
|
+
max_possible_distance = max(max_possible_distance, 1e-10)
|
28
|
+
|
29
|
+
diversity = avg_distance / max_possible_distance
|
30
|
+
return diversity
|
31
|
+
|
32
|
+
def multi_head_fitness(y_true, y_pred, diversity_score, accuracy, alpha=2, beta=1.5, lambda_div=0.05):
|
33
|
+
"""
|
34
|
+
The function calculates a fitness score based on accuracy, margin loss, and diversity score using
|
35
|
+
specified parameters.
|
36
|
+
|
37
|
+
@param y_true The `y_true` parameter represents the true labels of the data points. It is an array
|
38
|
+
or list containing the actual labels of the data points.
|
39
|
+
@param y_pred The `y_pred` parameter in the `multi_head_fitness` function represents the
|
40
|
+
predicted values for a given dataset. It is a NumPy array containing the predicted values for each
|
41
|
+
sample in the dataset.
|
42
|
+
@param diversity_score The `diversity_score` parameter in the `multi_head_fitness` function
|
43
|
+
represents a measure of diversity in the predictions. It is used as a factor in calculating the
|
44
|
+
fitness of the model. The function combines accuracy, margin loss, and diversity score to evaluate
|
45
|
+
the overall performance of the model
|
46
|
+
@param accuracy Accuracy is a measure of the correct predictions made by a model. It is typically
|
47
|
+
calculated as the number of correct predictions divided by the total number of predictions. In the
|
48
|
+
context of the `multi_head_fitness` function, the accuracy parameter represents the accuracy
|
49
|
+
of the model's predictions.
|
50
|
+
@param alpha Alpha is a parameter that controls the impact of accuracy on the overall fitness score
|
51
|
+
in the multi_head_fitness function. It is used as an exponent to raise the accuracy value
|
52
|
+
to, influencing its contribution to the fitness calculation.
|
53
|
+
@param beta The `beta` parameter in the `multi_head_fitness` function is used as an exponent
|
54
|
+
in the fitness calculation formula. It controls the impact of the margin loss term on the overall
|
55
|
+
fitness value. A higher value of `beta` will amplify the effect of the margin loss term, making it
|
56
|
+
@param lambda_div The `lambda_div` parameter in the `multi_head_fitness` function represents
|
57
|
+
the weight given to the diversity score in calculating the fitness value. It is a hyperparameter
|
58
|
+
that controls the impact of diversity score on the overall fitness calculation. A higher value of
|
59
|
+
`lambda_div` will increase the importance
|
60
|
+
@return The function `multi_head_fitness` returns the fitness value calculated based on the
|
61
|
+
icput parameters `y_true`, `y_pred`, `diversity_score`, `accuracy`, and optional parameters `alpha`,
|
62
|
+
`beta`, and `lambda_div`. The fitness value is computed using a formula that combines accuracy,
|
63
|
+
margin loss, and diversity score with specified weights and coefficients.
|
64
|
+
"""
|
65
|
+
incorrect = y_true != y_pred
|
66
|
+
margin_loss = cp.abs(y_true[incorrect] - y_pred[incorrect]).mean() if cp.any(incorrect) else 0
|
67
|
+
|
68
|
+
margin_loss = cp.nan_to_num(margin_loss, nan=0.0)
|
69
|
+
accuracy = max(accuracy, 1e-10)
|
70
|
+
|
71
|
+
fitness = (accuracy ** alpha) * ((1 - margin_loss) ** beta) * (1 + lambda_div * diversity_score)
|
72
|
+
return fitness
|
pyerualjetwork/plan.py
CHANGED
@@ -20,9 +20,7 @@ import numpy as np
|
|
20
20
|
### LIBRARY IMPORTS ###
|
21
21
|
from .ui import loading_bars, initialize_loading_bar
|
22
22
|
from .data_operations import normalization, batcher
|
23
|
-
from .loss_functions import binary_crossentropy, categorical_crossentropy
|
24
23
|
from .activation_functions import apply_activation, all_activations
|
25
|
-
from .metrics import metrics
|
26
24
|
from .model_operations import get_acc, get_preds, get_preds_softmax
|
27
25
|
from .memory_operations import optimize_labels
|
28
26
|
from .visualizations import (
|
@@ -83,10 +81,10 @@ def fit(
|
|
83
81
|
return normalization(weight, dtype=dtype)
|
84
82
|
|
85
83
|
|
86
|
-
def learner(x_train, y_train, optimizer, fit_start=True,
|
84
|
+
def learner(x_train, y_train, optimizer, fitness, fit_start=True, gen=None, batch_size=1, pop_size=None,
|
87
85
|
neural_web_history=False, show_current_activations=False, auto_normalization=False,
|
88
|
-
neurons_history=False, early_stop=False,
|
89
|
-
interval=33.33, target_acc=None,
|
86
|
+
neurons_history=False, early_stop=False, show_history=False,
|
87
|
+
interval=33.33, target_acc=None,
|
90
88
|
start_this_act=None, start_this_W=None, dtype=np.float32):
|
91
89
|
"""
|
92
90
|
Optimizes the activation functions for a neural network by leveraging train data to find
|
@@ -104,14 +102,15 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
104
102
|
|
105
103
|
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:
|
106
104
|
```python
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
105
|
+
optimizer = lambda *args, **kwargs: planeat.evolver(*args,
|
106
|
+
activation_add_prob=0.85,
|
107
|
+
strategy='aggressive',
|
108
|
+
**kwargs)
|
111
109
|
|
112
110
|
model = plan.learner(x_train,
|
113
111
|
y_train,
|
114
|
-
optimizer
|
112
|
+
optimizer,
|
113
|
+
fitness,
|
115
114
|
fit_start=True,
|
116
115
|
strategy='accuracy',
|
117
116
|
show_history=True,
|
@@ -120,9 +119,23 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
120
119
|
interval=16.67)
|
121
120
|
```
|
122
121
|
|
123
|
-
|
122
|
+
fitness (function): Fitness function. We have 'multi_head_fitness' function in the fitness_functions module. Use this: from pyerualjetwork import fitness_functions (and) fitness_function = lambda *args, **kwargs: fitness_functions.multi_head_fitness(*args, 'here give your fitness function hyperparameters for example: alpha=2, beta=1.5, lambda_div=0.05', **kwargs) Example:
|
123
|
+
```python
|
124
|
+
fitness = lambda *args, **kwargs: fitness_functions.multi_head_fitness(*args, alpha=2, beta=1.5, lambda_div=0.05, **kwargs)
|
124
125
|
|
125
|
-
|
126
|
+
model = plan.learner(x_train,
|
127
|
+
y_train,
|
128
|
+
optimizer,
|
129
|
+
fitness
|
130
|
+
fit_start=True,
|
131
|
+
strategy='accuracy',
|
132
|
+
show_history=True,
|
133
|
+
gen=15,
|
134
|
+
batch_size=0.05,
|
135
|
+
interval=16.67)
|
136
|
+
```
|
137
|
+
|
138
|
+
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. Options: True or False. Default: True
|
126
139
|
|
127
140
|
gen (int, optional): The generation count for genetic optimization.
|
128
141
|
|
@@ -138,14 +151,10 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
138
151
|
|
139
152
|
auto_normalization (bool, optional): Normalization may solves overflow problem. Default: False
|
140
153
|
|
141
|
-
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'.
|
142
|
-
|
143
154
|
interval (int, optional): The interval at which evaluations are conducted during training. (33.33 = 30 FPS, 16.67 = 60 FPS) Default is 100.
|
144
155
|
|
145
156
|
target_acc (int, optional): The target accuracy to stop training early when achieved. Default is None.
|
146
157
|
|
147
|
-
target_loss (float, optional): The target loss to stop training early when achieved. Default is None.
|
148
|
-
|
149
158
|
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
|
150
159
|
|
151
160
|
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
|
@@ -161,6 +170,7 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
161
170
|
|
162
171
|
"""
|
163
172
|
|
173
|
+
from .fitness_functions import diversity_score
|
164
174
|
from .planeat import define_genomes
|
165
175
|
|
166
176
|
data = 'Train'
|
@@ -181,7 +191,6 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
181
191
|
if gen is None:
|
182
192
|
gen = activation_potentiation_len
|
183
193
|
|
184
|
-
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'.")
|
185
194
|
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')
|
186
195
|
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')
|
187
196
|
|
@@ -190,12 +199,10 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
190
199
|
|
191
200
|
# Initialize variables
|
192
201
|
best_acc = 0
|
193
|
-
|
194
|
-
best_recall = 0
|
195
|
-
best_precision = 0
|
202
|
+
best_fit = 0
|
196
203
|
best_acc_per_gen_list = []
|
197
204
|
postfix_dict = {}
|
198
|
-
|
205
|
+
fit_list = []
|
199
206
|
target_pop = []
|
200
207
|
|
201
208
|
progress = initialize_loading_bar(total=activation_potentiation_len, desc="", ncols=78, bar_format=bar_format_learner)
|
@@ -234,36 +241,16 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
234
241
|
model = evaluate(x_train_batch, y_train_batch, W=weight_pop[j], activation_potentiation=act_pop[j])
|
235
242
|
acc = model[get_acc()]
|
236
243
|
|
237
|
-
|
238
|
-
|
239
|
-
elif strategy == 'f1' or strategy == 'precision' or strategy == 'recall':
|
240
|
-
precision_score, recall_score, f1_score = metrics(y_train_batch, model[get_preds()])
|
241
|
-
|
242
|
-
if strategy == 'precision':
|
243
|
-
target_pop.append(precision_score)
|
244
|
-
|
245
|
-
if i == 0 and j == 0:
|
246
|
-
best_precision = precision_score
|
244
|
+
div_score = diversity_score(weight_pop[j])
|
245
|
+
fit_score = fitness(y_train_batch, model[get_preds_softmax()], div_score, acc)
|
247
246
|
|
248
|
-
|
249
|
-
target_pop.append(recall_score)
|
247
|
+
target_pop.append(fit_score)
|
250
248
|
|
251
|
-
|
252
|
-
best_recall = recall_score
|
253
|
-
|
254
|
-
if strategy == 'f1':
|
255
|
-
target_pop.append(f1_score)
|
256
|
-
|
257
|
-
if i == 0 and j == 0:
|
258
|
-
best_f1 = f1_score
|
259
|
-
|
260
|
-
if ((strategy == 'accuracy' and acc >= best_acc) or
|
261
|
-
(strategy == 'f1' and f1_score >= best_f1) or
|
262
|
-
(strategy == 'precision' and precision_score >= best_precision) or
|
263
|
-
(strategy == 'recall' and recall_score >= best_recall)):
|
249
|
+
if fit_score >= best_fit:
|
264
250
|
|
265
251
|
best_acc = acc
|
266
|
-
|
252
|
+
best_fit = fit_score
|
253
|
+
best_weight = np.copy(weight_pop[j])
|
267
254
|
final_activations = act_pop[j].copy() if isinstance(act_pop[j], list) else act_pop[j]
|
268
255
|
|
269
256
|
best_model = model
|
@@ -277,33 +264,28 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
277
264
|
if show_current_activations:
|
278
265
|
print(f", Current Activations={final_activations}", end='')
|
279
266
|
|
280
|
-
if loss == 'categorical_crossentropy':
|
281
|
-
train_loss = categorical_crossentropy(y_true_batch=y_train_batch, y_pred_batch=model[get_preds_softmax()])
|
282
|
-
else:
|
283
|
-
train_loss = binary_crossentropy(y_true_batch=y_train_batch, y_pred_batch=model[get_preds_softmax()])
|
284
|
-
|
285
267
|
if batch_size == 1:
|
286
|
-
postfix_dict[f"{data}
|
268
|
+
postfix_dict[f"{data} Fitness"] = np.round(fit_score, 4)
|
287
269
|
progress.set_postfix(postfix_dict)
|
288
|
-
|
270
|
+
best_fit = fit_score
|
289
271
|
|
290
272
|
# Update visualizations during training
|
291
273
|
if show_history:
|
292
274
|
gen_list = range(1, len(best_acc_per_gen_list) + 2)
|
293
|
-
update_history_plots_for_learner(viz_objects, gen_list,
|
275
|
+
update_history_plots_for_learner(viz_objects, gen_list, fit_list + [fit_score],
|
294
276
|
best_acc_per_gen_list + [best_acc], x_train, final_activations)
|
295
277
|
|
296
278
|
if neurons_history:
|
297
279
|
viz_objects['neurons']['artists'] = (
|
298
|
-
update_neuron_history_for_learner(np.copy(
|
280
|
+
update_neuron_history_for_learner(np.copy(best_weight), viz_objects['neurons']['ax'],
|
299
281
|
viz_objects['neurons']['row'], viz_objects['neurons']['col'],
|
300
282
|
y_train[0], viz_objects['neurons']['artists'],
|
301
283
|
data=data, fig1=viz_objects['neurons']['fig'],
|
302
|
-
acc=best_acc, loss=
|
284
|
+
acc=best_acc, loss=fit_score)
|
303
285
|
)
|
304
286
|
|
305
287
|
if neural_web_history:
|
306
|
-
art5_1, art5_2, art5_3 = draw_neural_web(W=
|
288
|
+
art5_1, art5_2, art5_3 = draw_neural_web(W=best_weight, ax=viz_objects['web']['ax'],
|
307
289
|
G=viz_objects['web']['G'], return_objs=True)
|
308
290
|
art5_list = [art5_1] + [art5_2] + list(art5_3.values())
|
309
291
|
viz_objects['web']['artists'].append(art5_list)
|
@@ -311,69 +293,43 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
311
293
|
# Check target accuracy
|
312
294
|
if target_acc is not None and best_acc >= target_acc:
|
313
295
|
progress.close()
|
314
|
-
train_model = evaluate(x_train, y_train, W=
|
315
|
-
activation_potentiation=final_activations)
|
316
|
-
|
317
|
-
if loss == 'categorical_crossentropy':
|
318
|
-
train_loss = categorical_crossentropy(y_true_batch=y_train,
|
319
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
320
|
-
else:
|
321
|
-
train_loss = binary_crossentropy(y_true_batch=y_train,
|
322
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
323
|
-
|
324
|
-
print('\nActivations: ', final_activations)
|
325
|
-
print(f'Train Accuracy:', train_model[get_acc()])
|
326
|
-
print(f'Train Loss: ', train_loss, '\n')
|
327
|
-
|
328
|
-
# Display final visualizations
|
329
|
-
display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
|
330
|
-
train_loss, y_train, interval)
|
331
|
-
return best_weights, best_model[get_preds()], best_acc, final_activations
|
332
|
-
|
333
|
-
# Check target loss
|
334
|
-
if target_loss is not None and best_loss <= target_loss:
|
335
|
-
progress.close()
|
336
|
-
train_model = evaluate(x_train, y_train, W=best_weights,
|
296
|
+
train_model = evaluate(x_train, y_train, W=best_weight,
|
337
297
|
activation_potentiation=final_activations)
|
338
298
|
|
339
|
-
|
340
|
-
|
341
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
342
|
-
else:
|
343
|
-
train_loss = binary_crossentropy(y_true_batch=y_train,
|
344
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
299
|
+
div_score = diversity_score(best_weight)
|
300
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
345
301
|
|
346
302
|
print('\nActivations: ', final_activations)
|
347
303
|
print(f'Train Accuracy:', train_model[get_acc()])
|
348
|
-
print(f'
|
349
|
-
|
304
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
305
|
+
|
350
306
|
# Display final visualizations
|
351
|
-
display_visualizations_for_learner(viz_objects,
|
352
|
-
|
353
|
-
return
|
307
|
+
display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
|
308
|
+
fit_score, y_train, interval)
|
309
|
+
return best_weight, best_model[get_preds()], best_acc, final_activations
|
354
310
|
|
355
311
|
progress.update(1)
|
356
312
|
|
357
313
|
if batch_size != 1:
|
358
|
-
train_model = evaluate(x_train, y_train,
|
314
|
+
train_model = evaluate(x_train, y_train, best_weight, final_activations)
|
359
315
|
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
316
|
+
div_score = diversity_score(best_weight)
|
317
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
318
|
+
|
319
|
+
print('\nActivations: ', final_activations)
|
320
|
+
print(f'Train Accuracy:', train_model[get_acc()])
|
321
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
366
322
|
|
367
323
|
postfix_dict[f"{data} Accuracy"] = np.round(train_model[get_acc()], 4)
|
368
|
-
postfix_dict[f"{data}
|
324
|
+
postfix_dict[f"{data} Fitness"] = np.round(best_fit, 4)
|
369
325
|
progress.set_postfix(postfix_dict)
|
370
326
|
|
371
327
|
best_acc_per_gen_list.append(train_model[get_acc()])
|
372
|
-
|
328
|
+
fit_list.append(fit_score)
|
373
329
|
|
374
330
|
else:
|
375
331
|
best_acc_per_gen_list.append(best_acc)
|
376
|
-
|
332
|
+
fit_list.append(best_fit)
|
377
333
|
|
378
334
|
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)
|
379
335
|
target_pop = []
|
@@ -382,42 +338,36 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
382
338
|
if early_stop == True and i > 0:
|
383
339
|
if best_acc_per_gen_list[i] == best_acc_per_gen_list[i-1]:
|
384
340
|
progress.close()
|
385
|
-
train_model = evaluate(x_train, y_train, W=
|
341
|
+
train_model = evaluate(x_train, y_train, W=best_weight,
|
386
342
|
activation_potentiation=final_activations)
|
387
343
|
|
388
|
-
|
389
|
-
|
390
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
391
|
-
else:
|
392
|
-
train_loss = binary_crossentropy(y_true_batch=y_train,
|
393
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
344
|
+
div_score = diversity_score(best_weight)
|
345
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
394
346
|
|
395
347
|
print('\nActivations: ', final_activations)
|
396
348
|
print(f'Train Accuracy:', train_model[get_acc()])
|
397
|
-
print(f'
|
349
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
398
350
|
|
399
351
|
# Display final visualizations
|
400
|
-
display_visualizations_for_learner(viz_objects,
|
401
|
-
|
402
|
-
return
|
352
|
+
display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
|
353
|
+
fit_score, y_train, interval)
|
354
|
+
return best_weight, best_model[get_preds()], best_acc, final_activations
|
403
355
|
|
404
356
|
# Final evaluation
|
405
357
|
progress.close()
|
406
|
-
train_model = evaluate(x_train, y_train, W=
|
358
|
+
train_model = evaluate(x_train, y_train, W=best_weight,
|
407
359
|
activation_potentiation=final_activations)
|
408
360
|
|
409
|
-
|
410
|
-
|
411
|
-
else:
|
412
|
-
train_loss = binary_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
|
361
|
+
div_score = diversity_score(best_weight)
|
362
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
413
363
|
|
414
364
|
print('\nActivations: ', final_activations)
|
415
365
|
print(f'Train Accuracy:', train_model[get_acc()])
|
416
|
-
print(f'
|
366
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
417
367
|
|
418
368
|
# Display final visualizations
|
419
|
-
display_visualizations_for_learner(viz_objects,
|
420
|
-
return
|
369
|
+
display_visualizations_for_learner(viz_objects, best_weight, data, best_acc, fit_score, y_train, interval)
|
370
|
+
return best_weight, best_model[get_preds()], best_acc, final_activations
|
421
371
|
|
422
372
|
|
423
373
|
def evaluate(
|
@@ -444,6 +394,8 @@ def evaluate(
|
|
444
394
|
|
445
395
|
x_test = apply_activation(x_test, activation_potentiation)
|
446
396
|
result = x_test @ W.T
|
447
|
-
|
397
|
+
epsilon = 1e-10
|
398
|
+
accuracy = (np.argmax(result, axis=1) == np.argmax(y_test, axis=1)).mean()
|
399
|
+
softmax_preds = np.exp(result) / (np.sum(np.exp(result), axis=1, keepdims=True) + epsilon)
|
448
400
|
|
449
401
|
return W, None, accuracy, None, None, softmax_preds
|
pyerualjetwork/plan_cuda.py
CHANGED
@@ -20,9 +20,7 @@ import cupy as cp
|
|
20
20
|
### LIBRARY IMPORTS ###
|
21
21
|
from .ui import loading_bars, initialize_loading_bar
|
22
22
|
from .data_operations_cuda import normalization
|
23
|
-
from .loss_functions_cuda import binary_crossentropy, categorical_crossentropy
|
24
23
|
from .activation_functions_cuda import apply_activation, all_activations
|
25
|
-
from .metrics_cuda import metrics
|
26
24
|
from .model_operations_cuda import get_acc, get_preds, get_preds_softmax
|
27
25
|
from .memory_operations import transfer_to_gpu, transfer_to_cpu, optimize_labels
|
28
26
|
from .visualizations_cuda import (
|
@@ -82,10 +80,10 @@ def fit(
|
|
82
80
|
return normalization(weight, dtype=dtype)
|
83
81
|
|
84
82
|
|
85
|
-
def learner(x_train, y_train, optimizer, fit_start=True,
|
83
|
+
def learner(x_train, y_train, optimizer, fitness, fit_start=True, gen=None, batch_size=1, pop_size=None,
|
86
84
|
neural_web_history=False, show_current_activations=False, auto_normalization=False,
|
87
|
-
neurons_history=False, early_stop=False,
|
88
|
-
interval=33.33, target_acc=None,
|
85
|
+
neurons_history=False, early_stop=False, show_history=False,
|
86
|
+
interval=33.33, target_acc=None,
|
89
87
|
start_this_act=None, start_this_W=None, dtype=cp.float32, memory='gpu'):
|
90
88
|
"""
|
91
89
|
Optimizes the activation functions for a neural network by leveraging train data to find
|
@@ -118,10 +116,25 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
118
116
|
batch_size=0.05,
|
119
117
|
interval=16.67)
|
120
118
|
```
|
121
|
-
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. Options: True or False. Default: True
|
122
119
|
|
123
|
-
|
120
|
+
fitness (function): Fitness function. We have 'multi_head_fitness' function in the fitness_functions module. Use this: from pyerualjetwork import fitness_functions (and) fitness_function = lambda *args, **kwargs: fitness_functions.multi_head_fitness(*args, 'here give your fitness function hyperparameters for example: alpha=2, beta=1.5, lambda_div=0.05', **kwargs) Example:
|
121
|
+
```python
|
122
|
+
fitness = lambda *args, **kwargs: fitness_functions.multi_head_fitness(*args, alpha=2, beta=1.5, lambda_div=0.05, **kwargs)
|
123
|
+
|
124
|
+
model = plan.learner(x_train,
|
125
|
+
y_train,
|
126
|
+
optimizer,
|
127
|
+
fitness
|
128
|
+
fit_start=True,
|
129
|
+
strategy='accuracy',
|
130
|
+
show_history=True,
|
131
|
+
gen=15,
|
132
|
+
batch_size=0.05,
|
133
|
+
interval=16.67)
|
134
|
+
```
|
124
135
|
|
136
|
+
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. Options: True or False. Default: True
|
137
|
+
|
125
138
|
gen (int, optional): The generation count for genetic optimization.
|
126
139
|
|
127
140
|
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)
|
@@ -134,15 +147,11 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
134
147
|
|
135
148
|
show_history (bool, optional): If True, displays the training history after optimization. Default is False.
|
136
149
|
|
137
|
-
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'.
|
138
|
-
|
139
150
|
auto_normalization (bool, optional): Normalization may solves overflow problem. Default: False
|
140
151
|
|
141
152
|
interval (int, optional): The interval at which evaluations are conducted during training. (33.33 = 30 FPS, 16.67 = 60 FPS) Default is 100.
|
142
153
|
|
143
154
|
target_acc (int, optional): The target accuracy to stop training early when achieved. Default is None.
|
144
|
-
|
145
|
-
target_loss (float, optional): The target loss to stop training early when achieved. Default is None.
|
146
155
|
|
147
156
|
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
|
148
157
|
|
@@ -161,6 +170,7 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
161
170
|
|
162
171
|
"""
|
163
172
|
|
173
|
+
from .fitness_functions_cuda import diversity_score
|
164
174
|
from .planeat_cuda import define_genomes
|
165
175
|
|
166
176
|
data = 'Train'
|
@@ -189,7 +199,6 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
189
199
|
else:
|
190
200
|
raise ValueError("memory parameter must be 'cpu' or 'gpu'.")
|
191
201
|
|
192
|
-
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'.")
|
193
202
|
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')
|
194
203
|
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')
|
195
204
|
|
@@ -198,12 +207,10 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
198
207
|
|
199
208
|
# Initialize variables
|
200
209
|
best_acc = 0
|
201
|
-
|
202
|
-
best_recall = 0
|
203
|
-
best_precision = 0
|
210
|
+
best_fit = 0
|
204
211
|
best_acc_per_gen_list = []
|
205
212
|
postfix_dict = {}
|
206
|
-
|
213
|
+
fit_list = []
|
207
214
|
target_pop = []
|
208
215
|
|
209
216
|
progress = initialize_loading_bar(total=activation_potentiation_len, desc="", ncols=78, bar_format=bar_format_learner)
|
@@ -245,36 +252,16 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
245
252
|
model = evaluate(x_train_batch, y_train_batch, W=weight_pop[j], activation_potentiation=act_pop[j])
|
246
253
|
acc = model[get_acc()]
|
247
254
|
|
248
|
-
|
249
|
-
|
250
|
-
elif strategy == 'f1' or strategy == 'precision' or strategy == 'recall':
|
251
|
-
precision_score, recall_score, f1_score = metrics(y_train_batch, model[get_preds()])
|
252
|
-
|
253
|
-
if strategy == 'precision':
|
254
|
-
target_pop.append(precision_score)
|
255
|
-
|
256
|
-
if i == 0 and j == 0:
|
257
|
-
best_precision = precision_score
|
258
|
-
|
259
|
-
if strategy == 'recall':
|
260
|
-
target_pop.append(recall_score)
|
261
|
-
|
262
|
-
if i == 0 and j == 0:
|
263
|
-
best_recall = recall_score
|
264
|
-
|
265
|
-
if strategy == 'f1':
|
266
|
-
target_pop.append(f1_score)
|
255
|
+
div_score = diversity_score(weight_pop[j])
|
256
|
+
fit_score = fitness(y_train_batch, model[get_preds_softmax()], div_score, acc)
|
267
257
|
|
268
|
-
|
269
|
-
best_f1 = f1_score
|
258
|
+
target_pop.append(fit_score)
|
270
259
|
|
271
|
-
if
|
272
|
-
(strategy == 'f1' and f1_score >= best_f1) or
|
273
|
-
(strategy == 'precision' and precision_score >= best_precision) or
|
274
|
-
(strategy == 'recall' and recall_score >= best_recall)):
|
260
|
+
if fit_score >= best_fit:
|
275
261
|
|
276
262
|
best_acc = acc
|
277
|
-
|
263
|
+
best_fit = fit_score
|
264
|
+
best_weight = cp.copy(weight_pop[j])
|
278
265
|
final_activations = act_pop[j].copy() if isinstance(act_pop[j], list) else act_pop[j]
|
279
266
|
|
280
267
|
best_model = model
|
@@ -287,34 +274,29 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
287
274
|
|
288
275
|
if show_current_activations:
|
289
276
|
print(f", Current Activations={final_activations}", end='')
|
290
|
-
|
291
|
-
if loss == 'categorical_crossentropy':
|
292
|
-
train_loss = categorical_crossentropy(y_true_batch=transfer_to_gpu(y_train_batch, dtype=y_train_batch.dtype), y_pred_batch=model[get_preds_softmax()])
|
293
|
-
else:
|
294
|
-
train_loss = binary_crossentropy(y_true_batch=transfer_to_gpu(y_train_batch, dtype=y_train_batch.dtype), y_pred_batch=model[get_preds_softmax()])
|
295
277
|
|
296
278
|
if batch_size == 1:
|
297
|
-
postfix_dict[f"{data}
|
298
|
-
best_loss = train_loss
|
279
|
+
postfix_dict[f"{data} Fitness"] = cp.round(fit_score, 4)
|
299
280
|
progress.set_postfix(postfix_dict)
|
281
|
+
best_fit = fit_score
|
300
282
|
|
301
283
|
# Update visualizations during training
|
302
284
|
if show_history:
|
303
285
|
gen_list = range(1, len(best_acc_per_gen_list) + 2)
|
304
|
-
update_history_plots_for_learner(viz_objects, gen_list,
|
286
|
+
update_history_plots_for_learner(viz_objects, gen_list, fit_list + [fit_score],
|
305
287
|
best_acc_per_gen_list + [best_acc], x_train, final_activations)
|
306
288
|
|
307
289
|
if neurons_history:
|
308
290
|
viz_objects['neurons']['artists'] = (
|
309
|
-
update_neuron_history_for_learner(cp.copy(
|
291
|
+
update_neuron_history_for_learner(cp.copy(best_weight), viz_objects['neurons']['ax'],
|
310
292
|
viz_objects['neurons']['row'], viz_objects['neurons']['col'],
|
311
293
|
y_train[0], viz_objects['neurons']['artists'],
|
312
294
|
data=data, fig1=viz_objects['neurons']['fig'],
|
313
|
-
acc=best_acc, loss=
|
295
|
+
acc=best_acc, loss=fit_score)
|
314
296
|
)
|
315
297
|
|
316
298
|
if neural_web_history:
|
317
|
-
art5_1, art5_2, art5_3 = draw_neural_web(W=
|
299
|
+
art5_1, art5_2, art5_3 = draw_neural_web(W=best_weight, ax=viz_objects['web']['ax'],
|
318
300
|
G=viz_objects['web']['G'], return_objs=True)
|
319
301
|
art5_list = [art5_1] + [art5_2] + list(art5_3.values())
|
320
302
|
viz_objects['web']['artists'].append(art5_list)
|
@@ -322,67 +304,42 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
322
304
|
# Check target accuracy
|
323
305
|
if target_acc is not None and best_acc >= target_acc:
|
324
306
|
progress.close()
|
325
|
-
train_model = evaluate(x_train, y_train, W=
|
307
|
+
train_model = evaluate(x_train, y_train, W=best_weight,
|
326
308
|
activation_potentiation=final_activations)
|
327
309
|
|
328
|
-
|
329
|
-
|
330
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
331
|
-
else:
|
332
|
-
train_loss = binary_crossentropy(y_true_batch=y_train,
|
333
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
310
|
+
div_score = diversity_score(best_weight)
|
311
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
334
312
|
|
335
313
|
print('\nActivations: ', final_activations)
|
336
314
|
print(f'Train Accuracy:', train_model[get_acc()])
|
337
|
-
print(f'
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
return best_weights, best_model[get_preds()], best_acc, final_activations
|
343
|
-
|
344
|
-
# Check target loss
|
345
|
-
if target_loss is not None and best_loss <= target_loss:
|
346
|
-
progress.close()
|
347
|
-
train_model = evaluate(x_train, y_train, W=best_weights,
|
348
|
-
activation_potentiation=final_activations)
|
349
|
-
|
350
|
-
if loss == 'categorical_crossentropy':
|
351
|
-
train_loss = categorical_crossentropy(y_true_batch=y_train,
|
352
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
353
|
-
else:
|
354
|
-
train_loss = binary_crossentropy(y_true_batch=y_train,
|
355
|
-
y_pred_batch=train_model[get_preds_softmax()])
|
356
|
-
|
357
|
-
print('\nActivations: ', final_activations)
|
358
|
-
print(f'Train Accuracy: ', train_model[get_acc()])
|
359
|
-
print(f'Train Loss: ', train_loss, '\n')
|
360
|
-
|
361
|
-
# Display final visualizations
|
362
|
-
display_visualizations_for_learner(viz_objects, best_weights, data, best_acc,
|
363
|
-
train_loss, y_train, interval)
|
364
|
-
return best_weights, best_model[get_preds()], best_acc, final_activations
|
315
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
316
|
+
|
317
|
+
display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
|
318
|
+
fit_score, y_train, interval)
|
319
|
+
return best_weight, best_model[get_preds()], best_acc, final_activations
|
365
320
|
|
366
321
|
progress.update(1)
|
367
322
|
|
368
323
|
if batch_size != 1:
|
369
|
-
train_model = evaluate(x_train, y_train,
|
324
|
+
train_model = evaluate(x_train, y_train, best_weight, final_activations)
|
370
325
|
|
371
|
-
|
372
|
-
|
373
|
-
else:
|
374
|
-
train_loss = binary_crossentropy(y_true_batch=transfer_to_gpu(y_train, dtype=y_train.dtype), y_pred_batch=train_model[get_preds_softmax()])
|
326
|
+
div_score = diversity_score(best_weight)
|
327
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
375
328
|
|
329
|
+
print('\nActivations: ', final_activations)
|
330
|
+
print(f'Train Accuracy:', train_model[get_acc()])
|
331
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
332
|
+
|
376
333
|
postfix_dict[f"{data} Accuracy"] = cp.round(train_model[get_acc()], 4)
|
377
|
-
postfix_dict[f"{data}
|
334
|
+
postfix_dict[f"{data} Fitness"] = cp.round(best_fit, 4)
|
378
335
|
progress.set_postfix(postfix_dict)
|
379
|
-
|
336
|
+
|
380
337
|
best_acc_per_gen_list.append(train_model[get_acc()])
|
381
|
-
|
338
|
+
fit_list.append(fit_score)
|
382
339
|
|
383
340
|
else:
|
384
341
|
best_acc_per_gen_list.append(best_acc)
|
385
|
-
|
342
|
+
fit_list.append(best_fit)
|
386
343
|
|
387
344
|
weight_pop, act_pop = optimizer(cp.array(weight_pop, copy=False, dtype=dtype), act_pop, i, cp.array(target_pop, dtype=dtype, copy=False), bar_status=False)
|
388
345
|
target_pop = []
|
@@ -391,42 +348,36 @@ def learner(x_train, y_train, optimizer, fit_start=True, strategy='accuracy', ge
|
|
391
348
|
if early_stop == True and i > 0:
|
392
349
|
if best_acc_per_gen_list[i] == best_acc_per_gen_list[i-1]:
|
393
350
|
progress.close()
|
394
|
-
train_model = evaluate(x_train, y_train, W=
|
351
|
+
train_model = evaluate(x_train, y_train, W=best_weight,
|
395
352
|
activation_potentiation=final_activations)
|
396
353
|
|
397
|
-
|
398
|
-
|
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()])
|
354
|
+
div_score = diversity_score(best_weight)
|
355
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
403
356
|
|
404
357
|
print('\nActivations: ', final_activations)
|
405
358
|
print(f'Train Accuracy:', train_model[get_acc()])
|
406
|
-
print(f'
|
359
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
407
360
|
|
408
361
|
# Display final visualizations
|
409
|
-
display_visualizations_for_learner(viz_objects,
|
410
|
-
|
411
|
-
return
|
362
|
+
display_visualizations_for_learner(viz_objects, best_weight, data, best_acc,
|
363
|
+
fit_score, y_train, interval)
|
364
|
+
return best_weight, best_model[get_preds()], best_acc, final_activations
|
412
365
|
|
413
366
|
# Final evaluation
|
414
367
|
progress.close()
|
415
|
-
train_model = evaluate(x_train, y_train, W=
|
368
|
+
train_model = evaluate(x_train, y_train, W=best_weight,
|
416
369
|
activation_potentiation=final_activations)
|
417
370
|
|
418
|
-
|
419
|
-
|
420
|
-
else:
|
421
|
-
train_loss = binary_crossentropy(y_true_batch=y_train, y_pred_batch=train_model[get_preds_softmax()])
|
371
|
+
div_score = diversity_score(best_weight)
|
372
|
+
fit_score = fitness(y_train, train_model[get_preds_softmax()], div_score, train_model[get_acc()])
|
422
373
|
|
423
374
|
print('\nActivations: ', final_activations)
|
424
|
-
print(f'Train Accuracy:
|
425
|
-
print(f'
|
375
|
+
print(f'Train Accuracy:', train_model[get_acc()])
|
376
|
+
print(f'Fitness Value: ', fit_score, '\n')
|
426
377
|
|
427
378
|
# Display final visualizations
|
428
|
-
display_visualizations_for_learner(viz_objects,
|
429
|
-
return
|
379
|
+
display_visualizations_for_learner(viz_objects, best_weight, data, best_acc, fit_score, y_train, interval)
|
380
|
+
return best_weight, best_model[get_preds()], best_acc, final_activations
|
430
381
|
|
431
382
|
def evaluate(
|
432
383
|
x_test,
|
@@ -452,6 +403,8 @@ def evaluate(
|
|
452
403
|
|
453
404
|
x_test = apply_activation(x_test, activation_potentiation)
|
454
405
|
result = x_test @ W.T
|
455
|
-
|
406
|
+
epsilon = 1e-10
|
407
|
+
accuracy = (cp.argmax(result, axis=1) == cp.argmax(y_test, axis=1)).mean()
|
408
|
+
softmax_preds = cp.exp(result) / (cp.sum(cp.exp(result), axis=1, keepdims=True) + epsilon)
|
456
409
|
|
457
410
|
return W, None, accuracy, None, None, softmax_preds
|
pyerualjetwork/visualizations.py
CHANGED
@@ -754,9 +754,9 @@ def update_history_plots_for_learner(viz_objects, depth_list, loss_list, best_ac
|
|
754
754
|
|
755
755
|
hist = viz_objects['history']
|
756
756
|
|
757
|
-
#
|
758
|
-
art1 = hist['ax'][0].plot(depth_list, loss_list, color='
|
759
|
-
hist['ax'][0].set_title('
|
757
|
+
# Fitness plot
|
758
|
+
art1 = hist['ax'][0].plot(depth_list, loss_list, color='m', markersize=6, linewidth=2)
|
759
|
+
hist['ax'][0].set_title('Fitness Score Over Gen')
|
760
760
|
hist['artist1'].append(art1)
|
761
761
|
|
762
762
|
# Accuracy plot
|
@@ -771,7 +771,7 @@ def update_history_plots_for_learner(viz_objects, depth_list, loss_list, best_ac
|
|
771
771
|
translated_x_train += draw_activations(x, activation)
|
772
772
|
|
773
773
|
art3 = hist['ax'][2].plot(x, translated_x_train, color='b', markersize=6, linewidth=2)
|
774
|
-
hist['ax'][2].set_title('
|
774
|
+
hist['ax'][2].set_title('Activation Shape Over Gen')
|
775
775
|
hist['artist3'].append(art3)
|
776
776
|
|
777
777
|
def display_visualizations_for_learner(viz_objects, best_weights, data, best_acc, test_loss, y_train, interval):
|
@@ -748,13 +748,12 @@ def update_history_plots_for_learner(viz_objects, depth_list, loss_list, best_ac
|
|
748
748
|
for i in range(len(loss_list)):
|
749
749
|
loss_list[i] = loss_list[i].get()
|
750
750
|
|
751
|
-
#
|
752
|
-
art1 = hist['ax'][0].plot(depth_list, loss_list, color='
|
753
|
-
hist['ax'][0].set_title('
|
751
|
+
# Fitness plot
|
752
|
+
art1 = hist['ax'][0].plot(depth_list, loss_list, color='m', markersize=6, linewidth=2)
|
753
|
+
hist['ax'][0].set_title('Fitness Score Over Gen')
|
754
754
|
hist['artist1'].append(art1)
|
755
755
|
|
756
756
|
# Accuracy plot
|
757
|
-
|
758
757
|
for i in range(len(best_acc_per_depth_list)):
|
759
758
|
best_acc_per_depth_list[i] = best_acc_per_depth_list[i].get()
|
760
759
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: pyerualjetwork
|
3
|
-
Version: 4.3.
|
3
|
+
Version: 4.3.9b8
|
4
4
|
Summary: PyerualJetwork is a machine learning library supported with GPU(CUDA) acceleration written in Python for professionals and researchers including with PLAN algorithm, PLANEAT algorithm (genetic optimization). Also includes data pre-process and memory manegament
|
5
5
|
Author: Hasan Can Beydili
|
6
6
|
Author-email: tchasancan@gmail.com
|
@@ -43,6 +43,7 @@ YouTube Tutorials: https://www.youtube.com/watch?v=6wMQstZ00is&list=PLNgNWpM7Hbs
|
|
43
43
|
'tqdm==4.66.4',
|
44
44
|
'pandas==2.2.2',
|
45
45
|
'networkx==3.3',
|
46
|
+
'seaborn==0.13.2',
|
46
47
|
'numpy==1.26.4',
|
47
48
|
'matplotlib==3.9.0',
|
48
49
|
'colorama==0.4.6',
|
@@ -1,24 +1,24 @@
|
|
1
|
-
pyerualjetwork/__init__.py,sha256=
|
1
|
+
pyerualjetwork/__init__.py,sha256=yxGVyGpN6O4nRZG7ZavFC1-XIbG1hPZB3-R9cbAgHbY,641
|
2
2
|
pyerualjetwork/activation_functions.py,sha256=bKf00lsuuLJNO-4vVp4OqBi4zJ-qZ8L3v-vl52notkY,7721
|
3
3
|
pyerualjetwork/activation_functions_cuda.py,sha256=5y1Ti3GDfDteQDCUmODwe7tAyDAUlDTKmIikChQ8d6g,7772
|
4
4
|
pyerualjetwork/data_operations.py,sha256=Flteouu6rfSo2uHMqBHuzO02dXmbNa-I5qWmUpGTZ5Y,14760
|
5
5
|
pyerualjetwork/data_operations_cuda.py,sha256=ZcjmLXE1-HVwedextYdJZ1rgrns1OfSekzFpr1a9m6o,17625
|
6
|
+
pyerualjetwork/fitness_functions.py,sha256=kewx8yMtQFUO-9rjCD3n5z26X2vLeDsb0HPTIdizFF0,4084
|
7
|
+
pyerualjetwork/fitness_functions_cuda.py,sha256=cait3Cb0kn4SC9wZr-SKurnvTw1KuOrvigvbPm4jNiM,4083
|
6
8
|
pyerualjetwork/help.py,sha256=nQ_YbYA2RtuafhuvkreNpX0WWL1I_nzlelwCtvei0_Y,775
|
7
|
-
pyerualjetwork/loss_functions.py,sha256=6PyBI232SQRGuFnG3LDGvnv_PUdWzT2_2mUODJiejGI,618
|
8
|
-
pyerualjetwork/loss_functions_cuda.py,sha256=C93IZJcrOpT6HMK9x1O4AHJWXYTkN5WZiqdssPbvAPk,617
|
9
9
|
pyerualjetwork/memory_operations.py,sha256=I7QiZ--xSyRkFF0wcckPwZV7K9emEvyx5aJ3DiRHZFI,13468
|
10
10
|
pyerualjetwork/metrics.py,sha256=q7MkhnZDRbCjFBDDfUgrl8lBYnUT_1ro1LxeBq105pI,6077
|
11
11
|
pyerualjetwork/metrics_cuda.py,sha256=73h9GC7XwmnFCVzFEEiPQfF8CwHIz2wsCbxpZrJtYgw,5061
|
12
12
|
pyerualjetwork/model_operations.py,sha256=MCSCNYiiICRVZITobtS3ZIWmH5Q9gjyELuH32sAdgg4,12649
|
13
13
|
pyerualjetwork/model_operations_cuda.py,sha256=NT01BK5nrDYE7H1x3KnSI8gmx0QTGGB0mP_LqEb1uuU,13157
|
14
|
-
pyerualjetwork/plan.py,sha256=
|
15
|
-
pyerualjetwork/plan_cuda.py,sha256=
|
14
|
+
pyerualjetwork/plan.py,sha256=dE63JQs8-8mOLbz3g6xjrM9AIpqx3bGF5wI3LyFOT2w,20200
|
15
|
+
pyerualjetwork/plan_cuda.py,sha256=HlpJ8eWFNqQEBVaxLk2rtsusjV5mFSFUlK54DSyVzcA,20986
|
16
16
|
pyerualjetwork/planeat.py,sha256=OxwSjfSFPwh7kVrhnuAkr8Lrk73GB-Wk2ajUFIfZcbQ,37556
|
17
17
|
pyerualjetwork/planeat_cuda.py,sha256=aTdBmhJeIKC58pssODtqVsdOtJP7W6TRmVyGHp7k_CM,37612
|
18
18
|
pyerualjetwork/ui.py,sha256=wu2BhU1k-w3Kcho5Jtq4SEKe68ftaUeRGneUOSCVDjU,575
|
19
|
-
pyerualjetwork/visualizations.py,sha256=
|
20
|
-
pyerualjetwork/visualizations_cuda.py,sha256=
|
21
|
-
pyerualjetwork-4.3.
|
22
|
-
pyerualjetwork-4.3.
|
23
|
-
pyerualjetwork-4.3.
|
24
|
-
pyerualjetwork-4.3.
|
19
|
+
pyerualjetwork/visualizations.py,sha256=6uY9U4qGOlG4aIhGdAFcTm2Z5m9Hxww4tAAG61_Vqb8,28294
|
20
|
+
pyerualjetwork/visualizations_cuda.py,sha256=o6MyZUVz040lmdI2kGlOEU2IoxvyWJPhSOKe9vkLMvY,28753
|
21
|
+
pyerualjetwork-4.3.9b8.dist-info/METADATA,sha256=3mzAM10ATqA5dIf6rNAM1dgw8NlsLx1uAr3-EFBqmLk,7498
|
22
|
+
pyerualjetwork-4.3.9b8.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
23
|
+
pyerualjetwork-4.3.9b8.dist-info/top_level.txt,sha256=BRyt62U_r3ZmJpj-wXNOoA345Bzamrj6RbaWsyW4tRg,15
|
24
|
+
pyerualjetwork-4.3.9b8.dist-info/RECORD,,
|
pyerualjetwork/loss_functions.py
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
|
2
|
-
import numpy as np
|
3
|
-
|
4
|
-
def categorical_crossentropy(y_true_batch, y_pred_batch):
|
5
|
-
epsilon = 1e-7
|
6
|
-
y_pred_batch = np.clip(y_pred_batch, epsilon, 1. - epsilon)
|
7
|
-
|
8
|
-
losses = -np.sum(y_true_batch * np.log(y_pred_batch), axis=1)
|
9
|
-
|
10
|
-
mean_loss = np.mean(losses)
|
11
|
-
return mean_loss
|
12
|
-
|
13
|
-
|
14
|
-
def binary_crossentropy(y_true_batch, y_pred_batch):
|
15
|
-
epsilon = 1e-7
|
16
|
-
y_pred_batch = np.clip(y_pred_batch, epsilon, 1. - epsilon)
|
17
|
-
|
18
|
-
losses = -np.mean(y_true_batch * np.log(y_pred_batch) + (1 - y_true_batch) * np.log(1 - y_pred_batch), axis=1)
|
19
|
-
|
20
|
-
mean_loss = np.mean(losses)
|
21
|
-
return mean_loss
|
@@ -1,21 +0,0 @@
|
|
1
|
-
|
2
|
-
import cupy as cp
|
3
|
-
|
4
|
-
def categorical_crossentropy(y_true_batch, y_pred_batch):
|
5
|
-
epsilon = 1e-7
|
6
|
-
y_pred_batch = cp.clip(y_pred_batch, epsilon, 1. - epsilon)
|
7
|
-
|
8
|
-
losses = -cp.sum(y_true_batch * cp.log(y_pred_batch), axis=1)
|
9
|
-
|
10
|
-
mean_loss = cp.mean(losses)
|
11
|
-
return mean_loss
|
12
|
-
|
13
|
-
|
14
|
-
def binary_crossentropy(y_true_batch, y_pred_batch):
|
15
|
-
epsilon = 1e-7
|
16
|
-
y_pred_batch = cp.clip(y_pred_batch, epsilon, 1. - epsilon)
|
17
|
-
|
18
|
-
losses = -cp.mean(y_true_batch * cp.log(y_pred_batch) + (1 - y_true_batch) * cp.log(1 - y_pred_batch), axis=1)
|
19
|
-
|
20
|
-
mean_loss = cp.mean(losses)
|
21
|
-
return mean_loss
|
File without changes
|
File without changes
|