What is the main effect of applying post-training quantization to a TensorFlow model?
Think about how changing number formats affects size and speed.
Post-training quantization converts weights from 32-bit floats to 8-bit integers, reducing model size and speeding up inference with minimal accuracy loss.
What will be the output of the following code snippet after training for 2 epochs?
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())
Consider the pruning schedule and how sparsity changes over steps.
The pruning schedule increases sparsity gradually from 0 to 0.5 over 100 steps. After 2 epochs (less than 100 steps), sparsity is still low, so fraction of zero weights is close to 0.01.
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?
Think about techniques that reduce model size without retraining.
Post-training quantization reduces model size and speeds up inference by converting weights to int8 without needing retraining, ideal for edge devices.
In TensorFlow model pruning, what is the effect of setting final_sparsity to 0.8 in the pruning schedule?
Focus on what sparsity means in pruning context.
Final sparsity of 0.8 means 80% of weights are set to zero at pruning end, making the model very sparse and smaller.
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?
Consider how quantization affects numerical precision.
Dynamic range quantization reduces precision by converting weights and activations to lower bit formats, which can cause some accuracy loss.
