1
- .. _header-n0 :
1
+ .. _header-n194 :
2
2
3
3
``pygad.kerasga `` Module
4
4
========================
@@ -25,7 +25,7 @@ The contents of this module are:
25
25
26
26
More details are given in the next sections.
27
27
28
- .. _header-n13 :
28
+ .. _header-n207 :
29
29
30
30
Steps Summary
31
31
=============
@@ -45,7 +45,7 @@ follows:
45
45
46
46
6. Run the genetic algorithm.
47
47
48
- .. _header-n28 :
48
+ .. _header-n222 :
49
49
50
50
Create Keras Model
51
51
==================
@@ -93,7 +93,7 @@ This is the same model created using the Functional API.
93
93
94
94
Feel free to add the layers of your choice.
95
95
96
- .. _header-n44 :
96
+ .. _header-n238 :
97
97
98
98
``pygad.kerasga.KerasGA `` Class
99
99
===============================
@@ -103,7 +103,7 @@ an initial population for the genetic algorithm based on a Keras model.
103
103
The constructor, methods, and attributes within the class are discussed
104
104
in this section.
105
105
106
- .. _header-n46 :
106
+ .. _header-n240 :
107
107
108
108
``__init__() ``
109
109
--------------
@@ -116,7 +116,7 @@ parameters:
116
116
- ``num_solutions ``: Number of solutions in the population. Each
117
117
solution has different parameters of the model.
118
118
119
- .. _header-n53 :
119
+ .. _header-n247 :
120
120
121
121
Instance Attributes
122
122
-------------------
@@ -134,15 +134,15 @@ Here is a list of all instance attributes:
134
134
- ``population_weights ``: A nested list holding the weights of all
135
135
solutions in the population.
136
136
137
- .. _header-n63 :
137
+ .. _header-n257 :
138
138
139
139
Methods in the ``KerasGA `` Class
140
140
--------------------------------
141
141
142
142
This section discusses the methods available for instances of the
143
143
``pygad.kerasga.KerasGA `` class.
144
144
145
- .. _header-n65 :
145
+ .. _header-n259 :
146
146
147
147
``create_population() ``
148
148
~~~~~~~~~~~~~~~~~~~~~~~
@@ -152,14 +152,14 @@ genetic algorithm as a list of solutions where each solution represents
152
152
different model parameters. The list of networks is assigned to the
153
153
``population_weights `` attribute of the instance.
154
154
155
- .. _header-n67 :
155
+ .. _header-n261 :
156
156
157
157
Functions in the ``pygad.kerasga `` Module
158
158
=========================================
159
159
160
160
This section discusses the functions in the ``pygad.kerasga `` module.
161
161
162
- .. _header-n69 :
162
+ .. _header-n263 :
163
163
164
164
``pygad.kerasga.model_weights_as_vector() ``
165
165
--------------------------------------------
@@ -170,13 +170,19 @@ holding all model weights. The reason for representing the model weights
170
170
as a vector is that the genetic algorithm expects all parameters of any
171
171
solution to be in a 1D vector form.
172
172
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
+
173
179
The function accepts the following parameters:
174
180
175
181
- ``model ``: The Keras model.
176
182
177
183
It returns a 1D vector holding the model weights.
178
184
179
- .. _header-n76 :
185
+ .. _header-n270 :
180
186
181
187
``pygad.kerasga.model_weights_as_matrix() ``
182
188
-------------------------------------------
@@ -190,7 +196,7 @@ parameters:
190
196
191
197
It returns the restored model weights after reshaping the vector.
192
198
193
- .. _header-n84 :
199
+ .. _header-n278 :
194
200
195
201
Examples
196
202
========
@@ -199,7 +205,7 @@ This section gives the complete code of some examples that build and
199
205
train a Keras model using PyGAD. Each subsection builds a different
200
206
network.
201
207
202
- .. _header-n86 :
208
+ .. _header-n280 :
203
209
204
210
Example 1: Regression Example
205
211
-----------------------------
@@ -296,7 +302,7 @@ subsections discuss each part in the code.
296
302
abs_error = mae(data_outputs, predictions).numpy()
297
303
print (" Absolute Error : " , abs_error)
298
304
299
- .. _header-n89 :
305
+ .. _header-n283 :
300
306
301
307
Create a Keras Model
302
308
~~~~~~~~~~~~~~~~~~~~
@@ -328,7 +334,7 @@ The model can also be build using the Keras Sequential Model API.
328
334
model.add(dense_layer1)
329
335
model.add(output_layer)
330
336
331
- .. _header-n94 :
337
+ .. _header-n288 :
332
338
333
339
Create an Instance of the ``pygad.kerasga.KerasGA `` Class
334
340
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -344,7 +350,7 @@ Change this number according to your needs.
344
350
keras_ga = pygad.kerasga.KerasGA(model = model,
345
351
num_solutions = 10 )
346
352
347
- .. _header-n97 :
353
+ .. _header-n291 :
348
354
349
355
Prepare the Training Data
350
356
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -369,7 +375,7 @@ output.
369
375
[1.3 ],
370
376
[2.5 ]])
371
377
372
- .. _header-n100 :
378
+ .. _header-n294 :
373
379
374
380
Build the Fitness Function
375
381
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -400,7 +406,7 @@ function to calculate the fitness value.
400
406
401
407
return solution_fitness
402
408
403
- .. _header-n104 :
409
+ .. _header-n298 :
404
410
405
411
Create an Instance of the ``pygad.GA `` Class
406
412
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -435,7 +441,7 @@ accepts <https://pygad.readthedocs.io/en/latest/README_pygad_ReadTheDocs.html#in
435
441
keep_parents = keep_parents,
436
442
on_generation = callback_generation)
437
443
438
- .. _header-n108 :
444
+ .. _header-n302 :
439
445
440
446
Run the Genetic Algorithm
441
447
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -508,7 +514,7 @@ The next code measures the trained model error.
508
514
509
515
Absolute Error : 0.013740465
510
516
511
- .. _header-n124 :
517
+ .. _header-n318 :
512
518
513
519
Example 2: XOR Binary Classification
514
520
------------------------------------
@@ -679,7 +685,7 @@ Here is some information about the trained model. Its fitness value is
679
685
680
686
Accuracy : 1.0
681
687
682
- .. _header-n144 :
688
+ .. _header-n338 :
683
689
684
690
Example 3: Image Multi-Class Classification (Dense Layers)
685
691
----------------------------------------------------------
@@ -789,7 +795,7 @@ cross entropy.
789
795
cce = tensorflow.keras.losses.CategoricalCrossentropy()
790
796
solution_fitness = 1.0 / (cce(data_outputs, predictions).numpy() + 0.00000001 )
791
797
792
- .. _header-n149 :
798
+ .. _header-n343 :
793
799
794
800
Prepare the Training Data
795
801
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -839,7 +845,7 @@ Here are some statistics about the trained model.
839
845
Categorical Crossentropy : 0.23823906
840
846
Accuracy : 0.9852192
841
847
842
- .. _header-n164 :
848
+ .. _header-n358 :
843
849
844
850
Example 4: Image Multi-Class Classification (Conv Layers)
845
851
---------------------------------------------------------
@@ -974,7 +980,7 @@ each input sample is 100x100x3.
974
980
975
981
model = tensorflow.keras.Model(inputs = input_layer, outputs = output_layer)
976
982
977
- .. _header-n170 :
983
+ .. _header-n364 :
978
984
979
985
Prepare the Training Data
980
986
~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments