pyerualjetwork 4.1.9b2__py3-none-any.whl → 4.2.0b0__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/data_operations.py +1 -1
- pyerualjetwork/plan.py +1 -10
- pyerualjetwork/planeat.py +170 -172
- pyerualjetwork/planeat_cuda.py +1 -1
- {pyerualjetwork-4.1.9b2.dist-info → pyerualjetwork-4.2.0b0.dist-info}/METADATA +1 -1
- {pyerualjetwork-4.1.9b2.dist-info → pyerualjetwork-4.2.0b0.dist-info}/RECORD +9 -9
- {pyerualjetwork-4.1.9b2.dist-info → pyerualjetwork-4.2.0b0.dist-info}/WHEEL +0 -0
- {pyerualjetwork-4.1.9b2.dist-info → pyerualjetwork-4.2.0b0.dist-info}/top_level.txt +0 -0
pyerualjetwork/__init__.py
CHANGED
@@ -48,7 +48,7 @@ for package_name in package_names:
|
|
48
48
|
|
49
49
|
print(f"PyerualJetwork is ready to use with {err} errors")
|
50
50
|
|
51
|
-
__version__ = "4.
|
51
|
+
__version__ = "4.2.0b0"
|
52
52
|
__update__ = "* Changes: https://github.com/HCB06/PyerualJetwork/blob/main/CHANGES\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"
|
53
53
|
|
54
54
|
def print_version(__version__):
|
@@ -11,7 +11,7 @@ def encode_one_hot(y_train, y_test=None, summary=False):
|
|
11
11
|
Args:
|
12
12
|
y_train (numpy.ndarray): Train label data.
|
13
13
|
y_test (numpy.ndarray): Test label data one-hot encoded. (optional).
|
14
|
-
summary (bool): If True, prints the class-to-index mapping. Default: False
|
14
|
+
summary (bool, optional): If True, prints the class-to-index mapping. Default: False
|
15
15
|
|
16
16
|
Returns:
|
17
17
|
tuple: One-hot encoded y_train and (if given) y_test.
|
pyerualjetwork/plan.py
CHANGED
@@ -177,7 +177,7 @@ def learner(x_train, y_train, optimizer, x_test=None, y_test=None, strategy='acc
|
|
177
177
|
neural_web_history=False, show_current_activations=False, auto_normalization=True,
|
178
178
|
neurons_history=False, early_stop=False, loss='categorical_crossentropy', show_history=False,
|
179
179
|
interval=33.33, target_acc=None, target_loss=None, except_this=None,
|
180
|
-
only_this=None, start_this_act=None, start_this_W=None, target_fitness='max',
|
180
|
+
only_this=None, start_this_act=None, start_this_W=None, target_fitness='max', dtype=np.float32):
|
181
181
|
"""
|
182
182
|
Optimizes the activation functions for a neural network by leveraging train data to find
|
183
183
|
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.
|
@@ -282,15 +282,6 @@ def learner(x_train, y_train, optimizer, x_test=None, y_test=None, strategy='acc
|
|
282
282
|
if gen is None:
|
283
283
|
gen = len(activation_potentiation)
|
284
284
|
|
285
|
-
default_act_list_length = len(activation_potentiation)
|
286
|
-
|
287
|
-
if pop_size is not None and pop_size > len(activation_potentiation):
|
288
|
-
for i in range(pop_size - default_act_list_length):
|
289
|
-
rand_index = random.randint(0, default_act_list_length-1)
|
290
|
-
activation_potentiation.append(all_activations()[rand_index])
|
291
|
-
|
292
|
-
elif pop_size is not None and pop_size < len(activation_potentiation): raise ValueError(f"'pop_size' must be greater then activation_potentiation list length. But your act pot list length: {len(activation_potentiation)}")
|
293
|
-
|
294
285
|
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'.")
|
295
286
|
|
296
287
|
if start_this_act is None and len(activation_potentiation) % 2 != 0: raise ValueError("Activation length must be even number. Please use 'except_this' parameter and except some activation. For example: except_this=['linear']")
|
pyerualjetwork/planeat.py
CHANGED
@@ -12,7 +12,7 @@ ANAPLAN document: https://github.com/HCB06/Anaplan/blob/main/Welcome_to_Anaplan/
|
|
12
12
|
|
13
13
|
import numpy as np
|
14
14
|
import random
|
15
|
-
|
15
|
+
import math
|
16
16
|
|
17
17
|
### LIBRARY IMPORTS ###
|
18
18
|
from .plan import feed_forward
|
@@ -69,7 +69,7 @@ def define_genomes(input_shape, output_shape, population_size, dtype=np.float32)
|
|
69
69
|
return np.array(population_weights, dtype=dtype), population_activations
|
70
70
|
|
71
71
|
|
72
|
-
def
|
72
|
+
def evolver(weights, activation_potentiations, what_gen, fitness, show_info=False, strategy='cross_over', bad_genomes_selection_prob=None, bar_status=True, policy='normal_selective', target_fitness='max', mutations=True, bad_genomes_mutation_prob=None, activation_mutate_prob=0.5, save_best_genom=True, cross_over_mode='tpm', activation_add_prob=0.5, activation_delete_prob=0.5, activation_change_prob=0.5, weight_mutate_prob=1, weight_mutate_rate=32, activation_selection_add_prob=0.7, activation_selection_change_prob=0.5, activation_selection_rate=2, dtype=np.float32):
|
73
73
|
"""
|
74
74
|
Applies the evolving process of a population of genomes using selection, crossover, mutation, and activation function potentiation.
|
75
75
|
The function modifies the population's weights and activation functions based on a specified policy, mutation probabilities, and strategy.
|
@@ -109,7 +109,7 @@ Args:
|
|
109
109
|
mutations (bool, optional): If True, mutations are applied to the bad genomes and potentially
|
110
110
|
to the best genomes as well. Default is True.
|
111
111
|
|
112
|
-
|
112
|
+
bad_genomes_mutation_prob (float, optional): The probability of applying mutation to the bad genomes.
|
113
113
|
Must be in the range [0, 1]. Also affects the mutation probability of the best genomes inversely.
|
114
114
|
For example, a value of 0.7 for bad genomes implies 0.3 for best genomes. Default is None,
|
115
115
|
which means it is determined by the `policy` argument.
|
@@ -156,7 +156,7 @@ Raises:
|
|
156
156
|
ValueError:
|
157
157
|
- If `policy` is not one of the specified values ('normal_selective', 'more_selective', 'less_selective').
|
158
158
|
- If `cross_over_mode` is not one of the specified values ('tpm', 'plantic').
|
159
|
-
- If `
|
159
|
+
- If `bad_genomes_mutation_prob`, `activation_mutate_prob`, or other probability parameters are not in the range [0, 1].
|
160
160
|
- If the population size is odd (ensuring an even number of genomes is required for proper selection).
|
161
161
|
|
162
162
|
Returns:
|
@@ -176,7 +176,7 @@ Notes:
|
|
176
176
|
|
177
177
|
- **Mutation**:
|
178
178
|
- Mutation is applied to both the best and bad genomes, depending on the mutation probability and the `policy`.
|
179
|
-
- `
|
179
|
+
- `bad_genomes_mutation_prob` determines the probability of applying mutations to the bad genomes.
|
180
180
|
- If `activation_mutate_prob` is provided, activation function mutations are applied to the genomes based on this probability.
|
181
181
|
|
182
182
|
- **Population Size**: The population size must be an even number to properly split the best and bad genomes. If `fitness` has an odd length, an error is raised.
|
@@ -194,16 +194,16 @@ Example:
|
|
194
194
|
### ERROR AND CONFIGURATION CHECKS:
|
195
195
|
|
196
196
|
if policy == 'normal_selective':
|
197
|
-
if
|
198
|
-
|
197
|
+
if bad_genomes_mutation_prob is None: bad_genomes_mutation_prob = 0.7
|
198
|
+
if bad_genomes_selection_prob is None: bad_genomes_selection_prob = 0.25
|
199
199
|
|
200
200
|
elif policy == 'more_selective':
|
201
|
-
if
|
202
|
-
|
201
|
+
if bad_genomes_mutation_prob is None: bad_genomes_mutation_prob = 0.85
|
202
|
+
if bad_genomes_selection_prob is None: bad_genomes_selection_prob = 0.1
|
203
203
|
|
204
204
|
elif policy == 'less_selective':
|
205
|
-
if
|
206
|
-
|
205
|
+
if bad_genomes_mutation_prob is None: bad_genomes_mutation_prob = 0.6
|
206
|
+
if bad_genomes_selection_prob is None: bad_genomes_selection_prob = 0.5
|
207
207
|
|
208
208
|
else:
|
209
209
|
raise ValueError("policy parameter must be: 'normal_selective' or 'more_selective' or 'less_selective'")
|
@@ -212,12 +212,9 @@ Example:
|
|
212
212
|
if (activation_add_prob < 0 or activation_add_prob > 1) or (activation_change_prob < 0 or activation_change_prob > 1) or (activation_delete_prob < 0 or activation_delete_prob > 1) or (weight_mutate_prob < 0 or weight_mutate_prob > 1) or (activation_selection_add_prob < 0 or activation_selection_add_prob > 1) or (activation_selection_change_prob < 0 or activation_selection_change_prob > 1):
|
213
213
|
raise ValueError("All hyperparameters ending with 'prob' must be a number between 0 and 1.")
|
214
214
|
|
215
|
-
if
|
216
|
-
|
217
|
-
|
218
|
-
if bad_genoms_mutation_prob is not None:
|
219
|
-
if not isinstance(bad_genoms_mutation_prob, float) or bad_genoms_mutation_prob < 0 or bad_genoms_mutation_prob > 1:
|
220
|
-
raise ValueError("bad_genoms_mutation_prob parameter must be float and 0-1 range")
|
215
|
+
if bad_genomes_mutation_prob is not None:
|
216
|
+
if not isinstance(bad_genomes_mutation_prob, float) or bad_genomes_mutation_prob < 0 or bad_genomes_mutation_prob > 1:
|
217
|
+
raise ValueError("bad_genomes_mutation_prob parameter must be float and 0-1 range")
|
221
218
|
|
222
219
|
if activation_mutate_prob is not None:
|
223
220
|
if not isinstance(activation_mutate_prob, float) or activation_mutate_prob < 0 or activation_mutate_prob > 1:
|
@@ -242,90 +239,86 @@ Example:
|
|
242
239
|
|
243
240
|
### GENOMES ARE DIVIDED INTO TWO GROUPS: GOOD GENOMES AND BAD GENOMES:
|
244
241
|
|
245
|
-
|
242
|
+
good_weights = weights[slice_center:]
|
246
243
|
bad_weights = weights[:slice_center]
|
247
|
-
|
244
|
+
best_weights = good_weights[-1]
|
248
245
|
|
249
|
-
|
246
|
+
good_activations = list(activation_potentiations[slice_center:])
|
250
247
|
bad_activations = list(activation_potentiations[:slice_center])
|
251
|
-
|
248
|
+
best_activations = good_activations[-1]
|
252
249
|
|
253
250
|
|
254
|
-
###
|
251
|
+
### PLANEAT IS APPLIED ACCORDING TO THE SPECIFIED POLICY, STRATEGY, AND PROBABILITY CONFIGURATION:
|
255
252
|
|
256
253
|
bar_format = loading_bars()[0]
|
257
254
|
|
258
|
-
if bar_status: progress = initialize_loading_bar(len(bad_weights), desc="GENERATION: " + str(what_gen), bar_format=bar_format, ncols=50
|
255
|
+
if bar_status: progress = initialize_loading_bar(len(bad_weights), desc="GENERATION: " + str(what_gen), bar_format=bar_format, ncols=50)
|
256
|
+
normalized_fitness = normalization(fitness, dtype=dtype)
|
257
|
+
|
258
|
+
best_fitness = normalized_fitness[-1]
|
259
259
|
|
260
260
|
for i in range(len(bad_weights)):
|
261
|
+
second_parent_W, second_parent_act, s_i = second_parent_selection(good_weights, bad_weights, good_activations, bad_activations, bad_genomes_selection_prob)
|
261
262
|
|
262
|
-
if policy == 'normal_selective':
|
263
|
-
|
263
|
+
if policy == 'normal_selective' or policy == 'less_selective':
|
264
264
|
if strategy == 'cross_over':
|
265
|
-
bad_weights[i], bad_activations[i] = cross_over(
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
if mutation_prob > bad_genoms_mutation_prob:
|
277
|
-
if (save_best_genom == True and not np.array_equal(best_weights[i], best_weight)) or save_best_genom == False:
|
278
|
-
best_weights[i], best_activations[i] = mutation(best_weights[i], best_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
|
265
|
+
bad_weights[i], bad_activations[i] = cross_over(good_weights[i],
|
266
|
+
second_parent_W,
|
267
|
+
good_activations[i],
|
268
|
+
second_parent_act,
|
269
|
+
cross_over_mode=cross_over_mode,
|
270
|
+
activation_selection_add_prob=activation_selection_add_prob,
|
271
|
+
activation_selection_change_prob=activation_selection_change_prob,
|
272
|
+
activation_selection_rate=activation_selection_rate,
|
273
|
+
first_parent_fitness=normalized_fitness[i],
|
274
|
+
second_parent_fitness=normalized_fitness[s_i]
|
275
|
+
)
|
279
276
|
|
280
|
-
|
281
|
-
bad_weights[i], bad_activations[i] = mutation(bad_weights[i], bad_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
|
282
|
-
|
277
|
+
|
283
278
|
if policy == 'more_selective':
|
284
|
-
|
285
|
-
|
286
|
-
|
279
|
+
if strategy == 'cross_over':
|
280
|
+
bad_weights[i], bad_activations[i] = cross_over(best_weights,
|
281
|
+
second_parent_W,
|
282
|
+
best_activations,
|
283
|
+
second_parent_act,
|
284
|
+
cross_over_mode=cross_over_mode,
|
285
|
+
activation_selection_add_prob=activation_selection_add_prob,
|
286
|
+
activation_selection_change_prob=activation_selection_change_prob,
|
287
|
+
activation_selection_rate=activation_selection_rate,
|
288
|
+
first_parent_fitness=best_fitness,
|
289
|
+
second_parent_fitness=normalized_fitness[s_i]
|
290
|
+
)
|
291
|
+
|
292
|
+
if mutations is True:
|
293
|
+
mutation_prob = random.uniform(0, 1)
|
294
|
+
|
295
|
+
if mutation_prob > bad_genomes_mutation_prob:
|
296
|
+
if (save_best_genom == True and not np.array_equal(good_weights[i], best_weights)) or save_best_genom == False:
|
297
|
+
good_weights[i], good_activations[i] = mutation(good_weights[i],
|
298
|
+
good_activations[i],
|
299
|
+
activation_mutate_prob=activation_mutate_prob,
|
300
|
+
activation_add_prob=activation_add_prob,
|
301
|
+
activation_delete_prob=activation_delete_prob,
|
302
|
+
activation_change_prob=activation_change_prob,
|
303
|
+
weight_mutate_prob=weight_mutate_prob,
|
304
|
+
threshold=weight_mutate_rate,
|
305
|
+
dtype=dtype)
|
287
306
|
|
288
|
-
|
289
|
-
|
307
|
+
elif mutation_prob < bad_genomes_mutation_prob:
|
308
|
+
bad_weights[i], bad_activations[i] = mutation(bad_weights[i],
|
309
|
+
bad_activations[i],
|
310
|
+
activation_mutate_prob=activation_mutate_prob,
|
311
|
+
activation_add_prob=activation_add_prob,
|
312
|
+
activation_delete_prob=activation_delete_prob,
|
313
|
+
activation_change_prob=activation_change_prob,
|
314
|
+
weight_mutate_prob=weight_mutate_prob,
|
315
|
+
threshold=weight_mutate_rate,
|
316
|
+
dtype=dtype)
|
290
317
|
|
291
|
-
if mutations is True:
|
292
|
-
|
293
|
-
mutation_prob = random.uniform(0, 1)
|
294
|
-
|
295
|
-
if mutation_prob > bad_genoms_mutation_prob:
|
296
|
-
if (save_best_genom == True and not np.array_equal(best_weights[i], best_weight)) or save_best_genom == False:
|
297
|
-
best_weights[i], best_activations[i] = mutation(best_weights[i], best_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
|
298
|
-
|
299
|
-
elif mutation_prob < bad_genoms_mutation_prob:
|
300
|
-
bad_weights[i], bad_activations[i] = mutation(bad_weights[i], bad_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
if policy == 'less_selective':
|
305
|
-
|
306
|
-
random_index = int(random.uniform(0, len(best_weights) - 1))
|
307
|
-
|
308
|
-
if strategy == 'cross_over':
|
309
|
-
bad_weights[i], bad_activations[i] = cross_over(best_weights[random_index], best_weights[i], best_activations=best_activations[random_index], good_activations=best_activations[i], cross_over_mode=cross_over_mode, activation_selection_add_prob=activation_selection_add_prob, activation_selection_change_prob=activation_selection_change_prob, activation_selection_rate=activation_selection_rate)
|
310
|
-
|
311
|
-
elif strategy == 'potentiate':
|
312
|
-
bad_weights[i], bad_activations[i] = potentiate(best_weights[random_index], best_weights[i], best_activations=best_activations[random_index], good_activations=best_activations[i])
|
313
|
-
|
314
|
-
if mutations is True:
|
315
|
-
|
316
|
-
mutation_prob = random.uniform(0, 1)
|
317
|
-
|
318
|
-
if mutation_prob > bad_genoms_mutation_prob:
|
319
|
-
if (save_best_genom == True and not np.array_equal(best_weights[i], best_weight)) or save_best_genom == False:
|
320
|
-
best_weights[i], best_activations[i] = mutation(best_weights[i], best_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
|
321
|
-
|
322
|
-
elif mutation_prob < bad_genoms_mutation_prob:
|
323
|
-
bad_weights[i], bad_activations[i] = mutation(bad_weights[i], bad_activations[i], activation_mutate_prob=activation_mutate_prob, activation_add_prob=activation_add_prob, activation_delete_prob=activation_delete_prob, activation_change_prob=activation_change_prob, weight_mutate_prob=weight_mutate_prob, threshold=weight_mutate_rate, dtype=dtype)
|
324
|
-
|
325
318
|
if bar_status: progress.update(1)
|
326
319
|
|
327
|
-
weights = np.vstack((bad_weights,
|
328
|
-
activation_potentiations = bad_activations +
|
320
|
+
weights = np.vstack((bad_weights, good_weights))
|
321
|
+
activation_potentiations = bad_activations + good_activations
|
329
322
|
|
330
323
|
### INFO PRINTING CONSOLE
|
331
324
|
|
@@ -340,8 +333,8 @@ Example:
|
|
340
333
|
|
341
334
|
print(" POLICY: ", policy)
|
342
335
|
print(" MUTATIONS: ", str(mutations))
|
343
|
-
print(" BAD GENOMES MUTATION PROB: ", str(
|
344
|
-
print(" GOOD GENOMES MUTATION PROB: ", str(round(1 -
|
336
|
+
print(" BAD GENOMES MUTATION PROB: ", str(bad_genomes_mutation_prob))
|
337
|
+
print(" GOOD GENOMES MUTATION PROB: ", str(round(1 - bad_genomes_mutation_prob, 2)))
|
345
338
|
print(" WEIGHT MUTATE PROB: ", str(weight_mutate_prob))
|
346
339
|
print(" WEIGHT MUTATE RATE (THRESHOLD VALUE FOR SINGLE MUTATION): ", str(weight_mutate_rate))
|
347
340
|
print(" ACTIVATION MUTATE PROB: ", str(activation_mutate_prob))
|
@@ -435,23 +428,30 @@ def evaluate(x_population, weights, activation_potentiations, rl_mode=False, dty
|
|
435
428
|
return outputs
|
436
429
|
|
437
430
|
|
438
|
-
def cross_over(
|
431
|
+
def cross_over(first_parent_W,
|
432
|
+
second_parent_W,
|
433
|
+
first_parent_act,
|
434
|
+
second_parent_act,
|
435
|
+
cross_over_mode,
|
436
|
+
activation_selection_add_prob,
|
437
|
+
activation_selection_change_prob,
|
438
|
+
activation_selection_rate,
|
439
|
+
bad_genomes_selection_prob,
|
440
|
+
first_parent_fitness,
|
441
|
+
second_parent_fitness):
|
439
442
|
"""
|
440
443
|
Performs a selected Crossover operation on two sets of weights and activation functions.
|
441
444
|
This function combines two individuals (represented by their weights and activation functions)
|
442
445
|
to create a new individual by exchanging parts of their weight matrices and activation functions.
|
443
446
|
|
444
447
|
Args:
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
448
|
+
first_parent_W (numpy.ndarray): The weight matrix of the first individual (parent).
|
449
|
+
second_parent_W (numpy.ndarray): The weight matrix of the second individual (parent).
|
450
|
+
first_parent_act (str or list): The activation function(s) of the first individual.
|
451
|
+
second_parent_act (str or list): The activation function(s) of the second individual.
|
449
452
|
cross_over_mode (str): Determines the crossover method to be used. Options:
|
450
453
|
- 'tpm': Two-Point Matrix Crossover, where sub-matrices of weights
|
451
454
|
are swapped between parents.
|
452
|
-
- 'plan': Output Connections Crossover, where specific connections
|
453
|
-
in the weight matrix are crossed over. Default is 'tpm'.
|
454
|
-
|
455
455
|
Returns:
|
456
456
|
tuple: A tuple containing:
|
457
457
|
- new_weight (numpy.ndarray): The weight matrix of the new individual created by crossover.
|
@@ -460,7 +460,6 @@ def cross_over(best_weight, good_weight, best_activations, good_activations, cro
|
|
460
460
|
Notes:
|
461
461
|
- The crossover is performed based on the selected `cross_over_mode`.
|
462
462
|
- In 'tpm', random sub-matrices from the parent weight matrices are swapped.
|
463
|
-
- In 'plantic', specific connections in the weight matrix are swapped between parents.
|
464
463
|
- The crossover operation combines the activation functions of both parents:
|
465
464
|
- If the activation functions are passed as strings, they are converted to lists for uniform handling.
|
466
465
|
- The resulting activation functions depend on the crossover method and the parent's configuration.
|
@@ -475,8 +474,29 @@ def cross_over(best_weight, good_weight, best_activations, good_activations, cro
|
|
475
474
|
|
476
475
|
start = 0
|
477
476
|
|
478
|
-
row_end =
|
479
|
-
col_end =
|
477
|
+
row_end = first_parent_W.shape[0]
|
478
|
+
col_end = first_parent_W.shape[1]
|
479
|
+
|
480
|
+
total_gene = row_end * col_end
|
481
|
+
half_of_gene = int(total_gene / 2)
|
482
|
+
|
483
|
+
decision = dominant_parent_selection(bad_genomes_selection_prob)
|
484
|
+
|
485
|
+
if decision == 'first_parent':
|
486
|
+
dominant_parent_W = np.copy(first_parent_W)
|
487
|
+
dominant_parent_act = first_parent_act
|
488
|
+
|
489
|
+
undominant_parent_W = np.copy(second_parent_W)
|
490
|
+
undominant_parent_act = second_parent_act
|
491
|
+
succes = abs(second_parent_fitness)
|
492
|
+
|
493
|
+
elif decision == 'second_parent':
|
494
|
+
dominant_parent_W = np.copy(second_parent_W)
|
495
|
+
dominant_parent_act = second_parent_act
|
496
|
+
|
497
|
+
undominant_parent_W = np.copy(first_parent_W)
|
498
|
+
undominant_parent_act = first_parent_act
|
499
|
+
succes = abs(first_parent_fitness)
|
480
500
|
|
481
501
|
while True:
|
482
502
|
|
@@ -486,48 +506,44 @@ def cross_over(best_weight, good_weight, best_activations, good_activations, cro
|
|
486
506
|
row_cut_end = int(random.uniform(start, row_end))
|
487
507
|
col_cut_end = int(random.uniform(start, col_end))
|
488
508
|
|
489
|
-
if (row_cut_end > row_cut_start) and
|
509
|
+
if ((row_cut_end > row_cut_start) and
|
510
|
+
(col_cut_end > col_cut_start) and
|
511
|
+
(((row_cut_end + 1) - (row_cut_start + 1) * 2) + ((col_cut_end + 1) - (col_cut_start + 1) * 2) <= half_of_gene)):
|
490
512
|
break
|
513
|
+
|
514
|
+
row_cut_start = math.floor(row_cut_start * succes)
|
515
|
+
row_cut_end = math.ceil(row_cut_end * succes)
|
491
516
|
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
if cross_over_mode == 'tpm':
|
496
|
-
new_weight[row_cut_start:row_cut_end, col_cut_start:col_cut_end] = best_w2[row_cut_start:row_cut_end, col_cut_start:col_cut_end]
|
497
|
-
|
498
|
-
elif cross_over_mode == 'plantic':
|
499
|
-
new_weight[row_cut_start:row_cut_end,:] = best_w2[row_cut_start:row_cut_end,:]
|
500
|
-
|
517
|
+
col_cut_start = math.floor(col_cut_start * succes)
|
518
|
+
col_cut_end = math.ceil(col_cut_end * succes)
|
501
519
|
|
502
|
-
|
503
|
-
best = [best_activations]
|
520
|
+
child_W = dominant_parent_W
|
504
521
|
|
505
|
-
if
|
506
|
-
|
522
|
+
if cross_over_mode == 'tpm':
|
523
|
+
child_W[row_cut_start:row_cut_end, col_cut_start:col_cut_end] = undominant_parent_W[row_cut_start:row_cut_end, col_cut_start:col_cut_end]
|
507
524
|
|
508
|
-
if isinstance(best_activations, list):
|
509
|
-
best = best_activations
|
510
525
|
|
511
|
-
if isinstance(
|
512
|
-
|
526
|
+
if isinstance(dominant_parent_act, str): dominant_parent_act = [dominant_parent_act]
|
527
|
+
if isinstance(undominant_parent_act, str): undominant_parent_act = [undominant_parent_act]
|
513
528
|
|
514
|
-
|
529
|
+
child_act = list(np.copy(dominant_parent_act))
|
515
530
|
|
516
531
|
activation_selection_add_prob = 1 - activation_selection_add_prob # if prob 0.8 (%80) then 1 - 0.8. Because 0-1 random number probably greater than 0.2
|
517
532
|
potential_activation_selection_add = random.uniform(0, 1)
|
518
533
|
|
519
534
|
if potential_activation_selection_add > activation_selection_add_prob:
|
520
535
|
|
536
|
+
activation_selection_rate = activation_selection_rate / succes
|
521
537
|
new_threshold = activation_selection_rate
|
522
538
|
|
523
539
|
while True:
|
524
540
|
|
525
|
-
|
526
|
-
|
541
|
+
random_index = int(random.uniform(0, len(undominant_parent_act)-1))
|
542
|
+
random_undominant_activation = undominant_parent_act[random_index]
|
527
543
|
|
528
|
-
|
544
|
+
child_act.append(random_undominant_activation)
|
529
545
|
|
530
|
-
if len(
|
546
|
+
if len(dominant_parent_act) > new_threshold:
|
531
547
|
new_threshold += activation_selection_rate
|
532
548
|
pass
|
533
549
|
|
@@ -538,67 +554,26 @@ def cross_over(best_weight, good_weight, best_activations, good_activations, cro
|
|
538
554
|
potential_activation_selection_change_prob = random.uniform(0, 1)
|
539
555
|
|
540
556
|
if potential_activation_selection_change_prob > activation_selection_change_prob:
|
541
|
-
|
557
|
+
|
558
|
+
activation_selection_rate = activation_selection_rate / succes
|
542
559
|
new_threshold = activation_selection_rate
|
543
560
|
|
544
561
|
while True:
|
545
562
|
|
546
|
-
|
547
|
-
|
548
|
-
|
563
|
+
random_index_undominant = int(random.uniform(0, len(undominant_parent_act)-1))
|
564
|
+
random_index_dominant = int(random.uniform(0, len(dominant_parent_act)-1))
|
565
|
+
random_undominant_activation = undominant_parent_act[random_index_undominant]
|
549
566
|
|
550
|
-
|
567
|
+
child_act[random_index_dominant] = random_undominant_activation
|
551
568
|
|
552
|
-
if len(
|
569
|
+
if len(dominant_parent_act) > new_threshold:
|
553
570
|
new_threshold += activation_selection_rate
|
554
571
|
pass
|
555
572
|
|
556
573
|
else:
|
557
574
|
break
|
558
575
|
|
559
|
-
return
|
560
|
-
|
561
|
-
def potentiate(best_weight, good_weight, best_activations, good_activations, dtype=np.float32):
|
562
|
-
"""
|
563
|
-
Combines two sets of weights and activation functions by adding the weight matrices and
|
564
|
-
concatenating the activation functions. The resulting weight matrix is normalized. (Max abs normalization.)
|
565
|
-
|
566
|
-
Args:
|
567
|
-
best_weight (numpy.ndarray): The weight matrix of the first individual (parent).
|
568
|
-
good_weight (numpy.ndarray): The weight matrix of the second individual (parent).
|
569
|
-
best_activations (str or list): The activation function(s) of the first individual.
|
570
|
-
good_activations (str or list): The activation function(s) of the second individual.
|
571
|
-
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)
|
572
|
-
|
573
|
-
Returns:
|
574
|
-
tuple: A tuple containing:
|
575
|
-
- new_weight (numpy.ndarray): The new weight matrix after potentiation and normalization. (Max abs normalization.)
|
576
|
-
- new_activations (list): The new activation functions after concatenation.
|
577
|
-
|
578
|
-
Notes:
|
579
|
-
- The weight matrices are element-wise added and then normalized using the `normalization` function. (Max abs normalization.)
|
580
|
-
- The activation functions from both parents are concatenated to form the new activation functions list.
|
581
|
-
- If the activation functions are passed as strings, they are converted to lists for uniform handling.
|
582
|
-
"""
|
583
|
-
|
584
|
-
new_weight = best_weight + good_weight
|
585
|
-
new_weight = normalization(new_weight, dtype=dtype)
|
586
|
-
|
587
|
-
if isinstance(best_activations, str):
|
588
|
-
best = [best_activations]
|
589
|
-
|
590
|
-
if isinstance(good_activations, str):
|
591
|
-
good = [good_activations]
|
592
|
-
|
593
|
-
if isinstance(best_activations, list):
|
594
|
-
best = best_activations
|
595
|
-
|
596
|
-
if isinstance(good_activations, list):
|
597
|
-
good = good_activations
|
598
|
-
|
599
|
-
new_activations = best + good
|
600
|
-
|
601
|
-
return new_weight, new_activations
|
576
|
+
return child_W, child_act
|
602
577
|
|
603
578
|
def mutation(weight, activations, activation_mutate_prob, activation_add_prob, activation_delete_prob, activation_change_prob, weight_mutate_prob, threshold, dtype=np.float32):
|
604
579
|
"""
|
@@ -659,8 +634,7 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
|
|
659
634
|
pass
|
660
635
|
|
661
636
|
else:
|
662
|
-
break
|
663
|
-
|
637
|
+
break
|
664
638
|
|
665
639
|
activation_mutate_prob = 1 - activation_mutate_prob
|
666
640
|
potential_activation_mutation = random.uniform(0, 1)
|
@@ -718,7 +692,7 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
|
|
718
692
|
activations.pop(random_index)
|
719
693
|
weight = normalization(weight, dtype=dtype)
|
720
694
|
|
721
|
-
|
695
|
+
|
722
696
|
if potential_activation_change_prob > activation_change_prob:
|
723
697
|
|
724
698
|
random_index_all_act = int(random.uniform(0, len(all_acts)-1))
|
@@ -740,4 +714,28 @@ def mutation(weight, activations, activation_mutate_prob, activation_add_prob, a
|
|
740
714
|
|
741
715
|
weight = normalization(weight, dtype=dtype)
|
742
716
|
|
743
|
-
return weight, activations
|
717
|
+
return weight, activations
|
718
|
+
|
719
|
+
def second_parent_selection(best_weights, bad_weights, best_activations, bad_activations, bad_genomes_selection_prob):
|
720
|
+
|
721
|
+
selection_prob = random.uniform(0, 1)
|
722
|
+
random_index = int(random.uniform(0, len(best_weights) - 1))
|
723
|
+
|
724
|
+
if selection_prob > bad_genomes_selection_prob:
|
725
|
+
second_selected_W = best_weights[random_index]
|
726
|
+
second_selected_act = best_activations[random_index]
|
727
|
+
|
728
|
+
else:
|
729
|
+
second_selected_W = bad_weights[random_index]
|
730
|
+
second_selected_act = bad_activations[random_index]
|
731
|
+
|
732
|
+
return second_selected_W, second_selected_act, random_index
|
733
|
+
|
734
|
+
def dominant_parent_selection(bad_genomes_selection_prob):
|
735
|
+
|
736
|
+
selection_prob = random.uniform(0, 1)
|
737
|
+
|
738
|
+
if selection_prob > bad_genomes_selection_prob: decision = 'first_parent'
|
739
|
+
else: decision = 'second_parent'
|
740
|
+
|
741
|
+
return decision
|
pyerualjetwork/planeat_cuda.py
CHANGED
@@ -255,7 +255,7 @@ Example:
|
|
255
255
|
|
256
256
|
bar_format = loading_bars()[0]
|
257
257
|
|
258
|
-
if bar_status: progress = initialize_loading_bar(len(bad_weights), desc="GENERATION: " + str(what_gen), bar_format=bar_format, ncols=50
|
258
|
+
if bar_status: progress = initialize_loading_bar(len(bad_weights), desc="GENERATION: " + str(what_gen), bar_format=bar_format, ncols=50)
|
259
259
|
|
260
260
|
for i in range(len(bad_weights)):
|
261
261
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: pyerualjetwork
|
3
|
-
Version: 4.
|
3
|
+
Version: 4.2.0b0
|
4
4
|
Summary: PyerualJetwork is a machine learning library written in Python for professionals, incorporating advanced, unique, new, and modern techniques.
|
5
5
|
Author: Hasan Can Beydili
|
6
6
|
Author-email: tchasancan@gmail.com
|
@@ -1,7 +1,7 @@
|
|
1
|
-
pyerualjetwork/__init__.py,sha256=
|
1
|
+
pyerualjetwork/__init__.py,sha256=Zu-KnB701xy5cpP7oARUTk8PhAwyDNzWRNcpeZSqno4,2177
|
2
2
|
pyerualjetwork/activation_functions.py,sha256=WWOdMd5pI6ZKe-ieKCIsKAYPQODHuXYxx7tzhA5xjes,11767
|
3
3
|
pyerualjetwork/activation_functions_cuda.py,sha256=KmXJ5Cdig46XAMYakXFPEOlxSxtFJjD21-i3nGtxPjE,11807
|
4
|
-
pyerualjetwork/data_operations.py,sha256=
|
4
|
+
pyerualjetwork/data_operations.py,sha256=HjyW2QE18age6J8iG0jpbwqGOylL_nM-vE2CLbP9Wes,14690
|
5
5
|
pyerualjetwork/data_operations_cuda.py,sha256=UpoJoFhIwTU4xg9dVuLAxLAT4CkRaGsxvtJG9j1xrNo,17629
|
6
6
|
pyerualjetwork/help.py,sha256=OZghUy7GZTgEX_i3NYtgcpzUgCDOi6r2vVUF1ROkFiI,774
|
7
7
|
pyerualjetwork/loss_functions.py,sha256=6PyBI232SQRGuFnG3LDGvnv_PUdWzT2_2mUODJiejGI,618
|
@@ -11,14 +11,14 @@ pyerualjetwork/metrics.py,sha256=q7MkhnZDRbCjFBDDfUgrl8lBYnUT_1ro1LxeBq105pI,607
|
|
11
11
|
pyerualjetwork/metrics_cuda.py,sha256=73h9GC7XwmnFCVzFEEiPQfF8CwHIz2wsCbxpZrJtYgw,5061
|
12
12
|
pyerualjetwork/model_operations.py,sha256=hnhR8dtoICNJWIwGgJ65-LN3GYN_DYH4LMe6YpZVbnI,12967
|
13
13
|
pyerualjetwork/model_operations_cuda.py,sha256=XnKKq54ZLaqCm-NaJ6d8IToACKcKg2Ttq6moowVRRWo,13365
|
14
|
-
pyerualjetwork/plan.py,sha256=
|
14
|
+
pyerualjetwork/plan.py,sha256=QwF7kAzEGSTbYaBHHjaPcqYsMfb2I9FGDykSouBhxmk,34335
|
15
15
|
pyerualjetwork/plan_cuda.py,sha256=KoKjsoWTLM-q07G1Gy0-LYXGlp15Fno6JqHz-Jzi_yE,35983
|
16
|
-
pyerualjetwork/planeat.py,sha256=
|
17
|
-
pyerualjetwork/planeat_cuda.py,sha256=
|
16
|
+
pyerualjetwork/planeat.py,sha256=AkiZSK3fxsNBZircylV8JNmA5987qyiinjRcuLZyXL0,39166
|
17
|
+
pyerualjetwork/planeat_cuda.py,sha256=mxh5DzJvSwNJ_nc4b_SNMDDIoyBistjK5IjjpOJR3hM,40221
|
18
18
|
pyerualjetwork/ui.py,sha256=wu2BhU1k-w3Kcho5Jtq4SEKe68ftaUeRGneUOSCVDjU,575
|
19
19
|
pyerualjetwork/visualizations.py,sha256=QaYSIyVkJZ8NqpBKArQKkI1y37nCQo_KIM98IMssnRc,28766
|
20
20
|
pyerualjetwork/visualizations_cuda.py,sha256=F60vQ92AXlMgBka3InXnOtGoM25vQJAlBIU2AlYTwks,29200
|
21
|
-
pyerualjetwork-4.
|
22
|
-
pyerualjetwork-4.
|
23
|
-
pyerualjetwork-4.
|
24
|
-
pyerualjetwork-4.
|
21
|
+
pyerualjetwork-4.2.0b0.dist-info/METADATA,sha256=aC9Bzvxyc8bhOawSkrf2Y9eBJHLf2VgnwiuTOCROwZE,7795
|
22
|
+
pyerualjetwork-4.2.0b0.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
|
23
|
+
pyerualjetwork-4.2.0b0.dist-info/top_level.txt,sha256=BRyt62U_r3ZmJpj-wXNOoA345Bzamrj6RbaWsyW4tRg,15
|
24
|
+
pyerualjetwork-4.2.0b0.dist-info/RECORD,,
|
File without changes
|
File without changes
|