TensorFlow Test 3 β Questions and Answers
Question 1: Which TensorFlow utility helps you measure per-class precision and recall on a test set?
- tf.keras.metrics.Accuracy
- sklearn.metrics.classification_report (used alongside TF) (Correct answer)
- tf.keras.metrics.MeanIoU
- tf.losses.BinaryCrossentropy
Correct answer: sklearn.metrics.classification_report (used alongside TF)
scikit-learn's classification_report is commonly paired with TensorFlow predictions to show per-class precision, recall, and F1.
Question 2: When testing a SavedModel loaded with tf.saved_model.load(), how do you run inference?
- Call model.evaluate() directly
- Access the serving function via model.signatures and call it (Correct answer)
- Call model.predict() as usual
- Use tf.keras.Model.test_on_batch()
Correct answer: Access the serving function via model.signatures and call it
A raw SavedModel exposes its computation through signatures (e.g., model.signatures['serving_default'](input=tensor)).
Question 3: What does tf.keras.metrics.MeanAbsoluteError() measure in a regression test?
- Average squared difference between predictions and targets
- Average absolute difference between predictions and targets (Correct answer)
- Percentage of correct predictions
- Logarithm of prediction error
Correct answer: Average absolute difference between predictions and targets
MAE averages the absolute differences |y_pred - y_true| over all samples, giving an interpretable error in the original units.
Question 4: What is the correct way to reset a Keras metric object between test batches?
- Delete and recreate it
- Call metric.reset_state() (Correct answer)
- Call metric.update_state() with zeros
- Set metric.result() = 0
Correct answer: Call metric.reset_state()
reset_state() clears accumulated state so the metric can start fresh for a new evaluation pass.
Question 5: During model testing, which TensorFlow context manager prevents unnecessary gradient computation?
- tf.GradientTape()
- tf.function()
- tf.no_gradient()
- No special context is needed; use inference_mode in PyTorch instead (Correct answer)
Correct answer: No special context is needed; use inference_mode in PyTorch instead
TensorFlow does not track gradients outside GradientTape by default, so inference requires no special context unlike PyTorch's torch.no_grad().
Question 6: What is a confusion matrix used for in TensorFlow model testing?
- Visualizing weight distributions
- Summarizing correct vs. incorrect class predictions (Correct answer)
- Plotting learning curves
- Checking gradient flow
Correct answer: Summarizing correct vs. incorrect class predictions
A confusion matrix shows counts of true positives, false positives, true negatives, and false negatives per class.
Question 7: Which function computes a confusion matrix directly in TensorFlow?
- tf.math.confusion_matrix() (Correct answer)
- tf.keras.metrics.ConfusionMatrix()
- tf.linalg.diag()
- tf.summary.confusion_matrix()
Correct answer: tf.math.confusion_matrix()
tf.math.confusion_matrix(labels, predictions, num_classes) returns an integer matrix of shape [num_classes, num_classes].
Which TensorFlow utility helps you measure per-class precision and recall on a test set?