TensorFlow Test 4 β Questions and Answers
Question 1: What is the difference between model.test_on_batch() and model.evaluate() in Keras?
- test_on_batch processes one batch without looping; evaluate loops over the full dataset (Correct answer)
- test_on_batch is faster for large datasets
- evaluate only works with tf.data; test_on_batch works with numpy
- They are identical
Correct answer: test_on_batch processes one batch without looping; evaluate loops over the full dataset
test_on_batch() runs a single forward pass on one batch and returns metrics immediately, while evaluate() iterates over all batches.
Question 2: In TensorFlow, what does AUC stand for and what does it measure in binary classification testing?
- Aggregate Uncertainty Coefficient; measures model calibration
- Area Under the Curve; measures the ROC curve area representing ranking quality (Correct answer)
- Average Uncertainty Cost; measures false positive rate
- Absolute Unit Comparison; measures absolute accuracy
Correct answer: Area Under the Curve; measures the ROC curve area representing ranking quality
AUC (Area Under the ROC Curve) summarizes classifier performance across all decision thresholds; 1.0 is perfect, 0.5 is random.
Question 3: When testing a TF model for fairness, you want to measure accuracy across demographic subgroups. Which approach is most appropriate?
- Use model.evaluate() on the full dataset only
- Filter the test set per subgroup and call model.evaluate() on each subset (Correct answer)
- Use higher learning rate during evaluation
- Remove demographic features from the model
Correct answer: Filter the test set per subgroup and call model.evaluate() on each subset
Slicing the test set by subgroup and evaluating each slice separately reveals performance disparities across groups.
Question 4: Which TensorFlow Model Analysis (TFMA) concept allows you to evaluate metrics on slices of data?
- SlicingSpec (Correct answer)
- FeatureSpec
- TransformSpec
- EvalSpec
Correct answer: SlicingSpec
SlicingSpec in TFMA defines which feature columns to slice on, enabling per-slice metric computation during evaluation.
Question 5: What does the return_dict=True argument do in model.evaluate()?
- Returns metrics as a Python dict keyed by metric name instead of a list (Correct answer)
- Enables distributed evaluation
- Caches results to disk
- Returns only the loss value
Correct answer: Returns metrics as a Python dict keyed by metric name instead of a list
With return_dict=True, evaluate() returns {'loss': ..., 'accuracy': ..., ...} making it easier to access metrics by name.
Question 6: What is the role of the sample_weight argument in model.evaluate()?
- Sets the learning rate scaling factor
- Assigns per-sample importance weights when computing the loss and metrics (Correct answer)
- Controls dropout rate during evaluation
- Specifies the batch size
Correct answer: Assigns per-sample importance weights when computing the loss and metrics
sample_weight lets you weight certain examples more heavily (e.g., rare classes) when computing aggregated loss and metric values.
Question 7: In TensorFlow, tf.keras.metrics.SparseCategoricalAccuracy is preferred over CategoricalAccuracy when:
- Labels are one-hot encoded vectors
- Labels are integer class indices rather than one-hot vectors (Correct answer)
- You have binary classification
- You are using regression
Correct answer: Labels are integer class indices rather than one-hot vectors
SparseCategoricalAccuracy expects integer labels (e.g., 3), while CategoricalAccuracy expects one-hot vectors (e.g., [0,0,0,1,0]).
What is the difference between model.test_on_batch() and model.evaluate() in Keras?