0
0
MLOpsdevops~10 mins

Model optimization for serving (quantization, pruning) in MLOps - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to apply post-training quantization using TensorFlow Lite.

MLOps
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [[1]]
tflite_model = converter.convert()
Drag options to blanks, or click blank then click option'
Atf.lite.Optimize.DEFAULT
Btf.lite.Quantize.DEFAULT
Ctf.lite.Optimize.QUANTIZE
Dtf.lite.Optimize.NONE
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent flag like tf.lite.Quantize.DEFAULT
Using NONE disables optimization, so no quantization happens
2fill in blank
medium

Complete the code to prune a Keras model with 50% sparsity.

MLOps
import tensorflow_model_optimization as tfmot
prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude
pruning_params = {'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=0.0, final_sparsity=[1], begin_step=0, end_step=1000)}
model = prune_low_magnitude(original_model, **pruning_params)
Drag options to blanks, or click blank then click option'
A50
B0.5
C0.05
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 50 instead of 0.5 causes errors because sparsity must be between 0 and 1
Using 5 or 0.05 are incorrect values for 50% sparsity
3fill in blank
hard

Fix the error in the pruning callback setup to correctly update pruning steps during training.

MLOps
callbacks = [tfmot.sparsity.keras.UpdatePruningStep(), [1]]
model.fit(train_data, epochs=10, callbacks=callbacks)
Drag options to blanks, or click blank then click option'
Atf.keras.callbacks.EarlyStopping()
Btfmot.sparsity.keras.PruningCallback()
Ctfmot.sparsity.keras.PruningSummaries(log_dir)
Dtf.keras.callbacks.ModelCheckpoint()
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated callbacks like EarlyStopping or ModelCheckpoint
Using a non-existent PruningCallback
4fill in blank
hard

Fill both blanks to create a TensorFlow Lite converter that applies quantization and sets the supported ops.

MLOps
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [[1]]
converter.target_spec.supported_ops = [[2]]
tflite_model = converter.convert()
Drag options to blanks, or click blank then click option'
Atf.lite.Optimize.DEFAULT
Btf.lite.OpsSet.TFLITE_BUILTINS_INT8
Ctf.lite.OpsSet.TFLITE_BUILTINS
Dtf.lite.Optimize.NONE
Attempts:
3 left
💡 Hint
Common Mistakes
Using NONE disables optimization.
Using TFLITE_BUILTINS without INT8 does not fully enable quantized ops.
5fill in blank
hard

Fill all three blanks to create a pruning schedule with 20% initial sparsity, 80% final sparsity, and pruning ending at step 2000.

MLOps
pruning_params = {
  'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=[1], final_sparsity=[2], begin_step=0, end_step=[3])
}
Drag options to blanks, or click blank then click option'
A0.2
B0.8
C2000
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping initial and final sparsity values.
Using incorrect end step values like 1000 instead of 2000.