Bird
Raised Fist0
TensorFlowml~20 mins

Feature extraction approach in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Feature Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of feature extraction with TensorFlow Hub
What is the shape of the output tensor after extracting features from a batch of 3 images using a TensorFlow Hub MobileNetV2 feature vector model?
TensorFlow
import tensorflow as tf
import tensorflow_hub as hub

model_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
feature_extractor = hub.KerasLayer(model_url, input_shape=(224, 224, 3))

batch_images = tf.random.uniform([3, 224, 224, 3])
features = feature_extractor(batch_images)
output_shape = features.shape
print(output_shape)
A(3, 1280)
B(224, 224, 3)
C(3, 1001)
D(1, 1280)
Attempts:
2 left
💡 Hint
The MobileNetV2 feature vector outputs a 1280-dimensional vector per image.
❓ Model Choice
intermediate
1:30remaining
Choosing a pre-trained model for feature extraction
You want to extract features from images to use in a new classifier. Which pre-trained model is best suited for feature extraction without the classification head?
AA fully connected network trained on tabular data
BResNet50 with include_top=True
CResNet50 with include_top=False
DA randomly initialized CNN
Attempts:
2 left
💡 Hint
Feature extraction requires a model trained on images without the final classification layer.
❓ Hyperparameter
advanced
2:00remaining
Effect of freezing layers during feature extraction
When using a pre-trained CNN for feature extraction, what is the effect of freezing all convolutional layers during training of a new classifier on top?
AThe convolutional layers' weights do not update, speeding up training and preserving learned features.
BThe convolutional layers' weights update, allowing the model to learn new features from scratch.
CThe model will overfit immediately due to frozen layers.
DThe model will not train because frozen layers cause errors.
Attempts:
2 left
💡 Hint
Freezing layers means their weights stay fixed during training.
❓ Metrics
advanced
1:30remaining
Evaluating feature extraction effectiveness
You extracted features from images using a pre-trained model and trained a new classifier. Which metric best indicates if the extracted features are useful for the classification task?
ATraining loss of the pre-trained model
BValidation accuracy of the new classifier
CNumber of layers in the pre-trained model
DSize of the input images
Attempts:
2 left
💡 Hint
The quality of features is reflected in how well the classifier performs on unseen data.
🔧 Debug
expert
2:30remaining
Debugging feature extraction output shape mismatch
You use a TensorFlow Hub feature extractor expecting output shape (None, 2048), but your code raises a shape mismatch error. Which code change fixes this?
TensorFlow
import tensorflow_hub as hub
import tensorflow as tf

feature_extractor = hub.KerasLayer('https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/5', input_shape=(224, 224, 3))

inputs = tf.random.uniform([5, 224, 224, 3])
features = feature_extractor(inputs)
print(features.shape)
AChange input_shape to (None, 224, 224, 3) in KerasLayer
BResize input images to (299, 299, 3) before passing to feature_extractor
CUse a different model URL that outputs shape (None, 1280)
DChange input_shape to (224, 224, 3) and ensure inputs have batch size dimension
Attempts:
2 left
💡 Hint
The input_shape parameter expects the shape of a single image, excluding batch size.

Practice

(1/5)
1. What is the main purpose of using feature extraction with a pre-trained TensorFlow model?
easy
A. To use learned features from a large dataset to help a new task
B. To train a model from scratch with random weights
C. To increase the size of the dataset automatically
D. To reduce the number of layers in the model

Solution

  1. Step 1: Understand feature extraction concept

    Feature extraction uses a pre-trained model to get useful features learned from a large dataset.
  2. Step 2: Identify the benefit for new tasks

    This helps when you have little data or want faster training by reusing learned features.
  3. Final Answer:

    To use learned features from a large dataset to help a new task -> Option A
  4. Quick Check:

    Feature extraction = reuse learned features [OK]
Hint: Feature extraction reuses knowledge from big datasets [OK]
Common Mistakes:
  • Thinking feature extraction trains from scratch
  • Confusing feature extraction with data augmentation
  • Believing it reduces model layers automatically
2. Which TensorFlow code snippet correctly freezes the base model layers for feature extraction?
easy
A. base_model.trainable = True
B. base_model.trainable = False
C. base_model.freeze()
D. base_model.layers.freeze = True

Solution

  1. Step 1: Recall how to freeze layers in TensorFlow

    Setting trainable = False on the base model freezes its weights.
  2. Step 2: Check each option's correctness

    base_model.trainable = False correctly sets base_model.trainable = False. Others are invalid syntax or do not freeze layers.
  3. Final Answer:

    base_model.trainable = False -> Option B
  4. Quick Check:

    Freeze layers = trainable False [OK]
Hint: Freeze layers by setting trainable to False [OK]
Common Mistakes:
  • Using trainable = True to freeze layers
  • Calling non-existent freeze() method
  • Trying to set freeze attribute on layers directly
3. Given this code, what is the output shape of features?
base_model = tf.keras.applications.MobileNetV2(input_shape=(128,128,3), include_top=False)
base_model.trainable = False
inputs = tf.keras.Input(shape=(128,128,3))
features = base_model(inputs)
print(features.shape)
medium
A. (None, 4, 4, 1280)
B. (None, 7, 7, 512)
C. (None, 1, 1, 1000)
D. (None, 128, 128, 3)

Solution

  1. Step 1: Understand MobileNetV2 output with include_top=False

    MobileNetV2 outputs feature maps of size (4,4,1280) for 128x128 input when top layers are excluded.
  2. Step 2: Confirm batch dimension is None

    The batch size is None (unspecified), so output shape is (None, 4, 4, 1280).
  3. Final Answer:

    (None, 4, 4, 1280) -> Option A
  4. Quick Check:

    MobileNetV2 feature shape = (None,4,4,1280) [OK]
Hint: MobileNetV2 outputs 4x4x1280 features for 128x128 input [OK]
Common Mistakes:
  • Confusing output with classification layer shape
  • Assuming output shape equals input shape
  • Ignoring batch dimension None
4. Identify the error in this feature extraction code snippet:
base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False)
base_model.trainable = False
inputs = tf.keras.Input(shape=(224,224,3))
x = base_model(inputs)
x = tf.keras.layers.Dense(10)(x)
model = tf.keras.Model(inputs, x)
medium
A. Base model weights not loaded correctly
B. Input shape is incorrect for ResNet50
C. Dense layer applied to 4D tensor without flattening
D. trainable should be True to freeze layers

Solution

  1. Step 1: Check tensor shapes before Dense layer

    Base model outputs 4D tensor (batch, height, width, channels). Dense expects 2D input.
  2. Step 2: Identify missing flatten or global pooling

    Applying Dense directly to 4D tensor causes error; flatten or global pooling needed first.
  3. Final Answer:

    Dense layer applied to 4D tensor without flattening -> Option C
  4. Quick Check:

    Dense needs 2D input, flatten first [OK]
Hint: Flatten or pool before Dense layer [OK]
Common Mistakes:
  • Applying Dense directly to 4D output
  • Confusing input shape requirements
  • Misunderstanding trainable flag usage
5. You want to use feature extraction with MobileNetV2 on 64x64 images but get an error when adding a Dense layer after the base model. What is the best way to fix this?
hard
A. Set base_model.trainable = True to allow training
B. Change input shape to 224x224 to match MobileNetV2 default
C. Remove the Dense layer and use base model output directly
D. Add a GlobalAveragePooling2D layer before the Dense layer

Solution

  1. Step 1: Understand why Dense layer causes error

    MobileNetV2 outputs 4D feature maps; Dense layers need 2D input.
  2. Step 2: Use GlobalAveragePooling2D to convert 4D to 2D

    Adding GlobalAveragePooling2D flattens spatial dimensions, making output suitable for Dense.
  3. Step 3: Evaluate other options

    Changing input size is not required; training base model is optional; removing Dense loses classification ability.
  4. Final Answer:

    Add a GlobalAveragePooling2D layer before the Dense layer -> Option D
  5. Quick Check:

    Pool features before Dense layer [OK]
Hint: Use pooling to flatten features before Dense [OK]
Common Mistakes:
  • Changing input size unnecessarily
  • Trying to train frozen base model
  • Skipping pooling and causing shape errors
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