Made minor changes to sdk-metric-parameter-tracking-for-custom-jobs file (#479)

* modified file

* modified file

* ran linter test

* deleted file in community folder

* ran linter test

* changed folder name in links

* ran linter test

* resolved comments

* ran linter test

* modified file

* ran linter test
This commit is contained in:
sudarshan-SpringML
2022-04-22 00:05:07 -07:00
committed by GitHub
parent 9d8caf7888
commit 741c6732f1
2 changed files with 50 additions and 1105 deletions
@@ -8,7 +8,7 @@
},
"outputs": [],
"source": [
"# Copyright 2021 Google LLC\n",
"# Copyright 2022 Google LLC\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
@@ -42,6 +42,12 @@
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/ml_metadata/sdk-metric-parameter-tracking-for-custom-jobs.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",
" </a>\n",
" </td> \n",
"</table>"
]
},
@@ -51,7 +57,7 @@
"id": "j9gUDU_3vV9d"
},
"source": [
"#Vertex AI: Track parameters and metrics for custom training jobs"
"# Vertex AI: Track parameters and metrics for custom training jobs"
]
},
{
@@ -69,7 +75,7 @@
"This example uses the Abalone Dataset. For more information about this dataset please visit: https://archive.ics.uci.edu/ml/datasets/abalone\n",
"### Objective\n",
"\n",
"In this notebook, you will learn how to use Vertex AI SDK to:\n",
"In this notebook, you will learn how to use Vertex AI SDK for Python to:\n",
"\n",
" * Track training parameters and prediction metrics for a custom training job.\n",
" * Extract and perform analysis for all parameters and metrics within an Experiment.\n",
@@ -97,7 +103,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Vertex AI Workbench notebooks**, your environment already meets\n",
"**If you are using Colab or Vertex AI Workbench**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step."
]
},
@@ -146,7 +152,7 @@
"source": [
"### Install additional packages\n",
"\n",
"Run the following commands to install the Vertex AI SDK and other packages used in this notebook."
"Install additional package dependencies not installed in your notebook environment."
]
},
{
@@ -168,15 +174,6 @@
" USER_FLAG = \"--user\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "W-eC8bOL4PcJ"
},
"source": [
"Install Vertex AI SDK."
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -185,27 +182,9 @@
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade google-cloud-aiplatform"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BN3NFbw64SzI"
},
"source": [
"Install tensorflow and sklearn for training and evaluation models."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Nbio3QBp3_E-"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade tensorflow sklearn"
"! pip3 install -U tensorflow $USER_FLAG\n",
"! python3 -m pip install {USER_FLAG} google-cloud-aiplatform --upgrade\n",
"! pip3 install scikit-learn {USER_FLAG}\n"
]
},
{
@@ -300,7 +279,7 @@
"\n",
"# Get your Google Cloud project ID from gcloud\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID: \", PROJECT_ID)"
]
@@ -378,7 +357,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Vertex AI Workbench notebooks**, your environment is already\n",
"**If you are using Vertex AI Workbench**, your environment is already\n",
"authenticated. Skip this step."
]
},
@@ -401,9 +380,9 @@
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"AI Platform\"\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **AI Platform Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
@@ -429,9 +408,7 @@
"# requests.\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -456,10 +433,10 @@
"\n",
"\n",
"When you submit a training job using the Cloud SDK, you upload a Python package\n",
"containing your training code to a Cloud Storage bucket. AI Platform runs\n",
"the code from this package. In this tutorial, AI Platform also saves the\n",
"containing your training code to a Cloud Storage bucket. Vertex AI runs\n",
"the code from this package. In this tutorial, Vertex AI also saves the\n",
"trained model that results from your job in the same bucket. Using this model artifact, you can then\n",
"create AI Platform model and endpoint resources in order to serve\n",
"create Vertex AI model and endpoint resources in order to serve\n",
"online predictions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
@@ -468,7 +445,7 @@
"You may also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Make sure to [choose a region where Vertex AI services are\n",
"available](https://cloud.google.com/vertex-ai/docs/general/locations#available_regions). You may\n",
"not use a Multi-Regional Storage bucket for training with AI Platform."
"not use a Multi-Regional Storage bucket for training with Vertex AI."
]
},
{
@@ -479,7 +456,7 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
"REGION = \"[your-region]\" # @param {type:\"string\"}"
]
},
@@ -491,8 +468,11 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"-aip-\" + TIMESTAMP"
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"-aip-\" + TIMESTAMP\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -512,7 +492,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -532,7 +512,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -637,7 +617,7 @@
"aiplatform.init(\n",
" project=PROJECT_ID,\n",
" location=REGION,\n",
" staging_bucket=BUCKET_NAME,\n",
" staging_bucket=BUCKET_URI,\n",
" experiment=EXPERIMENT_NAME,\n",
")"
]
@@ -669,9 +649,9 @@
"outputs": [],
"source": [
"!wget https://storage.googleapis.com/download.tensorflow.org/data/abalone_train.csv\n",
"!gsutil cp abalone_train.csv {BUCKET_NAME}/data/\n",
"!gsutil cp abalone_train.csv {BUCKET_URI}/data/\n",
"\n",
"gcs_csv_path = f\"{BUCKET_NAME}/data/abalone_train.csv\""
"gcs_csv_path = f\"{BUCKET_URI}/data/abalone_train.csv\""
]
},
{
@@ -760,7 +740,7 @@
" validation_data=data_prep(os.environ[\"AIP_VALIDATION_DATA_URI\"]))\n",
"print(model.evaluate(*data_prep(os.environ[\"AIP_TEST_DATA_URI\"])))\n",
"\n",
"# save as AI Platform Managed model\n",
"# save as Vertex AI Managed model\n",
"tf.saved_model.save(model, os.environ[\"AIP_MODEL_DIR\"])"
]
},
@@ -784,9 +764,9 @@
"job = aiplatform.CustomTrainingJob(\n",
" display_name=\"train-abalone-dist-1-replica\",\n",
" script_path=\"training_script.py\",\n",
" container_uri=\"gcr.io/cloud-aiplatform/training/tf-cpu.2-2:latest\",\n",
" container_uri=\"us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-8:latest\",\n",
" requirements=[\"gcsfs==0.7.1\"],\n",
" model_serving_container_image_uri=\"gcr.io/cloud-aiplatform/prediction/tf2-cpu.2-2:latest\",\n",
" model_serving_container_image_uri=\"us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-8:latest\",\n",
")"
]
},
@@ -875,7 +855,7 @@
"outputs": [],
"source": [
"def read_data(uri):\n",
" dataset_path = data_utils.get_file(\"auto-mpg.data\", uri)\n",
" dataset_path = data_utils.get_file(\"abalone_test.data\", uri)\n",
" col_names = [\n",
" \"Length\",\n",
" \"Diameter\",\n",
@@ -1021,8 +1001,9 @@
"Model\n",
"Cloud Storage Bucket\n",
"\n",
"* Training Job\n",
"* Model\n",
"* Vertex AI Dataset\n",
"* Training Job\n",
"* Model\n",
"* Endpoint\n",
"* Cloud Storage Bucket\n"
]
@@ -1035,25 +1016,27 @@
},
"outputs": [],
"source": [
"delete_training_job = True\n",
"delete_model = True\n",
"delete_endpoint = True\n",
"\n",
"# Warning: Setting this to true will delete everything in your bucket\n",
"delete_bucket = False\n",
"\n",
"# Delete dataset\n",
"ds.delete()\n",
"\n",
"# Delete the training job\n",
"job.delete()\n",
"\n",
"# Undeploy and delete the endpoint\n",
"# Undeploy model from endpoint\n",
"endpoint.undeploy_all()\n",
"\n",
"# Delete the endpoint\n",
"endpoint.delete()\n",
"\n",
"# Delete the model\n",
"model.delete()\n",
"\n",
"if delete_bucket and \"BUCKET_NAME\" in globals():\n",
" ! gsutil -m rm -r $BUCKET_NAME"
"\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil -m rm -r $BUCKET_URI"
]
}
],