Bird
Raised Fist0
TensorFlowml~20 mins

Model optimization (quantization, pruning) in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Model Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Quantization Impact

What is the main effect of applying post-training quantization to a TensorFlow model?

AIt reduces the model size and speeds up inference by converting weights from float32 to int8.
BIt increases the model accuracy by adding more layers to the neural network.
CIt removes unnecessary neurons to make the model smaller without changing data types.
DIt converts the model to use floating-point 64-bit precision for better precision.
Attempts:
2 left
💡 Hint

Think about how changing number formats affects size and speed.

❓ Predict Output
intermediate
2:00remaining
Output of Pruning Callback in TensorFlow

What will be the output of the following code snippet after training for 2 epochs?

TensorFlow
import tensorflow as tf
from tensorflow_model_optimization.sparsity import keras as sparsity

model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=(20,)),
    tf.keras.layers.Dense(1)
])

pruning_params = {
    'pruning_schedule': sparsity.PolynomialDecay(
        initial_sparsity=0.0,
        final_sparsity=0.5,
        begin_step=0,
        end_step=100
    )
}

pruned_model = sparsity.prune_low_magnitude(model, **pruning_params)

pruned_model.compile(optimizer='adam', loss='mse')

import numpy as np
x = np.random.rand(10, 20)
y = np.random.rand(10, 1)

history = pruned_model.fit(x, y, epochs=2, verbose=0)

final_weights = sparsity.strip_pruning(pruned_model).layers[0].get_weights()[0]

print((final_weights == 0).mean())
AA float value close to 0.5 representing the fraction of zero weights after 2 epochs.
BA float value close to 1.0 representing all weights pruned after 2 epochs.
CRaises an AttributeError because get_weights() is not accessible.
DA float value close to 0.01 representing the fraction of zero weights after 2 epochs.
Attempts:
2 left
💡 Hint

Consider the pruning schedule and how sparsity changes over steps.

❓ Model Choice
advanced
2:00remaining
Choosing Optimization for Edge Devices

You want to deploy a TensorFlow model on a low-power edge device with limited memory and CPU. Which model optimization technique is best to reduce model size and improve inference speed without retraining?

APost-training quantization converting weights to int8 format.
BTraining a larger model with more layers to improve accuracy.
CApplying dropout layers during training to reduce overfitting.
DUsing batch normalization layers to stabilize training.
Attempts:
2 left
💡 Hint

Think about techniques that reduce model size without retraining.

❓ Hyperparameter
advanced
2:00remaining
Effect of Pruning Hyperparameters

In TensorFlow model pruning, what is the effect of setting final_sparsity to 0.8 in the pruning schedule?

APruning will stop after 80 training epochs automatically.
BThe model will keep 80% of weights active and prune only 20%.
C80% of the model weights will be zeroed out at the end of pruning, making the model very sparse.
DThe learning rate will be reduced by 80% during pruning.
Attempts:
2 left
💡 Hint

Focus on what sparsity means in pruning context.

❓ Metrics
expert
2:00remaining
Evaluating Quantized Model Accuracy

You quantized a TensorFlow model using dynamic range quantization. After quantization, you evaluate accuracy on the test set and observe a drop from 92% to 85%. What is the most likely reason for this accuracy drop?

AThe model architecture changed during quantization, adding extra layers.
BDynamic range quantization reduces precision of weights and activations, causing some accuracy loss.
CThe test data was shuffled incorrectly after quantization.
DQuantization always improves accuracy, so this must be a measurement error.
Attempts:
2 left
💡 Hint

Consider how quantization affects numerical precision.

Practice

(1/5)
1. What is the main goal of model pruning in TensorFlow model optimization?
easy
A. Remove less important weights to reduce model size
B. Convert model weights to lower precision
C. Add more layers to improve accuracy
D. Increase the batch size during training

Solution

  1. Step 1: Understand pruning concept

    Pruning means removing parts of the model that contribute less, usually weights with small values.
  2. Step 2: Identify pruning effect

    This reduces model size and speeds up inference by making the model simpler.
  3. Final Answer:

    Remove less important weights to reduce model size -> Option A
  4. Quick Check:

    Pruning = Remove less important weights [OK]
Hint: Pruning cuts small weights to shrink model size [OK]
Common Mistakes:
  • Confusing pruning with quantization
  • Thinking pruning adds layers
  • Believing pruning changes batch size
2. Which of the following is the correct way to apply post-training quantization in TensorFlow Lite?
easy
A. converter.optimizations = [tf.lite.Optimize.DEFAULT]
B. model.compile(optimizer='adam', loss='mse')
C. tf.keras.layers.Dense(10, activation='relu')
D. model.fit(x_train, y_train, epochs=10)

Solution

  1. Step 1: Recognize quantization syntax

    Post-training quantization in TensorFlow Lite uses the converter.optimizations property set to [tf.lite.Optimize.DEFAULT].
  2. Step 2: Eliminate unrelated options

    Other options relate to training or model layers, not quantization.
  3. Final Answer:

    converter.optimizations = [tf.lite.Optimize.DEFAULT] -> Option A
  4. Quick Check:

    Quantization uses converter.optimizations = [tf.lite.Optimize.DEFAULT] [OK]
Hint: Quantization uses converter.optimizations setting [OK]
Common Mistakes:
  • Confusing training code with quantization setup
  • Using model.compile for quantization
  • Applying quantization inside model.fit
3. What will be the output shape of the pruned model after applying TensorFlow Model Optimization pruning API on a model with input shape (None, 28, 28, 1) and a Dense layer with 10 units?
medium
A. (None, 28, 28, 10)
B. (None, 10)
C. (None, 28, 28, 1)
D. (10,)

Solution

  1. Step 1: Understand pruning effect on shape

    Pruning removes weights but does not change the model architecture or output shape.
  2. Step 2: Identify output shape of Dense layer

    A Dense layer with 10 units outputs shape (None, 10) regardless of pruning.
  3. Final Answer:

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

    Pruning keeps output shape same = (None, 10) [OK]
Hint: Pruning doesn't change output shape, only weights [OK]
Common Mistakes:
  • Thinking pruning changes output dimensions
  • Confusing input shape with output shape
  • Assuming pruning adds extra dimensions
4. You applied pruning to your TensorFlow model but noticed no size reduction after saving. What is the likely cause?
medium
A. You used quantization instead of pruning
B. Pruning increases model size by adding masks
C. Model was not stripped of pruning wrappers before saving
D. Model was saved in HDF5 format

Solution

  1. Step 1: Understand pruning wrappers

    Pruning adds wrappers around layers that must be stripped before saving to reduce size.
  2. Step 2: Identify saving step

    If wrappers are not stripped, the saved model keeps extra pruning info, so size doesn't reduce.
  3. Final Answer:

    Model was not stripped of pruning wrappers before saving -> Option C
  4. Quick Check:

    Strip pruning wrappers before saving = smaller model [OK]
Hint: Strip pruning wrappers before saving model [OK]
Common Mistakes:
  • Assuming pruning always reduces size without stripping
  • Confusing pruning with quantization effects
  • Ignoring model saving format impact
5. You want to optimize a TensorFlow model for deployment on a mobile device with limited memory and CPU. Which combination of techniques is best to reduce model size and speed up inference without much accuracy loss?
hard
A. Convert model to float64 precision
B. Only increase training epochs without optimization
C. Use dropout layers and batch normalization
D. Apply pruning during training and then post-training quantization

Solution

  1. Step 1: Identify optimization goals

    For mobile deployment, reducing size and speeding inference are key, while keeping accuracy.
  2. Step 2: Choose suitable techniques

    Pruning removes unnecessary weights, quantization reduces number precision, both reduce size and speed up inference.
  3. Step 3: Eliminate wrong options

    Increasing epochs doesn't optimize size; dropout and batch norm are for training regularization; float64 increases size.
  4. Final Answer:

    Apply pruning during training and then post-training quantization -> Option D
  5. Quick Check:

    Pruning + quantization = smaller, faster model [OK]
Hint: Combine pruning and quantization for best optimization [OK]
Common Mistakes:
  • Ignoring quantization after pruning
  • Confusing training tricks with optimization
  • Using higher precision increases size
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