Bird
Raised Fist0
TensorFlowml~20 mins

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

Choose your learning style10 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).

Practice

(1/5)
1. What is the main purpose of a Flatten layer in a TensorFlow model?
easy
A. To convert multi-dimensional input into a one-dimensional vector
B. To reduce the number of neurons in the model
C. To add non-linearity to the model
D. To perform convolution operations on the input

Solution

  1. Step 1: Understand the role of Flatten layer

    The Flatten layer takes input with multiple dimensions (like images) and converts it into a single long list of numbers.
  2. Step 2: Connect Flatten to Dense layers

    This one-dimensional vector is needed because Dense layers expect flat input to connect every input to neurons.
  3. Final Answer:

    To convert multi-dimensional input into a one-dimensional vector -> Option A
  4. Quick Check:

    Flatten = flatten input shape [OK]
Hint: Flatten makes input flat for Dense layers [OK]
Common Mistakes:
  • Thinking Flatten reduces neurons
  • Confusing Flatten with convolution
  • Assuming Flatten adds activation
2. Which of the following is the correct way to add a Flatten layer followed by a Dense layer with 10 neurons in TensorFlow?
easy
A. model.add(tf.keras.layers.Dense(10)); model.add(tf.keras.layers.Flatten())
B. model.add(tf.keras.layers.Flatten(10)); model.add(tf.keras.layers.Dense())
C. model.add(tf.keras.layers.Flatten()); model.add(tf.keras.layers.Dense(10))
D. model.add(tf.keras.layers.Dense()); model.add(tf.keras.layers.Flatten(10))

Solution

  1. Step 1: Check the order of layers

    Flatten must come before Dense because Dense needs flat input.
  2. Step 2: Verify syntax for layers

    Flatten() takes no arguments; Dense(10) creates 10 neurons.
  3. Final Answer:

    model.add(tf.keras.layers.Flatten()); model.add(tf.keras.layers.Dense(10)) -> Option C
  4. Quick Check:

    Flatten first, then Dense with units [OK]
Hint: Flatten first, then Dense with units [OK]
Common Mistakes:
  • Placing Dense before Flatten
  • Passing arguments to Flatten
  • Omitting units in Dense
3. What will be the output shape after applying a Flatten layer to an input tensor of shape (None, 28, 28, 3)?
medium
A. (None, 28, 28, 3)
B. (None, 2352)
C. (None, 28)
D. (None, 784)

Solution

  1. Step 1: Calculate total elements per sample

    Input shape is (28, 28, 3). Multiply 28 * 28 * 3 = 2352.
  2. Step 2: Understand Flatten output shape

    Flatten converts input to (None, 2352), where None is batch size.
  3. Final Answer:

    (None, 2352) -> Option B
  4. Quick Check:

    28*28*3 = 2352 [OK]
Hint: Multiply all dimensions except batch [OK]
Common Mistakes:
  • Confusing batch size with feature size
  • Using 28*28 instead of 28*28*3
  • Assuming Flatten changes batch size
4. Identify the error in this TensorFlow model snippet:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(64, activation='relu', input_shape=(28,28,1)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='softmax'))
medium
A. Flatten layer should come before the first Dense layer
B. Dense layer cannot have input_shape with 3D data
C. Activation function 'relu' is not allowed in Dense layers
D. The model is correct and will run without errors

Solution

  1. Step 1: Analyze layer order

    The first Dense layer expects flat input but input_shape is 3D (28,28,1). Flatten should come first.
  2. Step 2: Check Flatten placement

    Flatten must convert 3D input to 1D before Dense layers.
  3. Final Answer:

    Flatten layer should come before the first Dense layer -> Option A
  4. Quick Check:

    Flatten before Dense with multi-dim input [OK]
Hint: Flatten before Dense if input is multi-dimensional [OK]
Common Mistakes:
  • Placing Dense before Flatten with image input
  • Thinking activation relu is invalid
  • Ignoring input_shape format
5. You want to build a simple image classifier using TensorFlow for 32x32 RGB images. Which model architecture correctly uses Flatten and Dense layers to prepare the data for classification into 5 classes?
hard
A.
model = tf.keras.Sequential([
  tf.keras.layers.Dense(5, activation='softmax', input_shape=(32,32,3)),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(64, activation='relu')
])
B.
model = tf.keras.Sequential([
  tf.keras.layers.Dense(64, activation='relu', input_shape=(32,32,3)),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(5, activation='softmax')
])
C.
model = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(32,32,3)),
  tf.keras.layers.Dense(5, activation='softmax'),
  tf.keras.layers.Dense(64, activation='relu')
])
D.
model = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(32,32,3)),
  tf.keras.layers.Dense(64, activation='relu'),
  tf.keras.layers.Dense(5, activation='softmax')
])

Solution

  1. Step 1: Check input shape and Flatten usage

    Input is (32,32,3), so Flatten must come first to convert to 1D vector.
  2. Step 2: Verify Dense layers order

    Dense(64) with relu comes after Flatten, then Dense(5) with softmax for 5 classes.
  3. Step 3: Identify incorrect options

    Some architectures place Dense before Flatten, which is wrong.
    model = tf.keras.Sequential([
      tf.keras.layers.Flatten(input_shape=(32,32,3)),
      tf.keras.layers.Dense(5, activation='softmax'),
      tf.keras.layers.Dense(64, activation='relu')
    ])
    places output Dense before hidden Dense, which is incorrect.
  4. Final Answer:

    model = tf.keras.Sequential([ tf.keras.layers.Flatten(input_shape=(32,32,3)), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(5, activation='softmax') ]) -> Option D
  5. Quick Check:

    Flatten first, hidden Dense, then output Dense [OK]
Hint: Flatten first, then hidden Dense, then output Dense [OK]
Common Mistakes:
  • Putting Dense before Flatten with image input
  • Placing output layer before hidden layers
  • Wrong input_shape placement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard