The Sequential API is perfect for simple stacks. But real problems often need more: a model that looks at a photo and a table of details, predicts two things at once, or lets information skip past layers. The Functional API lets you build any of these by connecting layers like a flowchart.
- Level: Intermediate
- Time: about 40 minutes
- Needs: Modules 1 to 5
By the end of this module you will be able to
- Explain when the Functional API is needed instead of Sequential
- Build a model by calling layers on tensors and wrapping them in keras.Model
- Create models with multiple inputs using Concatenate
- Create models with multiple outputs, each with its own loss
- Use loss_weights to balance several outputs
- Add skip connections with the Add layer
- Train, evaluate and predict with named inputs and outputs
6.1Why go beyond Sequential?
A Sequential model is a single straight line of layers: one input, one output, no branches. That covers many problems, but not these:
| Need | Example |
|---|---|
| Several inputs | Predict house prices from a photo and the number of bedrooms |
| Several outputs | From one customer record, predict spend and whether they'll leave |
| Branches | Process an input in two different ways, then combine the results |
| Skip connections | Let information jump over layers, as in ResNet |
| Shared layers | Use the same layer, with the same weights, on two inputs, such as comparing two sentences |
The Functional API handles all of them. It's the same Keras, the same layers and the same compile, fit and predict. Only the way you connect layers changes.
6.2How the Functional API works
In the Functional API, a layer is like a function. You call it on the output of the previous layer, and it gives back a new output. At the end, you tell keras.Model where the data starts and finishes:
inputs = keras.Input(shape=(13,)) # 1. where data enters
x = layers.Dense(32, activation="relu")(inputs) # 2. call each layer on the previous output
x = layers.Dense(16, activation="relu")(x)
outputs = layers.Dense(3, activation="softmax")(x)
model = keras.Model(inputs=inputs, outputs=outputs) # 3. wrap it up
This builds exactly the same model as the Sequential wine classifier in Module 3. Notice the pattern layers.Dense(32)(inputs): the first brackets create the layer, and the second brackets connect it to its input. Reusing the name x is just a convenient habit.
Sequential or Functional?
Use Sequential when your model is a simple stack; it's shorter and easier to read. Use the Functional API as soon as you need more than one input, more than one output, or any connection that isn't a straight line.
6.3Four shapes of model
Choose a design to see its diagram and the Functional API code that builds it.
Try it: model architecture explorer
6.4Multiple inputs
Give each input its own keras.Input, and a name. Each can have its own branch of layers suited to its type of data. Then join the branches with layers.Concatenate(), which places their outputs side by side in one longer list.
When training, pass the inputs as a dictionary using the names, so there's no risk of mixing them up:
model.fit({"car": car_data, "listing": listing_data}, y, epochs=40)
Other ways to join branches include layers.Add() (adds them together, which needs matching shapes) and layers.Average().
6.5Multiple outputs
A model can finish with several heads, one per thing you want to predict. Each head needs its own loss, and you can give each one different metrics. Match them to the outputs using the layer names:
model.compile(
optimizer="adam",
loss={"price": "mse", "sold": "binary_crossentropy"},
loss_weights={"price": 0.1, "sold": 1.0},
metrics={"price": ["mae"], "sold": ["accuracy"]},
)
During training, Keras adds the losses together into one total. loss_weights controls how much each one counts. That matters because losses can be on very different scales: a price error measured in thousands of pounds, squared, can be far bigger than a yes/no cross-entropy, and would otherwise dominate training.
Why share layers between outputs?
The shared hidden layers learn features that help with both tasks. Knowing what drives a car's price also helps predict whether it will sell. One shared model is also smaller and faster than two separate ones.
6.6Skip connections
A skip connection (or residual connection) adds a block's input to its output: layers.Add()([x, block_output]). It gives information, and the learning signal during training, a shortcut past the layers in between.
This simple idea, introduced in the ResNet model in 2015, made it possible to train networks with more than 100 layers. Skip connections are now in almost every large model, including the Transformers behind modern chatbots. Add needs both inputs to have the same shape, which is why both Dense layers in the explorer have 64 units.
6.7A two-input, two-output model in Python
A used-car website wants one model that takes details about the car (age, mileage, engine size) and the listing (number of photos, seller rating), and predicts both the price and whether the car will sell within 30 days.
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import numpy as np
import keras
from keras import layers
keras.utils.set_random_seed(42)
rng = np.random.default_rng(0)
n = 4000
# ---------- Data: used-car listings on a website ----------
# Input 1: the car (age in years, mileage in 1,000s, engine size in litres)
age = rng.uniform(0, 15, n); miles = rng.uniform(0, 150, n); engine = rng.uniform(1.0, 3.0, n)
car = np.c_[age, miles, engine].astype("float32")
# Input 2: the listing (number of photos, seller rating out of 5)
photos = rng.integers(1, 21, n); rating = rng.uniform(2.5, 5, n)
listing = np.c_[photos, rating].astype("float32")
# Output 1: price in £1,000s. Output 2: sold within 30 days (1) or not (0)
price = (25 - 1.1 * age - 0.06 * miles + 4 * engine + rng.normal(0, 1.5, n)).clip(1)
sold = ((0.15 * photos + 1.2 * rating - 0.12 * price + rng.normal(0, 1, n)) > 3.4).astype("float32")
price = price.astype("float32")
split = 3200
# ---------- Build a model with 2 inputs and 2 outputs ----------
car_in = keras.Input(shape=(3,), name="car")
listing_in = keras.Input(shape=(2,), name="listing")
car_norm = layers.Normalization(); car_norm.adapt(car[:split])
listing_norm = layers.Normalization(); listing_norm.adapt(listing[:split])
x1 = layers.Dense(16, activation="relu")(car_norm(car_in)) # car branch
x2 = layers.Dense(8, activation="relu")(listing_norm(listing_in)) # listing branch
x = layers.Concatenate()([x1, x2]) # join the branches
x = layers.Dense(32, activation="relu")(x)
price_out = layers.Dense(1, name="price")(x) # regression head
sold_out = layers.Dense(1, activation="sigmoid", name="sold")(x) # classification head
model = keras.Model(inputs=[car_in, listing_in], outputs=[price_out, sold_out],
name="car_listing_model")
model.summary()
# ---------- One loss and metric per output, matched by name ----------
model.compile(
optimizer="adam",
loss={"price": "mse", "sold": "binary_crossentropy"},
loss_weights={"price": 0.1, "sold": 1.0}, # price errors are much bigger numbers
metrics={"price": ["mae"], "sold": ["accuracy"]},
)
model.fit({"car": car[:split], "listing": listing[:split]},
{"price": price[:split], "sold": sold[:split]},
epochs=40, batch_size=32, validation_split=0.2, verbose=0)
results = model.evaluate({"car": car[split:], "listing": listing[split:]},
{"price": price[split:], "sold": sold[split:]},
verbose=0, return_dict=True)
print(f"Price: average error £{results['price_mae'] * 1000:,.0f}")
print(f"Sold within 30 days: accuracy {results['sold_accuracy']:.3f} "
f"(guessing the most common answer: {max(sold[split:].mean(), 1 - sold[split:].mean()):.3f})")
# ---------- Predict for one new listing ----------
new_car = np.array([[3, 30, 1.6]], dtype="float32") # 3 years, 30k miles, 1.6 litre
new_listing = np.array([[12, 4.8]], dtype="float32") # 12 photos, rated 4.8
pred_price, pred_sold = model.predict({"car": new_car, "listing": new_listing}, verbose=0)
print(f"New listing: price £{pred_price[0, 0] * 1000:,.0f}, "
f"{pred_sold[0, 0]:.0%} chance of selling within 30 days")
Model: "car_listing_model" ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Layer (type) ┃ Output Shape ┃ Param # ┃ Connected to ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ car (InputLayer) │ (None, 3) │ 0 │ - │ ├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤ │ listing (InputLayer) │ (None, 2) │ 0 │ - │ ├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤ │ normalization (Normalization) │ (None, 3) │ 7 │ car[0][0] │ ├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤ │ normalization_1 │ (None, 2) │ 5 │ listing[0][0] │ │ (Normalization) │ │ │ │ ├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤ │ dense (Dense) │ (None, 16) │ 64 │ normalization[0][0] │ ├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤ │ dense_1 (Dense) │ (None, 8) │ 24 │ normalization_1[0][0] │ ├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤ │ concatenate (Concatenate) │ (None, 24) │ 0 │ dense[0][0], dense_1[0][0] │ ├───────────────────────────────┼───────────────────────────┼─────────────────┼────────────────────────────┤ │ dense_2 (Dense) │ (None, 32) │
What the output tells us
- The summary has a new column, "Connected to", showing how the graph is wired:
concatenatereceives bothdenseanddense_1, and both heads connect todense_2. - The model is small: 954 trainable parameters, plus 12 non-trainable scaling numbers from the two Normalization layers.
- Price is predicted to within about £1,200 on average, on 800 unseen listings.
- "Sold within 30 days" is 81% accurate, well above the 54.4% you'd get by always guessing the most common answer.
- predict() returns both outputs: a 3-year-old car with 12 photos and a 4.8 seller rating is priced at about £26,800, with an 85% chance of selling within a month.
Draw your model
keras.utils.plot_model(model, show_shapes=True) draws a diagram like Figure 1. It needs the pydot and Graphviz packages, which are already installed in Google Colab.
SummaryKey takeaways
- The Functional API builds models with several inputs, several outputs, branches, shared layers and skip connections.
- Call each layer on the previous output:
x = layers.Dense(32)(x), thenkeras.Model(inputs, outputs). - Join branches with
Concatenate(side by side) orAdd(same shapes). - Each output gets its own loss and metrics, matched by name.
loss_weightsbalances outputs whose losses are on different scales.- Pass named inputs and outputs as dictionaries to
fit,evaluateandpredict. - Skip connections (
Add) let very deep networks train.
Check your understanding
Your Keras roadmap
- 01Introduction to KerasWhat Keras is, setup and your first model
- 02Tensors and Data for KerasShapes, scaling and splitting data
- 03The Sequential API and Dense LayersStacking layers into a model
- 04Compiling ModelsLosses, optimisers and metrics
- 05Training and Evaluatingfit(), validation and callbacks
- 06The Functional APIYou are here
- 07RegularisationDropout, batch normalisation and early stopping
- 08Image Classification with KerasCNNs and data augmentation
- 09Text and Sequences with KerasEmbeddings, LSTMs and text classification
- 10Pretrained Models, Saving and DeploymentFine-tuning and sharing your models
Next module
Regularisation