liveBook · Manning

Keras

As explained in Deep Learning with Python, Third Edition

The programming interface of Keras that exposes multiple model-construction workflows and lets them interoperate.

Keras is a high-level Python API for defining and training neural networks. It supplies layers, models, losses, optimizers, metrics, and training loops, leaving low-level tensor computation to a backend. The practical choice is how much of the modeling and training process to let Keras handle. section 3.2 section 7.1

A first model: handwritten digits

Load MNIST with `keras.datasets.mnist.load_data()`. Build a `keras.Sequential` model with a 512-unit `Dense` layer using `relu` and a 10-unit `Dense` layer using `softmax`. Compile it with the `adam` optimizer, `sparse_categorical_crossentropy` loss, and `accuracy` metric. After reshaping and scaling the images, call `fit()` to train, `predict()` to get class scores, and `evaluate()` to measure performance on the test set. This is the compact workflow: assemble, compile, fit, predict, evaluate. section 2.1

Choose the level of control the problem needs:

  • Use compatible layers as building blocks. `Sequential` stacks them into a data-transformation pipeline. section 3.6.2
  • For a straightforward training workflow, call `fit()` and let Keras run the training loop. More flexible workflows let you write progressively more of the logic yourself. section 7.1
  • Reuse components across workflows: the shared `Layer` and `Model` APIs let simpler and more customized approaches interoperate. section 7.1
Listing 3.33 A simple dense layer from scratch in Keras
import keras

class SimpleDense(keras.Layer):                                     #1
    def __init__(self, units, activation=None):
        super().__init__()
        self.units = units
        self.activation = activation

    def build(self, input_shape):                                    #2
        batch_dim, input_dim = input_shape
        self.W = self.add_weight(                                    #3
            shape=(input_dim, self.units), initializer="random_normal"   #3
        )                                                                 #3
        self.b = self.add_weight(shape=(self.units,), initializer="zeros")

    def call(self, inputs):                                          #4
        y = keras.ops.matmul(inputs, self.W) + self.b
        if self.activation is not None:
            y = self.activation(y)
        return y

A custom dense layer shows what greater control looks like: subclass `keras.Layer`, create weights with `add_weight()` in `build()`, and define the forward computation in `call()`. section 3.6.2

Keras runs on TensorFlow by default; PyTorch and JAX are also available as backends. Set `KERAS_BACKEND` before importing Keras to choose another. A model can move between backend ecosystems as needs change. NumPy can serve as a backend for prediction, but not training, because it lacks the required gradient API. section 3.6.1 section 3.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through an end-to-end model and sketches custom Layer implementation, while deferring fuller explanation of build() and call().
Examples
Uses MNIST digit classification and a custom dense layer to show both ready-made and subclassed components.
What it assumes
Introduces Keras through a first example without assuming prior experience with Keras or similar libraries.
What you can do after
Apply it via Keras to train and evaluate a layered model, then choose a more customized workflow when needed.
How it connects
Connects layers and models to losses, optimizers, metrics, training loops, and TensorFlow, PyTorch, or JAX backends.

Also explained in

Compare how 6 books explain Keras →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.