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





