Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 40 ยท Building Models with Keras

The Functional API

Use the Keras Functional API to build models with multiple inputs and outputs, skip connections and shared layers, with a runnable NumPy graph stand-in.

The Sequential API cannot express a model where two inputs are processed separately and then merged, where one input feeds two heads, or where a layer's output skips ahead and is added back later. The Functional API can. Instead of listing layers in a stack, you treat each layer as a function: call it on a tensor to get a new tensor, and finally wrap the input and output tensors in a keras.Model. The result is a directed graph of layers.

The basic pattern

Python
import keras
from keras import layers

inputs = keras.Input(shape=(784,), name="pixels")
x = layers.Dense(128, activation="relu")(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10, activation="softmax")(x)

model = keras.Model(inputs=inputs, outputs=outputs, name="mlp")

Each call such as layers.Dense(128)(inputs) does not compute numbers. It creates a symbolic tensor that records which layer produced it and from what. When you build the Model, Keras walks the graph from outputs back to inputs. This model is equivalent to the Sequential one with the same layers, and it has the same summary(), compile, fit and predict.

Multiple inputs

Imagine predicting a house price from numeric features (area, rooms, age) and an extra block of 5 neighborhood scores. Process each input with its own layers, then merge with Concatenate.

Python
numeric_in = keras.Input(shape=(10,), name="numeric")
area_in    = keras.Input(shape=(5,), name="neighborhood")

a = layers.Dense(16, activation="relu")(numeric_in)   # 10*16+16 = 176
b = layers.Dense(8, activation="relu")(area_in)       # 5*8+8   = 48

merged = layers.Concatenate()([a, b])                 # (None, 24)
price  = layers.Dense(1, name="price")(merged)        # 24*1+1  = 25

model = keras.Model(inputs=[numeric_in, area_in], outputs=price)
# total parameters: 176 + 48 + 25 = 249

Training uses a list or dictionary keyed by input name:

Python
model.compile(optimizer="adam", loss="mse")
model.fit({"numeric": x_numeric, "neighborhood": x_area}, y_price, epochs=20)
flowchart TB
    N["numeric (10)"] --> D1["Dense 16"]
    A["neighborhood (5)"] --> D2["Dense 8"]
    D1 --> C["Concatenate (24)"]
    D2 --> C
    C --> P["Dense 1: price"]

Two branches run separately, then merge. A Sequential model cannot draw this picture.

Multiple outputs

A single trunk can feed several heads, each with its own loss. For example, predict both a category and a numeric score from one text embedding:

Python
inputs = keras.Input(shape=(128,))
x = layers.Dense(64, activation="relu")(inputs)

category = layers.Dense(5, activation="softmax", name="category")(x)
score    = layers.Dense(1, name="score")(x)

model = keras.Model(inputs, [category, score])

model.compile(
    optimizer="adam",
    loss={"category": "sparse_categorical_crossentropy", "score": "mse"},
    loss_weights={"category": 1.0, "score": 0.5},
)

loss_weights balances the two objectives. Multi-output models are common when tasks share useful features (multi-task learning).

Skip (residual) connections

A skip connection adds a layer's input back to its output: out = x + F(x). Residual connections let gradients flow straight through and made very deep networks (ResNet) trainable. The idea is impossible in Sequential but a one-liner with Add:

Python
inputs = keras.Input(shape=(32,))
x = layers.Dense(32, activation="relu")(inputs)
y = layers.Dense(32)(x)
z = layers.Add()([x, y])                    # shapes must match: (None, 32)
z = layers.Activation("relu")(z)
outputs = layers.Dense(1)(z)
model = keras.Model(inputs, outputs)

Add requires equal shapes; use a Dense or Conv with a 1x1 kernel on the shortcut when they differ.

Shared layers

Call the same layer object on different inputs and the weights are shared. This is how siamese networks compare two inputs with one encoder.

Python
encoder = layers.Dense(16, activation="relu")   # one set of weights

left_in  = keras.Input(shape=(8,))
right_in = keras.Input(shape=(8,))
left  = encoder(left_in)
right = encoder(right_in)                        # same weights

diff = layers.Subtract()([left, right])
out  = layers.Dense(1, activation="sigmoid")(diff)
model = keras.Model([left_in, right_in], out)

The encoder's 8*16 + 16 = 144 parameters are counted once.

A graph executor in NumPy (stand-in)

To see that "layers as functions on tensors" is not magic, here is a minimal graph model. Nodes are stored by name with the names of their inputs. Running the model evaluates nodes in order. It reproduces the two-input example above: parameter count 249, and a forward pass.

Python Stand-in: a two-branch graph with Concatenate (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real Keras graph adds shape inference, saving and the training machinery, but the idea is this small.

Shared weights and skip connections in NumPy

Two quick experiments make the last two ideas concrete: a shared encoder gives identical outputs for identical inputs (and its parameters are counted once), and a skip connection preserves the input when the inner transformation is zero.

Python Stand-in: shared weights and a residual connection (NumPy)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

That second experiment shows why residual blocks train well: at the start the block is close to the identity function, so adding layers does not hurt, and each block only needs to learn a small correction.

Inspecting and reusing sub-models

Because the model is a graph, you can build new models from parts of it:

Python
feature_extractor = keras.Model(
    inputs=model.input,
    outputs=model.get_layer("dense_1").output,
)
embeddings = feature_extractor.predict(x)

This is the basis of feature extraction and transfer learning (Chapter 5). You can also use a whole model as a layer inside another model, since a Model is a Layer.

Visualizing

keras.utils.plot_model(model, show_shapes=True) writes a diagram of the graph. It needs the pydot package and the Graphviz program, which are optional installs, so many people rely on model.summary() instead, whose Connected-to column also lists each layer's inputs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does calling layers.Dense(128)(inputs) in the Functional API return?

    Functional calls build the graph; no numbers flow until you call the model with data.
  2. 2. Which layer merges two branches by joining their features side by side?

    Concatenate joins tensors along an axis (features by default the last axis); Add sums tensors of equal shape.
  3. 3. What happens to the parameter count when the same layer object is called on two inputs?

    Calling one layer object on several tensors reuses its weights.
  4. 4. Why do residual (skip) connections help train deep networks?

    The identity path gives gradients a short route to early layers and starts blocks near the identity function.

Summary

  • Functional API: create keras.Input, call layers on tensors, wrap the ends in keras.Model.
  • It supports multiple inputs and outputs, merges (Concatenate, Add), skip connections and shared layers.
  • Multi-output models use per-output losses and loss_weights.
  • Sub-graphs can become new models (feature extractors); a Model is itself a layer.
  • A model is a graph of simple function calls, as the NumPy executor shows.

Next: Initializers and Regularizers