CHAPTER 10
Intermediate
Working with TensorFlow Keras API
Updated: May 16, 2026
6 min read
# CHAPTER 10
Working with TensorFlow Keras API
1. Introduction
Until now, we have used theSequential API. It is incredibly easy to read, but it has a massive limitation: it forces data to move in a single, straight line. What if you want to build an advanced model that takes an Image *and* Text as inputs simultaneously? What if you want a model that outputs a Regression prediction *and* a Classification prediction at the same time? To build these cutting-edge architectures, we must graduate to the Keras Functional API.
2. Learning Objectives
By the end of this chapter, you will be able to:- Explain the limitations of the Sequential API.
- Understand the syntax of the Keras Functional API.
- Build models with non-linear topologies (branching).
- Build a Multi-Input neural network.
-
Utilize the
Modelclass.
3. The Functional API Syntax
In the Sequential API, you pass a list of layers. In the Functional API, you define each layer as an independent function, and you explicitly pass the output of the previous layer into it, like connecting pipes.Let's recreate the simple digit classifier using the Functional API to see the difference.
python
*It looks a bit stranger, but notice how we explicitly control exactly where the data x flows at every step.*
4. Non-Linear Topologies (Branching)
Because we control the data flow, we can split the data into two different paths! This is the core concept behind complex modern architectures like ResNets.
python
5. Multi-Input Models
Imagine you are predicting house prices. You have tabular data (Square footage, Bedrooms) AND an image of the house. You need a Multi-Input model!
python
6. Training Multi-Input Models
When you callmodel.fit() on a multi-input model, you simply pass a Python list containing your multiple datasets!
python
7. Model Subclassing (Advanced)
For absolute bleeding-edge researchers, Keras offers a third way to build models: Subclassing. You write standard Object-Oriented Python classes (class MyModel(tf.keras.Model):) and write a custom call() method to dictate the forward pass. This is complex and generally not recommended for beginners, but you will see it in PyTorch-style codebases.
8. Common Mistakes
-
Forgetting the Input Layer: In the Sequential API, you can sometimes get away with omitting the
Inputlayer. In the Functional API, theInput()object is absolutely mandatory. It is the starting pipe for the entire network.
-
Variable Name Confusion: In Functional API code, you often see
x = Dense()(x). Beginners get confused because the variable is constantly overwriting itself. This is standard practice in Deep Learning to save memory, as you don't need to save the intermediate outputs of every hidden layer into a unique variable.
9. Best Practices
-
Name your layers: In multi-input/output models, use the
name=""parameter (e.g.,Dense(10, name="price_output")). When you print themodel.summary(), having named layers makes debugging a massive architecture 100x easier.
10. Exercises
- 1. Rewrite a simple 2-layer Sequential model into Functional API syntax.
-
2.
In the Functional API, what specific Keras class is used at the very end to bind the
inputsandoutputstogether?
11. MCQ Quiz with Answers
Question 1
What is the primary limitation of the Keras Sequential API?
Question 2
In the Functional API, how do you combine data from two different parallel layers (branches)?
12. Interview Questions
- Q: Explain the syntactical difference between defining a layer in the Sequential API vs the Functional API.
- Q: Give a real-world business example of a scenario where a Multi-Input model would be required over a standard Sequential model.