mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
refactor, chore, feat(egen): contracts content, steps & template based fixes (#3075)
* refactor, chore, feat: removes/contracts long explanations, template based fixes, adds step to remove locally generated files * fix: adds the missing import, replaces code markdown with bold style at some places * fix: replaces REGION with LOCATION * chore: addresses review comments --------- Co-authored-by: krishr2d2 <krishna.movva@egen.ai>
This commit is contained in:
co-authored by
krishr2d2
parent
0e662eca2b
commit
f5c17c0700
@@ -32,24 +32,26 @@
|
||||
"# Vertex AI Hyperparameter Tuning for XGBoost\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/training/hyperparameter_tuning_xgboost.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/training/hyperparameter_tuning_xgboost.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Ftraining%2Fhyperparameter_tuning_xgboost.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" </td> \n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/training/hyperparameter_tuning_xgboost.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/training/hyperparameter_tuning_xgboost.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>"
|
||||
]
|
||||
},
|
||||
@@ -61,9 +63,9 @@
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This tutorial demonstrates how to Vertex AI Hyperparameter Tuning with XGBoost.\n",
|
||||
"This tutorial demonstrates how to Vertex AI hyperparameter tuning with XGBoost.\n",
|
||||
"\n",
|
||||
"Learn more about [Vertex AI Hyperparameter Tuning](https://cloud.google.com/vertex-ai/docs/training/hyperparameter-tuning-overview)."
|
||||
"Learn more about [Vertex AI hyperparameter tuning](https://cloud.google.com/vertex-ai/docs/training/hyperparameter-tuning-overview)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -74,21 +76,19 @@
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to use `Vertex AI Hyperparameter Tuning` for training a XGBoost custom model.\n",
|
||||
"In this tutorial, you learn how to use **Vertex AI hyperparameter tuning** service for training an XGBoost model.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services:\n",
|
||||
"This tutorial uses the following Vertex AI services:\n",
|
||||
"\n",
|
||||
"- `Vertex AI Training`\n",
|
||||
"- `Vertex AI Hyperparameter Tuning`\n",
|
||||
"- `Vertex AI Vizier`\n",
|
||||
"- `Vertex AI Model` resource\n",
|
||||
"- **Vertex AI training**\n",
|
||||
"- **Vertex AI hyperparameter tuning** (uses **Vertex AI Vizier**)\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Training using a Python package.\n",
|
||||
"- Report accuracy when hyperparameter tuning.\n",
|
||||
"- Train using a Python training application package.\n",
|
||||
"- Report accuracy during hyperparameter tuning.\n",
|
||||
"- Save the model artifacts to Cloud Storage using GCSFuse.\n",
|
||||
"- Create a `Vertex AI Model` resource."
|
||||
"- List the best model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -123,15 +123,22 @@
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f0316df526f8"
|
||||
},
|
||||
"source": [
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "install_mlops"
|
||||
},
|
||||
"source": [
|
||||
"## Installations\n",
|
||||
"\n",
|
||||
"Install the following packages to execute this notebook."
|
||||
"### Install Vertex AI SDK for Python\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -143,7 +150,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# install packages\n",
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform -q"
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -152,7 +159,9 @@
|
||||
"id": "e9255e3b156f"
|
||||
},
|
||||
"source": [
|
||||
"### Colab Only: Uncomment the following cell to restart the kernel"
|
||||
"### Restart runtime (Colab only)\n",
|
||||
"\n",
|
||||
"To use the newly installed packages, you must restart the runtime on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -163,11 +172,14 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# import IPython\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"# app = IPython.Application.instance()\n",
|
||||
"# app.kernel.do_shutdown(True)"
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" import IPython\n",
|
||||
"\n",
|
||||
" app = IPython.Application.instance()\n",
|
||||
" app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -176,109 +188,60 @@
|
||||
"id": "435b8e413535"
|
||||
},
|
||||
"source": [
|
||||
"### Before you begin\n",
|
||||
"<div class=\"alert alert-block alert-warning\">\n",
|
||||
"<b>⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️</b>\n",
|
||||
"</div>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "92e68cfc3a90"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"#### Set your project ID\n",
|
||||
"\n",
|
||||
"**If you don't know your project ID**, try the following:\n",
|
||||
"- Run `gcloud config list`\n",
|
||||
"- Run `gcloud projects list`\n",
|
||||
"- See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)"
|
||||
"Authenticate your environment on Google Colab.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "be175254a715"
|
||||
"id": "46604f70e831"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "1ee0f8020b0c"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information\n",
|
||||
"\n",
|
||||
"Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "294fe4e5a671"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"# set the project id\n",
|
||||
"! gcloud config set project $PROJECT_ID"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "2e6b8b324ce1"
|
||||
},
|
||||
"source": [
|
||||
"#### Region\n",
|
||||
"\n",
|
||||
"You can also change the `REGION` variable used by Vertex AI. \n",
|
||||
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "region"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6c43a8673066"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench** \n",
|
||||
"- Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab Instance,** uncomment and run."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "fbc9cd30cc4b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cd0da2c26879"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab,** uncomment and run:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "a336a05c6149"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\n",
|
||||
"# auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f6b2ccc891ed"
|
||||
},
|
||||
"source": [
|
||||
"**4. Service account or other**\n",
|
||||
"* See how to grant Cloud Storage permissions to your service account at https://cloud.google.com/storage/docs/gsutil/commands/iam#ch-examples."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -309,7 +272,7 @@
|
||||
"id": "create_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -320,52 +283,31 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "setup_vars"
|
||||
},
|
||||
"source": [
|
||||
"### Set up variables\n",
|
||||
"\n",
|
||||
"Next, set up some variables used throughout the tutorial.\n",
|
||||
"### Import libraries and define constants"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "import_aip:mbsdk"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import google.cloud.aiplatform as aiplatform"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "init_aip:mbsdk"
|
||||
"id": "306bf9ab7f7f"
|
||||
},
|
||||
"source": [
|
||||
"### Initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
|
||||
"To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "JZg2sszQox9T"
|
||||
"id": "5878e859ba4d"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -378,16 +320,16 @@
|
||||
"\n",
|
||||
"You can set hardware accelerators for training and prediction.\n",
|
||||
"\n",
|
||||
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Telsa K80 GPUs allocated to each VM, you would specify:\n",
|
||||
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Telsa T4 GPUs allocated to each VM, you would specify:\n",
|
||||
"\n",
|
||||
" (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
|
||||
" (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_T4, 4)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Otherwise specify `(None, None)` to use a container image to run on a CPU.\n",
|
||||
"\n",
|
||||
"Learn more about [hardware accelerator support for your region](https://cloud.google.com/vertex-ai/docs/general/locations#accelerators).\n",
|
||||
"\n",
|
||||
"*Note*: TF releases before 2.3 for GPU support will fail to load the custom model in this tutorial. It is a known issue and fixed in TF 2.3. This is caused by static graph ops that are generated in the serving function. If you encounter this issue on your own custom models, use a container image for TF 2.3 with GPU support."
|
||||
"**Note**: TF releases before version 2.3 for GPU support are known to fail while loading the custom model in this tutorial. The issue is fixed in TF versions 2.3 and above. This is caused by static graph ops that are generated in the serving function. If you encounter this issue on your own custom models, use a container image for TF 2.3 with GPU support."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -398,8 +340,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"TRAIN_GPU, TRAIN_NGPU = (None, None)\n",
|
||||
"DEPLOY_GPU, DEPLOY_NGPU = (None, None)"
|
||||
]
|
||||
@@ -433,10 +373,10 @@
|
||||
"DEPLOY_VERSION = \"xgboost-cpu.1-1\"\n",
|
||||
"\n",
|
||||
"TRAIN_IMAGE = \"{}-docker.pkg.dev/vertex-ai/training/{}:latest\".format(\n",
|
||||
" REGION.split(\"-\")[0], TRAIN_VERSION\n",
|
||||
" LOCATION.split(\"-\")[0], TRAIN_VERSION\n",
|
||||
")\n",
|
||||
"DEPLOY_IMAGE = \"{}-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(\n",
|
||||
" REGION.split(\"-\")[0], DEPLOY_VERSION\n",
|
||||
" LOCATION.split(\"-\")[0], DEPLOY_VERSION\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -448,21 +388,7 @@
|
||||
"source": [
|
||||
"#### Set machine type\n",
|
||||
"\n",
|
||||
"Next, set the machine type to use for training.\n",
|
||||
"\n",
|
||||
"- Set the variable `TRAIN_COMPUTE` to configure the compute resources for the VMs you will use for for training.\n",
|
||||
" - `machine type`\n",
|
||||
" - `n1-standard`: 3.75GB of memory per vCPU.\n",
|
||||
" - `n1-highmem`: 6.5GB of memory per vCPU\n",
|
||||
" - `n1-highcpu`: 0.9 GB of memory per vCPU\n",
|
||||
" - `vCPUs`: number of \\[2, 4, 8, 16, 32, 64, 96 \\]\n",
|
||||
"\n",
|
||||
"*Note: The following is not supported for training:*\n",
|
||||
"\n",
|
||||
" - `standard`: 2 vCPUs\n",
|
||||
" - `highcpu`: 2, 4 and 8 vCPUs\n",
|
||||
"\n",
|
||||
"*Note: You may also use n2 and e2 machine types for training and deployment, but they do not support GPUs*."
|
||||
"Set the variable `TRAIN_COMPUTE` to configure the compute resources for VMs that you use for training. Learn more about the [machine types supported for training](https://cloud.google.com/vertex-ai/docs/training/configure-compute#machine-types)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -477,38 +403,19 @@
|
||||
"print(\"Train machine type\", TRAIN_COMPUTE)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "xgboost_intro"
|
||||
},
|
||||
"source": [
|
||||
"## Introduction to XGBoost training\n",
|
||||
"\n",
|
||||
"Once you have trained a XGBoost model, you will want to save it at a Cloud Storage location, so it can subsequently be uploaded to a `Vertex AI Model` resource.\n",
|
||||
"The XGBoost package does not have support to save the model to a Cloud Storage location. Instead, you will do the following steps to save to a Cloud Storage location.\n",
|
||||
"\n",
|
||||
"1. Save the in-memory model to the local filesystem (e.g., model.bst).\n",
|
||||
"2. Use gsutil to copy the local copy to the specified Cloud Storage location.\n",
|
||||
"\n",
|
||||
"## Vertex AI Hyperparameter Tuning service\n",
|
||||
"\n",
|
||||
"The following example demonstrates how to setup, execute and evaluate trials using the Vertex AI Hyperparameter Tuning service with `Vizier` search algorithm.\n",
|
||||
"\n",
|
||||
"Learn more about [Overview of hyperparameter tuning](https://cloud.google.com/vertex-ai/docs/training/hyperparameter-tuning-overview)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "examine_training_package:xgboost"
|
||||
},
|
||||
"source": [
|
||||
"### Examine the training package\n",
|
||||
"## Python training application package\n",
|
||||
"\n",
|
||||
"#### Package layout\n",
|
||||
"In this example, you use Vertex AI hyperparameter tuning service with a training job that executes a Python training application package.\n",
|
||||
"\n",
|
||||
"Before you start the training, you will look at how a Python package is assembled for a custom training job. When unarchived, the package contains the following directory/file layout.\n",
|
||||
"Learn more about [hyperparameter tuning in Vertex AI](https://cloud.google.com/vertex-ai/docs/training/hyperparameter-tuning-overview).\n",
|
||||
"\n",
|
||||
"Take a look at how a Python package can be structured for running a custom training job in Vertex AI. The package contains the following directory structure:\n",
|
||||
"\n",
|
||||
"- PKG-INFO\n",
|
||||
"- README.md\n",
|
||||
@@ -518,13 +425,18 @@
|
||||
" - \\_\\_init\\_\\_.py\n",
|
||||
" - task.py\n",
|
||||
"\n",
|
||||
"The files `setup.cfg` and `setup.py` are the instructions for installing the package into the operating environment of the Docker image.\n",
|
||||
"The files *setup.cfg* and *setup.py* provide instructions for installing the package into the operating environment of the Docker image.\n",
|
||||
"\n",
|
||||
"The file `trainer/task.py` is the Python script for executing the custom training job. *Note*, when we referred to it in the worker pool specification, we replace the directory slash with a dot (`trainer.task`) and dropped the file suffix (`.py`).\n",
|
||||
"\n",
|
||||
"#### Package Assembly\n",
|
||||
"\n",
|
||||
"In the following cells, you will assemble the training package."
|
||||
"The file *trainer/task.py* is the Python script that is executed when you run the custom training job. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "509c7082b6e4"
|
||||
},
|
||||
"source": [
|
||||
"### Create a folder structure as Python package\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -562,23 +474,23 @@
|
||||
"id": "taskpy_contents:iris,xgboost"
|
||||
},
|
||||
"source": [
|
||||
"### Create the task script for the Python training package\n",
|
||||
"### Create a training script\n",
|
||||
"\n",
|
||||
"Next, you create the `task.py` script for driving the training package. Some noteable steps include:\n",
|
||||
"Next, you create *task.py* script for your training job. Some noteable steps include:\n",
|
||||
"\n",
|
||||
"- Command-line arguments:\n",
|
||||
" - `model-dir`: The location to save the trained model. When using Vertex AI custom training, the location will be specified in the environment variable: `AIP_MODEL_DIR`,\n",
|
||||
"1. <u>Handling command-line arguments:</u>\n",
|
||||
" - `model-dir`: The location to save the trained model. If no value is passed, the location to save the model is obtained from the environment variable `AIP_MODEL_DIR` that defaults to the staging bucket location.\n",
|
||||
" - `dataset_data_url`: The location of the training data to download.\n",
|
||||
" - `dataset_labels_url`: The location of the training labels to download.\n",
|
||||
" - `boost-rounds`: Tunable hyperparameter\n",
|
||||
"- Data preprocessing (`get_data()`):\n",
|
||||
"1. <u>Data preprocessing (`get_data()`):</u>\n",
|
||||
" - Download the dataset and split into training and test.\n",
|
||||
"- Training (`train_model()`):\n",
|
||||
"1. <u>Training (`train_model()`):</u>\n",
|
||||
" - Trains the model\n",
|
||||
"- Evaluation (`evaluate_model()`):\n",
|
||||
"1. <u>Evaluation (`evaluate_model()`):</u>\n",
|
||||
" - Evaluates the model.\n",
|
||||
" - If hyperparameter tuning, reports the metric for accuracy.\n",
|
||||
"- Model artifact saving\n",
|
||||
"1. <u>Saving model artifacts:</u>\n",
|
||||
" - Saves the model artifacts and evaluation metrics where the Cloud Storage location specified by `model-dir`."
|
||||
]
|
||||
},
|
||||
@@ -619,9 +531,8 @@
|
||||
"\n",
|
||||
"def get_data():\n",
|
||||
" logging.info(\"Downloading training data and labelsfrom: {}, {}\".format(args.dataset_data_url, args.dataset_labels_url))\n",
|
||||
" # gsutil outputs everything to stderr so we need to divert it to stdout.\n",
|
||||
" # gsutil outputs everything to stderr. Hence, the need to divert it to stdout.\n",
|
||||
" subprocess.check_call(['gsutil', 'cp', args.dataset_data_url, 'data.csv'], stderr=sys.stdout)\n",
|
||||
" # gsutil outputs everything to stderr so we need to divert it to stdout.\n",
|
||||
" subprocess.check_call(['gsutil', 'cp', args.dataset_labels_url, 'labels.csv'], stderr=sys.stdout)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -692,9 +603,9 @@
|
||||
"id": "tarball_training_script"
|
||||
},
|
||||
"source": [
|
||||
"#### Store training script on your Cloud Storage bucket\n",
|
||||
"### Store training script on Cloud Storage bucket\n",
|
||||
"\n",
|
||||
"Next, you package the training folder into a compressed tar ball, and then store it in your Cloud Storage bucket."
|
||||
"Compress the whole training folder as a tar ball and then store it in a Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -717,12 +628,14 @@
|
||||
"id": "train_custom_job_machine_specification"
|
||||
},
|
||||
"source": [
|
||||
"### Prepare your machine specification\n",
|
||||
"## Define machine configuration\n",
|
||||
"\n",
|
||||
"Now define the machine specification for your custom hyperparameter tuning job. This tells Vertex what type of machine instance to provision for the hyperparameter tuning.\n",
|
||||
"Define the machine configuration for your custom hyperparameter tuning job. This tells Vertex AI what type of machine instance to provision when the job gets started. \n",
|
||||
"\n",
|
||||
"You can specify the following parameters: \n",
|
||||
" - `machine_type`: The type of GCP instance to provision -- e.g., n1-standard-8.\n",
|
||||
" - `accelerator_type`: The type, if any, of hardware accelerator. In this tutorial if you previously set the variable `TRAIN_GPU != None`, you are using a GPU; otherwise you will use a CPU.\n",
|
||||
" - `accelerator_count`: The number of accelerators."
|
||||
" - `accelerator_type`: The type, if any, of hardware accelerator. In this tutorial if you previously set the variable `TRAIN_GPU`, you're using a GPU. Otherwise you're using a CPU.\n",
|
||||
" - `accelerator_count`: Number of accelerators."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -749,10 +662,11 @@
|
||||
"id": "train_custom_job_disk_specification"
|
||||
},
|
||||
"source": [
|
||||
"### Prepare your disk specification\n",
|
||||
"## Define disk configuration (optional)\n",
|
||||
"\n",
|
||||
"(optional) Now define the disk specification for your custom hyperparameter tuning job. This tells Vertex what type and size of disk to provision in each machine instance for the hyperparameter tuning.\n",
|
||||
"Optionally, define the disk configuration for your custom hyperparameter tuning job. This tells Vertex AI what type and size of disk to provision in each machine instance for the hyperparameter tuning.\n",
|
||||
"\n",
|
||||
"You can specify the following parameters:\n",
|
||||
" - `boot_disk_type`: Either SSD or Standard. SSD is faster, and Standard is less expensive. Defaults to SSD.\n",
|
||||
" - `boot_disk_size_gb`: Size of disk in GB."
|
||||
]
|
||||
@@ -766,7 +680,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"DISK_TYPE = \"pd-ssd\" # [ pd-ssd, pd-standard]\n",
|
||||
"DISK_SIZE = 200 # GB\n",
|
||||
"DISK_SIZE = 100 # GB\n",
|
||||
"\n",
|
||||
"disk_spec = {\"boot_disk_type\": DISK_TYPE, \"boot_disk_size_gb\": DISK_SIZE}"
|
||||
]
|
||||
@@ -777,17 +691,15 @@
|
||||
"id": "prepare_custom_cmdargs:iris,xgboost"
|
||||
},
|
||||
"source": [
|
||||
"### Prepare your command-line arguments\n",
|
||||
"## Set worker pool specs\n",
|
||||
"\n",
|
||||
"Now define the command-line arguments for your custom training container:\n",
|
||||
"Specify the following worker pool specs for your custom training container:\n",
|
||||
"\n",
|
||||
"- `args`: The command-line arguments to pass to the executable that is set as the entry point into the container.\n",
|
||||
" - `--model-dir` : For our demonstrations, we use this command-line argument to specify where to store the model artifacts.\n",
|
||||
" - direct: You pass the Cloud Storage location as a command line argument to your training script (set variable `DIRECT = True`), or\n",
|
||||
" - indirect: The service passes the Cloud Storage location as the environment variable `AIP_MODEL_DIR` to your training script (set variable `DIRECT = False`). In this case, you tell the service the model artifact location in the job specification.\n",
|
||||
" - `--model-dir`: Specifies where to store the model artifacts in the Cloud Storage bucket.\n",
|
||||
" - `--dataset-data-url`: The location of the training data to download.\n",
|
||||
" - `--dataset-labels-url`: The location of the training labels to download.\n",
|
||||
" - `--boost-rounds`: Tunable hyperparameter."
|
||||
" - `--boost-rounds`: Sets the tunable hyperparameter `num_boost_round` while training the XGBoost model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -798,15 +710,18 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set path to save model\n",
|
||||
"MODEL_DIR = \"{}/aiplatform-custom-job\".format(BUCKET_URI)\n",
|
||||
"# Set the source path to the dataset\n",
|
||||
"DATASET_DIR = \"gs://cloud-samples-data/ai-platform/iris\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Set the command-line arguments\n",
|
||||
"CMDARGS = [\n",
|
||||
" \"--dataset-data-url=\" + DATASET_DIR + \"/iris_data.csv\",\n",
|
||||
" \"--dataset-labels-url=\" + DATASET_DIR + \"/iris_target.csv\",\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"# Set the worker pool specs\n",
|
||||
"worker_pool_spec = [\n",
|
||||
" {\n",
|
||||
" \"replica_count\": 1,\n",
|
||||
@@ -828,9 +743,9 @@
|
||||
"id": "create_custom_job:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Create a custom job\n",
|
||||
"## Create a custom training job\n",
|
||||
"\n",
|
||||
"Use the class `CustomJob` to create a custom job, such as for hyperparameter tuning, with the following parameters:\n",
|
||||
"Use the `CustomJob` class to create a custom training job with the following parameters:\n",
|
||||
"\n",
|
||||
"- `display_name`: A human readable name for the custom job.\n",
|
||||
"- `worker_pool_specs`: The specification for the corresponding VM instances.\n",
|
||||
@@ -860,13 +775,13 @@
|
||||
"source": [
|
||||
"## Create a hyperparameter tuning job\n",
|
||||
"\n",
|
||||
"Use the class `HyperparameterTuningJob` to create a hyperparameter tuning job, with the following parameters:\n",
|
||||
"Use the `HyperparameterTuningJob` class to create a hyperparameter tuning job with the following parameters:\n",
|
||||
"\n",
|
||||
"- `display_name`: A human readable name for the custom job.\n",
|
||||
"- `custom_job`: The worker pool spec from this custom job applies to the CustomJobs created in all the trials.\n",
|
||||
"- `custom_job`: The CustomJob object created for training. The worker pool spec from this custom job applies to the CustomJobs created in all the trials.\n",
|
||||
"- `metrics_spec`: The metrics to optimize. The dictionary key is the metric_id, which is reported by your training job, and the dictionary value is the optimization goal of the metric('minimize' or 'maximize').\n",
|
||||
"- `parameter_spec`: The parameters to optimize. The dictionary key is the metric_id, which is passed into your training job as a command line key word argument, and the dictionary value is the parameter specification of the metric.\n",
|
||||
"- `search_algorithm`: The search algorithm to use: `grid`, `random` and `None`. If `None` is specified, the `Vizier` service (Bayesian) is used.\n",
|
||||
"- `search_algorithm`: The search algorithm to use. Takes `grid`, `random` and `None` as values. Hyperparameter tuning for custom training uses [Vertex AI Vizier](https://cloud.google.com/vertex-ai/docs/vizier/overview) for training jobs. \n",
|
||||
"- `max_trial_count`: The maximum number of trials to perform."
|
||||
]
|
||||
},
|
||||
@@ -925,7 +840,7 @@
|
||||
"source": [
|
||||
"### Display the hyperparameter tuning job trial results\n",
|
||||
"\n",
|
||||
"After the hyperparameter tuning job has completed, the property `trials` will return the results for each trial."
|
||||
"Once the hyperparameter tuning job successfully finishes, you can access the results from each trial using the `trials` property."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -945,9 +860,9 @@
|
||||
"id": "best_trial:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"### Best trial\n",
|
||||
"## Fetch the best trial\n",
|
||||
"\n",
|
||||
"Now look at which trial was the best:"
|
||||
"Identify the best trial and print the details."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -958,7 +873,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Initialize a tuple to identify the best configuration\n",
|
||||
"best = (None, None, None, 0.0)\n",
|
||||
"# Iterate through the trails and update the best configuration\n",
|
||||
"for trial in hpt_job.trials:\n",
|
||||
" # Keep track of the best outcome\n",
|
||||
" if float(trial.final_measurement.metrics[0].value) > best[3]:\n",
|
||||
@@ -977,6 +894,7 @@
|
||||
" float(trial.final_measurement.metrics[0].value),\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"# print details of the best configuration\n",
|
||||
"print(best)"
|
||||
]
|
||||
},
|
||||
@@ -986,9 +904,9 @@
|
||||
"id": "get_best_model"
|
||||
},
|
||||
"source": [
|
||||
"### Get the Best Model\n",
|
||||
"## List the best model\n",
|
||||
"\n",
|
||||
"If you used the method of having the service tell the tuning script where to save the model artifacts (`DIRECT = False`), then the model artifacts for the best model are saved at:\n",
|
||||
"The model artifacts for the best model are saved at: \n",
|
||||
"\n",
|
||||
" MODEL_DIR/<best_trial_id>/model"
|
||||
]
|
||||
@@ -1001,48 +919,28 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Fetch the best model\n",
|
||||
"BEST_MODEL_DIR = MODEL_DIR + \"/\" + best[0] + \"/model\"\n",
|
||||
"\n",
|
||||
"! gsutil ls {BEST_MODEL_DIR}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "delete_hpt_job"
|
||||
},
|
||||
"source": [
|
||||
"### Delete the hyperparameter tuning job\n",
|
||||
"\n",
|
||||
"The method 'delete()' will delete the hyperparameter tuning job."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "delete_hpt_job"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"hpt_job.delete()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cleanup:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"# Cleaning up\n",
|
||||
"## Cleaning up\n",
|
||||
"\n",
|
||||
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
|
||||
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
|
||||
"\n",
|
||||
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
|
||||
"\n",
|
||||
"- Custom Job (Custome Training job is remove in previous step)\n",
|
||||
"- Cloud Storage Bucket"
|
||||
"- HyperparameterTuning Job \n",
|
||||
"- Cloud Storage bucket\n",
|
||||
"- Locally generated files"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1053,10 +951,18 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"delete_bucket = False\n",
|
||||
"# Delete the hyperparameter tuning job\n",
|
||||
"hpt_job.delete()\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil rm -r $BUCKET_URI"
|
||||
"# Delete the Cloud Storage bucket\n",
|
||||
"delete_bucket = False # Set True to delete the bucket\n",
|
||||
"\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil rm -r $BUCKET_URI\n",
|
||||
"\n",
|
||||
"# Delete the locally generated files\n",
|
||||
"! rm -rf custom/\n",
|
||||
"! rm custom.tar.gz"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user