Model classes Stay organized with collections Save and categorize content based on your preferences.
The Vertex AI SDK includes theModel classto work with a model that you train and then use for predictions. The SDK alsoincludes theModelEvaluation class to evaluatemetrics on trained AutoML models. For more information about models, seeTrainand use your own models.
Model
TheModel class represents a trained model that isregistered in the Vertex AI Model Registry. You use a trained model togenerate predictions.
Use theaiplatform.Model() method to find andreturn a reference to a model. You can specify a model using its name or ID.Because more than one model in a project can share the same name, we recommendspecifying a model with its model ID. The following code sample shows how touse a model ID to find and return a reference to an existing model:
MODEL_ID="my-sample-model-ID"model=aiplatform.Model(model_name=MODEL_ID)After you have a reference to a trained model, you can use theproperties andmethods of theModel towork with it and get predictions.
Create a registered model
To create a model resource that's registered in theVertex AI Model Registry, call therun method on a training job class.The following methods create a model, train the model, register the model in theVertex AI Model Registry, then return a reference to the model.
AutoMLForecastingTrainingJob.runAutoMLImageTrainingJob.runAutoMLTabularTrainingJob.runAutoMLTextTrainingJob.runAutoMLVideoTrainingJob.runCustomContainerTrainingJob.runCustomPythonPackageTrainingJob.runCustomTrainingJob.run
The following sample code shows you how to create aCustomTrainingJob resourceand then use itsrun method to create a model, train the model, register themodel in the Vertex AI Model Registry, and return a reference to themodel:
# Create a custom training job using a scriptjob=aiplatform.CustomTrainingJob(display_name="my-training-job",script_path="task.py",container_uri="us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-8:latest",requirements=["google-cloud-bigquery>=2.20.0","db-dtypes","protobuf<3.20.0"],model_serving_container_image_uri="us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-8:latest",)# Create and train your model using a BigQuery dataset. The method# returns a reference to the trained model.model=job.run(dataset=dataset,model_display_name="my-model-name",bigquery_destination=f"bq://{project_id}",args=CMDARGS,)Create an unregistered model
To create a model that's not registered to the Vertex AI Model Registry,use theCustomJob class and itsrun method. TheCustomJob.runmethod trains a model, but it doesn't register the model in theVertex AI Model Registry and it doesn't return a reference to the model.
If you use theCustomJob class, you need to use a script to write yourmodel to a location such as a Cloud Storage bucket. For more information, seeExport a trained ML model.
Register a model
If you have a model that isn't registered with theVertex AI Model Registry, then you need to register it so you canmanage your model's lifecycle. Vertex AI Model Registry is a centralrepository that provides an overview of your models so you can manage them. Formore information, seeIntroduction to Vertex AI Model Registry.
The Vertex AI SDK includes the following methods to import a modelto the Vertex AI Model Registry. Click one of the methods to learn moreabout it in the Vertex AI SDK reference guide.
Model.uploadModel.upload_scikit_learn_model_fileModel.upload_tensorflow_saved_modelModel.upload_xgboost_model_file
Deploy a model
After you register a model, you need to deploy the model to an endpoint beforeyou can use it for predictions. Use theModel.deploy methodto deploy your model to anEndpoint. Formore information, seeDeploy a model to an endpoint.
ModelEvaluation
Use theModelEvaluation class to getevaluation metrics for AutoML models, such as precision and recall, to helpdetermine the performance of your models. For more information, seeModelevaluation in Vertex AI.
The following code sample shows how to list all evaluations for a model withmodel IDmodel-id that's in a project with a project ID ofmy-project andthat's in theus-central1 region:
model=aiplatform.Model('projects/my-project/locations/us-central1/models/{model-id}')evaluations=model.list_model_evaluations()The following code sample shows how to get the model evaluation for a model withmodel IDmodel-id that's in a project with a project ID ofmy-project andthat's in theus-central1 region:
model=aiplatform.Model('projects/my-project/locations/us-central1/models/{model-id}')# Return the first evaluation with no arguments. You can also specify a model# using its model ID.evaluation=model.get_model_evaluation()eval_metrics=evaluation.metricsTo create a reference to a model evaluation, use its resource name or model IDand the evaluation ID. The following code sample shows how to create a referenceto a model evaluation using its resource name:
evaluation=aiplatform.ModelEvaluation(evaluation_name='projects/my-project/locations/us-central1/models/{model-id}/evaluations/{evaluation-id}')eval_metrics=evaluation.metricsThe following code sample shows how to create a reference to a model evaluationusing the model ID and the evaluation ID:
evaluation.metrics=aiplatform.ModelEvaluation(evaluation_name={evaluation-id},model_id={model-id})eval_metrics=evaluation.metricsWhat's next
- Learn about theVertex AI SDK.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.