liveBook · Manning

Keras

As explained in Evolutionary Deep Learning: Genetic algorithms and neural networks

Keras is an open-source deep-learning library providing high-level APIs for constructing and training neural networks.

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

How Evolutionary Deep Learning: Genetic algorithms and neural networks covers it

How deep it goes
Descends through executable model definition, compilation, fitting, parameter counting, and weight replacement, but not framework internals.
Examples
Uses moons and circles classification datasets, Dense-layer models, loss plots, prediction displays, and evolved model weights.
What it assumes
Assumes basic neural-network and classification familiarity while demonstrating model construction and training incrementally.
What you can do after
Builds and trains a Keras classifier, inspects its parameters, and integrates genetic-algorithm weight optimization.
How it connects
Connects neural networks, Adam, binary cross-entropy, genetic algorithms, TensorFlow, and convolutional neural networks.

Also explained in

Compare how 6 books explain Keras →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Evolutionary Deep Learning: Genetic algorithms and neural networks. The section links point to the parts of the book it draws on.