Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the Functional API in TensorFlow?
The Functional API is a way to build neural networks by defining layers as functions and connecting them. It allows creating complex models with multiple inputs and outputs, unlike the simple sequential model.
Click to reveal answer
beginner
How do you define an input layer using the Functional API?
You use tf.keras.Input(shape=(input_shape,)) to create an input layer that specifies the shape of the input data.
Click to reveal answer
beginner
How do you connect layers in the Functional API?
You call a layer like a function on the output of the previous layer. For example, x = tf.keras.layers.Dense(10)(input_layer) connects a Dense layer to the input.
Click to reveal answer
beginner
How do you create a model using the Functional API?
You create a model by specifying inputs and outputs: model = tf.keras.Model(inputs=input_layer, outputs=output_layer).
Click to reveal answer
intermediate
Why use the Functional API instead of Sequential API?
The Functional API supports models with multiple inputs or outputs, shared layers, and non-linear topology, which the Sequential API cannot handle.
Click to reveal answer
Which TensorFlow function is used to define the input layer in the Functional API?
Atf.keras.Input()
Btf.keras.Layer()
Ctf.keras.Sequential()
Dtf.keras.Dense()
✗ Incorrect
tf.keras.Input() defines the input layer specifying the shape of the input data.
How do you connect layers in the Functional API?
ABy stacking layers sequentially only
BBy adding layers to a list
CBy calling one layer as a function on the output of another layer
DBy using a for loop
✗ Incorrect
In the Functional API, layers are connected by calling them as functions on previous layers' outputs.
What is the correct way to create a model in the Functional API?
You create a model by specifying the input and output layers explicitly.
Which of the following is a key advantage of the Functional API over the Sequential API?
ASupports multiple inputs and outputs
BEasier to use for simple models
CAutomatically compiles the model
DRequires less code for sequential models
✗ Incorrect
The Functional API supports complex architectures with multiple inputs and outputs.
What does the following code do? x = Dense(10)(input_layer)
ACreates a new input layer
BDefines the output layer only
CCompiles the model
DConnects a Dense layer with 10 units to the input_layer
✗ Incorrect
This code applies a Dense layer with 10 units to the input_layer, connecting them.
Explain how to build a simple neural network model using the Functional API in TensorFlow.
Think about how layers connect and how the model is finalized.
You got /3 concepts.
Describe the benefits of using the Functional API compared to the Sequential API.
Consider model complexity and flexibility.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using TensorFlow's Functional API over the Sequential API?
easy
A. It allows building models with multiple inputs and outputs.
B. It automatically tunes hyperparameters.
C. It requires less code to build simple models.
D. It only supports linear stacks of layers.
Solution
Step 1: Understand Functional API capabilities
The Functional API allows explicit connections between layers, supporting complex architectures.
Step 2: Compare with Sequential API
Sequential API only supports simple linear stacks, while Functional API supports multiple inputs and outputs.
Final Answer:
It allows building models with multiple inputs and outputs. -> Option A
Quick Check:
Functional API = multiple inputs/outputs [OK]
Hint: Functional API supports complex models with multiple inputs/outputs [OK]
Common Mistakes:
Thinking Functional API is simpler for linear models
Confusing hyperparameter tuning with model building
Assuming Sequential API supports multiple inputs
2. Which of the following is the correct way to start defining a model using the Functional API?
easy
A. inputs = tf.keras.Input(shape=(32,))
B. inputs = tf.keras.layers.Dense(32)
C. model = tf.keras.Model()
D. model = tf.keras.Sequential()
Solution
Step 1: Identify how to define input in Functional API
Functional API starts with tf.keras.Input() to define the input shape.
Step 2: Check other options
Sequential() is for Sequential API, Model() requires inputs and outputs, Dense is a layer, not input.
Final Answer:
inputs = tf.keras.Input(shape=(32,)) -> Option A
Quick Check:
Start Functional API with Input() [OK]
Hint: Use Input() to start Functional API models [OK]
Common Mistakes:
Using Sequential() instead of Input() to start
Trying to create Model() without inputs and outputs
Confusing layers with input definitions
3. What will be the output shape of the model defined below?
inputs = tf.keras.Input(shape=(10,))
x = tf.keras.layers.Dense(5)(inputs)
outputs = tf.keras.layers.Dense(2)(x)
model = tf.keras.Model(inputs, outputs)
print(model.output_shape)
medium
A. (None, 10)
B. (10, 2)
C. (None, 5)
D. (None, 2)
Solution
Step 1: Trace the model layers
Input shape is (10,), first Dense layer outputs (5,), second Dense outputs (2,).
Step 2: Understand output shape format
Output shape includes batch size None, so final output shape is (None, 2).
Final Answer:
(None, 2) -> Option D
Quick Check:
Output shape = (None, 2) [OK]
Hint: Output shape matches last layer units with batch None [OK]
Common Mistakes:
Confusing input shape with output shape
Ignoring batch dimension None
Mixing layer output sizes
4. Identify the error in this Functional API model code:
inputs = tf.keras.Input(shape=(8,))
x = tf.keras.layers.Dense(4)(inputs)
outputs = tf.keras.layers.Dense(1)(inputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
medium
A. Input shape must be (4,), not (8,).
B. Output layer should connect to x, not inputs.
C. Model() requires no arguments.
D. Dense layers cannot be used in Functional API.
Solution
Step 1: Check layer connections
The output layer is connected directly to inputs, skipping the intermediate Dense layer x.
Step 2: Correct the output connection
Output should connect to x to use the transformed data, not inputs.
Final Answer:
Output layer should connect to x, not inputs. -> Option B
Quick Check:
Output must connect to last layer, not input [OK]
Hint: Connect outputs to last layer, not inputs [OK]
Common Mistakes:
Connecting output directly to inputs
Changing input shape unnecessarily
Misunderstanding Model() arguments
5. You want to build a model with two inputs: one for images (shape 64x64x3) and one for metadata (shape 10). Which Functional API code snippet correctly defines the inputs?
hard
A. img_input = tf.keras.layers.Input(shape=(64,64,3))
meta_input = tf.keras.layers.Input(shape=(10,))
B. inputs = tf.keras.Input(shape=(64,64,3,10))
C. img_input = tf.keras.Input(shape=(64,64,3))
meta_input = tf.keras.Input(shape=(10,))
D. inputs = tf.keras.Input(shape=(64,64,3))
inputs = tf.keras.Input(shape=(10,))
Solution
Step 1: Define separate inputs for each data type
Functional API allows multiple inputs by defining each with tf.keras.Input and correct shapes.
Step 2: Check each option for correctness
img_input = tf.keras.Input(shape=(64,64,3))
meta_input = tf.keras.Input(shape=(10,)) correctly defines two inputs separately; B merges shapes incorrectly; A uses wrong module; D overwrites inputs variable.
Final Answer:
img_input = tf.keras.Input(shape=(64,64,3))
meta_input = tf.keras.Input(shape=(10,)) -> Option C
Quick Check:
Multiple inputs need separate Input() calls [OK]
Hint: Use separate Input() for each input tensor [OK]