0
0
TensorFlowml~20 mins

Functional API basics in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Functional API Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output shape of a Functional API model
Consider the following TensorFlow Functional API model. What is the shape of the output tensor?
TensorFlow
import tensorflow as tf
inputs = tf.keras.Input(shape=(32,))
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
outputs = tf.keras.layers.Dense(10)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
print(model.output_shape)
A(32, 64)
B(32, 10)
C(None, 64)
D(None, 10)
Attempts:
2 left
💡 Hint
Remember the first dimension is batch size and is None by default.
Model Choice
intermediate
2:00remaining
Choosing the correct Functional API model for multiple inputs
You want to build a model that takes two inputs: one with shape (10,) and another with shape (5,). Which Functional API model code correctly defines this?
A
input1 = tf.keras.Input(shape=(10,))
input2 = tf.keras.Input(shape=(5,))
concat = tf.keras.layers.Concatenate()([input1, input2])
output = tf.keras.layers.Dense(1)(concat)
model = tf.keras.Model(inputs=[input1, input2], outputs=output)
B
input1 = tf.keras.Input(shape=(10,))
input2 = tf.keras.Input(shape=(5,))
concat = tf.keras.layers.Dense(15)(input1)
output = tf.keras.layers.Dense(1)(concat)
model = tf.keras.Model(inputs=[input1, input2], outputs=output)
C
input1 = tf.keras.Input(shape=(10,))
input2 = tf.keras.Input(shape=(5,))
concat = tf.keras.layers.Multiply()([input1, input2])
output = tf.keras.layers.Dense(1)(concat)
model = tf.keras.Model(inputs=[input1, input2], outputs=output)
D
input1 = tf.keras.Input(shape=(10,))
input2 = tf.keras.Input(shape=(5,))
concat = tf.keras.layers.Add()([input1, input2])
output = tf.keras.layers.Dense(1)(concat)
model = tf.keras.Model(inputs=[input1, input2], outputs=output)
Attempts:
2 left
💡 Hint
Concatenate layer joins inputs along last axis; Add and Multiply require same shape inputs.
Hyperparameter
advanced
2:00remaining
Effect of activation function in Functional API model
In a Functional API model, you replace the activation in a Dense layer from 'relu' to 'linear'. What is the most likely effect on the model's training?
AThe model will train faster because linear activation is simpler.
BThe model may train slower or fail to learn complex patterns due to lack of non-linearity.
CThe model will always overfit because linear activation increases complexity.
DThe model will have better accuracy because linear activation avoids vanishing gradients.
Attempts:
2 left
💡 Hint
Activation functions add non-linearity needed for learning complex data.
🔧 Debug
advanced
2:00remaining
Identifying error in Functional API model definition
What error will this Functional API code raise when run?
TensorFlow
import tensorflow as tf
inputs = tf.keras.Input(shape=(20,))
x = tf.keras.layers.Dense(10)(inputs)
outputs = tf.keras.layers.Dense(5)(inputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='mse')
ATypeError: Inputs and outputs must be layers or tensors connected in a graph.
BValueError: Output tensor must be connected to the input tensor.
CNo error; model compiles successfully.
DRuntimeError: Model outputs must come from the last layer applied to inputs.
Attempts:
2 left
💡 Hint
Outputs can be any tensor derived from inputs, not necessarily last layer.
🧠 Conceptual
expert
3:00remaining
Understanding model summary in Functional API
Given a Functional API model with two input layers merged by Concatenate and followed by two Dense layers, what does the model.summary() show for the total params if the first Dense layer has 8 units and the second has 4 units? Assume input shapes are (3,) and (2,).
ATotal params = (3+2)*8 + 8 + 8*4 + 4 = 84
BTotal params = (3+2)*8 + 8 + (3+2)*4 + 4 = 72
CTotal params = (3+2)*8 + 8 + 8*4 + 4*2 = 70
DTotal params = (3+2)*8 + 8 + 8*4 + 4*1 = 66
Attempts:
2 left
💡 Hint
Calculate params as (input_dim * units) + bias per Dense layer, sum all layers.