Skip to content

Commit 43fd38a

Browse files
authored
Add files via upload
1 parent ad3d443 commit 43fd38a

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

docs/source/README_pygad_gacnn_ReadTheDocs.rst

+48-47
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _header-n0:
1+
.. _header-n176:
22

33
``pygad.gacnn`` Module
44
======================
@@ -10,7 +10,7 @@ The ``pygad.gacnn`` module trains convolutional neural networks using
1010
the genetic algorithm. It makes use of the 2 modules ``pygad`` and
1111
``pygad.cnn``.
1212

13-
.. _header-n4:
13+
.. _header-n180:
1414

1515
``pygad.gacnn.GACNN`` Class
1616
===========================
@@ -20,7 +20,7 @@ training convolutional neural networks (CNNs) using the genetic
2020
algorithm. The constructor, methods, function, and attributes within the
2121
class are discussed in this section.
2222

23-
.. _header-n6:
23+
.. _header-n182:
2424

2525
``__init__()``
2626
--------------
@@ -39,7 +39,7 @@ parameters:
3939
CNNs are created where their parameters are optimized using the
4040
genetic algorithm.
4141

42-
.. _header-n14:
42+
.. _header-n190:
4343

4444
Instance Attributes
4545
-------------------
@@ -52,15 +52,15 @@ which is:
5252
- ``population_networks``: A list holding references to all the
5353
solutions (i.e. CNNs) used in the population.
5454

55-
.. _header-n19:
55+
.. _header-n195:
5656

5757
Methods in the GACNN Class
5858
--------------------------
5959

6060
This section discusses the methods available for instances of the
6161
``pygad.gacnn.GACNN`` class.
6262

63-
.. _header-n21:
63+
.. _header-n197:
6464

6565
``create_population()``
6666
~~~~~~~~~~~~~~~~~~~~~~~
@@ -72,7 +72,7 @@ are copied from the CNN model passed to constructor of the GACNN class.
7272
The list of networks is assigned to the ``population_networks``
7373
attribute of the instance.
7474

75-
.. _header-n24:
75+
.. _header-n200:
7676

7777
``update_population_trained_weights()``
7878
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -89,14 +89,14 @@ Accepts the following parameters:
8989
all networks as matrices. Such matrices are to be assigned to the
9090
``trained_weights`` attribute of all layers of all networks.
9191

92-
.. _header-n30:
92+
.. _header-n206:
9393

9494
Functions in the ``pygad.gacnn`` Module
9595
=======================================
9696

9797
This section discusses the functions in the ``pygad.gacnn`` module.
9898

99-
.. _header-n32:
99+
.. _header-n208:
100100

101101
``pygad.gacnn.population_as_vectors()``
102102
----------------------------------------
@@ -118,7 +118,7 @@ Accepts the following parameters:
118118
Returns a list holding the weights vectors for all solutions (i.e.
119119
networks).
120120

121-
.. _header-n40:
121+
.. _header-n216:
122122

123123
``pygad.gacnn.population_as_matrices()``
124124
----------------------------------------
@@ -142,7 +142,7 @@ Accepts the following parameters:
142142
Returns a list holding the weights matrices for all solutions (i.e.
143143
networks).
144144

145-
.. _header-n50:
145+
.. _header-n226:
146146

147147
Steps to Build and Train CNN using Genetic Algorithm
148148
====================================================
@@ -174,7 +174,7 @@ using the genetic algorithm are as follows:
174174

175175
Let's start covering all of these steps.
176176

177-
.. _header-n76:
177+
.. _header-n252:
178178

179179
Prepare the Training Data
180180
-------------------------
@@ -215,10 +215,10 @@ elements in the output array must range from 0 to 4 inclusive.
215215
Generally, the class labels start from ``0`` to ``N-1`` where ``N`` is
216216
the number of classes.
217217

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.
220220

221-
.. _header-n89:
221+
.. _header-n265:
222222

223223
Building the Network Architecture
224224
---------------------------------
@@ -246,7 +246,7 @@ Here is an example for a CNN architecture.
246246
After the network architecture is prepared, the next step is to create a
247247
CNN model.
248248

249-
.. _header-n93:
249+
.. _header-n269:
250250

251251
Building Model
252252
--------------
@@ -263,7 +263,7 @@ class. Here is an example.
263263
After the model is created, a summary of the model architecture can be
264264
printed.
265265

266-
.. _header-n97:
266+
.. _header-n273:
267267

268268
Model Summary
269269
-------------
@@ -287,7 +287,7 @@ summary of the CNN model.
287287
The next step is to create an instance of the ``pygad.gacnn.GACNN``
288288
class.
289289

290-
.. _header-n102:
290+
.. _header-n278:
291291

292292
Create an Instance of the ``pygad.gacnn.GACNN`` Class
293293
-----------------------------------------------------
@@ -311,7 +311,7 @@ specified by the ``model`` parameter.
311311
After creating the instance of the ``pygad.gacnn.GACNN`` class, next is
312312
to fetch the weights of the population as a list of vectors.
313313

314-
.. _header-n107:
314+
.. _header-n283:
315315

316316
Fetch the Population Weights as Vectors
317317
---------------------------------------
@@ -343,7 +343,7 @@ prepare 2 functions which are:
343343

344344
2. Callback function after each generation.
345345

346-
.. _header-n120:
346+
.. _header-n296:
347347

348348
Prepare the Fitness Function
349349
----------------------------
@@ -383,7 +383,7 @@ fitness value is returned.
383383
384384
return solution_fitness
385385
386-
.. _header-n132:
386+
.. _header-n308:
387387

388388
Prepare the Generation Callback Function
389389
----------------------------------------
@@ -431,7 +431,7 @@ solutions within the population.
431431
After preparing the fitness and callback function, next is to create an
432432
instance of the ``pygad.GA`` class.
433433

434-
.. _header-n141:
434+
.. _header-n317:
435435

436436
Create an Instance of the ``pygad.GA`` Class
437437
--------------------------------------------
@@ -475,7 +475,7 @@ number of generations is 10.
475475
The last step for training the neural networks using the genetic
476476
algorithm is calling the ``run()`` method.
477477

478-
.. _header-n145:
478+
.. _header-n321:
479479

480480
Run the Created Instance of the ``pygad.GA`` Class
481481
--------------------------------------------------
@@ -488,7 +488,7 @@ specified in its ``num_generations`` parameter.
488488
489489
ga_instance.run()
490490
491-
.. _header-n148:
491+
.. _header-n324:
492492

493493
Plot the Fitness Values
494494
-----------------------
@@ -503,7 +503,7 @@ be called to show how the fitness values evolve by generation.
503503
.. figure:: https://user-images.githubusercontent.com/16560492/83429675-ab744580-a434-11ea-8f21-9d3804b50d15.png
504504
:alt:
505505

506-
.. _header-n152:
506+
.. _header-n328:
507507

508508
Information about the Best Solution
509509
-----------------------------------
@@ -534,7 +534,7 @@ Here is how such information is returned.
534534
Index of the best solution : 0
535535
Best fitness value reached after 4 generations.
536536
537-
.. _header-n164:
537+
.. _header-n340:
538538

539539
Making Predictions using the Trained Weights
540540
--------------------------------------------
@@ -548,7 +548,7 @@ the labels correctly.
548548
predictions = pygad.cnn.predict(last_layer=GANN_instance.population_networks[solution_idx], data_inputs=data_inputs)
549549
print("Predictions of the trained network : {predictions}".format(predictions=predictions))
550550
551-
.. _header-n167:
551+
.. _header-n343:
552552

553553
Calculating Some Statistics
554554
---------------------------
@@ -572,7 +572,7 @@ addition to the classification accuracy.
572572
Number of wrong classifications : 13.
573573
Classification accuracy : 83.75.
574574
575-
.. _header-n171:
575+
.. _header-n347:
576576

577577
Examples
578578
========
@@ -581,7 +581,7 @@ This section gives the complete code of some examples that build and
581581
train neural networks using the genetic algorithm. Each subsection
582582
builds a different network.
583583

584-
.. _header-n173:
584+
.. _header-n349:
585585

586586
Image Classification
587587
--------------------
@@ -618,12 +618,13 @@ complete code is listed below.
618618
def callback_generation(ga_instance):
619619
global GACNN_instance, last_fitness
620620
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,
622622
population_vectors=ga_instance.population)
623623
624624
GACNN_instance.update_population_trained_weights(population_trained_weights=population_matrices)
625625
626626
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
627+
print("Fitness = {fitness}".format(fitness=ga_instance.best_solutions_fitness))
627628
628629
data_inputs = numpy.load("dataset_inputs.npy")
629630
data_outputs = numpy.load("dataset_outputs.npy")
@@ -634,35 +635,35 @@ complete code is listed below.
634635
data_inputs = data_inputs
635636
data_outputs = data_outputs
636637
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)
645646
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")
650651
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)
654655
655656
model.summary()
656657
657658
658-
GACNN_instance = gacnn.GACNN(model=model,
659+
GACNN_instance = pygad.gacnn.GACNN(model=model,
659660
num_solutions=4)
660661
661662
# GACNN_instance.update_population_trained_weights(population_trained_weights=population_matrices)
662663
663664
# 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.
664665
# 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)
666667
667668
# To prepare the initial population, there are 2 ways:
668669
# 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

Comments
 (0)