Vertex AI: Export and deploy a BigQuery Machine Learning Model for Prediction

1. Overview

In this lab, you will train a model with BigQuery Machine Learning, and then export and deploy that model to Vertex AI. This is the newest AI product offering on Google Cloud.

What you learn

You'll learn how to:

  • Train a model with BigQuery Machine Learning (BQML)
  • Export your BQML model to Cloud Storage
  • Deploy your trained BQML to Vertex AI
  • Get predictions on your deployed model

The total cost to run this lab on Google Cloud is about $2.

2. Intro to Vertex AI

This lab uses the newest AI product offering available on Google Cloud. Vertex AI integrates the ML offerings across Google Cloud into a seamless development experience. Previously, models trained with AutoML and custom models were accessible via separate services. The new offering combines both into a single API, along with other new products. You can also migrate existing projects to Vertex AI. If you have any feedback, please see the support page.

Vertex AI includes many different products to support end-to-end ML workflows. This lab will focus on the products highlighted below: Prediction and Notebooks.

Vertex product overview

3. Setup your environment

You'll need a Google Cloud Platform project with billing enabled to run this codelab. To create a project, follow the instructions here.

Step 1: Enable the Compute Engine API

Navigate to Compute Engine and select Enable if it isn't already enabled. You'll need this to create your notebook instance.

Step 2: Enable the Vertex AI API

Navigate to the Vertex AI section of your Cloud Console and click Enable Vertex AI API.

Vertex AI dashboard

Step 3: Create a Notebooks instance

We'll use Notebooks to get predictions after we've deployed our model. From the Vertex AI section of your Cloud Console, click on Notebooks:

Vertex AI menu

From there, select New Instance. Then select the TensorFlow Enterprise 2.3 instance type without GPUs:

TFE instance

Use the default options and then click Create. Once the instance has been created, select Open JupyterLab:

Open CAIP Notebook

4. Train a BigQuery ML model

BigQuery ML lets you train custom machine learning models using data stored in BigQuery. You can train and get predictions from models all in SQL. For this lab, we'll use a publicly available dataset in BigQuery to predict the likelihood of default on a credit card payment.

Step 1: Create a BigQuery dataset in your project

To train a model in BigQuery ML, you'll need to create a dataset within your project to store this model. Click on your project in the left menu bar, and then select Create Dataset:

Create BQ dataset

In the Dataset ID field, enter cc_default. Leave the rest of the fields as is and click Create dataset.

Step 2: Run a CREATE MODEL query

In the BigQuery Query editor, run the following CREATE MODEL query to create and train a BigQuery ML model on the public dataset we'll be using. Be sure to replace your-project with the name of your Cloud project:

  `your-project.cc_default.logistic_model` OPTIONS(model_type='LOGISTIC_REG',
    input_label_cols=['default_payment_next_month']) AS

This will create a logistic regression model that outputs a numerical value ranging from 0 to 1, indicating take about a minute to run. When it completes, you'll see the model under your dataset:

BQML model details

You can click on the model to look at its training and evaluation metrics.

5. Export your BigQuery ML model

With a trained BQML model, we can use the BQML SQL syntax to get predictions or we can export the model to deploy it elsewhere. Here we'll export our model so that we can deploy it to Vertex AI to scalably serve the model and get predictions.

Step 1: Create a Cloud Storage Bucket for your model

In the model details, click Export Model:

Export BQML model

This will prompt you to enter the Google Cloud Storage (GCS) location where you'd like your model's assets to be exported. If you don't have a GCS bucket yet, don't worry! We're about to create one. First, click Browse:

Browse GCS buckets

Then click the + icon to create a new bucket:

New GCS bucket

Give it a unique name (Storage bucket names need to be globally unique). Click Continue. In the next step, under Location type select Region and choose any of the regions from the dropdown:

Bucket location

Use the default storage class, and under access control make sure Uniform is selected:

Uniform access control

Click continue and use the defaults for the rest of the options. Then click Create.

Step 2: Export the BQML model

With your new bucket created, enter model-assets (or anything you'd like) in the Name field and then click Select:

Model export path

Then click Export. This will create a job in BigQuery to export your model in TensorFlow's SavedModel format to the newly created GCS bucket you specified. This will take about a minute to export.

While your model is being exported, navigate to the Storage section of your Cloud console. When your job completes, you should see your model assets exported to the bucket you just created under a model-assets subdirectory:

Exported model assets

6. Import the model to Vertex AI

In this step we'll reference the GCS storage location where we just exported our model assets to create and import the model to Vertex AI.

Step 1: Import the model

In your Cloud console, navigate to the Vertex AI Models section. From there, select Import:

Import model

In the first step, give your model the name predict_default. Select the same region where you created your bucket (either us-central1, europe-west4, or asia-east1. Then click Continue. In Model settings, keep "Import model artifacts into a new pre-built container" selected.

In the Model framework dropdown, select TensorFlow. Then select 2.3 as the framework version.

In the Model artifact location field, click Browse, click into the GCS bucket you just created, and click on the model-assets directory:

Model assets path

Then click Import. It will take a few minutes to import your model. Once it has been created, you'll see it in the models section of your Cloud console:

Models section

7. Deploy the model to an endpoint

Now that we've uploaded our model, the next step is to create an Endpoint in Vertex. A Model resource in Vertex can have multiple endpoints associated with it, and you can split traffic between endpoints.

Step 1: Creating an endpoint

On your model page, navigate to the Deploy and test tab and click Deploy to endpoint:

Deploy and test

Give your endpoint a name, like default_pred_v1, leave the traffic splitting settings as is, and then select a machine type for your model deployment. We used an n1-highcpu-2 here, but you can choose whichever machine type you'd like.

Then select Done and click Continue. Leave the selected location settings as is and then click Deploy. Your endpoint will take a few minutes to deploy. When it is completed you'll see a green check mark next to it:

Deployed endpoint

You're getting close! Now you're ready to get predictions on your deployed model.

8. Getting predictions on our deployed model

There are a few options for getting model predictions:

  • Vertex AI UI
  • Vertex AI API

Here we'll show how to get predictions through the API

Step 1: Get model predictions with the Vertex AI API

To show you how to get model predictions here, we'll be using the Vertex Notebook instance you created at the beginning of this lab.

Next, open the notebook instance you created, and open a Python 3 notebook from the Launcher:

Open notebook

In your notebook, run the following in a cell to write a test example to a JSON file named default-pred.json. We'll send this example to our model for a prediction:

%%writefile default-pred.json
  "instances": [
    {"age": 39,
    "bill_amt_1": 47174,
    "bill_amt_2": 47974,
    "bill_amt_3": 48630,
    "bill_amt_4": 50803,
    "bill_amt_5": 30789,
    "bill_amt_6": 15874,
    "education_level": "1",
    "limit_balance": 50000,
    "marital_status": "2",
    "pay_0": 0,
    "pay_3": 0,
    "pay_4": 0,
    "pay_5": "0",
    "pay_6": "0",
    "pay_amt_1": 1800,
    "pay_amt_2": 2000,
    "pay_amt_3": 3000,
    "pay_amt_4": 2000,
    "pay_amt_5": 2000,
    "pay_amt_6": 2000,
    "sex": "1"}

Next, in the Vertex UI, click on Sample Request for the endpoint you just deployed:

Sample request

Copy the code from Step 4 on the REST tab into your notebook and run the cell:

Request variables

Then add a variable for the region where you've created your bucket and model. Replace your-region in the string below:

REGION="your-region" # either us-central1, europe-west4, or asia-east1

Finally, make a prediction request to your endpoint with curl by copying the following code into your notebook:

!curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \$PROJECT_ID/locations/$REGION/endpoints/$ENDPOINT_ID:predict \
-d "@default-pred.json"

You should see something like the following (exact prediction values will vary slightly):

  "predictions": [
      "default_payment_next_month_values": [
      "default_payment_next_month_probs": [
      "predicted_default_payment_next_month": [
  "deployedModelId": "your-model-id"

The values in default_payment_next_month_probs show the probability for each class. For this example, our model predicts there is an 81% chance that this person will not default on their next payment. The .819 value corresponds with the 0 class (not default).

🎉 Congratulations! 🎉

You've learned how to:

  • Train a model in BQML
  • Export your BQML model to Cloud Storage
  • Import the BQML model to Vertex AI for prediction
  • Create a Vertex AI model and deploy the model to an endpoint
  • Get predictions on your deployed endpoint via Vertex Notebooks and curl

To learn more about different parts of the Vertex AI, check out the documentation.

9. Cleanup

If you'd like to continue using the notebook you created in this lab, it is recommended that you turn it off when not in use. From the Notebooks UI in your Cloud Console, select the notebook and then select Stop:

Stop instance

If you'd like to delete the notebook entirely, simply click the Delete button in the top right.

To delete the endpoint you deployed, navigate to the Endpoints section of your Vertex AI console and click the delete icon:

Delete endpoint

To delete the Storage Bucket, using the Navigation menu in your Cloud Console, browse to Storage, select your bucket, and click Delete:

Delete storage