Keras provides a practical high-level interface for constructing and training neural networks, including multilayer perceptrons and convolutional neural networks. It supports established optimizers such as Adam and can also serve as the model layer for neuroevolution experiments. section 6.4 section 7.0
A typical Keras workflow includes:
- Define a Sequential model with input, hidden, and output Dense layers. section 6.4
- Compile the model with an optimizer, binary cross-entropy loss, and accuracy metrics. section 6.4
- Fit the model on a classification dataset while inspecting losses and predictions. section 6.4
- Count trainable parameters when mapping model weights to genes for genetic-algorithm optimization. section 6.4
Classifying moons or circles
A small Keras network uses 16- and 32-unit ReLU hidden layers, a sigmoid output, binary cross-entropy, and Adam. Training can be monitored with PlotLossesKeras, then the weights can be replaced from an evolved individual. section 6.4
Listing 6.8 EDL_6_4_Keras_GA.ipynb: Setting up the Keras model
model = tf.keras.models.Sequential([ #1
tf.keras.layers.Dense(16, activation='relu', #1
input_shape=(X.shape[1],)), #1
tf.keras.layers.Dense(32, activation='relu'), #1
tf.keras.layers.Dense(1, activation='sigmoid') #1
])
optimizer = tf.keras.optimizers.Adam
(learning_rate=.001) #2
model.compile(optimizer=optimizer,
loss='binary_crossentropy', #3
metrics=['accuracy']) #4
model.summary() #5
trainableParams = np.sum([np.prod(v.get_shape()) for v in #5
model.trainable_weights]) #5
print(f"Trainable parameters: {trainableParams}") #5
Inspect the executable model definition, compilation settings, parameter count, and relationships between weights and genetic-algorithm genes. section 6.4





