How to create a Sequential model in Keras

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.

Steps to create a Sequential model in Keras:

  1. Install Keras and select a backend.
    $ python -m pip install --upgrade keras jax
    $ export KERAS_BACKEND=jax
  2. Create create_sequential_model.py.
    create_sequential_model.py
    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.

  3. Run the script.
    $ 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.

  4. Add compile() and fit() only after the architecture is correct.
    model.compile(
        optimizer="adam",
        loss="sparse_categorical_crossentropy",
        metrics=["accuracy"],
    )