1- .. _header-n0 :
1+ .. _header-n194 :
22
33``pygad.kerasga `` Module
44========================
@@ -25,7 +25,7 @@ The contents of this module are:
2525
2626More details are given in the next sections.
2727
28- .. _header-n13 :
28+ .. _header-n207 :
2929
3030Steps Summary
3131=============
@@ -45,7 +45,7 @@ follows:
4545
46466. Run the genetic algorithm.
4747
48- .. _header-n28 :
48+ .. _header-n222 :
4949
5050Create Keras Model
5151==================
@@ -93,7 +93,7 @@ This is the same model created using the Functional API.
9393
9494 Feel free to add the layers of your choice.
9595
96- .. _header-n44 :
96+ .. _header-n238 :
9797
9898``pygad.kerasga.KerasGA `` Class
9999===============================
@@ -103,7 +103,7 @@ an initial population for the genetic algorithm based on a Keras model.
103103The constructor, methods, and attributes within the class are discussed
104104in this section.
105105
106- .. _header-n46 :
106+ .. _header-n240 :
107107
108108``__init__() ``
109109--------------
@@ -116,7 +116,7 @@ parameters:
116116- ``num_solutions ``: Number of solutions in the population. Each
117117 solution has different parameters of the model.
118118
119- .. _header-n53 :
119+ .. _header-n247 :
120120
121121Instance Attributes
122122-------------------
@@ -134,15 +134,15 @@ Here is a list of all instance attributes:
134134- ``population_weights ``: A nested list holding the weights of all
135135 solutions in the population.
136136
137- .. _header-n63 :
137+ .. _header-n257 :
138138
139139Methods in the ``KerasGA `` Class
140140--------------------------------
141141
142142This section discusses the methods available for instances of the
143143``pygad.kerasga.KerasGA `` class.
144144
145- .. _header-n65 :
145+ .. _header-n259 :
146146
147147``create_population() ``
148148~~~~~~~~~~~~~~~~~~~~~~~
@@ -152,14 +152,14 @@ genetic algorithm as a list of solutions where each solution represents
152152different model parameters. The list of networks is assigned to the
153153``population_weights `` attribute of the instance.
154154
155- .. _header-n67 :
155+ .. _header-n261 :
156156
157157Functions in the ``pygad.kerasga `` Module
158158=========================================
159159
160160This section discusses the functions in the ``pygad.kerasga `` module.
161161
162- .. _header-n69 :
162+ .. _header-n263 :
163163
164164``pygad.kerasga.model_weights_as_vector() ``
165165--------------------------------------------
@@ -170,13 +170,19 @@ holding all model weights. The reason for representing the model weights
170170as a vector is that the genetic algorithm expects all parameters of any
171171solution to be in a 1D vector form.
172172
173+ This function filters the layers based on the ``trainable `` attribute to
174+ see whether the layer weights are trained or not. For each layer, if its
175+ ``trainable=False ``, then its weights will not be evolved using the
176+ genetic algorithm. Otherwise, it will be represented in the chromosome
177+ and evolved.
178+
173179The function accepts the following parameters:
174180
175181- ``model ``: The Keras model.
176182
177183It returns a 1D vector holding the model weights.
178184
179- .. _header-n76 :
185+ .. _header-n270 :
180186
181187``pygad.kerasga.model_weights_as_matrix() ``
182188-------------------------------------------
@@ -190,7 +196,7 @@ parameters:
190196
191197It returns the restored model weights after reshaping the vector.
192198
193- .. _header-n84 :
199+ .. _header-n278 :
194200
195201Examples
196202========
@@ -199,7 +205,7 @@ This section gives the complete code of some examples that build and
199205train a Keras model using PyGAD. Each subsection builds a different
200206network.
201207
202- .. _header-n86 :
208+ .. _header-n280 :
203209
204210Example 1: Regression Example
205211-----------------------------
@@ -296,7 +302,7 @@ subsections discuss each part in the code.
296302 abs_error = mae(data_outputs, predictions).numpy()
297303 print (" Absolute Error : " , abs_error)
298304
299- .. _header-n89 :
305+ .. _header-n283 :
300306
301307Create a Keras Model
302308~~~~~~~~~~~~~~~~~~~~
@@ -328,7 +334,7 @@ The model can also be build using the Keras Sequential Model API.
328334 model.add(dense_layer1)
329335 model.add(output_layer)
330336
331- .. _header-n94 :
337+ .. _header-n288 :
332338
333339Create an Instance of the ``pygad.kerasga.KerasGA `` Class
334340~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -344,7 +350,7 @@ Change this number according to your needs.
344350 keras_ga = pygad.kerasga.KerasGA(model = model,
345351 num_solutions = 10 )
346352
347- .. _header-n97 :
353+ .. _header-n291 :
348354
349355Prepare the Training Data
350356~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -369,7 +375,7 @@ output.
369375 [1.3 ],
370376 [2.5 ]])
371377
372- .. _header-n100 :
378+ .. _header-n294 :
373379
374380Build the Fitness Function
375381~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -400,7 +406,7 @@ function to calculate the fitness value.
400406
401407 return solution_fitness
402408
403- .. _header-n104 :
409+ .. _header-n298 :
404410
405411Create an Instance of the ``pygad.GA `` Class
406412~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -435,7 +441,7 @@ accepts <https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#in
435441 keep_parents = keep_parents,
436442 on_generation = callback_generation)
437443
438- .. _header-n108 :
444+ .. _header-n302 :
439445
440446Run the Genetic Algorithm
441447~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -508,7 +514,7 @@ The next code measures the trained model error.
508514
509515 Absolute Error : 0.013740465
510516
511- .. _header-n124 :
517+ .. _header-n318 :
512518
513519Example 2: XOR Binary Classification
514520------------------------------------
@@ -679,7 +685,7 @@ Here is some information about the trained model. Its fitness value is
679685
680686 Accuracy : 1.0
681687
682- .. _header-n144 :
688+ .. _header-n338 :
683689
684690Example 3: Image Multi-Class Classification (Dense Layers)
685691----------------------------------------------------------
@@ -789,7 +795,7 @@ cross entropy.
789795 cce = tensorflow.keras.losses.CategoricalCrossentropy()
790796 solution_fitness = 1.0 / (cce(data_outputs, predictions).numpy() + 0.00000001 )
791797
792- .. _header-n149 :
798+ .. _header-n343 :
793799
794800Prepare the Training Data
795801~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -839,7 +845,7 @@ Here are some statistics about the trained model.
839845 Categorical Crossentropy : 0.23823906
840846 Accuracy : 0.9852192
841847
842- .. _header-n164 :
848+ .. _header-n358 :
843849
844850Example 4: Image Multi-Class Classification (Conv Layers)
845851---------------------------------------------------------
@@ -974,7 +980,7 @@ each input sample is 100x100x3.
974980
975981 model = tensorflow.keras.Model(inputs = input_layer, outputs = output_layer)
976982
977- .. _header-n170 :
983+ .. _header-n364 :
978984
979985Prepare the Training Data
980986~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments