Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Prediction failed: Could not initialize DMatrix from inputs

I have trained the model in bigquery (BOOSTED_TREE_CLASSIFIER) and also tested it on test data, their model worked well and gave 98% accuracy and predicted the result, but when I imported the model in the model registry and created the endpoint, I faced this error while testing it in endpoint.Screenshot 2024-11-28 143401.pngScreenshot 2024-12-02 232851.png

0 2 435
2 REPLIES 2

Hi @andy_08,

Welcome to Google Cloud Community!

The error โ€œPrediction failed: Could not initialize DMatrix from inputsโ€ suggests that your prediction is sending a one-dimensional NumPy array to the model, while the model expects a two-dimensional array. This is a common issue when transitioning from training/testing to real-time predictions.

Here's a breakdown of the problem and possible way to fix it:

The problem: 

  • Local Testing: During local testing, you may provide prediction data as a single row (1D array), which BigQueryโ€™s BOOSTED_TREE_CLASSIFIER typically handles when predicting a single instance.
  • Endpoint Prediction: However, the prediction endpoint requires input data in a 2D array format, even for a single row, to align with the model's training format.

You may try this workaround:

You may need to reshape your input JSON into a 2D array, even if it includes only a single sample.

In addition, after making these changes, re-test the prediction endpoint with the updated JSON.

You may refer to the following documentation, which can help you understand the overall workflow of deploying models:

I hope the above information is helpful. 

I ran into a similar problem recently. I found out that this issue is caused when the prediction schema is either not defined or ill-defined at the time of deployment. I was able to fix this by exporting the model directly to Vertex AI during the training phase by adding the MODEL_REGISTRY, VERTEX_AI_MODEL_ID, and VERTEX_AI_MODEL_VERSION_ALIASES parameters in the CREATE MODEL statement. These automatically export the model to the Vertex AI registry and define the schema, which fixes the problem.