0
0
TensorFlowml~20 mins

Input shape specification in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Input Shape Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output shape of the model's first layer?
Consider the following TensorFlow Keras model code. What is the output shape of the first Dense layer?
TensorFlow
import tensorflow as tf
model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(28, 28)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu')
])
output_shape = model.layers[2].output_shape
A(None, 28, 28, 64)
B(None, 64)
C(64,)
D(28, 28, 64)
Attempts:
2 left
💡 Hint
Remember that Flatten changes the input shape before the Dense layer.
Model Choice
intermediate
2:00remaining
Which input shape is correct for grayscale 32x32 images?
You want to build a CNN model in TensorFlow for grayscale images of size 32x32 pixels. Which input_shape argument is correct for the first Conv2D layer?
A(32, 32)
B(1, 32, 32)
C(32, 32, 1)
D(None, 32, 32, 1)
Attempts:
2 left
💡 Hint
TensorFlow expects channels last format by default.
Hyperparameter
advanced
2:00remaining
What happens if input_shape is omitted in the first layer?
In TensorFlow Keras, what is the effect of not specifying input_shape in the first layer of a Sequential model?
AModel will automatically add an InputLayer with shape (None, None).
BModel will raise an error when building because input shape is unknown.
CModel will use a default input shape of (None, 1).
DModel will infer input shape from the first batch of data during training.
Attempts:
2 left
💡 Hint
Think about how Keras can build models dynamically.
🔧 Debug
advanced
2:00remaining
Why does this model raise a shape error?
Given this model code, why does it raise a shape mismatch error when training?
TensorFlow
import tensorflow as tf
model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=(20,)),
    tf.keras.layers.Dense(5)
])

import numpy as np
x = np.random.rand(32, 10)
y = np.random.rand(32, 5)
model.compile(optimizer='adam', loss='mse')
model.fit(x, y, epochs=1)
AInput data x has shape (32, 10) but model expects input shape (20,), causing mismatch.
BOutput data y has wrong shape; it should be (32, 10) to match last layer.
CThe Dense layers require 3D input but x is 2D.
DThe model is missing an activation function in the last layer.
Attempts:
2 left
💡 Hint
Check the input_shape argument and the shape of x.
🧠 Conceptual
expert
3:00remaining
Why specify batch size in Input layer for stateful RNNs?
In TensorFlow, why is it necessary to specify batch_size in the Input layer when building a stateful RNN model?
ABecause stateful RNNs keep states between batches, fixed batch size is needed to maintain state consistency.
BBecause batch_size controls the number of neurons in the RNN layer.
CBecause batch_size defines the sequence length for the RNN input.
DBecause batch_size is required to enable GPU acceleration.
Attempts:
2 left
💡 Hint
Think about how RNN states are preserved across batches.