3

I built and deployed an XGBoost regressor model on vertex AI and I am trying to make some predictions using Vertex AI python SDK. Here's my code:

client = aiplatform.gapic.PredictionServiceClient.from_service_account_json(filename="filename.json",client_options=client_options)

endpoint = client.endpoint_path(project=project, location=location, endpoint=endpoint_id)
 
response = client.predict(endpoint=endpoint, instances=instances)
   

predictions = response.predictions

Here is the value of the variable instances :

array([[4.8700000e+00, 3.6505380e+06, 2.0000000e+01, 2.0210000e+03,
    4.0000000e+00, 5.3000000e+01, 0.0000000e+00, 0.0000000e+00,
    1.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    1.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    0.0000000e+00, 0.0000000e+00, 1.0000000e+00, 0.0000000e+00,
    0.0000000e+00],
   [3.7100000e+00, 1.2678515e+07, 2.0000000e+01, 2.0210000e+03,
    4.0000000e+00, 5.3000000e+01, 0.0000000e+00, 0.0000000e+00,
    1.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    1.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    0.0000000e+00, 0.0000000e+00, 0.0000000e+00, 0.0000000e+00,
    0.0000000e+00, 0.0000000e+00, 1.0000000e+00, 0.0000000e+00,
    0.0000000e+00]])

(Here I am trying to make two predictions). When doing this I am getting an error:

Unable to coerce value

I tried to look at the number of features allowed by the model and it is the same as the input here (29). I tried looking into the xgboost code and according to the code the input is correct. So I am guessing it is an issue on vertex AI side. But I don't know how to fix it.

Carlos S
  • 31
  • 2
  • Can you try converting your `instances` variable to protobuf value before prediction? This is based from [predict()](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.prediction_service.PredictionServiceClient#google_cloud_aiplatform_v1_services_prediction_service_PredictionServiceClient_predict) See this [helper to_proto_value function](https://github.com/googleapis/google-cloud-python/issues/9887#issuecomment-560953735). – Ricco D May 11 '22 at 03:15
  • I tried that approach and I had the same issue as when I am trying to make predictions with lists instead of numpy array. I get the error: 400 "Prediction failed: Exception during sklearn prediction: 'list' object has no attribute 'shape'" – Carlos S May 12 '22 at 10:57
  • Vertex AI does not seem to accept ndrarrays (or any kind of arrays) as input for prediction on its endpoints. I believe it may need to be a plain list. I added an answer for a similar problem with Vertex AI (https://stackoverflow.com/a/75811588/14123992). Maybe that brings you closer to a solution? @CarlosS also, when you sent out the predictions as lists, was it just a list of two lists for the example you posted above? – sneekiesnek Mar 28 '23 at 07:48
  • Any chance your input contains a wrong datatype? And were you able to run the code on this input outside vertex? – Dennis Jaheruddin Apr 08 '23 at 07:31

0 Answers0