Bird
Raised Fist0
TensorFlowml~20 mins

CNN architecture for image classification in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CNN Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output shape after Conv2D layer
Given the following TensorFlow code snippet for a CNN layer, what is the output shape of the tensor after the Conv2D layer?
TensorFlow
import tensorflow as tf
input_tensor = tf.random.uniform([1, 64, 64, 3])
conv_layer = tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=2, padding='same')
output_tensor = conv_layer(input_tensor)
print(output_tensor.shape)
A(1, 64, 64, 16)
B(1, 31, 31, 16)
C(1, 62, 62, 16)
D(1, 32, 32, 16)
Attempts:
2 left
💡 Hint
Remember that padding='same' keeps the spatial size divided by stride.
❓ Model Choice
intermediate
1:30remaining
Choosing activation function for CNN output layer
You are building a CNN to classify images into 10 categories. Which activation function should you use in the output layer to get probabilities for each class?
ASoftmax
BReLU
CSigmoid
DTanh
Attempts:
2 left
💡 Hint
The output should represent probabilities that sum to 1.
❓ Hyperparameter
advanced
2:00remaining
Effect of increasing kernel size in Conv2D
In a CNN, what is the most likely effect of increasing the kernel size from 3x3 to 7x7 in the first Conv2D layer?
AThe model captures larger spatial features but increases parameters and computation
BThe model reduces overfitting by decreasing parameters
CThe model output size increases significantly
DThe model training speed increases due to fewer computations
Attempts:
2 left
💡 Hint
Think about how kernel size affects receptive field and parameter count.
❓ Metrics
advanced
2:00remaining
Interpreting CNN training accuracy and loss
During CNN training for image classification, you observe training accuracy steadily increasing but validation accuracy plateaus and then decreases. What does this indicate?
AThe dataset is too small to train
BThe model is underfitting the training data
CThe model is overfitting the training data
DThe learning rate is too low
Attempts:
2 left
💡 Hint
Think about what it means when training improves but validation worsens.
🔧 Debug
expert
2:30remaining
Identifying error in CNN model definition
What error will this TensorFlow CNN model code raise when run?
TensorFlow
import tensorflow as tf
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu', input_shape=(28, 28, 1)),
    tf.keras.layers.MaxPooling2D(pool_size=2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
ARuntimeError: Missing Flatten layer before Dense
BValueError: Input shape must have 3 dimensions for Conv2D
CTypeError: activation function 'relu' not recognized
DNo error, model compiles successfully
Attempts:
2 left
💡 Hint
Check the input_shape parameter for Conv2D layer.

Practice

(1/5)
1. What is the main role of the Conv2D layer in a CNN for image classification?
easy
A. To reduce the size of the image by pooling
B. To decide the final class of the image
C. To flatten the image into a 1D array
D. To detect patterns like edges and shapes in small parts of the image

Solution

  1. Step 1: Understand the function of Conv2D

    The Conv2D layer scans small parts of the image to find patterns such as edges and textures.
  2. Step 2: Compare with other layers

    Pooling layers reduce size, flatten layers reshape data, and dense layers classify. Conv2D focuses on pattern detection.
  3. Final Answer:

    To detect patterns like edges and shapes in small parts of the image -> Option D
  4. Quick Check:

    Conv2D = pattern detection [OK]
Hint: Conv2D finds patterns; pooling shrinks images [OK]
Common Mistakes:
  • Confusing Conv2D with pooling layers
  • Thinking Conv2D flattens the image
  • Believing Conv2D decides the final class
2. Which of the following is the correct way to add a MaxPooling2D layer in TensorFlow Keras?
easy
A. tf.keras.layers.MaxPooling2D(pool_size=(2, 2))
B. tf.keras.layers.MaxPooling(pool=(2, 2))
C. tf.keras.layers.MaxPool2D(size=(2, 2))
D. tf.keras.layers.Pooling2D(pool_size=2)

Solution

  1. Step 1: Recall correct TensorFlow layer syntax

    The correct class name is MaxPooling2D and it uses the argument pool_size to set the window size.
  2. Step 2: Check each option

    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)) uses correct class and argument. Others have wrong class names or argument names.
  3. Final Answer:

    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)) -> Option A
  4. Quick Check:

    MaxPooling2D with pool_size=(2,2) is correct [OK]
Hint: MaxPooling2D uses pool_size, not pool or size [OK]
Common Mistakes:
  • Using wrong layer class names
  • Using incorrect argument names like pool or size
  • Omitting parentheses or tuple for pool_size
3. What will be the output shape after applying this CNN block on an input image of shape (64, 64, 3)?
model = tf.keras.Sequential([
  tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(64,64,3)),
  tf.keras.layers.MaxPooling2D(pool_size=(2,2))
])
output_shape = model.output_shape
medium
A. (None, 32, 32, 32)
B. (None, 62, 62, 32)
C. (None, 31, 31, 32)
D. (None, 64, 64, 32)

Solution

  1. Step 1: Calculate Conv2D output shape

    Conv2D with kernel size 3x3 and default padding='valid' reduces each spatial dimension by 2: 64 - 3 + 1 = 62, so output shape is (62, 62, 32).
  2. Step 2: Calculate MaxPooling2D output shape

    MaxPooling2D with pool size 2x2 halves each spatial dimension: 62 / 2 = 31, so output shape is (31, 31, 32).
  3. Final Answer:

    (None, 31, 31, 32) -> Option C
  4. Quick Check:

    Conv2D valid padding reduces size; pooling halves it [OK]
Hint: Conv2D valid padding shrinks; pooling halves size [OK]
Common Mistakes:
  • Assuming padding='same' by default
  • Forgetting spatial dimension reduction by Conv2D
  • Misapplying pooling size effect
4. Identify the error in this CNN model code snippet:
model = tf.keras.Sequential([
  tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(28,28,1)),
  tf.keras.layers.MaxPooling2D(pool_size=2),
  tf.keras.layers.Dense(10, activation='softmax')
])
medium
A. Missing Flatten layer before Dense
B. Wrong input_shape format
C. MaxPooling2D pool_size should be a tuple
D. Dense layer should have no activation

Solution

  1. Step 1: Check layer order and compatibility

    Conv2D and MaxPooling2D output 3D tensors, but Dense expects 1D input.
  2. Step 2: Identify missing layer

    A Flatten layer is needed to convert 3D output to 1D before Dense.
  3. Final Answer:

    Missing Flatten layer before Dense -> Option A
  4. Quick Check:

    Dense needs 1D input; add Flatten [OK]
Hint: Add Flatten before Dense after Conv/Pooling [OK]
Common Mistakes:
  • Skipping Flatten layer before Dense
  • Assuming Dense accepts 3D input
  • Confusing pool_size tuple requirement
5. You want to build a CNN to classify 100x100 RGB images into 5 classes. Which architecture below is best suited for this task?
hard
A. Dense(128, relu) -> Dense(64, relu) -> Dense(5, softmax)
B. Conv2D(16, (3,3)) -> MaxPooling2D(2,2) -> Conv2D(32, (3,3)) -> MaxPooling2D(2,2) -> Flatten -> Dense(5, softmax)
C. Conv2D(64, (5,5), padding='same') -> Flatten -> Dense(5, softmax)
D. Conv2D(32, (3,3)) -> Conv2D(32, (3,3)) -> Dense(5, softmax)

Solution

  1. Step 1: Analyze each architecture's suitability

    Conv2D(16, (3,3)) -> MaxPooling2D(2,2) -> Conv2D(32, (3,3)) -> MaxPooling2D(2,2) -> Flatten -> Dense(5, softmax) uses multiple Conv2D and MaxPooling layers to extract features and reduce size, then flattens before classification. This is a standard CNN design.
  2. Step 2: Identify issues in other options

    Dense(128, relu) -> Dense(64, relu) -> Dense(5, softmax) uses only Dense layers, ignoring spatial info. Conv2D(64, (5,5), padding='same') -> Flatten -> Dense(5, softmax) lacks pooling, risking large feature maps. Conv2D(32, (3,3)) -> Conv2D(32, (3,3)) -> Dense(5, softmax) misses Flatten and pooling, causing shape errors.
  3. Final Answer:

    Conv2D(16, (3,3)) -> MaxPooling2D(2,2) -> Conv2D(32, (3,3)) -> MaxPooling2D(2,2) -> Flatten -> Dense(5, softmax) -> Option B
  4. Quick Check:

    Multiple Conv+Pooling layers + Flatten + Dense = best CNN [OK]
Hint: Use Conv + Pooling layers before Flatten and Dense [OK]
Common Mistakes:
  • Using only Dense layers for images
  • Skipping pooling layers
  • Missing Flatten before Dense
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