How to create a Sequential model in Keras

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.

Steps to create a Sequential model in Keras:

  1. Create create_sequential_model.py with the initial runtime declarations and two four-feature rows.
    create_sequential_model.py
    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",
    )
  2. Append the named model initialization and four-feature input contract.
    create_sequential_model.py
    model = keras.Sequential(name="ticket_classifier")
    model.add(keras.Input(shape=(4,), name="features"))
  3. Append the hidden Dense layer that transforms each row into eight activated values.
    create_sequential_model.py
    model.add(layers.Dense(8, activation="relu", name="hidden"))
  4. Append the output Dense layer that returns three class probabilities.
    create_sequential_model.py
    model.add(layers.Dense(3, activation="softmax", name="class_probs"))
  5. Append the inference call, fail-capable assertions, and result display.
    create_sequential_model.py
    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()}")
  6. Run the completed model to verify its layer stack, tensor shapes, and normalized probabilities.
    $ 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]