下列程式碼範例說明如何建立 CustomTrainingJob 資源,然後使用其 run 方法建立模型、訓練模型、在 Vertex AI Model Registry 中註冊模型,並傳回模型的參照:
# 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,)
建立未註冊的模型
如要建立未註冊至 Vertex AI Model Registry 的模型,請使用 CustomJob 類別及其 run 方法。CustomJob.run 方法會訓練模型,但不會在 Vertex AI Model Registry 中註冊模型,也不會傳回模型的參照。
以下程式碼範例示範如何取得模型評估結果,該模型的模型 ID 為 model-id,專案 ID 為 my-project,且位於 us-central1 區域:
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
如要建立模型評估的參照,請使用其資源名稱或模型 ID 和評估 ID。以下程式碼範例說明如何使用資源名稱建立模型評估的參照:
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Model classes\n\nThe Vertex AI SDK includes the [`Model`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model) class\nto work with a model that you train and then use for predictions. The SDK also\nincludes the [`ModelEvaluation`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.ModelEvaluation) class to evaluate\nmetrics on trained AutoML models. For more information about models, see [Train\nand use your own models](/vertex-ai/docs/training-overview).\n\n### [`Model`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model)\n\nThe [`Model`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model) class represents a trained model that is\nregistered in the Vertex AI Model Registry. You use a trained model to\ngenerate predictions.\n\nUse the [`aiplatform.Model()`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#parameters) method to find and\nreturn a reference to a model. You can specify a model using its name or ID.\nBecause more than one model in a project can share the same name, we recommend\nspecifying a model with its model ID. The following code sample shows how to\nuse a model ID to find and return a reference to an existing model: \n\n MODEL_ID=\"my-sample-model-ID\"\n model = aiplatform.Model(model_name=MODEL_ID)\n\nAfter you have a reference to a trained model, you can use the\n[properties](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#properties) and\n[methods](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#methods) of the [`Model`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model) to\nwork with it and get predictions.\n\n#### Create a registered model\n\nTo create a model resource that's registered in the\nVertex AI Model Registry, call the `run` method on a training job class.\nThe following methods create a model, train the model, register the model in the\nVertex AI Model Registry, then return a reference to the model.\n\n- [`AutoMLForecastingTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.AutoMLForecastingTrainingJob#google_cloud_aiplatform_AutoMLForecastingTrainingJob_run)\n- [`AutoMLImageTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.AutoMLImageTrainingJob#google_cloud_aiplatform_AutoMLImageTrainingJob_run)\n- [`AutoMLTabularTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.AutoMLTabularTrainingJob#google_cloud_aiplatform_AutoMLTabularTrainingJob_run)\n- [`AutoMLTextTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.AutoMLTextTrainingJob#google_cloud_aiplatform_AutoMLTextTrainingJob_run)\n- [`AutoMLVideoTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.AutoMLVideoTrainingJob#google_cloud_aiplatform_AutoMLVideoTrainingJob_run)\n- [`CustomContainerTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.CustomContainerTrainingJob#google_cloud_aiplatform_CustomContainerTrainingJob_run)\n- [`CustomPythonPackageTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.CustomPythonPackageTrainingJob#google_cloud_aiplatform_CustomPythonPackageTrainingJob_run)\n- [`CustomTrainingJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.CustomTrainingJob#google_cloud_aiplatform_CustomTrainingJob_run)\n\nThe following sample code shows you how to create a `CustomTrainingJob` resource\nand then use its `run` method to create a model, train the model, register the\nmodel in the Vertex AI Model Registry, and return a reference to the\nmodel: \n\n # Create a custom training job using a script\n job = aiplatform.CustomTrainingJob(\n display_name=\"my-training-job\",\n script_path=\"task.py\",\n container_uri=\"us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-8:latest\",\n requirements=[\"google-cloud-bigquery\u003e=2.20.0\", \"db-dtypes\", \"protobuf\u003c3.20.0\"],\n model_serving_container_image_uri=\"us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-8:latest\",\n )\n\n # Create and train your model using a BigQuery dataset. The method\n # returns a reference to the trained model.\n model = job.run(\n dataset=dataset,\n model_display_name=\"my-model-name\",\n bigquery_destination=f\"bq://{project_id}\",\n args=CMDARGS,\n )\n\n#### Create an unregistered model\n\nTo create a model that's not registered to the Vertex AI Model Registry,\nuse the [`CustomJob`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.CustomJob) class and its `run` method. The\n[`CustomJob.run`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.CustomJob#google_cloud_aiplatform_CustomJob_run)\nmethod trains a model, but it doesn't register the model in the\nVertex AI Model Registry and it doesn't return a reference to the model.\n\nIf you use the `CustomJob` class, you need to use a script to write your\nmodel to a location such as a Cloud Storage bucket. For more information, see\n[Export a trained ML model](/vertex-ai/docs/training/code-requirements#export).\n\n#### Register a model\n\nIf you have a model that isn't registered with the\nVertex AI Model Registry, then you need to register it so you can\nmanage your model's lifecycle. Vertex AI Model Registry is a central\nrepository that provides an overview of your models so you can manage them. For\nmore information, see\n[Introduction to Vertex AI Model Registry](/vertex-ai/docs/model-registry/introduction).\n\nThe Vertex AI SDK includes the following methods to import a model\nto the Vertex AI Model Registry. Click one of the methods to learn more\nabout it in the Vertex AI SDK reference guide.\n\n- [`Model.upload`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#google_cloud_aiplatform_Model_upload)\n- [`Model.upload_scikit_learn_model_file`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#google_cloud_aiplatform_Model_upload_scikit_learn_model_file)\n- [`Model.upload_tensorflow_saved_model`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#google_cloud_aiplatform_Model_upload_tensorflow_saved_model)\n- [`Model.upload_xgboost_model_file`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#google_cloud_aiplatform_Model_upload_xgboost_model_file)\n\n#### Deploy a model\n\nAfter you register a model, you need to deploy the model to an endpoint before\nyou can use it for predictions. Use the\n[`Model.deploy`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.Model#google_cloud_aiplatform_Model_deploy) method\nto deploy your model to an\n[`Endpoint`](/vertex-ai/docs/python-sdk/prediction-classes#endpoint). For\nmore information, see\n[Deploy a model to an endpoint](/vertex-ai/docs/general/deployment).\n\n### [`ModelEvaluation`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.ModelEvaluation)\n\nUse the [`ModelEvaluation`](/python/docs/reference/aiplatform/latest/google.cloud.aiplatform.ModelEvaluation) class to get\nevaluation metrics for AutoML models, such as precision and recall, to help\ndetermine the performance of your models. For more information, see [Model\nevaluation in Vertex AI](/vertex-ai/docs/evaluation/introduction).\n\nThe following code sample shows how to list all evaluations for a model with\nmodel ID `model-id` that's in a project with a project ID of `my-project` and\nthat's in the `us-central1` region: \n\n model = aiplatform.Model('projects/my-project/locations/us-central1/models/{model-id}')\n\n evaluations = model.list_model_evaluations()\n\nThe following code sample shows how to get the model evaluation for a model with\nmodel ID `model-id` that's in a project with a project ID of `my-project` and\nthat's in the `us-central1` region: \n\n model = aiplatform.Model('projects/my-project/locations/us-central1/models/{model-id}')\n\n # Return the first evaluation with no arguments. You can also specify a model\n # using its model ID.\n evaluation = model.get_model_evaluation()\n\n eval_metrics = evaluation.metrics\n\nTo create a reference to a model evaluation, use its resource name or model ID\nand the evaluation ID. The following code sample shows how to create a reference\nto a model evaluation using its resource name: \n\n evaluation = aiplatform.ModelEvaluation(\n evaluation_name='projects/my-project/locations/us-central1/\n models/{model-id}/evaluations/{evaluation-id}')\n\n eval_metrics = evaluation.metrics\n\nThe following code sample shows how to create a reference to a model evaluation\nusing the model ID and the evaluation ID: \n\n evaluation.metrics = aiplatform.ModelEvaluation(\n evaluation_name={evaluation-id},\n model_id={model-id})\n\n eval_metrics = evaluation.metrics\n\nWhat's next\n-----------\n\n- Learn about the [Vertex AI SDK](/vertex-ai/docs/python-sdk/use-vertex-ai-python-sdk)."]]