Return Vertex AI 批量预​​测的自定义模型的置信度分数

Return confidence score with custom model for Vertex AI batch predictions

我将预训练的 scikit 学习分类模型上传到 Vertex AI 并 运行 对 5 个样本进行批量预测。它只是返回了一个没有置信度分数的错误预测列表。我没有在 SDK 文档或 Google 控制台中的任何地方看到如何获取包含置信度分数的批量预测。这是 Vertex AI 可以做的事情吗?

我的目的是使用以下代码自动化批量预测管道。

# Predict
# "csv", ""bigquery", "tf-record", "tf-record-gzip", or "file-list"
batch_prediction_job = model.batch_predict(
    job_display_name = job_display_name,
    gcs_source = input_path,
    instances_format = "", # jsonl, csv, bigquery, 
    gcs_destination_prefix = output_path,
    starting_replica_count = 1,
    max_replica_count = 10,
    sync = True,
)

batch_prediction_job.wait()

return batch_prediction_job.resource_name

我在 google 控制台中进行了测试,以确保我的输入数据格式正确。

我不这么认为;我猜顶点提供的股票 sklearn 容器没有提供这样的分数。您可能需要写一个 custom container.

您现在可以使用 custom prediction routines 执行此操作。这里有几个很好的 e2e 例子

下面是 predictor.py 的界面示例:

%%writefile src/predictor.py
import joblib
import numpy as np
import pickle

from google.cloud import storage
from google.cloud.aiplatform.prediction.sklearn.predictor import SklearnPredictor
import json

class CprPredictor(SklearnPredictor):
    
    def __init__(self):
        return
    
    def load(self, gcs_artifacts_uri: str):
        """Loads the preprocessor artifacts."""
        gcs_client = storage.Client()
        with open("model.joblib", 'wb') as gcs_model:
            gcs_client.download_blob_to_file(
                gcs_artifacts_uri + "/model.joblib", gcs_model
            )

        with open("model.joblib", "rb") as f:
            self._model = joblib.load("model.joblib")

    
    def predict(self, instances):
        outputs = self._model.predict_proba(instances) 
        return outputs

请注意,您目前必须使用 SDK 的实验分支,可能会更改为官方分支。