TensorFlow Test 5 β Questions and Answers
Question 1: What is the purpose of tf.keras.callbacks.CSVLogger during model evaluation training runs?
- Logs hyperparameters to a CSV for tuning
- Appends epoch-level metrics to a CSV file for later analysis (Correct answer)
- Converts the model to CSV format
- Reads test data from a CSV
Correct answer: Appends epoch-level metrics to a CSV file for later analysis
CSVLogger writes epoch number, loss, and metric values to a CSV after each epoch, making it easy to plot learning curves later.
Question 2: How can you measure inference latency of a TensorFlow model on a test input?
- Use model.compile(metrics=['latency'])
- Time model.predict() calls using Python's time module or tf.timestamp() (Correct answer)
- Use model.evaluate(latency=True)
- Enable verbose=2 in model.predict()
Correct answer: Time model.predict() calls using Python's time module or tf.timestamp()
Wrapping model.predict() with time.time() before and after gives wall-clock latency; averaging over multiple runs reduces variance.
Question 3: What does model.predict() return for a multi-output Keras model?
- A single concatenated numpy array
- A list of numpy arrays, one per output (Correct answer)
- A dictionary keyed by output layer name
- A tf.Tensor of shape (batch, total_outputs)
Correct answer: A list of numpy arrays, one per output
For multi-output models, predict() returns a list where each element corresponds to one output layer's predictions.
Question 4: When evaluating a TF model deployed via TensorFlow Serving, which protocol is used for REST inference requests?
- gRPC only
- HTTP/REST via the Predict API endpoint (Correct answer)
- WebSocket
- MQTT
Correct answer: HTTP/REST via the Predict API endpoint
TensorFlow Serving exposes both a gRPC interface and an HTTP/REST API; the REST endpoint accepts POST requests to /v1/models/{name}:predict.
Question 5: Which metric should you use when testing a TensorFlow model on a highly imbalanced binary classification dataset?
- Accuracy
- F1-Score or AUC-ROC (Correct answer)
- Mean Squared Error
- Cosine Similarity
Correct answer: F1-Score or AUC-ROC
Accuracy is misleading on imbalanced data; F1-Score balances precision and recall, and AUC-ROC evaluates ranking across thresholds.
Question 6: In TensorFlow Lite, how do you run inference on a quantized model during testing?
- Use model.evaluate() directly
- Load with tf.lite.Interpreter, allocate tensors, set input, invoke(), get output (Correct answer)
- Use tf.saved_model.load() and call signatures
- Run tflite_convert --evaluate
Correct answer: Load with tf.lite.Interpreter, allocate tensors, set input, invoke(), get output
TFLite inference requires creating a tf.lite.Interpreter, calling allocate_tensors(), setting the input tensor, invoking the interpreter, and reading the output tensor.
Question 7: What does the class_weight parameter in model.fit() affect, and how does this impact how you interpret test metrics?
- It changes model architecture; test metrics are unaffected
- It upweights minority class loss during training; test metrics should still be computed on unweighted data for fair comparison (Correct answer)
- It applies weights at test time automatically
- It has no effect on training or testing
Correct answer: It upweights minority class loss during training; test metrics should still be computed on unweighted data for fair comparison
class_weight adjusts training loss to compensate for imbalance; model.evaluate() on the test set should be run without class weights to reflect real-world performance.
What is the purpose of tf.keras.callbacks.CSVLogger during model evaluation training runs?