The Vertex AI SDK includes the Model
class
to work with a model that you train and then use for predictions. The SDK also
includes the ModelEvaluation
class to evaluate
metrics on trained AutoML models. For more information about models, see Train
and use your own models.
Model
The Model
class represents a trained model that is
registered in the Vertex AI Model Registry. You use a trained model to
generate predictions.
Use the aiplatform.Model()
method to find and
return 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 recommend
specifying a model with its model ID. The following code sample shows how to
use 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 the
properties and
methods of the Model
to
work with it and get predictions.
Create a registered model
To create a model resource that's registered in the
Vertex AI Model Registry, call the run
method on a training job class.
The following methods create a model, train the model, register the model in the
Vertex AI Model Registry, then return a reference to the model.
AutoMLForecastingTrainingJob.run
AutoMLImageTrainingJob.run
AutoMLTabularTrainingJob.run
AutoMLTextTrainingJob.run
AutoMLVideoTrainingJob.run
CustomContainerTrainingJob.run
CustomPythonPackageTrainingJob.run
CustomTrainingJob.run
The following sample code shows you how to create a CustomTrainingJob
resource
and then use its run
method to create a model, train the model, register the
model in the Vertex AI Model Registry, and return a reference to the
model:
# Create a custom training job using a script
job = 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 the CustomJob
class and its run
method. The
CustomJob.run
method trains a model, but it doesn't register the model in the
Vertex AI Model Registry and it doesn't return a reference to the model.
If you use the CustomJob
class, you need to use a script to write your
model to a location such as a Cloud Storage bucket. For more information, see
Export a trained ML model.
Register a model
If you have a model that isn't registered with the Vertex AI Model Registry, then you need to register it so you can manage your model's lifecycle. Vertex AI Model Registry is a central repository that provides an overview of your models so you can manage them. For more information, see Introduction to Vertex AI Model Registry.
The Vertex AI SDK includes the following methods to import a model to the Vertex AI Model Registry. Click one of the methods to learn more about it in the Vertex AI SDK reference guide.
Model.upload
Model.upload_scikit_learn_model_file
Model.upload_tensorflow_saved_model
Model.upload_xgboost_model_file
Deploy a model
After you register a model, you need to deploy the model to an endpoint before
you can use it for predictions. Use the
Model.deploy
method
to deploy your model to an
Endpoint
. For
more information, see
Deploy a model to an endpoint.
ModelEvaluation
Use the ModelEvaluation
class to get
evaluation metrics for AutoML models, such as precision and recall, to help
determine the performance of your models. For more information, see Model
evaluation in Vertex AI.
The following code sample shows how to list all evaluations for a model with
model ID model-id
that's in a project with a project ID of my-project
and
that's in the us-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 with
model ID model-id
that's in a project with a project ID of my-project
and
that's in the us-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.metrics
To create a reference to a model evaluation, use its resource name or model ID and the evaluation ID. The following code sample shows how to create a reference to 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.metrics
The following code sample shows how to create a reference to a model evaluation using the model ID and the evaluation ID:
evaluation.metrics = aiplatform.ModelEvaluation(
evaluation_name={evaluation-id},
model_id={model-id})
eval_metrics = evaluation.metrics
What's next
- Learn about the Vertex AI SDK.