Keras - Models
Keras - Models (Sequential & Functional)
Section titled “Keras - Models (Sequential & Functional)”In tf.keras, the Model is the central object that groups layers into a trainable architecture. It defines the forward pass (how input data flows through layers to produce output) and provides methods for training (fit), evaluation (evaluate), and prediction (predict). tf.keras offers two main ways to create models: the Sequential API and the Functional API, along with Model Subclassing for advanced customization.
1. Sequential API (tf.keras.Sequential)
Section titled “1. Sequential API (tf.keras.Sequential)”The Sequential model is the simplest way to build a model in Keras. It’s appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor. Think of it as a linear pipeline for data.
Creating a Sequential model:
import tensorflow as tf
# Method 1: Pass a list of layers to the constructormodel = tf.keras.Sequential([ tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)), # First layer needs input_shape tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(10, activation='softmax')], name="my_sequential_model")
# Method 2: Incrementally add layers using model.add()model_alt = tf.keras.Sequential(name="my_alt_sequential_model")model_alt.add(tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)))model_alt.add(tf.keras.layers.Dense(64, activation='relu'))model_alt.add(tf.keras.layers.Dense(10, activation='softmax'))Key characteristics:
- Easy and concise for simple architectures.
- Only the first layer needs the
input_shapeargument; subsequent layers infer their input shape. - Not suitable for models with multiple inputs/outputs, shared layers, or non-linear topologies.
2. Functional API
Section titled “2. Functional API”The Functional API provides a more flexible way to define models. It allows you to build complex graphs of layers, including those with multiple inputs and outputs, shared layers, and branching connections. You treat layers as functions that operate on and return tensors.
Creating a model with the Functional API:
import tensorflow as tf
# 1. Define the Input tensor(s)# Specifies the shape and dtype of the input data the model expects.inputs = tf.keras.Input(shape=(784,), name="digits")
# 2. Define layers and connect them (like a function call)# Each layer call takes a tensor and returns a tensor.x = tf.keras.layers.Dense(64, activation='relu', name="dense_1")(inputs)x = tf.keras.layers.Dense(64, activation='relu', name="dense_2")(x)
# 3. Define the Output tensor(s)outputs = tf.keras.layers.Dense(10, activation='softmax', name="predictions")(x)
# 4. Create the Model# Specify the input and output tensors to define the model's boundaries.functional_model = tf.keras.Model(inputs=inputs, outputs=outputs, name="mnist_functional_model")Key characteristics:
- Highly flexible for arbitrary model architectures.
- Enables layer sharing and non-linear connectivity.
- Models are explicitly defined graphs with clear inputs and outputs.
- Slightly more verbose than the Sequential API for simple stacks.
The Functional API is powerful for tasks like multi-input models (e.g., combining image and text data) or models with multiple output heads (e.g., predicting both class and bounding box).
Model Inspection
Section titled “Model Inspection”Once a model is defined (using either API), you can inspect its properties:
model.summary(): Prints a textual summary of the model, including layers, output shapes, and number of parameters.
# Example using the functional_model defined abovefunctional_model.summary()Example summary() output:
Model: "mnist_functional_model"_________________________________________________________________ Layer (type) Output Shape Param #================================================================= digits (InputLayer) [(None, 784)] 0
dense_1 (Dense) (None, 64) 50240
dense_2 (Dense) (None, 64) 4160
predictions (Dense) (None, 10) 650
=================================================================Total params: 55050 (215.04 KB)Trainable params: 55050 (215.04 KB)Non-trainable params: 0 (0.00 Byte)_________________________________________________________________model.layers: Access a flat list of the model’s layers.model.inputs/model.outputs: Access the model’s input/output tensors (useful with Functional API).
Saving and Loading Models
Section titled “Saving and Loading Models”Saving your trained model allows you to reuse it later without retraining or deploy it. tf.keras offers robust saving mechanisms.
Recommended Format: TensorFlow SavedModel / Keras v3 (.keras)
Section titled “Recommended Format: TensorFlow SavedModel / Keras v3 (.keras)”The preferred way is to save the entire model (architecture, weights, optimizer state, compilation info) in TensorFlow’s SavedModel format or the new Keras v3 format (.keras extension). This is the most comprehensive option.
# Save the entire model (recommended)model.save("my_complete_model.keras") # Keras v3 format# or# model.save("my_saved_model_directory") # TensorFlow SavedModel format
# Load the model backloaded_model = tf.keras.models.load_model("my_complete_model.keras")# loaded_model.summary()Saving Only Weights
Section titled “Saving Only Weights”If you only need the learned parameters and plan to recreate the architecture in code:
# Save only the weightsmodel.save_weights("my_model_weights.weights.h5") # Using HDF5 format
# To load weights, you need to create the same model architecture first# new_model = create_model_architecture() # Your function to build the model# new_model.load_weights("my_model_weights.weights.h5")Legacy HDF5 Format (.h5)
Section titled “Legacy HDF5 Format (.h5)”Keras previously used the HDF5 format (.h5 extension) for saving models. While still supported for backward compatibility, the .keras format is now recommended for saving the entire model state.
# Saving in legacy HDF5 format# model.save("my_legacy_model.h5")
# Loading from HDF5# loaded_legacy_model = tf.keras.models.load_model("my_legacy_model.h5")Choosing between Sequential and Functional APIs depends on the complexity of your desired neural network architecture. Both allow you to build powerful deep learning models within the tf.keras framework.