1
- .. _header-n0 :
1
+ .. _header-n176 :
2
2
3
3
``pygad.gacnn `` Module
4
4
======================
@@ -10,7 +10,7 @@ The ``pygad.gacnn`` module trains convolutional neural networks using
10
10
the genetic algorithm. It makes use of the 2 modules ``pygad `` and
11
11
``pygad.cnn ``.
12
12
13
- .. _header-n4 :
13
+ .. _header-n180 :
14
14
15
15
``pygad.gacnn.GACNN `` Class
16
16
===========================
@@ -20,7 +20,7 @@ training convolutional neural networks (CNNs) using the genetic
20
20
algorithm. The constructor, methods, function, and attributes within the
21
21
class are discussed in this section.
22
22
23
- .. _header-n6 :
23
+ .. _header-n182 :
24
24
25
25
``__init__() ``
26
26
--------------
@@ -39,7 +39,7 @@ parameters:
39
39
CNNs are created where their parameters are optimized using the
40
40
genetic algorithm.
41
41
42
- .. _header-n14 :
42
+ .. _header-n190 :
43
43
44
44
Instance Attributes
45
45
-------------------
@@ -52,15 +52,15 @@ which is:
52
52
- ``population_networks ``: A list holding references to all the
53
53
solutions (i.e. CNNs) used in the population.
54
54
55
- .. _header-n19 :
55
+ .. _header-n195 :
56
56
57
57
Methods in the GACNN Class
58
58
--------------------------
59
59
60
60
This section discusses the methods available for instances of the
61
61
``pygad.gacnn.GACNN `` class.
62
62
63
- .. _header-n21 :
63
+ .. _header-n197 :
64
64
65
65
``create_population() ``
66
66
~~~~~~~~~~~~~~~~~~~~~~~
@@ -72,7 +72,7 @@ are copied from the CNN model passed to constructor of the GACNN class.
72
72
The list of networks is assigned to the ``population_networks ``
73
73
attribute of the instance.
74
74
75
- .. _header-n24 :
75
+ .. _header-n200 :
76
76
77
77
``update_population_trained_weights() ``
78
78
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -89,14 +89,14 @@ Accepts the following parameters:
89
89
all networks as matrices. Such matrices are to be assigned to the
90
90
``trained_weights `` attribute of all layers of all networks.
91
91
92
- .. _header-n30 :
92
+ .. _header-n206 :
93
93
94
94
Functions in the ``pygad.gacnn `` Module
95
95
=======================================
96
96
97
97
This section discusses the functions in the ``pygad.gacnn `` module.
98
98
99
- .. _header-n32 :
99
+ .. _header-n208 :
100
100
101
101
``pygad.gacnn.population_as_vectors() ``
102
102
----------------------------------------
@@ -118,7 +118,7 @@ Accepts the following parameters:
118
118
Returns a list holding the weights vectors for all solutions (i.e.
119
119
networks).
120
120
121
- .. _header-n40 :
121
+ .. _header-n216 :
122
122
123
123
``pygad.gacnn.population_as_matrices() ``
124
124
----------------------------------------
@@ -142,7 +142,7 @@ Accepts the following parameters:
142
142
Returns a list holding the weights matrices for all solutions (i.e.
143
143
networks).
144
144
145
- .. _header-n50 :
145
+ .. _header-n226 :
146
146
147
147
Steps to Build and Train CNN using Genetic Algorithm
148
148
====================================================
@@ -174,7 +174,7 @@ using the genetic algorithm are as follows:
174
174
175
175
Let's start covering all of these steps.
176
176
177
- .. _header-n76 :
177
+ .. _header-n252 :
178
178
179
179
Prepare the Training Data
180
180
-------------------------
@@ -215,10 +215,10 @@ elements in the output array must range from 0 to 4 inclusive.
215
215
Generally, the class labels start from ``0 `` to ``N-1 `` where ``N `` is
216
216
the number of classes.
217
217
218
- Note that the project only supports classification problems where each
219
- sample is assigned to only one class.
218
+ Note that the project only supports that each sample is assigned to only
219
+ one class.
220
220
221
- .. _header-n89 :
221
+ .. _header-n265 :
222
222
223
223
Building the Network Architecture
224
224
---------------------------------
@@ -246,7 +246,7 @@ Here is an example for a CNN architecture.
246
246
After the network architecture is prepared, the next step is to create a
247
247
CNN model.
248
248
249
- .. _header-n93 :
249
+ .. _header-n269 :
250
250
251
251
Building Model
252
252
--------------
@@ -263,7 +263,7 @@ class. Here is an example.
263
263
After the model is created, a summary of the model architecture can be
264
264
printed.
265
265
266
- .. _header-n97 :
266
+ .. _header-n273 :
267
267
268
268
Model Summary
269
269
-------------
@@ -287,7 +287,7 @@ summary of the CNN model.
287
287
The next step is to create an instance of the ``pygad.gacnn.GACNN ``
288
288
class.
289
289
290
- .. _header-n102 :
290
+ .. _header-n278 :
291
291
292
292
Create an Instance of the ``pygad.gacnn.GACNN `` Class
293
293
-----------------------------------------------------
@@ -311,7 +311,7 @@ specified by the ``model`` parameter.
311
311
After creating the instance of the ``pygad.gacnn.GACNN `` class, next is
312
312
to fetch the weights of the population as a list of vectors.
313
313
314
- .. _header-n107 :
314
+ .. _header-n283 :
315
315
316
316
Fetch the Population Weights as Vectors
317
317
---------------------------------------
@@ -343,7 +343,7 @@ prepare 2 functions which are:
343
343
344
344
2. Callback function after each generation.
345
345
346
- .. _header-n120 :
346
+ .. _header-n296 :
347
347
348
348
Prepare the Fitness Function
349
349
----------------------------
@@ -383,7 +383,7 @@ fitness value is returned.
383
383
384
384
return solution_fitness
385
385
386
- .. _header-n132 :
386
+ .. _header-n308 :
387
387
388
388
Prepare the Generation Callback Function
389
389
----------------------------------------
@@ -431,7 +431,7 @@ solutions within the population.
431
431
After preparing the fitness and callback function, next is to create an
432
432
instance of the ``pygad.GA `` class.
433
433
434
- .. _header-n141 :
434
+ .. _header-n317 :
435
435
436
436
Create an Instance of the ``pygad.GA `` Class
437
437
--------------------------------------------
@@ -475,7 +475,7 @@ number of generations is 10.
475
475
The last step for training the neural networks using the genetic
476
476
algorithm is calling the ``run() `` method.
477
477
478
- .. _header-n145 :
478
+ .. _header-n321 :
479
479
480
480
Run the Created Instance of the ``pygad.GA `` Class
481
481
--------------------------------------------------
@@ -488,7 +488,7 @@ specified in its ``num_generations`` parameter.
488
488
489
489
ga_instance.run()
490
490
491
- .. _header-n148 :
491
+ .. _header-n324 :
492
492
493
493
Plot the Fitness Values
494
494
-----------------------
@@ -503,7 +503,7 @@ be called to show how the fitness values evolve by generation.
503
503
.. figure :: https://user-images.githubusercontent.com/16560492/83429675-ab744580-a434-11ea-8f21-9d3804b50d15.png
504
504
:alt:
505
505
506
- .. _header-n152 :
506
+ .. _header-n328 :
507
507
508
508
Information about the Best Solution
509
509
-----------------------------------
@@ -534,7 +534,7 @@ Here is how such information is returned.
534
534
Index of the best solution : 0
535
535
Best fitness value reached after 4 generations.
536
536
537
- .. _header-n164 :
537
+ .. _header-n340 :
538
538
539
539
Making Predictions using the Trained Weights
540
540
--------------------------------------------
@@ -548,7 +548,7 @@ the labels correctly.
548
548
predictions = pygad.cnn.predict(last_layer = GANN_instance .population_networks[solution_idx], data_inputs = data_inputs)
549
549
print (" Predictions of the trained network : {predictions} " .format(predictions = predictions))
550
550
551
- .. _header-n167 :
551
+ .. _header-n343 :
552
552
553
553
Calculating Some Statistics
554
554
---------------------------
@@ -572,7 +572,7 @@ addition to the classification accuracy.
572
572
Number of wrong classifications : 13.
573
573
Classification accuracy : 83.75.
574
574
575
- .. _header-n171 :
575
+ .. _header-n347 :
576
576
577
577
Examples
578
578
========
@@ -581,7 +581,7 @@ This section gives the complete code of some examples that build and
581
581
train neural networks using the genetic algorithm. Each subsection
582
582
builds a different network.
583
583
584
- .. _header-n173 :
584
+ .. _header-n349 :
585
585
586
586
Image Classification
587
587
--------------------
@@ -618,12 +618,13 @@ complete code is listed below.
618
618
def callback_generation (ga_instance ):
619
619
global GACNN_instance , last_fitness
620
620
621
- population_matrices = gacnn.population_as_matrices(population_networks = GACNN_instance .population_networks,
621
+ population_matrices = pygad. gacnn.population_as_matrices(population_networks = GACNN_instance .population_networks,
622
622
population_vectors = ga_instance.population)
623
623
624
624
GACNN_instance .update_population_trained_weights(population_trained_weights = population_matrices)
625
625
626
626
print (" Generation = {generation} " .format(generation = ga_instance.generations_completed))
627
+ print (" Fitness = {fitness} " .format(fitness = ga_instance.best_solutions_fitness))
627
628
628
629
data_inputs = numpy.load(" dataset_inputs.npy" )
629
630
data_outputs = numpy.load(" dataset_outputs.npy" )
@@ -634,35 +635,35 @@ complete code is listed below.
634
635
data_inputs = data_inputs
635
636
data_outputs = data_outputs
636
637
637
- input_layer = cnn.Input2D(input_shape = sample_shape)
638
- conv_layer1 = cnn.Conv2D(num_filters = 2 ,
639
- kernel_size = 3 ,
640
- previous_layer = input_layer,
641
- activation_function = " relu" )
642
- average_pooling_layer = cnn.AveragePooling2D(pool_size = 5 ,
643
- previous_layer = conv_layer1,
644
- stride = 3 )
638
+ input_layer = pygad. cnn.Input2D(input_shape = sample_shape)
639
+ conv_layer1 = pygad. cnn.Conv2D(num_filters = 2 ,
640
+ kernel_size = 3 ,
641
+ previous_layer = input_layer,
642
+ activation_function = " relu" )
643
+ average_pooling_layer = pygad. cnn.AveragePooling2D(pool_size = 5 ,
644
+ previous_layer = conv_layer1,
645
+ stride = 3 )
645
646
646
- flatten_layer = cnn.Flatten(previous_layer = average_pooling_layer)
647
- dense_layer2 = cnn.Dense(num_neurons = num_classes,
648
- previous_layer = flatten_layer,
649
- activation_function = " softmax" )
647
+ flatten_layer = pygad. cnn.Flatten(previous_layer = average_pooling_layer)
648
+ dense_layer2 = pygad. cnn.Dense(num_neurons = num_classes,
649
+ previous_layer = flatten_layer,
650
+ activation_function = " softmax" )
650
651
651
- model = cnn.Model(last_layer = dense_layer2,
652
- epochs = 1 ,
653
- learning_rate = 0.01 )
652
+ model = pygad. cnn.Model(last_layer = dense_layer2,
653
+ epochs = 1 ,
654
+ learning_rate = 0.01 )
654
655
655
656
model.summary()
656
657
657
658
658
- GACNN_instance = gacnn.GACNN(model = model,
659
+ GACNN_instance = pygad. gacnn.GACNN(model = model,
659
660
num_solutions = 4 )
660
661
661
662
# GACNN_instance.update_population_trained_weights(population_trained_weights=population_matrices)
662
663
663
664
# population does not hold the numerical weights of the network instead it holds a list of references to each last layer of each network (i.e. solution) in the population. A solution or a network can be used interchangeably.
664
665
# If there is a population with 3 solutions (i.e. networks), then the population is a list with 3 elements. Each element is a reference to the last layer of each network. Using such a reference, all details of the network can be accessed.
665
- population_vectors = gacnn.population_as_vectors(population_networks = GACNN_instance .population_networks)
666
+ population_vectors = pygad. gacnn.population_as_vectors(population_networks = GACNN_instance .population_networks)
666
667
667
668
# To prepare the initial population, there are 2 ways:
668
669
# 1) Prepare it yourself and pass it to the initial_population parameter. This way is useful when the user wants to start the genetic algorithm with a custom initial population.
0 commit comments