Neural-network layers often form a direct pipeline in which each tensor passes through one transformation after another. keras.Sequential represents that linear topology as an ordered stack, making it suitable for a classifier with one feature input and one probability output.
The sample classifier selects the JAX backend before importing Keras and defines two rows of four numeric features. An explicit keras.Input establishes that four-feature contract immediately, so each added Dense layer can create its weights and expose its output shape before training.
Use the Functional API when tensors branch, merge, share layers, or enter through multiple inputs. Model subclassing remains the better fit when the forward pass depends on custom Python control flow rather than a fixed layer stack.
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
import os os.environ["KERAS_BACKEND"] = "jax" import keras import numpy as np from keras import layers, ops keras.utils.set_random_seed(7) features = ops.convert_to_tensor( [ [0.10, 0.40, 0.20, 0.80], [0.70, 0.30, 0.60, 0.10], ], dtype="float32", )
model = keras.Sequential(name="ticket_classifier") model.add(keras.Input(shape=(4,), name="features"))
model.add(layers.Dense(8, activation="relu", name="hidden"))
model.add(layers.Dense(3, activation="softmax", name="class_probs"))
probabilities = model(features, training=False) row_sums = ops.convert_to_numpy(ops.sum(probabilities, axis=1)) assert model.input_shape == (None, 4) assert model.output_shape == (None, 3) assert probabilities.shape == (2, 3) np.testing.assert_allclose(row_sums, np.ones(2), rtol=1e-5, atol=1e-5) print(f"backend: {keras.config.backend()}") print(f"model: {model.name}") print(f"layers: {[layer.name for layer in model.layers]}") print(f"parameters: {model.count_params()}") print(f"input shape: {model.input_shape}") print(f"output shape: {model.output_shape}") print(f"prediction shape: {probabilities.shape}") print(f"probability sums: {row_sums.round(6).tolist()}")
$ python create_sequential_model.py backend: jax model: ticket_classifier layers: ['hidden', 'class_probs'] parameters: 67 input shape: (None, 4) output shape: (None, 3) prediction shape: (2, 3) probability sums: [1.0, 1.0]
Related: How to compile a model in Keras