Why the Functional API?
In the Sequential API the data goes in a straight line. Real projects often need more: two different inputs (an image and some text), two outputs, or a shortcut that skips a layer. The Functional API treats layers like functions that you connect like pipes.
Basic Example
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="digit_model")
model.summary()
keras.Input creates the starting point
- Each layer is called like a function:
layer(previous_output)
keras.Model(inputs, outputs) ties the graph together
Same result, more freedom: This model does exactly the same as the Sequential one. The difference is that now you can connect the layers in any way you like.
Multiple Inputs
Suppose we predict the price of a house from numeric data (size, rooms) and a text description.
python
umeric_in = keras.Input(shape=(5,), name="numeric")
text_in = keras.Input(shape=(100,), name="text_features")
a = layers.Dense(32, activation="relu")(numeric_in)
b = layers.Dense(32, activation="relu")(text_in)
merged = layers.Concatenate()([a, b])
x = layers.Dense(32, activation="relu")(merged)
price = layers.Dense(1, name="price")(x)
model = keras.Model(inputs=[numeric_in, text_in], outputs=price)
Multiple Outputs
python
inputs = keras.Input(shape=(64,))
x = layers.Dense(64, activation="relu")(inputs)
category = layers.Dense(5, activation="softmax", name="category")(x)
price = layers.Dense(1, name="price")(x)
model = keras.Model(inputs, [category, price])
model.compile(
optimizer="adam",
loss={"category": "sparse_categorical_crossentropy", "price": "mse"}
)
Skip (Residual) Connections
A skip connection adds the input of a block to its output. This makes very deep networks easier to train and is the key idea behind ResNet.
python
inputs = keras.Input(shape=(64,))
x = layers.Dense(64, activation="relu")(inputs)
y = layers.Dense(64)(x)
added = layers.Add()([x, y]) # skip connection
added = layers.Activation("relu")(added)
outputs = layers.Dense(10, activation="softmax")(added)
model = keras.Model(inputs, outputs)
Reusing and Sharing Layers
python
shared = layers.Dense(32, activation="relu")
a = keras.Input(shape=(16,))
b = keras.Input(shape=(16,))
out_a = shared(a) # the same weights are used for both inputs
out_b = shared(b)
| Use | API |
| Simple stack of layers | Sequential |
| Branches, merges, multiple inputs or outputs | Functional |
| Completely custom behavior | Model subclassing (advanced lesson) |
Conclusion
The Functional API is the professional choice for most architectures. If you can draw the network as a diagram of boxes and arrows, you can build it with the Functional API.
Quick Summary: Create Input, call layers like functions, and join with Model(inputs, outputs). Use Concatenate and Add for merging.