keras.Sequential builds a model by stacking layers in a single straight path. It is the simplest Keras model type when each layer has exactly one input tensor and one output tensor that feeds the next layer.
A Sequential model can be compiled, trained, evaluated, saved, and used for prediction like other Keras models. Add an explicit keras.Input layer first when the input shape is known so the model is built immediately and can report shapes.
Use the Functional API instead when the model needs multiple inputs, multiple outputs, branches, merges, or shared layers. Use subclassing only when the forward pass needs custom Python control flow.
Related: How to create a functional model in Keras
Related: How to create a subclassed model in Keras
Related: How to show model summary in Keras
$ python -m pip install --upgrade keras jax $ export KERAS_BACKEND=jax
import os os.environ["KERAS_BACKEND"] = "jax" import keras import numpy as np keras.utils.set_random_seed(5) model = keras.Sequential( [ keras.Input(shape=(4,), name="features"), keras.layers.Dense(8, activation="relu", name="hidden"), keras.layers.Dense(3, activation="softmax", name="class_probs"), ], name="ticket_classifier", ) sample = np.array([[0.1, 0.4, 0.2, 0.8], [0.7, 0.3, 0.6, 0.1]], dtype="float32") prediction = model.predict(sample, verbose=0) print(f"backend: {keras.backend.backend()}") print(f"model name: {model.name}") print(f"layer count: {len(model.layers)}") print(f"parameter count: {model.count_params()}") print(f"input shape: {model.input_shape}") print(f"output shape: {model.output_shape}") print(f"prediction shape: {prediction.shape}") print(f"first row probability sum: {prediction[0].sum():.6f}")
The input layer defines four numeric features. The final softmax layer returns three class probabilities.
$ python create_sequential_model.py backend: jax model name: ticket_classifier layer count: 2 parameter count: 67 input shape: (None, 4) output shape: (None, 3) prediction shape: (2, 3) first row probability sum: 1.000000
Confirm that the prediction shape shows two input rows and three class probabilities per row.
model.compile( optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"], )
Related: How to compile a model in Keras