Use the TrainingJob.deploy method to deploy a trained model in Google Vertex AI environment or in Vantage system. A TDPredictor object is returned which can be used to run prediction.
- For Vertex AI environment, it requires argument format the same as the Model.deploy function in Vertex AI Python SDK.
- For Vantage system, it requires arguments that are required by BYOM and Predictor functions in teradataml.
- You should have registered the model files in Vertex AI before running this method.
- This method should be called after the fit method, that is, model training has completed.
Required Arguments:
- model: Specifies a Vertex AI model object.
- platform: Specifies the platform to deploy the given model:
- 'vantage'
- 'vx-endpoint'
- model_type: Required if platform is 'vantage'. Specifies the type of the model:
- 'pmml'
- 'onnx'
- 'h2o'
- model_filename: Required if platform is 'vantage'. Specifies the Google Cloud Storage file name of the model artifact.
Optional Arguments:
- save_byom_kwargs: Specifies the keyword arguments of teradataml save_byom() function.
If neither table_name is provided nor BYOM catalog set using set_byom_catalog, a table with name 'tdapiclient_byom_models' will be created in the current schema.
- retrieve_byom_kwargs: Specifies the keyword arguments of teradataml retrieve_byom() function.
- vertex_kwargs: Specifies any additional arguments required by TrainingJob.run method.
These parameters are directly supplied to TrainingJob.run method.
Example 1: Use Vertex AI CustomTrainingJob with Vantage DataFrame
- Import required packages.
from tdapiclient import create_tdapi_context, TDApiClient from teradataml import DataFrame
- Create TDApiClient class.
tdapi_context = create_tdapi_context('gcp', gcp_bucket_name="tdapiclient", gcp_bucket_path="/tmp/") tdapi_client = TDApiClient(tdapi_context)
- Create teradataml DataFrame for model training.
input_df = DataFrame(table_name="inputTable")
- Create training job with TDApiClient.
job = tdapi_client.CustomTrainingJob( display_name="custom-train", script_path="train.py", container_uri="<TRAINING_IMAGE_URI>", model_serving_container_image_uri="<PREDICTION_IMAGE_URI>", )
- Submit training job.
model = job.fit(input_df, replica_count=1, model_display_name="custom-model")
- Call model.deploy method to deploy the model in a Vertex AI endpoint.
td_predictor = job.deploy(model, platform='vertex-endpoint')
- Run predict using the deployed model in UDF mode.
df = DataFrame(query="select feature1, feature2 from tbl") output = td_predictor.predict(df, mode='udf')
Example 2: Use Vertex AI without TDApiClient
- Import required packages.
import os from google.cloud import aiplatform
- Define Vertex AI constants.
PROJECT_ID = 'gcp-project' LOCATION = 'us-central1' TRAINING_IMAGE = "us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-9:latest" PREDICTION_IMAGE = "us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-9:latest" BUCKET_ROOT = "gs://tdapiclient-vertex-test"
- Define the credentials to access Google Cloud - the service account JSON file.
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "local/path/to/credentials.json"
- Initialize Vertex AI client.
aiplatform.init( project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_ROOT)
- Create a Managed Tabular Dataset from CSV file.
ds = aiplatform.TabularDataset.create( display_name="abalone", gcs_source=[f"{BUCKET_ROOT}/data/abalone_train.csv"])
- Launch a training job to create a Model.
job = aiplatform.CustomTrainingJob( display_name="custom-train", script_path="train.py", container_uri=TRAINING_IMAGE, requirements=["gcsfs==0.7.1", "tf2onnx"], model_serving_container_image_uri=PREDICTION_IMAGE, )
model = job.run(ds, replica_count=1, model_display_name="abalone-model")
- Deploy the Model.
endpoint = model.deploy(machine_type="n1-standard-4")
endpoint.predict( [ [0.435, 0.335, 0.11, 0.33399999999999996, 0.1355, 0.0775, 0.0965], [0.585, 0.45, 0.125, 0.874, 0.3545, 0.2075, 0.225], ] )
Example 3: Deploy Vertex AI model in Vantage through BYOM mode
- Import required packages.
from tdapiclient import create_tdapi_context, TDApiClient from teradataml import DataFrame
- Create TDApiClient class.
tdapi_context = create_tdapi_context('gcp', gcp_bucket_name="tdapiclient", gcp_bucket_path="/tmp/") tdapi_client = TDApiClient(tdapi_context)
- Create teradataml DataFrames for model training and testing.
train_df = DataFrame(table_name="input_train_table") test_df = DataFrame(table_name="input_test_table")
- Create training job with TDApiClient.
job = tdapi_client.CustomTrainingJob( display_name="custom-train", script_path="train.py", container_uri="<TRAINING_IMAGE_URI>", model_serving_container_image_uri="<PREDICTION_IMAGE_URI>", )
- Submit training job.
model = job.fit(train_df, replica_count=1, model_display_name="custom-model")
- Model.deploy method uses model artifact found in Cloud Storage after training.
td_predictor = job.deploy(model, "vantage", model_type="pmml", model_filename="model.pmml")
- Access DataFrame of model deployed in Vantage.
print(td_predictor.model_df)
- Score model.
output = td_predictor.predict(test_df, mode='udf')