0
0
TensorFlowml~20 mins

Flatten and Dense layers in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flatten and Dense Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output shape after Flatten layer
Given the following TensorFlow model snippet, what is the shape of the tensor after the Flatten layer?
TensorFlow
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(28, 28, 3)),
    tf.keras.layers.Flatten(),
])

output_shape = model.output_shape
A(None, 3)
B(None, 28, 28, 3)
C(None, 784)
D(None, 2352)
Attempts:
2 left
💡 Hint
Multiply the dimensions except the batch size: 28 * 28 * 3.
Model Choice
intermediate
2:00remaining
Choosing the correct Dense layer output size
You want to build a classification model with 10 classes. Which Dense layer configuration correctly outputs the class scores?
Atf.keras.layers.Dense(1, activation='sigmoid')
Btf.keras.layers.Dense(10, activation='softmax')
Ctf.keras.layers.Dense(10, activation='relu')
Dtf.keras.layers.Dense(5, activation='softmax')
Attempts:
2 left
💡 Hint
For multi-class classification, output layer size equals number of classes with softmax activation.
Hyperparameter
advanced
2:00remaining
Effect of changing units in Dense layer
What is the effect of increasing the number of units in a Dense layer from 64 to 256 in a neural network?
AIncreases model capacity and may improve learning but risks overfitting
BHas no effect on model capacity or performance
CDecreases model capacity and reduces overfitting risk
DAlways guarantees better accuracy on test data
Attempts:
2 left
💡 Hint
More units mean more parameters and complexity.
Metrics
advanced
2:00remaining
Interpreting accuracy with Dense output layer
A model with a Dense output layer of 3 units and softmax activation achieves 85% accuracy on validation data. What does this accuracy represent?
AThe percentage of samples predicted as class 1
BThe average probability output by the model
CThe percentage of samples correctly classified into one of the 3 classes
DThe loss value of the model on validation data
Attempts:
2 left
💡 Hint
Accuracy measures correct predictions over total samples.
🔧 Debug
expert
2:00remaining
Identifying error in model with Flatten and Dense layers
Consider this model code snippet. What error will it raise when running model.fit()?
TensorFlow
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(28, 28, 3)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
AValueError: Input to Dense layer must be 2D, but received 3D input
BNo error, model compiles and fits correctly
CTypeError: Flatten layer cannot follow Dense layer
DRuntimeError: Output shape mismatch in last Dense layer
Attempts:
2 left
💡 Hint
Dense layers expect 2D inputs (batch_size, features).