Vertex AI SDK Logging: Metrics vs Classification Metrics

PMLE By islever Editorial Last updated

Short answer

D Use the aiplatform.log_metrics function to log the F1 score, and use the aiplatform.log_classification_metrics function to log the confusion matrix.

Question

You want to migrate a scikit-learn classifier model to TensorFlow. You plan to train the TensorFlow classifier model using the same training set that was used to train the scikit-learn model, and then compare the performances using a common test set. You want to use the Vertex AI Python SDK to manually log the evaluation metrics of each model and compare them based on their F1 scores and confusion matrices. How should you log the metrics?
  1. Use the aiplatform.log_classification_metrics function to log the F1 score, and use the aiplatform.log_metrics function to log the confusion matrix.
  2. Use the aiplatform.log_classification_metrics function to log the F1 score and the confusion matrix.
  3. Use the aiplatform.log_metrics function to log the F1 score and the confusion matrix.
  4. Use the aiplatform.log_metrics function to log the F1 score: and use the aiplatform.log_classification_metrics function to log the confusion matrix. Correct Answer

Key Takeaways

The trap is assuming one function handles all metrics; log_classification_metrics is strictly for structured classification outputs (confusion matrices, ROC), while log_metrics is for scalar values.

Learn how to properly log evaluation metrics in Vertex AI using the Python SDK. Correctly separate scalar metrics like F1 scores from structured data like confusion matrices using the appropriate logging functions.

Common mistake: Selecting Option B (using `log_classification_metrics` for both) is the most common mistake. Candidates often assume the 'classification' logger is a catch-all for any classification-related metric, failing to recognize its limitation to structured matrix/curve data rather than scalar scores.

Expert Analysis

Distinction Between General and Classification-Specific Logging

The core of this question tests knowledge of the Vertex AI Python SDK's logging functions, specifically aiplatform.log_metrics versus aiplatform.log_classification_metrics. The scenario involves comparing a scikit-learn model to a TensorFlow model using F1 scores and confusion matrices. To correctly log these, one must understand what data structures each function accepts.

Analysis of aiplatform.log_classification_metrics

According to Google Cloud documentation, aiplatform.log_classification_metrics is specialized for classification tasks. It explicitly supports logging complex structures like confusion matrices (as a dictionary with 'rows' and 'labels') and ROC curves. However, it does not natively accept simple scalar metrics like the F1 score as direct arguments in the same way general metrics are logged. Its primary purpose is to handle the structural integrity of multi-class or binary classification outputs that require label alignment.

Analysis of aiplatform.log_metrics

The aiplatform.log_metrics function is designed for generic, scalar metric logging. It takes a dictionary of key-value pairs where values are numeric. This makes it ideal for logging single-point evaluation results like the F1 score, accuracy, or precision derived from sklearn.metrics. Using log_metrics for a confusion matrix would be inappropriate because a confusion matrix is a 2D array/table, which exceeds the scope of simple scalar logging and lacks the semantic context provided by the classification-specific logger.

Why Other Options Fail

Option B suggests logging both using log_classification_metrics. While this function handles confusion matrices well, it is not designed to ingest a pre-calculated scalar F1 score directly without additional processing or specific formatting that isn't standard for this function's signature. Option A reverses the logic, attempting to use the general logger for structured data and the specific logger for scalars, which contradicts their design purposes. Option D correctly separates them: use the general logger (log_metrics) for the scalar F1 score and the specialized logger (log_classification_metrics) for the structured confusion matrix. This aligns with the SDK's best practices for maintaining clear, parseable experiment data in Vertex AI Experiments.

Official Reference

Exam Strategy

When answering questions about Vertex AI SDK logging, always check the data type of the metric. If it is a simple number (scalar), use log_metrics. If it is a structure requiring labels (like a confusion matrix or ROC curve), use log_classification_metrics. Do not force structured data into the general logger, nor scalar data into the specialized classifier logger.

Frequently Asked Questions

Can I log a confusion matrix using aiplatform.log_metrics?

No, log_metrics is designed for scalar numeric values. A confusion matrix is a 2D structure that requires the semantic understanding of log_classification_metrics to be properly visualized and tracked in Vertex AI Experiments.

Does log_classification_metrics support F1 scores?

Not directly as a standalone scalar argument. It is optimized for passing true labels, predictions, and computing/confusing matrices or ROC curves. For pre-calculated scalar scores like F1, use log_metrics.

More PMLE FAQ →

Community Votes

D
50%
B
50%

50% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.

Discussion 23 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 23

  1. b1a8fae 👍 8 Answered D
    I go with D. log_classification_metrics currently support confusion matrix and ROC curve. https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_classification_metrics Because it is not explicitly mentioned in the docs of log_classification_metrics, I assume F1 Score must be logged with log_metrics. https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_metrics (if accuracy and recall are logged in the example, probably F1 is done the same way)
  2. fitri001 👍 7 Answered B
    aiplatform.log_classification_metrics is specifically designed for logging classification metrics, which includes F1 score and confusion matrix. aiplatform.log_metrics is a more generic function for logging any kind of metric, but it wouldn't capture the rich structure of a confusion matrix. Therefore, using aiplatform.log_classification_metrics allows you to log both F1 score and confusion matrix in a single call, simplifying your code and ensuring proper handling of these classification-specific metrics.
  3. Omi_04040 👍 4 Answered D
    D. Utilize the aiplatform.log_metrics function to log the F1 score, and employ the aiplatform.log_classification_metrics function to log the confusion matrix. Utilize the aiplatform.log_metrics function to log the F1 score, and employ the aiplatform.log_classification_metrics function to log the confusion matrix. This is the correct approach. aiplatform.log_metrics is appropriate for logging general metrics such as the F1 score, and aiplatform.log_classification_metrics is ideal for logging classification-specific metrics like the confusion matrix. Reference: https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_classification_metrics
  4. b1a8fae 👍 4
    Option B also suggests sing aiplatform.log_classification_metrics for the confusion matrix. Which is supported, btw. https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_classification_metrics
  5. bobjr 👍 4 Answered D
    https://cloud.google.com/vertex-ai/docs/experiments/log-data#classification-metrics log_classification_metrics -> only the confusion matrix, not the F1scores log_metrics -> any number you want -> you can use it to store a F1 scores
Show 18 more comments
  1. qaz09 👍 3 Answered B
    In this example linked below you can see that they use log_classification_metrics and the output contains both F1 score and confusion matrix. Hence I am voting for B. https://colab.research.google.com/github/whylabs/whylogs/blob/mainline/python/examples/integrations/writers/Writing_Classification_Performance_Metrics_to_WhyLabs.ipynb
  2. gscharly 👍 3 Answered D
    According to docs, log_classification_metrics supports confusion matrix and ROC curve. Not sure if it means that it only supports those... Assuming those are the only ones supported, I would got with D
  3. fitri001 👍 2
    While aiplatform.log_metrics can handle numeric values like F1 score, it wouldn't capture the complexity of a confusion matrix. Confusion matrix is a two-dimensional table and requires specific handling for proper logging.expand_more aiplatform.log_classification_metrics is designed for classification tasks and understands the structure of both F1 score and confusion matrix, allowing them to be logged efficiently in a single function call.
  4. tardigradum 👍 2
    Hi fitri001. You are usually right but, I this particular case, I think D is the right answer. As you can see here in the link I provide you below, it "Currently support confusion matrix and ROC curve." Link: https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_classification_metrics
  5. Mohamed_Tareq 👍 2 Answered D
    f1 = f1_score(y_true, y_pred) aiplatform.log_metrics({ "f1_score_manual": f1 }) ========================================= with aiplatform.start_run("sklearn-model-eval"): aiplatform.log_classification_metrics( labels=["0", "1"], confusion_matrix={ "rows": cm.tolist() }, )
  6. fitri001 👍 2
    Therefore, using separate functions like log_metrics for F1 score and log_classification_metrics for confusion matrix would be inefficient and might not capture the matrix structure accurately.
  7. gscharly 👍 2
    forgot to add the link: https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_classification_metrics
  8. daidai75 👍 2 Answered B
    Actually, the F1 score is calculated by the Precision and recall metrics. The the log_classification_metrics is OK for both confusion matrix and F1 score
  9. pinimichele01 👍 2
    https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_classification_metrics
  10. YangG 👍 2 Answered D
    d
  11. Yan_X 👍 1 Answered B
    The aiplatform.log_classification_metrics function is designed to log classification metrics, including the F1 score and the confusion matrix. It takes the following arguments: predictions: The predicted labels. labels: The true labels. weight: The weight of each sample. logger: The logger to use. ---------------------------- The aiplatform.log_metrics function is designed to log general metrics, such as accuracy, loss, and precision. It takes the following arguments: metric: The metric to log. value: The value of the metric. step: The step at which the metric was logged. logger: The logger to use.
  12. pikachu007 👍 1 Answered B
    Option A: It's incorrect because aiplatform.log_metrics is a more general function that doesn't provide the same specialized structure for classification metrics. Option C: While technically possible to log both metrics using aiplatform.log_metrics, it's less optimal as it requires manual formatting and might not be as easily interpreted by Vertex AI's visualization tools. Option D: This is incorrect as it suggests using aiplatform.log_classification_metrics for the confusion matrix, but that function doesn't support logging confusion matrices directly.
  13. joao_01 👍 1 Answered B
    Guys, Its b. Pls look at this link: https://www.youtube.com/watch?v=a_YXZ5UltkU In the minute 7:20 you'll see the result of the log_classification_metrics() and there (pay attention) its the F1. After, its shows the confusion matrix. You're welcome
  14. pinimichele01 👍 1
    link?? i find only: https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform#google_cloud_aiplatform_log_classification_metrics so D NOT B
  15. 364db8d 👍 1 Answered D
    D is correct, F1 can be captured with log_metrics, but not with log_classification_metrics. You have to use 2 methods in this case.
  16. omermahgoub 👍 1 Answered B
    aiplatform.log_classification_metrics to log metrics relevant to classification tasks, including F1 score and confusion matrix.
  17. rajshiv 👍 1 Answered B
    I think we can log both metrics together. D is a close second but B seems to be a better answer
  18. OpenKnowledge 👍 1 Answered D
    F1 score is not a classification matrix. So use aiplatform.log_metrics for logging F1 score

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Practice All PMLE Questions

Access 65 questions with complete answers and detailed explanations.

View Full PMLE Practice Test →

← Back to PMLE Study Guide