mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-27 15:42:05 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9094dacd62 |
@@ -26,9 +26,6 @@ from utils import util
|
||||
|
||||
# This script is used to execute a notebook and write out the output notebook.
|
||||
|
||||
# This is used to force papermill to use this kernel to run the notebook instead of any defined inside the notebook itself
|
||||
DEFAULT_KERNEL_NAME = "python3"
|
||||
|
||||
|
||||
def execute_notebook(
|
||||
notebook_source: str,
|
||||
@@ -53,11 +50,14 @@ def execute_notebook(
|
||||
|
||||
execution_exception = None
|
||||
|
||||
|
||||
print("\n=== DOWNLOAD EXECUTED NOTEBOOK ===\n")
|
||||
print(f"Please debug the executed notebook by downloading the executed notebook:")
|
||||
print(
|
||||
f"Please debug the executed notebook by downloading the executed notebook:"
|
||||
)
|
||||
|
||||
print("Option 1. Using gsutil. Run the following command in your terminal.")
|
||||
print(f'\tgsutil cp "{output_file_or_uri}" .')
|
||||
print(f"\tgsutil cp \"{output_file_or_uri}\" .")
|
||||
|
||||
print("Option 2. Using this link.")
|
||||
print(f"\thttps://storage.googleapis.com/{output_file_or_uri[5:]}")
|
||||
@@ -72,7 +72,6 @@ def execute_notebook(
|
||||
output_path=notebook_source,
|
||||
progress_bar=should_log_output,
|
||||
request_save_on_cell_execute=should_log_output,
|
||||
kernel_name=DEFAULT_KERNEL_NAME,
|
||||
log_output=should_log_output,
|
||||
stdout_file=sys.stdout if should_log_output else None,
|
||||
stderr_file=sys.stderr if should_log_output else None,
|
||||
|
||||
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
+138
-427
@@ -1,62 +1,17 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "18ebbd838e32"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 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",
|
||||
"# You may obtain a copy of the License at\n",
|
||||
"#\n",
|
||||
"# https://www.apache.org/licenses/LICENSE-2.0\n",
|
||||
"#\n",
|
||||
"# Unless required by applicable law or agreed to in writing, software\n",
|
||||
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
||||
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
||||
"# See the License for the specific language governing permissions and\n",
|
||||
"# limitations under the License."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "aef73cfa8725"
|
||||
},
|
||||
"source": [
|
||||
"# Predictive Maintenance using Vertex AI\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\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/workbench/predictive_maintainance/predictive_maintenance_usecase.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>\n",
|
||||
"<br/><br/><br/>\n",
|
||||
"\n",
|
||||
"# Predictive Maintenance \n",
|
||||
"\n",
|
||||
"## Table of contents\n",
|
||||
"* [Overview](#section-1)\n",
|
||||
"* [Objective](#section-2)\n",
|
||||
"* [Dataset](#section-3)\n",
|
||||
"* [Dataset](#section-2)\n",
|
||||
"* [Objective](#section-3)\n",
|
||||
"* [Costs](#section-4)\n",
|
||||
"* [Data analysis](#section-5)\n",
|
||||
"* [Fit a regression model](#section-6)\n",
|
||||
@@ -67,32 +22,24 @@
|
||||
" * [Create an endpoint](#section-11)\n",
|
||||
" * [Deploy the model to the created endpoint](#section-12)\n",
|
||||
" * [Test calling the endpoint](#section-13)\n",
|
||||
"* [Clean up](#section-14)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e10c5167a061"
|
||||
},
|
||||
"source": [
|
||||
"* [Clean up](#section-14)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"## Overview\n",
|
||||
"<a name=\"section-1\"></a>\n",
|
||||
"\n",
|
||||
"In this notebook, you go through a predictive maintenance usecase on industrial data using machine learning techniques, deploy the machine learning model on Vertex AI, and automate the workflow using the executor feature of Vertex AI Workbench.\n",
|
||||
"This notebook demonstrates how to perform predictive maintenance on industrial data using machine learning techniques, deploy the machine learning model on Vertex AI, and automate the workflow using the executor feature of Vertex AI Workbench.\n",
|
||||
"\n",
|
||||
"*Note: This notebook file is developed to run in a [Vertex AI Workbench managed notebooks](https://console.cloud.google.com/vertex-ai/workbench/list/managed) instance using the XGBoost (Local) kernel. Some components of this notebook may not work in other notebook environments.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "fead9e83ebd7"
|
||||
},
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"*Note: This notebook file was developed to run in a [Vertex AI Workbench managed notebooks](https://console.cloud.google.com/vertex-ai/workbench/list/managed) instance using the XGBoost (Local) kernel. Some components of this notebook may not work in other notebook environments.*\n",
|
||||
"\n",
|
||||
"## Dataset\n",
|
||||
"<a name=\"section-2\"></a>\n",
|
||||
"\n",
|
||||
"The dataset used in this notebook is a part of the [NASA Turbofan Engine Degradation Simulation dataset](https://ti.arc.nasa.gov/tech/dash/groups/pcoe/prognostic-data-repository/), which consists of simulated time-series data for four sets of fleet engines under different combinations of operational conditions and fault modes. In this notebook, only one of the engine's simulated data (FD001) has been used to analyze and train a model that can predict the engine's remaining useful life.\n",
|
||||
"\n",
|
||||
"## Objectives\n",
|
||||
"<a name=\"section-3\"></a>\n",
|
||||
"\n",
|
||||
"The objectives of this notebook include:\n",
|
||||
"\n",
|
||||
"- Loading the required dataset from a Cloud Storage bucket.\n",
|
||||
@@ -102,28 +49,9 @@
|
||||
"- Evaluating the model.\n",
|
||||
"- Running the notebook end-to-end as a training job using Executor.\n",
|
||||
"- Deploying the model on Vertex AI.\n",
|
||||
"- Clean up."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "a71f4d96bf80"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"<a name=\"section-3\"></a>\n",
|
||||
"- Clean up.\n",
|
||||
"\n",
|
||||
"The dataset used in this notebook is a part of the [NASA Turbofan Engine Degradation Simulation dataset](https://ti.arc.nasa.gov/tech/dash/groups/pcoe/prognostic-data-repository/), which consists of simulated time-series data for four sets of fleet engines under different combinations of operational conditions and fault modes. A version of this dataset which is saved to a public Cloud Storage bucket is used in this notebook. In this notebook, one of the engine's simulated data (FD001) is used to analyze and train a model that can predict the engine's remaining useful life."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "36c53c95b4b9"
|
||||
},
|
||||
"source": [
|
||||
"### Costs\n",
|
||||
"## Costs\n",
|
||||
"<a name=\"section-4\"></a>\n",
|
||||
"\n",
|
||||
"This tutorial uses the following billable components of Google Cloud:\n",
|
||||
@@ -141,126 +69,24 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "629f52f6efe1"
|
||||
"id": "5b15a97278df"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### Kernel selection\n",
|
||||
"Select <b>XGBoost</b> kernel while running this notebook on Vertex AI Workbench's managed instances. Otherwise, ensure that the following libraries are installed in the environment where this notebook is being run.\n",
|
||||
"Select <b>XGBoost</b> kernel while running this notebook on Vertex AI Workbench managed notebooks instances or ensure that the following libraries are installed in the environment where this notebook is being run.\n",
|
||||
"- XGBoost\n",
|
||||
"- Pandas\n",
|
||||
"- Seaborn\n",
|
||||
"- Sklearn\n",
|
||||
"\n",
|
||||
"Along with the above libraries, th`e following google-cloud libraries are also used in this notebook.\n",
|
||||
"Along with the above libraries, the following google-cloud libraries are also used in this notebook.\n",
|
||||
"\n",
|
||||
"- google.cloud.aiplatform\n",
|
||||
"- google.cloud.storage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "16bee0754628"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"- google.cloud.storage\n",
|
||||
"\n",
|
||||
"Install the following packages to run this notebook outside Vertex AI Workbench's managed instances."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "69520a67e54c"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# The Vertex AI Workbench Notebook product has specific requirements\n",
|
||||
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
|
||||
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
|
||||
" \"/opt/deeplearning/metadata/env_version\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
|
||||
"USER_FLAG = \"\"\n",
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
" \n",
|
||||
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform \\\n",
|
||||
" google-cloud-storage \\\n",
|
||||
" xgboost \\\n",
|
||||
" seaborn \\\n",
|
||||
" sklearn \\\n",
|
||||
" fsspec \\\n",
|
||||
" gcsfs \\\n",
|
||||
" pandas -q"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "eda79cca981d"
|
||||
},
|
||||
"source": [
|
||||
"### Restart the kernel\n",
|
||||
"\n",
|
||||
"Once you've installed the additional packages, you need to restart the notebook kernel so it can find the packages."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "e200999cabe5"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" # Automatically restart kernel after installs\n",
|
||||
" import IPython\n",
|
||||
"\n",
|
||||
" app = IPython.Application.instance()\n",
|
||||
" app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5b15a97278df"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin \n",
|
||||
"\n",
|
||||
"### Set up your Google Cloud project\n",
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"\n",
|
||||
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
|
||||
"\n",
|
||||
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"1. [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
|
||||
"\n",
|
||||
"1. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
|
||||
"\n",
|
||||
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
|
||||
"Cloud SDK uses the right project for all the commands in this notebook.\n",
|
||||
"\n",
|
||||
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5aee4379e8e5"
|
||||
},
|
||||
"source": [
|
||||
"#### Set your project ID\n",
|
||||
"### Set your project ID\n",
|
||||
"\n",
|
||||
"**If you don't know your project ID**, you may be able to get your project ID using `gcloud`."
|
||||
]
|
||||
@@ -273,67 +99,36 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "5bf9979b96ff"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
|
||||
" # Get your GCP project id from gcloud\n",
|
||||
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"PROJECT_ID = \"\"\n",
|
||||
"\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",
|
||||
" PROJECT_ID = shell_output[0]\n",
|
||||
" print(\"Project ID:\", PROJECT_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "09021c90b34c"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gcloud config set project $PROJECT_ID"
|
||||
" print(\"Project ID: \", PROJECT_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "9658ecf524b1"
|
||||
"id": "750bf2883c2d"
|
||||
},
|
||||
"source": [
|
||||
"#### Region\n",
|
||||
"\n",
|
||||
"You can also change the `REGION` variable, which is used for operations\n",
|
||||
"throughout the rest of this notebook. Below are regions supported for Vertex AI. It is recommended that you choose the region closest to you.\n",
|
||||
"\n",
|
||||
"- Americas: `us-central1`\n",
|
||||
"- Europe: `europe-west4`\n",
|
||||
"- Asia Pacific: `asia-east1`\n",
|
||||
"\n",
|
||||
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
|
||||
"\n",
|
||||
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
"Otherwise, set your project ID here."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "5c615e53149f"
|
||||
"id": "3c6db1ca88b9"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
|
||||
"\n",
|
||||
"if REGION == \"[your-region]\":\n",
|
||||
" REGION = \"us-central1\""
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -342,9 +137,9 @@
|
||||
"id": "f66f96816fd0"
|
||||
},
|
||||
"source": [
|
||||
"#### UUID\n",
|
||||
"#### Timestamp\n",
|
||||
"\n",
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -355,84 +150,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import string\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Generate a uuid of a specifed length(default=8)\n",
|
||||
"def generate_uuid(length: int = 8) -> str:\n",
|
||||
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"UUID = generate_uuid()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "df899ce9999c"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
|
||||
"authenticated.\n",
|
||||
"\n",
|
||||
"**If you are using Colab**, run the cell below and follow the instructions\n",
|
||||
"when prompted to authenticate your account via oAuth.\n",
|
||||
"\n",
|
||||
"**Otherwise**, follow these steps:\n",
|
||||
"\n",
|
||||
"1. In the Cloud Console, go to the [**Create service account key**\n",
|
||||
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
|
||||
"\n",
|
||||
"2. Click **Create service account**.\n",
|
||||
"\n",
|
||||
"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 \"Vertex AI\"\n",
|
||||
"into the filter box, and select\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",
|
||||
"\n",
|
||||
"6. Enter the path to your service account key as the\n",
|
||||
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "201e8e760d22"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# If you are running this notebook in Colab, run this cell and follow the\n",
|
||||
"# instructions to authenticate your GCP account. This provides access to your\n",
|
||||
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
|
||||
"# requests.\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"# If on Vertex AI Workbench, then don't execute this code\n",
|
||||
"IS_COLAB = \"google.colab\" in sys.modules\n",
|
||||
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
|
||||
" \"DL_ANACONDA_HOME\"\n",
|
||||
"):\n",
|
||||
" if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth as google_auth\n",
|
||||
"\n",
|
||||
" google_auth.authenticate_user()\n",
|
||||
"\n",
|
||||
" # If you are running this notebook locally, replace the string below with the\n",
|
||||
" # path to your service account key and run this cell to authenticate your GCP\n",
|
||||
" # account.\n",
|
||||
" elif not os.getenv(\"IS_TESTING\"):\n",
|
||||
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
|
||||
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -441,18 +161,11 @@
|
||||
"id": "ea53caa30628"
|
||||
},
|
||||
"source": [
|
||||
"### Create a Cloud Storage bucket\n",
|
||||
"## Select or Create a Cloud Storage Bucket for storing the model\n",
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"When you create a model resource on Vertex AI using the Cloud SDK, you need to give a Cloud Storage bucket URI of the model where the model is stored. Using the model saved, you can then create Vertex AI model and endpoint resources in order to serve online predictions.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"When you create a model in Vertex AI using the Cloud SDK, you give a Cloud Storage path where the trained model is saved. \n",
|
||||
"In this tutorial, Vertex AI saves the trained model to a Cloud Storage bucket. Using this model artifact, you can then\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",
|
||||
"Cloud Storage buckets."
|
||||
"Set the name of your Cloud Storage bucket below. It must be unique across all Cloud Storage buckets. You may also change the `REGION` variable, which is used for operations throughout the rest of this notebook. Make sure to choose a region where Vertex AI services are available."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -463,8 +176,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
|
||||
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
|
||||
"BUCKET_NAME = \"[your-bucket-name]\"\n",
|
||||
"BUCKET_URI = f\"gs://{BUCKET_NAME}\"\n",
|
||||
"REGION = \"us-central1\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -475,9 +189,13 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
|
||||
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
|
||||
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
|
||||
"# Set a default bucketname in case bucket name is not given\n",
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None:\n",
|
||||
" from datetime import datetime\n",
|
||||
"\n",
|
||||
" TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
|
||||
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -497,7 +215,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil mb -l $REGION $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -517,7 +235,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil ls -al $BUCKET_URI"
|
||||
"! gsutil ls -al $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -526,7 +244,7 @@
|
||||
"id": "4c0f6aac282a"
|
||||
},
|
||||
"source": [
|
||||
"### Import the required libraries"
|
||||
"## Import the required libraries"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -569,7 +287,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# load the data from the source\n",
|
||||
"INPUT_PATH = \"gs://cloud-samples-data/ai-platform-unified/datasets/tabular/predictive_maintenance.csv\" # data source\n",
|
||||
"INPUT_PATH = \"gs://vertex_ai_managed_services_demo/mfg_predictive_maintenance/train_FD001.txt\" # data source\n",
|
||||
"raw_data = pd.read_csv(INPUT_PATH, sep=\" \", header=None)\n",
|
||||
"# check the data\n",
|
||||
"print(raw_data.shape)\n",
|
||||
@@ -774,7 +492,7 @@
|
||||
"id": "8197cdef2cff"
|
||||
},
|
||||
"source": [
|
||||
"As the current objective is to predict the remaining useful life (RUL) of each unit (ID), the target variable needs to be identified. Since you're dealing with a timeseries data that represents the lifetime of a unit, remaining useful life of a unit can be calculated by subtracting the current cycle from the maximum cycle of that unit.\n",
|
||||
"As the current objective is to predict the remaining useful life (RUL) of each unit (ID), the target variable needs to be identified. Since we're dealing with a timeseries data that represents the lifetime of a unit, remaining useful life of a unit can be calculated by subtracting the current cycle from the maximum cycle of that unit.\n",
|
||||
"\n",
|
||||
"\t\t\t\t\tRUL = Max. Cycle - Current Cycle \n",
|
||||
"## RUL calculation and Feature selection"
|
||||
@@ -1092,7 +810,6 @@
|
||||
"## Running a notebook end-to-end using executor\n",
|
||||
"<a name=\"section-9\"></a>\n",
|
||||
"\n",
|
||||
"**Note:** This section can only be considered when running this notebook on Managed instances from Vertex AI Workbench.\n",
|
||||
"### Automating the notebook execution\n",
|
||||
"All the steps followed until now can be run as a training job without using any additional code using the Vertex AI Workbench executor. The executor can help you run a notebook file from start to end, with your choice of the environment, machine type, input parameters, and other characteristics. After setting up an execution, the notebook is executed as a job in Vertex AI custom training. Your jobs can be monitored from the Executor pane in the left sidebar.\n",
|
||||
"\n",
|
||||
@@ -1100,13 +817,13 @@
|
||||
"\n",
|
||||
"The executor also lets you choose the environment and machine type while automating the runs similar to Vertex AI training jobs without switching to the training jobs UI. Apart from the custom container that replicates the existing kernel by default, pre-built environments like TensorFlow Enterprise, PyTorch, and others can also be selected to run the notebook. The required compute power can be specified by choosing from the list of machine types available, including GPUs.\n",
|
||||
"\n",
|
||||
"### Scheduled runs on executor\n",
|
||||
"## Scheduled runs on executor\n",
|
||||
"\n",
|
||||
"Notebook runs can also be scheduled recurringly with the executor. To do so, select Schedule-based recurring executions as the run type instead of One-time execution. The frequency of the job and the time when it executes is provided when you create the execution.\n",
|
||||
"\n",
|
||||
"<img src=\"https://storage.googleapis.com/gweb-cloudblog-publish/images/7_Vertex_AI_Workbench.max-1100x1100.jpg\">\n",
|
||||
"\n",
|
||||
"### Parameterizing the variables\n",
|
||||
"## Parameterizing the variables\n",
|
||||
"\n",
|
||||
"The executor lets you run a notebook with different sets of input parameters. If you’ve added parameter tags to any of your notebook cells, you can pass in your parameter values to the executor. More about how to use this feature can be found on this [blog](https://cloud.google.com/blog/products/ai-machine-learning/schedule-and-execute-notebooks-with-vertex-ai-workbench).\n",
|
||||
"\n",
|
||||
@@ -1138,37 +855,6 @@
|
||||
"ARTIFACT_GCS_PATH = f\"gs://{BUCKET_NAME}/{BLOB_PATH}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "1aa75b3d4616"
|
||||
},
|
||||
"source": [
|
||||
"Give a display name to the Vertex AI model resource."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "02ca350dba6c"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set the model-dsiplay-name\n",
|
||||
"MODEL_DISPLAY_NAME = \"[your-model-display-name]\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"# Otherwise, use the default name\n",
|
||||
"if (\n",
|
||||
" MODEL_DISPLAY_NAME == \"[your-model-display-name]\"\n",
|
||||
" or MODEL_DISPLAY_NAME is None\n",
|
||||
" or MODEL_DISPLAY_NAME == \"\"\n",
|
||||
"):\n",
|
||||
" MODEL_DISPLAY_NAME = \"pred_maint_model_\" + UUID\n",
|
||||
"\n",
|
||||
"print(MODEL_DISPLAY_NAME)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -1205,28 +891,6 @@
|
||||
"Next, create an endpoint resource for deploying the model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "e1e0cd571992"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set the endpoint-dsiplay-name\n",
|
||||
"ENDPOINT_DISPLAY_NAME = \"[your-endpoint-display-name]\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"# Otherwise, use the default name\n",
|
||||
"if (\n",
|
||||
" ENDPOINT_DISPLAY_NAME == \"[your-endpoint-display-name]\"\n",
|
||||
" or ENDPOINT_DISPLAY_NAME is None\n",
|
||||
" or ENDPOINT_DISPLAY_NAME == \"\"\n",
|
||||
"):\n",
|
||||
" ENDPOINT_DISPLAY_NAME = \"pred_maint_endpoint_\" + UUID\n",
|
||||
"\n",
|
||||
"print(ENDPOINT_DISPLAY_NAME)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -1235,7 +899,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create the Endpoint resource\n",
|
||||
"endpoint = aiplatform.Endpoint.create(display_name=ENDPOINT_DISPLAY_NAME)\n",
|
||||
"\n",
|
||||
"print(endpoint.display_name)\n",
|
||||
@@ -1252,11 +915,18 @@
|
||||
"<a name=\"section-12\"></a>\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Configure the following parameters and deploy the model to the created endpoint.\n",
|
||||
"\n",
|
||||
"- `endpoint`: The `Endpoint` object created using Vertex AI SDK.\n",
|
||||
"- `deployed_model_display_name`: A display-name for the deployment.\n",
|
||||
"- `machine_type`: Type of the machine required for the deployment environment. See [here](https://cloud.google.com/vertex-ai/docs/predictions/configure-compute) for references."
|
||||
"Configure the deployment name, machine type, and other parameters for the deployment and deploy the model to the created endpoint."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ca41cac871d6"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MACHINE_TYPE = \"n1-standard-2\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1270,8 +940,8 @@
|
||||
"# deploy the model to the endpoint\n",
|
||||
"model.deploy(\n",
|
||||
" endpoint=endpoint,\n",
|
||||
" deployed_model_display_name=MODEL_DISPLAY_NAME + \"_deployment\",\n",
|
||||
" machine_type=\"n1-standard-2\",\n",
|
||||
" deployed_model_display_name=DEPLOYED_MODEL_NAME,\n",
|
||||
" machine_type=MACHINE_TYPE,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model.wait()\n",
|
||||
@@ -1314,15 +984,7 @@
|
||||
"## Clean up\n",
|
||||
"<a name=\"section-14\"></a>\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",
|
||||
"* Vertex AI Model\n",
|
||||
"* Vertex AI Endpoint\n",
|
||||
"* Cloud Storage bucket\n",
|
||||
"\n",
|
||||
"Set `delete_bucket` to **True** to delete the Cloud Storage bucket."
|
||||
"Undeploy the model from the endpoint."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1333,19 +995,68 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Undeploy all the models from the endpoint\n",
|
||||
"endpoint.undeploy_all()\n",
|
||||
"\n",
|
||||
"# Delete the endpoint resource\n",
|
||||
"endpoint.delete()\n",
|
||||
"\n",
|
||||
"# Delete the model resource\n",
|
||||
"model.delete()\n",
|
||||
"\n",
|
||||
"# Delete the Cloud Storage bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
"DEPLOYED_MODEL_ID = \"\"\n",
|
||||
"endpoint.undeploy(deployed_model_id=DEPLOYED_MODEL_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "96e427b77791"
|
||||
},
|
||||
"source": [
|
||||
"Delete the endpoint."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ace028ac23ea"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"endpoint.delete()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4b77998d0512"
|
||||
},
|
||||
"source": [
|
||||
"Delete the model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "e034150a4c94"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.delete()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "23cb2deb122d"
|
||||
},
|
||||
"source": [
|
||||
"Remove the contents of the Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "98aaac27d85d"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -1068,7 +1068,7 @@
|
||||
"model = aiplatform.Model.upload(\n",
|
||||
" display_name=\"movies_\" + TIMESTAMP,\n",
|
||||
" artifact_uri=SAVEDMODEL_DIR,\n",
|
||||
" serving_container_image_uri=DEPLOY_IMAGE,\n",
|
||||
" serving_container_image_uri=DELOY_IMAGE,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 54,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ur8xi4C7S06n"
|
||||
},
|
||||
@@ -17,21 +17,12 @@
|
||||
"# https://www.apache.org/licenses/LICENSE-2.0\n",
|
||||
"#\n",
|
||||
"# Unless required by applicable law or agreed to in writing, software\n",
|
||||
"# distributed under the Lice`nse is distributed on an \"AS IS\" BASIS,\n",
|
||||
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
||||
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
||||
"# See the License for the specific language governing permissions and\n",
|
||||
"# limitations under the License."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0d2298941703"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI: Create, train, and deploy an AutoML text classification model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -52,8 +43,8 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl-text-classification.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/ai/platform/notebooks/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/automl/automl-text-classification.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",
|
||||
@@ -63,20 +54,19 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "1adb10a59bc3"
|
||||
"id": "0259a7ce8120"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI: Create, train, and deploy an AutoML text classification model\n",
|
||||
"\n",
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook walks you through the major phases of building and using an AutoML text classification model on [Vertex AI](https://cloud.google.com/vertex-ai/docs/). \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "9b9824ae2c91"
|
||||
},
|
||||
"source": [
|
||||
"This notebook walks you through the major phases of building and using an AutoML text classification model on [Vertex AI](https://cloud.google.com/vertex-ai/docs/). \n",
|
||||
"\n",
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"In this notebook, you use the \"Happy Moments\" sample dataset to train a model. The resulting model classifies happy moments into categores that reflect the causes of happiness. \n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to use `AutoML` to train a text classification model.\n",
|
||||
@@ -94,26 +84,8 @@
|
||||
"* Create an `Endpoint` resource.\n",
|
||||
"* Deploy the `Model` resource to the `Endpoint` resource.\n",
|
||||
"* Make an online prediction\n",
|
||||
"* Make a batch prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f67c62885df4"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"* Make a batch prediction\n",
|
||||
"\n",
|
||||
"In this notebook, you use the \"Happy Moments\" sample dataset to train a model. The resulting model classifies happy moments into categores that reflect the causes of happiness. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0259a7ce8120"
|
||||
},
|
||||
"source": [
|
||||
"### Costs\n",
|
||||
"\n",
|
||||
"This tutorial uses billable components of Google Cloud:\n",
|
||||
@@ -201,7 +173,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 55,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "b75757581291"
|
||||
},
|
||||
@@ -216,7 +188,6 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
|
||||
"\n",
|
||||
"USER_FLAG = \"\"\n",
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
@@ -237,7 +208,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 56,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "0c0b2427998a"
|
||||
},
|
||||
@@ -283,7 +254,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "be175254a715"
|
||||
},
|
||||
@@ -340,7 +311,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ae43d96c4b1b"
|
||||
},
|
||||
@@ -365,7 +336,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "953fa6e5ddda"
|
||||
},
|
||||
@@ -453,7 +424,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "d2de92accb67"
|
||||
},
|
||||
@@ -465,7 +436,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "5ba09496accc"
|
||||
},
|
||||
@@ -507,7 +478,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "96ad3d416327"
|
||||
},
|
||||
@@ -527,7 +498,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "152013538e59"
|
||||
},
|
||||
@@ -550,7 +521,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "740cd5c67c79"
|
||||
},
|
||||
@@ -580,15 +551,24 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "d35b8b6b94ae"
|
||||
"id": "6caf82e5e84e"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Use a timestamp to ensure unique resources\n",
|
||||
"src_uris = \"gs://cloud-ml-data/NL-classification/happiness.csv\"\n",
|
||||
"display_name = f\"e2e-text-dataset-{TIMESTAMP}\"\n",
|
||||
"\n",
|
||||
"text_dataset = aiplatform.TextDataset.create(\n",
|
||||
"display_name = f\"e2e-text-dataset-{TIMESTAMP}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "d35b8b6b94ae"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ds = aiplatform.TextDataset.create(\n",
|
||||
" display_name=display_name,\n",
|
||||
" gcs_source=src_uris,\n",
|
||||
" import_schema_uri=aiplatform.schema.dataset.ioformat.text.single_label_classification,\n",
|
||||
@@ -602,7 +582,53 @@
|
||||
"id": "5b3cc427353a"
|
||||
},
|
||||
"source": [
|
||||
"## Train your text classification model\n"
|
||||
"## Train your text classification model\n",
|
||||
"\n",
|
||||
"Once your dataset has finished importing data, you are ready to train your model. To do this, you first need the full resource name of your dataset, where the full name has the format `projects/[YOUR_PROJECT]/locations/[YOUR_REGIO)N]/datasets/[YOUR_DATASET_ID]`. If you don't have the resource name handy, you can list all of the datasets in your project using `TextDataset.list()`. \n",
|
||||
"\n",
|
||||
"As shown in the following code block, you can pass in the display name of your dataset in the call to `list()` to filter the results.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "52cf56f1c8a9"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"datasets = aiplatform.TextDataset.list(filter=f'display_name=\"{display_name}\"')\n",
|
||||
"print(datasets)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "58df3e02df82"
|
||||
},
|
||||
"source": [
|
||||
"When you create a new model, you need a reference to the `TextDataset` object that corresponds to your dataset. You can use the `ds` variable you created previously when you created the dataset or you can also list all of your datasets to get a reference to your dataset. Each item returned from `TextDataset.list()` is an instance of `TextDataset`.\n",
|
||||
"\n",
|
||||
"The following code block shows how to instantiate a `TextDataset` object using a dataset ID. Note that this code is intentionally verbose for demonstration purposes."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "aa667203da03"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the dataset ID if it's not available\n",
|
||||
"dataset_id = \"[your-dataset-id]\"\n",
|
||||
"\n",
|
||||
"if dataset_id == \"[your-dataset-id]\":\n",
|
||||
" # Use the reference to the new dataset captured when we created it\n",
|
||||
" dataset_id = ds.resource_name.split(\"/\")[-1]\n",
|
||||
" print(f\"Dataset ID: {dataset_id}\")\n",
|
||||
"\n",
|
||||
"text_dataset = aiplatform.TextDataset(dataset_id)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -627,7 +653,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "0aa0f01805ea"
|
||||
},
|
||||
@@ -656,8 +682,8 @@
|
||||
"model = job.run(\n",
|
||||
" dataset=text_dataset,\n",
|
||||
" model_display_name=model_display_name,\n",
|
||||
" training_fraction_split=0.1,\n",
|
||||
" validation_fraction_split=0.1,\n",
|
||||
" training_fraction_split=0.7,\n",
|
||||
" validation_fraction_split=0.2,\n",
|
||||
" test_fraction_split=0.1,\n",
|
||||
" sync=True,\n",
|
||||
")"
|
||||
@@ -714,11 +740,39 @@
|
||||
"deployed_model_display_name = f\"e2e-deployed-text-classification-model-{TIMESTAMP}\"\n",
|
||||
"\n",
|
||||
"endpoint = model.deploy(\n",
|
||||
" deployed_model_display_name=deployed_model_display_name, \n",
|
||||
" sync=True\n",
|
||||
" deployed_model_display_name=deployed_model_display_name, sync=True\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "531da446035b"
|
||||
},
|
||||
"source": [
|
||||
"In case you didn't record the name of the new endpoint, you can get a list of all your endpoints as you did before with datasets and models. For each endpoint, you can list the models deployed to that endpoint. To get a reference to the model that you just deployed, you can check the `display_name` of each model deployed to the endpoint against the model you're looking for."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "f61fb44181b4"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"endpoints = aiplatform.Endpoint.list()\n",
|
||||
"\n",
|
||||
"endpoint_with_deployed_model = []\n",
|
||||
"\n",
|
||||
"for endpoint_ in endpoints:\n",
|
||||
" for model in endpoint_.list_models():\n",
|
||||
" if model.display_name.find(deployed_model_display_name) == 0:\n",
|
||||
" endpoint_with_deployed_model.append(endpoint_)\n",
|
||||
"\n",
|
||||
"print(endpoint_with_deployed_model)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -727,7 +781,7 @@
|
||||
"source": [
|
||||
"## Get online predictions from your model\n",
|
||||
"\n",
|
||||
"Now that you have your endpoint, you can get online predictions from the text classification model. To get the online prediction, you send a prediction request to your endpoint."
|
||||
"Now that you have your endpoint's resource name, you can get online predictions from the text classification model. To get the online prediction, you send a prediction request to your endpoint."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -738,6 +792,13 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"endpoint_name = \"[your-endpoint-name]\"\n",
|
||||
"if endpoint_name == \"[your-endpoint-name]\":\n",
|
||||
" endpoint_name = endpoint.resource_name\n",
|
||||
"\n",
|
||||
"print(f\"Endpoint name: {endpoint_name}\")\n",
|
||||
"\n",
|
||||
"endpoint = aiplatform.Endpoint(endpoint_name)\n",
|
||||
"content = \"I got a high score on my math final!\"\n",
|
||||
"\n",
|
||||
"response = endpoint.predict(instances=[{\"content\": content}])\n",
|
||||
@@ -774,7 +835,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "e4b838cbcd99"
|
||||
},
|
||||
@@ -797,7 +858,7 @@
|
||||
"\n",
|
||||
"+ All of your prediction instances as individual files on Google Cloud Storage, as TXT files for your instances\n",
|
||||
"+ A JSONL file that lists the URIs of all your prediction instances\n",
|
||||
"+ A Cloud Storage bucket to hold the output from batch prediction\n",
|
||||
"+ A Google Cloud Storage bucket to hold the output from batch prediction\n",
|
||||
"\n",
|
||||
"For this tutorial, the following cells create a new Storage bucket, upload individual prediction instances as text files to the bucket, and then create the JSONL file with the URIs of your prediction instances."
|
||||
]
|
||||
@@ -811,15 +872,16 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Instantiate the Storage client and create the new bucket\n",
|
||||
"# from google.cloud import storage\n",
|
||||
"storage_client = storage.Client()\n",
|
||||
"bucket = storage_client.bucket(BUCKET_NAME)\n",
|
||||
"storage = storage.Client()\n",
|
||||
"bucket = storage.bucket(BUCKET_URI)\n",
|
||||
"\n",
|
||||
"# Iterate over the prediction instances, creating a new TXT file\n",
|
||||
"# for each.\n",
|
||||
"input_file_data = []\n",
|
||||
"for count, instance in enumerate(instances):\n",
|
||||
" instance_name = f\"input_{count}.txt\"\n",
|
||||
" instance_file_uri = f\"{BUCKET_URI}/{instance_name}\"\n",
|
||||
"\n",
|
||||
" # Add the data to store in the JSONL input file.\n",
|
||||
" tmp_data = {\"content\": instance_file_uri, \"mimeType\": \"text/plain\"}\n",
|
||||
" input_file_data.append(tmp_data)\n",
|
||||
@@ -839,7 +901,7 @@
|
||||
"id": "31c262320610"
|
||||
},
|
||||
"source": [
|
||||
"Now that you have the bucket with the prediction instances ready, you can send a batch prediction rhttps://storage.googleapis.com/upload/storage/v1/b/gs://vertex-ai-devaip-20220728004429/o?uploadType=multipartequest to Vertex AI. When you send a request to the service, you must provide the URI of your JSONL file and your output bucket, including the `gs://` protocols.\n",
|
||||
"Now that you have the bucket with the prediction instances ready, you can send a batch prediction request to Vertex AI. When you send a request to the service, you must provide the URI of your JSONL file and your output bucket, including the `gs://` protocols.\n",
|
||||
"\n",
|
||||
"With the Python SDK, you can create a batch prediction job by calling `Model.batch_predict()`."
|
||||
]
|
||||
@@ -853,13 +915,15 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job_display_name = \"e2e-text-classification-batch-prediction-job\"\n",
|
||||
"# model = aiplatform.Model(model_name=model.name)\n",
|
||||
"model = aiplatform.Model(model_name=model_name)\n",
|
||||
"\n",
|
||||
"batch_prediction_job = model.batch_predict(\n",
|
||||
" job_display_name=job_display_name,\n",
|
||||
" gcs_source=f\"{BUCKET_URI}/{input_file_name}\",\n",
|
||||
" gcs_destination_prefix=f\"{BUCKET_URI}/output\",\n",
|
||||
" sync=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"batch_prediction_job_name = batch_prediction_job.resource_name"
|
||||
]
|
||||
},
|
||||
@@ -874,15 +938,6 @@
|
||||
"The following code snippet demonstrates how to create an instance of the `BatchPredictionJob` class to review its status. Note that you need the full resource name printed out from the Python SDK for this snippet.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cd014de40e2f"
|
||||
},
|
||||
"source": [
|
||||
"## BatchPredictionJob"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -944,8 +999,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"RESULTS_DIRECTORY = \"prediction_results\"\n",
|
||||
"RESULTS_DIRECTORY_FULL = f\"{RESULTS_DIRECTORY}/output\"\n",
|
||||
"\n",
|
||||
@@ -967,15 +1020,6 @@
|
||||
"print(f\"Local results folder: {latest_directory}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e375109b7e40"
|
||||
},
|
||||
"source": [
|
||||
"## JsonLines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -1050,9 +1094,9 @@
|
||||
" ! gsutil rm -r $BUCKET_URI\n",
|
||||
"\n",
|
||||
"batch_job.delete()\n",
|
||||
"endpoint.undeploy_all()\n",
|
||||
"\n",
|
||||
"# `force` parameter ensures that models are undeployed before deletion\n",
|
||||
"endpoint.delete()\n",
|
||||
"endpoint.delete(force=True)\n",
|
||||
"\n",
|
||||
"model.delete()\n",
|
||||
"\n",
|
||||
|
||||
+7
-25
@@ -174,8 +174,9 @@
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade joblib fsspec gcsfs scikit-learn -q\n",
|
||||
"! pip3 install {USER_FLAG} --force-reinstall 'google-cloud-aiplatform>=1.15' -q"
|
||||
"! pip3 install {USER_FLAG} --upgrade fsspec gcsfs joblib -q\n",
|
||||
"! pip3 install {USER_FLAG} --force-reinstall 'google-cloud-aiplatform>=1.15' -q\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade sklearn"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -231,7 +232,7 @@
|
||||
"\n",
|
||||
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"1. [Enable APIs](https://console.cloud.google.com/flows/enableapi?apiid=cloudresourcemanager.googleapis.com,aiplatform.googleapis.com).\n",
|
||||
"1. [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
|
||||
"\n",
|
||||
"1. If you are running this notebook locally, you will need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
|
||||
"\n",
|
||||
@@ -392,14 +393,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 and select\n",
|
||||
"the following role into the filter box:\n",
|
||||
"\n",
|
||||
" * Storage Admin\n",
|
||||
" * Storage Object Admin\n",
|
||||
" * Service Account User\n",
|
||||
" * Vertex AI Administrator\n",
|
||||
"\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",
|
||||
" **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",
|
||||
@@ -627,13 +623,10 @@
|
||||
"logger = logging.getLogger(\"logger\")\n",
|
||||
"logging.basicConfig(level=logging.INFO)\n",
|
||||
"\n",
|
||||
"import collections\n",
|
||||
"import tempfile\n",
|
||||
"import time\n",
|
||||
"from json import dumps\n",
|
||||
"\n",
|
||||
"collections.Iterable = collections.abc.Iterable\n",
|
||||
"\n",
|
||||
"# Vertex AI\n",
|
||||
"from google.cloud import aiplatform as vertex_ai"
|
||||
]
|
||||
@@ -1247,17 +1240,6 @@
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "3f00c455b930"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!rm -Rf {DATA_PATH}"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
@@ -56,63 +56,26 @@
|
||||
"metadata": {
|
||||
"id": "tvgnzT1CKxrO"
|
||||
},
|
||||
"source": [
|
||||
"# Compare trained and evaluated model experiments using Vertex AI Experiments"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "3a0651225470"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"As a Data Scientist, you probably start running model experiments locally on your notebook. Depending on the framework you use, you would need to track parameters, training time series and evaluation metrics. In this way, you would be able to explain the modelling approach you would choose. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f3021b2963a6"
|
||||
},
|
||||
"source": [
|
||||
"As a Data Scientist, you probably start running model experiments locally on your notebook. Depending on the framework you use, you would need to track parameters, training time series and evaluation metrics. In this way, you would be able to explain the modelling approach you would choose. \n",
|
||||
"\n",
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset is the Tensorflow Dataset's Large Yelp Review Dataset. The Yelp reviews dataset consists of reviews from Yelp. For more information, please refer to this [link](http://www.yelp.com/dataset).\n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to use Vertex AI Experiments to compare and evaluate model experiments.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"\n",
|
||||
"- Vertex AI Workbench\n",
|
||||
"- Vertex AI Experiments\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"In this notebook, you will learn how to \n",
|
||||
"\n",
|
||||
"- log the model parameters\n",
|
||||
"- log the loss and metrics on every epoch to TensorBoard\n",
|
||||
"- log the evaluation metrics\n",
|
||||
"- compare two experiments\n",
|
||||
"\n",
|
||||
"in Vertex AI Experiment of a recurrent neural network (RNN) for sentiment analysis."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "20a5168cf05e"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"in Vertex AI Experiment of a recurrent neural network (RNN) for sentiment analysis.\n",
|
||||
"\n",
|
||||
"The dataset is the Tensorflow Dataset's Large Yelp Review Dataset. The Yelp reviews dataset consists of reviews from Yelp. For more information, please refer to this [link](http://www.yelp.com/dataset).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "de76bb18c85b"
|
||||
},
|
||||
"source": [
|
||||
"### Costs \n",
|
||||
"\n",
|
||||
"This tutorial uses billable components of Google Cloud:\n",
|
||||
@@ -136,8 +99,15 @@
|
||||
"### Set up your local development environment\n",
|
||||
"\n",
|
||||
"**If you are using Colab or Vertex AI Workbench Notebooks**, your environment already meets\n",
|
||||
"all the requirements to run this notebook. You can skip this step.\n",
|
||||
"\n",
|
||||
"all the requirements to run this notebook. You can skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "gCuSR8GkAgzl"
|
||||
},
|
||||
"source": [
|
||||
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
|
||||
"You need the following:\n",
|
||||
"\n",
|
||||
@@ -199,8 +169,17 @@
|
||||
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
|
||||
"USER_FLAG = \"\"\n",
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"\n",
|
||||
" USER_FLAG = \"--user\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "wyy5Lbnzg5fi"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip3 install {USER_FLAG} --upgrade tensorflow==2.8.0 tensorflow_datasets==4.5.2 -q\n",
|
||||
"! pip3 install --user --force-reinstall 'google-cloud-aiplatform>=1.15' -q"
|
||||
]
|
||||
@@ -397,8 +376,15 @@
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
|
||||
"authenticated. Skip this step.\n",
|
||||
"\n",
|
||||
"authenticated. Skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "sBCra4QMA2wR"
|
||||
},
|
||||
"source": [
|
||||
"**If you are using Colab**, run the cell below and follow the instructions\n",
|
||||
"when prompted to authenticate your account via oAuth.\n",
|
||||
"\n",
|
||||
@@ -412,14 +398,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 and select\n",
|
||||
"the following role into the filter box:\n",
|
||||
"\n",
|
||||
" * Storage Admin\n",
|
||||
" * Storage Object Admin\n",
|
||||
" * Service Account User\n",
|
||||
" * Vertex AI Administrator\n",
|
||||
"\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",
|
||||
" **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",
|
||||
@@ -445,11 +426,16 @@
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"# If on Vertex AI Workbench, then don't execute this code\n",
|
||||
"IS_COLAB = \"google.colab\" in sys.modules\n",
|
||||
"\n",
|
||||
"IS_COLAB = False\n",
|
||||
"\n",
|
||||
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
|
||||
" \"DL_ANACONDA_HOME\"\n",
|
||||
"):\n",
|
||||
" if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" IS_COLAB = True\n",
|
||||
"\n",
|
||||
" from google.colab import auth as google_auth\n",
|
||||
"\n",
|
||||
" google_auth.authenticate_user()\n",
|
||||
@@ -457,7 +443,9 @@
|
||||
" # If you are running this notebook locally, replace the string below with the\n",
|
||||
" # path to your service account key and run this cell to authenticate your GCP\n",
|
||||
" # account.\n",
|
||||
"\n",
|
||||
" elif not os.getenv(\"IS_TESTING\"):\n",
|
||||
"\n",
|
||||
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
|
||||
]
|
||||
},
|
||||
@@ -662,15 +650,7 @@
|
||||
"\n",
|
||||
"Vertex AI enables users to track the steps (for example, preprocessing, training) of an experiment run, and track inputs (for example, algorithm, parameters, datasets) and outputs (for example, models, checkpoints, metrics) of those steps. \n",
|
||||
"\n",
|
||||
"Below you have some example of how track experiments to train recurrent neural network for sentiment analysis. \n",
|
||||
"\n",
|
||||
"To simplify the code, here you have helper function to cover the following steps:\n",
|
||||
"\n",
|
||||
"- Collect training data\n",
|
||||
"- Create text encoder\n",
|
||||
"- Build a RNN as baseline model\n",
|
||||
"- Build a LSTM as challenger model\n",
|
||||
"- Train the model"
|
||||
"Below you have some example of how track experiments to train recurrent neural network for sentiment analysis."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -741,16 +721,16 @@
|
||||
" return encoder\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_baseline_model(encoder, model_params):\n",
|
||||
"def get_model(encoder, model_params, role):\n",
|
||||
" \"\"\"\n",
|
||||
" Returns a tf.keras.Model object for the model as baseline\n",
|
||||
" Returns a tf.keras.Model object for the model\n",
|
||||
" Args:\n",
|
||||
" encoder: A TextVectorization object for the encoder\n",
|
||||
" model_params: A dictionary with model parameters\n",
|
||||
" role: A variable to set the role of model\n",
|
||||
" Returns:\n",
|
||||
" tf.keras.Model: A tf.keras.Model object for the model\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" model = tf.keras.Sequential()\n",
|
||||
" model.add(encoder)\n",
|
||||
" model.add(\n",
|
||||
@@ -758,46 +738,20 @@
|
||||
" input_dim=len(encoder.get_vocabulary()), output_dim=64, mask_zero=True\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)))\n",
|
||||
" model.add(tf.keras.layers.Dense(64, activation=\"relu\"))\n",
|
||||
" model.add(tf.keras.layers.Dense(1))\n",
|
||||
" model.compile(\n",
|
||||
" loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
|
||||
" optimizer=tf.keras.optimizers.Adam(\n",
|
||||
" learning_rate=model_params[\"learning_rate\"],\n",
|
||||
" beta_1=model_params[\"beta_1\"],\n",
|
||||
" beta_2=model_params[\"beta_2\"],\n",
|
||||
" epsilon=model_params[\"epsilon\"],\n",
|
||||
" ),\n",
|
||||
" metrics=[\"accuracy\"],\n",
|
||||
" )\n",
|
||||
" return model\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_stacked_model(encoder, model_params):\n",
|
||||
" \"\"\"\n",
|
||||
" Returns a tf.keras.Model object for the model as challenger\n",
|
||||
" Args:\n",
|
||||
" encoder: A TextVectorization object for the encoder\n",
|
||||
" model_params: A dictionary with model parameters\n",
|
||||
" Returns:\n",
|
||||
" tf.keras.Model: A tf.keras.Model object for the model\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" model = tf.keras.Sequential()\n",
|
||||
" model.add(encoder)\n",
|
||||
" model.add(\n",
|
||||
" tf.keras.layers.Embedding(\n",
|
||||
" input_dim=len(encoder.get_vocabulary()), output_dim=64, mask_zero=True\n",
|
||||
" if role == \"baseline\":\n",
|
||||
" model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)))\n",
|
||||
" model.add(tf.keras.layers.Dense(64, activation=\"relu\"))\n",
|
||||
" model.add(tf.keras.layers.Dense(1))\n",
|
||||
" else:\n",
|
||||
" model.add(\n",
|
||||
" tf.keras.layers.Bidirectional(\n",
|
||||
" tf.keras.layers.LSTM(64, return_sequences=True)\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" )\n",
|
||||
" model.add(\n",
|
||||
" tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences=True))\n",
|
||||
" )\n",
|
||||
" model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)))\n",
|
||||
" model.add(tf.keras.layers.Dense(64, activation=\"relu\"))\n",
|
||||
" model.add(tf.keras.layers.Dropout(0.5))\n",
|
||||
" model.add(tf.keras.layers.Dense(1))\n",
|
||||
" model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)))\n",
|
||||
" model.add(tf.keras.layers.Dropout(0.5))\n",
|
||||
" model.add(tf.keras.layers.Dense(1))\n",
|
||||
"\n",
|
||||
" model.compile(\n",
|
||||
" loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
|
||||
" optimizer=tf.keras.optimizers.Adam(\n",
|
||||
@@ -852,15 +806,7 @@
|
||||
"source": [
|
||||
"#### Run experiment and evaluate experiment runs using `with` statement\n",
|
||||
"\n",
|
||||
"This step would takes **10 min** approx. to finish. And it covers the following steps:\n",
|
||||
"\n",
|
||||
"- Initialize an experiment run\n",
|
||||
"- Log the parameters associated to training data\n",
|
||||
"- Log the parameters of the encoder\n",
|
||||
"- Log the parameters of the model\n",
|
||||
"- Train the model\n",
|
||||
"- Log the metrics for each epochs\n",
|
||||
"- Log the overall training metrics"
|
||||
"This step would takes **10 min** approx. to finish.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -872,7 +818,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Experiment Settings ----------------------------------------------------------\n",
|
||||
"RUN_ID_1 = \"run-1\"\n",
|
||||
"ID_1 = \"run-1\"\n",
|
||||
"BUFFER_SIZE = 10000\n",
|
||||
"BATCH_SIZE = 64\n",
|
||||
"VOCAB_SIZE = 1000\n",
|
||||
@@ -886,7 +832,7 @@
|
||||
"\n",
|
||||
"# Initialize the experiment\n",
|
||||
"logging.info(\"Initialize the experiment.\")\n",
|
||||
"with vertex_ai.start_run(RUN_ID_1) as run:\n",
|
||||
"with vertex_ai.start_run(ID_1) as run:\n",
|
||||
"\n",
|
||||
" # Get the training and testing datasets\n",
|
||||
" logging.info(\"Get the training and testing datasets.\")\n",
|
||||
@@ -910,7 +856,7 @@
|
||||
" logging.info(\"Get the model.\")\n",
|
||||
" run.log_params({\"role\": ROLE})\n",
|
||||
" model_params = {\"learning_rate\": LR, \"beta_1\": B_1, \"beta_2\": B_2, \"epsilon\": EPS}\n",
|
||||
" model = get_baseline_model(encoder=encoder, model_params=model_params)\n",
|
||||
" model = get_model(encoder=encoder, model_params=model_params, role=ROLE)\n",
|
||||
" run.log_params(model_params)\n",
|
||||
"\n",
|
||||
" # Train the model\n",
|
||||
@@ -961,7 +907,7 @@
|
||||
"# Get experiment\n",
|
||||
"logging.info(\"Get experiment status.\")\n",
|
||||
"experiment_df = vertex_ai.get_experiment_df()\n",
|
||||
"experiment_df.T"
|
||||
"experiment_df"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1007,17 +953,17 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Experiment Settings ----------------------------------------------------------\n",
|
||||
"RUN_ID_2 = \"run-2\"\n",
|
||||
"ID_2 = \"run-2\"\n",
|
||||
"ROLE = \"stacked\"\n",
|
||||
"\n",
|
||||
"# Initialize the experiment\n",
|
||||
"logger.info(\"Initialize the experiment.\")\n",
|
||||
"vertex_ai.start_run(RUN_ID_2)\n",
|
||||
"vertex_ai.start_run(ID_2)\n",
|
||||
"\n",
|
||||
"# Get the model\n",
|
||||
"logging.info(\"Get the model.\")\n",
|
||||
"run.log_params({\"role\": ROLE})\n",
|
||||
"model = get_stacked_model(encoder=encoder, model_params=model_params)\n",
|
||||
"model = get_model(encoder=encoder, model_params=model_params, role=ROLE)\n",
|
||||
"vertex_ai.log_params(model_params)\n",
|
||||
"\n",
|
||||
"# Train the model\n",
|
||||
@@ -1068,7 +1014,7 @@
|
||||
"# Get experiment\n",
|
||||
"logging.info(\"Get experiment status.\")\n",
|
||||
"experiment_df = vertex_ai.get_experiment_df()\n",
|
||||
"experiment_df.T"
|
||||
"experiment_df"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1080,10 +1026,10 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get time series metrics\n",
|
||||
"exp_run = vertex_ai.ExperimentRun(RUN_ID_2, experiment=EXPERIMENT_NAME)\n",
|
||||
"exp_run = vertex_ai.ExperimentRun(ID_2, experiment=EXPERIMENT_NAME)\n",
|
||||
"logging.info(\"Get time series metrics.\")\n",
|
||||
"ts_runs_df = exp_run.get_time_series_data_frame()\n",
|
||||
"ts_runs_df"
|
||||
"print(ts_runs_df)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1110,29 +1056,34 @@
|
||||
"source": [
|
||||
"# Delete experiment\n",
|
||||
"exp = vertex_ai.Experiment(EXPERIMENT_NAME)\n",
|
||||
"exp.delete(delete_backing_tensorboard_runs=True)\n",
|
||||
"\n",
|
||||
"# Delete Tensorboard\n",
|
||||
"vertex_ai_tb.delete()\n",
|
||||
"\n",
|
||||
"# Delete Cloud Storage objects that were created\n",
|
||||
"delete_bucket = True\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil rm -rf {BUCKET_URI}\n",
|
||||
"\n",
|
||||
"!rm -Rf $DATA_DIR $LOG_DIR"
|
||||
"exp.delete(delete_backing_tensorboard_runs=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "15fbfe47e022"
|
||||
"id": "dde8937123d4"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!rm -Rf $DATA_DIR $LOG_DIR"
|
||||
"# Delete Tensorboard\n",
|
||||
"vertex_ai_tb.delete()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "sx_vKniMq9ZX"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Delete Cloud Storage objects that were created\n",
|
||||
"delete_bucket = False\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil rm -rf {BUCKET_URI}"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -214,7 +214,7 @@
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" # Automatically restart kernel after installs\n",
|
||||
" import IPython\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" app = IPython.Application.instance()\n",
|
||||
" app.kernel.do_shutdown(True)"
|
||||
]
|
||||
@@ -675,18 +675,6 @@
|
||||
"!./swivel_template_configuration.sh -pipeline_suffix {YOUR_PIPELINE_SUFFIX} -project_number {PROJECT_NUMBER} -project_id {PROJECT_ID} -machine_type {MACHINE_TYPE} -accelerator_count {ACCELERATOR_COUNT} -accelerator_type {ACCELERATOR_TYPE} -pipeline_root {BUCKET}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "1cacea95d68c"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! sed \"s:\\t: :g\" swivel_pipeline_basic.json >tmp.json\n",
|
||||
"! mv tmp.json swivel_pipeline_basic.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -777,15 +765,19 @@
|
||||
"# Instantiate PipelineJob object\n",
|
||||
"pl = aiplatform.PipelineJob(\n",
|
||||
" display_name=YOUR_PIPELINE_SUFFIX,\n",
|
||||
"\n",
|
||||
" # Whether or not to enable caching\n",
|
||||
" # True = always cache pipeline step result\n",
|
||||
" # False = never cache pipeline step result\n",
|
||||
" # None = defer to cache option for each pipeline component in the pipeline definition\n",
|
||||
" enable_caching=False,\n",
|
||||
"\n",
|
||||
" # Local or GCS path to a compiled pipeline definition\n",
|
||||
" template_path=\"swivel_pipeline_basic.json\",\n",
|
||||
"\n",
|
||||
" # Dictionary containing input parameters for your pipeline\n",
|
||||
" parameter_values=PARAMETER_VALUES,\n",
|
||||
"\n",
|
||||
" # GCS path to act as the pipeline root\n",
|
||||
" pipeline_root=PIPELINE_ROOT,\n",
|
||||
")\n",
|
||||
|
||||
@@ -32,24 +32,17 @@
|
||||
"# Vertex AI: Vertex AI Migration: AutoML Video Classificaton\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/migration/UJ14 Vertex SDK AutoML Video Classification.ipynb\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/ai-platform-samples/blob/master/vertex-ai-samples/tree/master/notebooks/official/migration/UJ14%20Vertex%20SDK%20AutoML%20Video%20Classification.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/migration/UJ14 Vertex SDK AutoML Video Classification.ipynb\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/ai-platform-samples/blob/master/vertex-ai-samples/tree/master/notebooks/official/migration/UJ14%20Vertex%20SDK%20AutoML%20Video%20Classification.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" 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/migration/UJ14 Vertex SDK AutoML Video Classification.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>\n",
|
||||
"<br/><br/><br/>"
|
||||
]
|
||||
@@ -157,6 +150,17 @@
|
||||
"Install the latest GA version of *google-cloud-storage* library as well."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "install_storage"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip3 install -U google-cloud-storage $USER_FLAG"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -166,7 +170,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! pip3 install -U google-cloud-storage --upgrade tensorflow $USER_FLAG"
|
||||
" ! pip3 install --upgrade tensorflow $USER_FLAG"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -293,10 +297,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
|
||||
"\n",
|
||||
"if REGION == \"[your-region]\":\n",
|
||||
" REGION = \"us-central1\""
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -305,9 +306,9 @@
|
||||
"id": "timestamp"
|
||||
},
|
||||
"source": [
|
||||
"#### UUID\n",
|
||||
"#### Timestamp\n",
|
||||
"\n",
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -318,16 +319,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import string\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Generate a uuid of a specifed length(default=8)\n",
|
||||
"def generate_uuid(length: int = 8) -> str:\n",
|
||||
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"UUID = generate_uuid()"
|
||||
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -338,7 +332,7 @@
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already authenticated. Skip this step.\n",
|
||||
"**If you are using Google Cloud Notebooks**, your environment is already authenticated. Skip this step.\n",
|
||||
"\n",
|
||||
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
|
||||
"\n",
|
||||
@@ -410,8 +404,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
|
||||
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
|
||||
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -422,9 +415,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
|
||||
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
|
||||
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
|
||||
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -444,7 +436,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil mb -l $REGION $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -464,7 +456,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil ls -al $BUCKET_URI"
|
||||
"! gsutil ls -al $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -509,7 +501,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
|
||||
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -610,7 +602,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset = aip.VideoDataset.create(\n",
|
||||
" display_name=\"MIT Human Motion\" + \"_\" + UUID,\n",
|
||||
" display_name=\"MIT Human Motion\" + \"_\" + TIMESTAMP,\n",
|
||||
" gcs_source=[IMPORT_FILE],\n",
|
||||
" import_schema_uri=aip.schema.dataset.ioformat.video.classification,\n",
|
||||
")\n",
|
||||
@@ -685,7 +677,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dag = aip.AutoMLVideoTrainingJob(\n",
|
||||
" display_name=\"hmdb_\" + UUID,\n",
|
||||
" display_name=\"hmdb_\" + TIMESTAMP,\n",
|
||||
" prediction_type=\"classification\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
@@ -733,7 +725,7 @@
|
||||
"source": [
|
||||
"model = dag.run(\n",
|
||||
" dataset=dataset,\n",
|
||||
" model_display_name=\"hmdb_\" + UUID,\n",
|
||||
" model_display_name=\"hmdb_\" + TIMESTAMP,\n",
|
||||
" training_fraction_split=0.8,\n",
|
||||
" test_fraction_split=0.2,\n",
|
||||
")"
|
||||
@@ -808,7 +800,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get model resource ID\n",
|
||||
"models = aip.Model.list(filter=\"display_name=hmdb_\" + UUID)\n",
|
||||
"models = aip.Model.list(filter=\"display_name=hmdb_\" + TIMESTAMP)\n",
|
||||
"\n",
|
||||
"# Get a reference to the Model Service client\n",
|
||||
"client_options = {\"api_endpoint\": f\"{REGION}-aiplatform.googleapis.com\"}\n",
|
||||
@@ -940,7 +932,7 @@
|
||||
"\n",
|
||||
"import tensorflow as tf\n",
|
||||
"\n",
|
||||
"gcs_input_uri = BUCKET_URI + \"/test.jsonl\"\n",
|
||||
"gcs_input_uri = BUCKET_NAME + \"/test.jsonl\"\n",
|
||||
"with tf.io.gfile.GFile(gcs_input_uri, \"w\") as f:\n",
|
||||
" data = {\n",
|
||||
" \"content\": test_item_1,\n",
|
||||
@@ -986,9 +978,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_predict_job = model.batch_predict(\n",
|
||||
" job_display_name=\"hmdb_\" + UUID,\n",
|
||||
" job_display_name=\"hmdb_\" + TIMESTAMP,\n",
|
||||
" gcs_source=gcs_input_uri,\n",
|
||||
" gcs_destination_prefix=BUCKET_URI,\n",
|
||||
" gcs_destination_prefix=BUCKET_NAME,\n",
|
||||
" sync=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
@@ -1216,8 +1208,8 @@
|
||||
" except Exception as e:\n",
|
||||
" print(e)\n",
|
||||
"\n",
|
||||
" if \"BUCKET_URI\" in globals():\n",
|
||||
" ! gsutil rm -r $BUCKET_URI"
|
||||
" if \"BUCKET_NAME\" in globals():\n",
|
||||
" ! gsutil rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
+24
-51
@@ -29,8 +29,6 @@
|
||||
"id": "JAPoU8Sm5E6e"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI: Track parameters and metrics for custom training jobs\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
@@ -56,20 +54,25 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "tvgnzT1CKxrO"
|
||||
"id": "j9gUDU_3vV9d"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to track metrics and parameters for `Vertex AI` custom training jobs, and how to perform detailed analysis using this data."
|
||||
"# Vertex AI: Track parameters and metrics for custom training jobs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "37147bd9c3c4"
|
||||
"id": "tvgnzT1CKxrO"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates how to track metrics and parameters for `Vertex AI` custom training jobs, and how to perform detailed analysis using this data.\n",
|
||||
"\n",
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"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 learn how to use `Vertex ML Metadata` to track training parameters and evaluation metrics.\n",
|
||||
@@ -82,26 +85,8 @@
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Track parameters and metrics for a `Vertex AI` custom trained model.\n",
|
||||
"- Extract and perform analysis for all parameters and metrics within an Experiment."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "96cb18467417"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"- Extract and perform analysis for all parameters and metrics within an Experiment.\n",
|
||||
"\n",
|
||||
"This example uses the Abalone Dataset. For more information about this dataset please visit: https://archive.ics.uci.edu/ml/datasets/abalone"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "c831245dc1d5"
|
||||
},
|
||||
"source": [
|
||||
"### Costs \n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -300,7 +285,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if PROJECT_ID == \"[your-project-id]\" or PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
||||
" # Get your GCP project id from gcloud\n",
|
||||
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
|
||||
" PROJECT_ID = shell_output[0]\n",
|
||||
@@ -346,10 +332,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
|
||||
"\n",
|
||||
"if REGION == \"[your-region]\":\n",
|
||||
" REGION = \"us-central1\""
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -385,7 +368,7 @@
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench**, your environment is already\n",
|
||||
"authenticated. "
|
||||
"authenticated. Skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -681,9 +664,9 @@
|
||||
"id": "35QVNhACqcTJ"
|
||||
},
|
||||
"source": [
|
||||
"### Create a Vertex AI Dataset from a CSV\n",
|
||||
"### Create a managed tabular dataset from a CSV\n",
|
||||
"\n",
|
||||
"A Vertex AI Dataset can be used to create an AutoML model or a custom model. "
|
||||
"A Managed dataset can be used to create an AutoML model or a custom model. "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -696,7 +679,7 @@
|
||||
"source": [
|
||||
"ds = aiplatform.TabularDataset.create(display_name=\"abalone\", gcs_source=[gcs_csv_path])\n",
|
||||
"\n",
|
||||
"print(ds.resource_name)"
|
||||
"ds.resource_name"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -797,11 +780,7 @@
|
||||
"id": "k_QorXXztzPH"
|
||||
},
|
||||
"source": [
|
||||
"Start a new experiment run to track training parameters and start the training job. \n",
|
||||
"\n",
|
||||
"Prior to executing the training job, you call the `start_run()` method to initialize the start of the experiment, and then use the `log_params()` to log the parameters used in the experiment.\n",
|
||||
"\n",
|
||||
"*Note:* This operation will take around 10 mins."
|
||||
"Start a new experiment run to track training parameters and start the training job. Note that this operation will take around 10 mins."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -839,7 +818,7 @@
|
||||
"id": "O-uCOL3Naap4"
|
||||
},
|
||||
"source": [
|
||||
"Deploy model to Google Cloud. This operation may take a few minutes."
|
||||
"Deploy model to Google Cloud. This operation will take 10-20 mins."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -920,7 +899,7 @@
|
||||
"id": "_HphZ38obJeB"
|
||||
},
|
||||
"source": [
|
||||
"### Perform online prediction"
|
||||
"Perform online prediction."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -932,7 +911,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prediction = endpoint.predict(test_dataset.tolist())\n",
|
||||
"print(prediction)"
|
||||
"prediction"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -941,11 +920,7 @@
|
||||
"id": "TDKiv_O7bNwE"
|
||||
},
|
||||
"source": [
|
||||
"### Calculate and track prediction evaluation metrics.\n",
|
||||
"\n",
|
||||
"Next, log the evaluation metrics for your experiment.\n",
|
||||
"\n",
|
||||
"Once the experiment is completed, you call the `end_run()` method to indicate the end of tracking for the experiment."
|
||||
"Calculate and track prediction evaluation metrics."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -959,9 +934,7 @@
|
||||
"mse = mean_squared_error(test_labels, prediction.predictions)\n",
|
||||
"mae = mean_absolute_error(test_labels, prediction.predictions)\n",
|
||||
"\n",
|
||||
"aiplatform.log_metrics({\"mse\": mse, \"mae\": mae})\n",
|
||||
"\n",
|
||||
"aiplatform.end_run()"
|
||||
"aiplatform.log_metrics({\"mse\": mse, \"mae\": mae})"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -59,8 +59,15 @@
|
||||
"id": "lA32H1oKGgpf"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"## Overview"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "t6Cd51FkG09E"
|
||||
},
|
||||
"source": [
|
||||
"### What is Vertex AI Model Monitoring?\n",
|
||||
"\n",
|
||||
"Modern applications rely on a well established set of capabilities to monitor the health of their services. Examples include:\n",
|
||||
@@ -81,15 +88,7 @@
|
||||
"\n",
|
||||
"[Vertex Explainable AI](https://cloud.google.com/vertex-ai/docs/explainable-ai/overview) adds another facet to model monitoring, which we call feature attribution monitoring. Explainable AI enables you to understand the relative contribution of each feature to a resulting prediction. In essence, it assesses the magnitude of each feature's influence.\n",
|
||||
"\n",
|
||||
"If production traffic differs from training data, or varies substantially over time, **either in terms of model predictions or feature attributions**, that's likely to impact the quality of the answers your model produces. When that happens, you'd like to be alerted automatically and responsively, so that **you can anticipate problems before they affect your customer experiences or your revenue streams**."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "t6Cd51FkG09E"
|
||||
},
|
||||
"source": [
|
||||
"If production traffic differs from training data, or varies substantially over time, **either in terms of model predictions or feature attributions**, that's likely to impact the quality of the answers your model produces. When that happens, you'd like to be alerted automatically and responsively, so that **you can anticipate problems before they affect your customer experiences or your revenue streams**.\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this notebook, you learn to use the `Vertex AI Model Monitoring` service to detect drift and anomalies in prediction requests from a deployed `Vertex AI Model` resource. \n",
|
||||
@@ -108,38 +107,18 @@
|
||||
"- Deploy the `Model` resource to the `Endpoint` resource.\n",
|
||||
"- Configure the `Endpoint` resource for model monitoring.\n",
|
||||
"- Generate synthetic prediction requests.\n",
|
||||
"- Understand how to interpret the statistics, visualizations, other data reported by the model monitoring feature."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "edba71dc9840"
|
||||
},
|
||||
"source": [
|
||||
"### Model\n",
|
||||
"- Understand how to interpret the statistics, visualizations, other data reported by the model monitoring feature.\n",
|
||||
"\n",
|
||||
"This tutorial uses a pre-trained model, where the model artifacts are stored in a public Cloud Storage bucket. The model predicts for an online gaming site, the probability that a player will churn, i.e. stop being an active player."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5abcd585354f"
|
||||
},
|
||||
"source": [
|
||||
"### Costs \n",
|
||||
"\n",
|
||||
"This tutorial uses billable components of Google Cloud:\n",
|
||||
"\n",
|
||||
"* Vertex AI\n",
|
||||
"* Vertext AI\n",
|
||||
"* BigQuery\n",
|
||||
"* Cloud Storage\n",
|
||||
"\n",
|
||||
"Learn about [Vertext AI pricing](https://cloud.google.com/vertex-ai/pricing), \n",
|
||||
"[Cloud Storage pricing](https://cloud.google.com/storage/pricing), \n",
|
||||
"and [BigQuery pricing](https://cloud.google.com/bigquery/pricing)\n",
|
||||
"and use the [Pricing\n",
|
||||
"Learn about [Vertext AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
@@ -150,8 +129,6 @@
|
||||
"id": "8yVpQt-JHKPF"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### Set up your local development environment\n",
|
||||
"\n",
|
||||
"**If you are using Colab or Vertex AI Workbench notebooks**, your environment already meets\n",
|
||||
@@ -188,6 +165,15 @@
|
||||
"1. Open this notebook in the Jupyter Notebook dashboard."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ze4-nDLfK4pw"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -315,7 +301,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
||||
" # Get your GCP project id from gcloud\n",
|
||||
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
|
||||
" PROJECT_ID = shell_output[0]\n",
|
||||
@@ -362,41 +349,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
|
||||
"\n",
|
||||
"if REGION == \"[your-region]\":\n",
|
||||
" REGION = \"us-central1\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "06571eb4063b"
|
||||
},
|
||||
"source": [
|
||||
"#### UUID\n",
|
||||
"\n",
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "4e166d927e36"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import string\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Generate a uuid of a specifed length(default=8)\n",
|
||||
"def generate_uuid(length: int = 8) -> str:\n",
|
||||
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"UUID = generate_uuid()"
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -519,6 +472,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import copy\n",
|
||||
"# Import required packages.\n",
|
||||
"import os\n",
|
||||
"import pprint as pp\n",
|
||||
@@ -526,9 +480,25 @@
|
||||
"\n",
|
||||
"import google.cloud.aiplatform as aiplatform\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from google.cloud.aiplatform import model_monitoring\n",
|
||||
"from google.cloud.aiplatform.explain.metadata.tf.v2 import \\\n",
|
||||
" saved_model_metadata_builder"
|
||||
" saved_model_metadata_builder\n",
|
||||
"from google.cloud.aiplatform_v1.services.endpoint_service import \\\n",
|
||||
" EndpointServiceClient\n",
|
||||
"from google.cloud.aiplatform_v1.services.job_service import JobServiceClient\n",
|
||||
"from google.cloud.aiplatform_v1.services.prediction_service import \\\n",
|
||||
" PredictionServiceClient\n",
|
||||
"from google.cloud.aiplatform_v1.types.io import BigQuerySource\n",
|
||||
"from google.cloud.aiplatform_v1.types.model_deployment_monitoring_job import (\n",
|
||||
" ModelDeploymentMonitoringJob, ModelDeploymentMonitoringObjectiveConfig,\n",
|
||||
" ModelDeploymentMonitoringScheduleConfig)\n",
|
||||
"from google.cloud.aiplatform_v1.types.model_monitoring import (\n",
|
||||
" ModelMonitoringAlertConfig, ModelMonitoringObjectiveConfig,\n",
|
||||
" SamplingStrategy, ThresholdConfig)\n",
|
||||
"from google.cloud.aiplatform_v1.types.prediction_service import (\n",
|
||||
" ExplainRequest, PredictRequest)\n",
|
||||
"from google.protobuf import json_format\n",
|
||||
"from google.protobuf.duration_pb2 import Duration\n",
|
||||
"from google.protobuf.struct_pb2 import Value"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -539,36 +509,16 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"SUFFIX = \"aiplatform.googleapis.com\"\n",
|
||||
"API_ENDPOINT = f\"{REGION}-{SUFFIX}\"\n",
|
||||
"PREDICT_API_ENDPOINT = f\"{REGION}-prediction-{SUFFIX}\"\n",
|
||||
"if os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gcloud --quiet components install beta\n",
|
||||
" ! gcloud --quiet components update\n",
|
||||
"\n",
|
||||
"! gcloud config set ai/region $REGION\n",
|
||||
" !gcloud --quiet components install beta\n",
|
||||
" !gcloud --quiet components update\n",
|
||||
"!gcloud config set ai/region $REGION\n",
|
||||
"os.environ[\"GOOGLE_CLOUD_PROJECT\"] = PROJECT_ID"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "init_aip:mbsdk,region"
|
||||
},
|
||||
"source": [
|
||||
"### Initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "init_aip:mbsdk,region"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -577,14 +527,14 @@
|
||||
"source": [
|
||||
"### The example model\n",
|
||||
"\n",
|
||||
"The model you use in this notebook is based on [this blog post](https://cloud.google.com/blog/topics/developers-practitioners/churn-prediction-game-developers-using-google-analytics-4-ga4-and-bigquery-ml). The idea behind this model is that your company has extensive log data describing how your game users have interacted with the site. The raw data contains the following categories of information:\n",
|
||||
"The model you'll use in this notebook is based on [this blog post](https://cloud.google.com/blog/topics/developers-practitioners/churn-prediction-game-developers-using-google-analytics-4-ga4-and-bigquery-ml). The idea behind this model is that your company has extensive log data describing how your game users have interacted with the site. The raw data contains the following categories of information:\n",
|
||||
"\n",
|
||||
"- identity - unique player identitity numbers\n",
|
||||
"- demographic features - information about the player, such as the geographic region in which a player is located\n",
|
||||
"- behavioral features - counts of the number of times a player has triggered certain game events, such as reaching a new level\n",
|
||||
"- churn propensity - this is the label or target feature, it provides an estimated probability that this player will churn, i.e. stop being an active player.\n",
|
||||
"\n",
|
||||
"The blog article referenced above explains how to use BigQuery to store the raw data, pre-process the data for machine learning, and train the corresponding model. Because this notebook focuses on model monitoring, rather than training models, you're going to reuse a pre-trained version of this model, which has been exported to Cloud Storage. In the next section, you will setup your environment and import this model into your own project."
|
||||
"The blog article referenced above explains how to use BigQuery to store the raw data, pre-process it for use in machine learning, and train a model. Because this notebook focuses on model monitoring, rather than training models, you're going to reuse a pre-trained version of this model, which has been exported to Cloud Storage. In the next section, you will setup your environment and import this model into your own project."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -593,9 +543,149 @@
|
||||
"id": "btZeLzqQ7pXc"
|
||||
},
|
||||
"source": [
|
||||
"### Define some helper data structures\n",
|
||||
"### Define some helper functions and data structures\n",
|
||||
"\n",
|
||||
"Run the following cell to define some data structures used throughout this notebook."
|
||||
"Run the following cells to define some utility functions and data structures used throughout this notebook. Some highlights:\n",
|
||||
"\n",
|
||||
"* create_monitoring_job - convenience function for requesting a model monitoring job\n",
|
||||
"* send_predict_request - convenience function for sending a prediction request and receiving the response\n",
|
||||
"\n",
|
||||
"Although these functions and data strctures are not critical to understand the main concepts, feel free to expand the cell if you're curious or want to dive deeper into how some of your API requests are made."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "yhDFSB2YDvfT"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Utility functions\n",
|
||||
"DEFAULT_THRESHOLD_VALUE = 0.001\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_monitoring_job(objective_configs):\n",
|
||||
" # Create sampling configuration.\n",
|
||||
" random_sampling = SamplingStrategy.RandomSampleConfig(sample_rate=LOG_SAMPLE_RATE)\n",
|
||||
" sampling_config = SamplingStrategy(random_sample_config=random_sampling)\n",
|
||||
"\n",
|
||||
" # Create schedule configuration.\n",
|
||||
" duration = Duration(seconds=MONITOR_INTERVAL)\n",
|
||||
" schedule_config = ModelDeploymentMonitoringScheduleConfig(monitor_interval=duration)\n",
|
||||
"\n",
|
||||
" # Create alerting configuration.\n",
|
||||
" emails = [USER_EMAIL]\n",
|
||||
" email_config = ModelMonitoringAlertConfig.EmailAlertConfig(user_emails=emails)\n",
|
||||
" alerting_config = ModelMonitoringAlertConfig(email_alert_config=email_config)\n",
|
||||
"\n",
|
||||
" # Create the monitoring job.\n",
|
||||
" endpoint = f\"projects/{PROJECT_ID}/locations/{REGION}/endpoints/{ENDPOINT_ID}\"\n",
|
||||
" predict_schema = \"\"\n",
|
||||
" analysis_schema = \"\"\n",
|
||||
" job = ModelDeploymentMonitoringJob(\n",
|
||||
" display_name=JOB_NAME,\n",
|
||||
" endpoint=endpoint,\n",
|
||||
" model_deployment_monitoring_objective_configs=objective_configs,\n",
|
||||
" logging_sampling_strategy=sampling_config,\n",
|
||||
" model_deployment_monitoring_schedule_config=schedule_config,\n",
|
||||
" model_monitoring_alert_config=alerting_config,\n",
|
||||
" predict_instance_schema_uri=predict_schema,\n",
|
||||
" analysis_instance_schema_uri=analysis_schema,\n",
|
||||
" )\n",
|
||||
" options = dict(api_endpoint=API_ENDPOINT)\n",
|
||||
" client = JobServiceClient(client_options=options)\n",
|
||||
" parent = f\"projects/{PROJECT_ID}/locations/{REGION}\"\n",
|
||||
" response = client.create_model_deployment_monitoring_job(\n",
|
||||
" parent=parent, model_deployment_monitoring_job=job\n",
|
||||
" )\n",
|
||||
" print(\"Created monitoring job:\")\n",
|
||||
" print(response)\n",
|
||||
" return response\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_thresholds(default_thresholds, custom_thresholds):\n",
|
||||
" thresholds = {}\n",
|
||||
" default_threshold = ThresholdConfig(value=DEFAULT_THRESHOLD_VALUE)\n",
|
||||
" for feature in default_thresholds.split(\",\"):\n",
|
||||
" feature = feature.strip()\n",
|
||||
" thresholds[feature] = default_threshold\n",
|
||||
" for custom_threshold in custom_thresholds.split(\",\"):\n",
|
||||
" pair = custom_threshold.split(\":\")\n",
|
||||
" if len(pair) != 2:\n",
|
||||
" print(f\"Invalid custom skew threshold: {custom_threshold}\")\n",
|
||||
" return\n",
|
||||
" feature, value = pair\n",
|
||||
" thresholds[feature] = ThresholdConfig(value=float(value))\n",
|
||||
" return thresholds\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_deployed_model_ids(endpoint_id):\n",
|
||||
" client_options = dict(api_endpoint=API_ENDPOINT)\n",
|
||||
" client = EndpointServiceClient(client_options=client_options)\n",
|
||||
" parent = f\"projects/{PROJECT_ID}/locations/{REGION}\"\n",
|
||||
" response = client.get_endpoint(name=f\"{parent}/endpoints/{endpoint_id}\")\n",
|
||||
" model_ids = []\n",
|
||||
" for model in response.deployed_models:\n",
|
||||
" model_ids.append(model.id)\n",
|
||||
" return model_ids\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def set_objectives(model_ids, objective_template):\n",
|
||||
" # Use the same objective config for all models.\n",
|
||||
" objective_configs = []\n",
|
||||
" for model_id in model_ids:\n",
|
||||
" objective_config = copy.deepcopy(objective_template)\n",
|
||||
" objective_config.deployed_model_id = model_id\n",
|
||||
" objective_configs.append(objective_config)\n",
|
||||
" return objective_configs\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def send_predict_request(endpoint, input, type=\"predict\"):\n",
|
||||
" client_options = {\"api_endpoint\": PREDICT_API_ENDPOINT}\n",
|
||||
" client = PredictionServiceClient(client_options=client_options)\n",
|
||||
" if type == \"predict\":\n",
|
||||
" obj = PredictRequest\n",
|
||||
" method = client.predict\n",
|
||||
" elif type == \"explain\":\n",
|
||||
" obj = ExplainRequest\n",
|
||||
" method = client.explain\n",
|
||||
" else:\n",
|
||||
" raise Exception(\"unsupported request type:\" + type)\n",
|
||||
" params = {}\n",
|
||||
" params = json_format.ParseDict(params, Value())\n",
|
||||
" request = obj(endpoint=endpoint, parameters=params)\n",
|
||||
" inputs = [json_format.ParseDict(input, Value())]\n",
|
||||
" request.instances.extend(inputs)\n",
|
||||
" response = None\n",
|
||||
" try:\n",
|
||||
" response = method(request)\n",
|
||||
" except Exception as ex:\n",
|
||||
" print(ex)\n",
|
||||
" return response\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def list_monitoring_jobs():\n",
|
||||
" client_options = dict(api_endpoint=API_ENDPOINT)\n",
|
||||
" parent = f\"projects/{PROJECT_ID}/locations/us-central1\"\n",
|
||||
" client = JobServiceClient(client_options=client_options)\n",
|
||||
" response = client.list_model_deployment_monitoring_jobs(parent=parent)\n",
|
||||
" print(response)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def pause_monitoring_job(job):\n",
|
||||
" client_options = dict(api_endpoint=API_ENDPOINT)\n",
|
||||
" client = JobServiceClient(client_options=client_options)\n",
|
||||
" response = client.pause_model_deployment_monitoring_job(name=job)\n",
|
||||
" print(response)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def delete_monitoring_job(job):\n",
|
||||
" client_options = dict(api_endpoint=API_ENDPOINT)\n",
|
||||
" client = JobServiceClient(client_options=client_options)\n",
|
||||
" response = client.delete_model_deployment_monitoring_job(name=job)\n",
|
||||
" print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -611,7 +701,6 @@
|
||||
"\n",
|
||||
"# Sampling distributions for categorical features...\n",
|
||||
"DAYOFWEEK = {1: 1040, 2: 1223, 3: 1352, 4: 1217, 5: 1078, 6: 1011, 7: 1110}\n",
|
||||
"\n",
|
||||
"LANGUAGE = {\n",
|
||||
" \"en-us\": 4807,\n",
|
||||
" \"en-gb\": 678,\n",
|
||||
@@ -634,11 +723,8 @@
|
||||
" \"en-nz\": 29,\n",
|
||||
" \"es-es\": 25,\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"OS = {\"IOS\": 3980, \"ANDROID\": 3798, \"null\": 253}\n",
|
||||
"\n",
|
||||
"MONTH = {6: 3125, 7: 1838, 8: 1276, 9: 1718, 10: 74}\n",
|
||||
"\n",
|
||||
"COUNTRY = {\n",
|
||||
" \"United States\": 4395,\n",
|
||||
" \"India\": 486,\n",
|
||||
@@ -707,7 +793,7 @@
|
||||
"source": [
|
||||
"### Generate model metadata for Vertex Explainable AI\n",
|
||||
"\n",
|
||||
"Run the following cell to extract metadata from the exported model, which is needed for generating the explanations for a prediction request."
|
||||
"Run the following cell to extract metadata from the exported model, which is needed for generating the prediction explanations."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -719,12 +805,10 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL_PATH = \"gs://mco-mm/churn\"\n",
|
||||
"\n",
|
||||
"params = {\"sampled_shapley_attribution\": {\"path_count\": 10}}\n",
|
||||
"EXPLAIN_PARAMS = aiplatform.explain.ExplanationParameters(params)\n",
|
||||
"\n",
|
||||
"builder = saved_model_metadata_builder.SavedModelMetadataBuilder(\n",
|
||||
" model_path=MODEL_PATH, outputs_to_explain=[\"churned_probs\"]\n",
|
||||
" MODEL_PATH, outputs_to_explain=[\"churned_probs\"]\n",
|
||||
")\n",
|
||||
"EXPLAIN_META = builder.get_metadata_protobuf()"
|
||||
]
|
||||
@@ -753,15 +837,15 @@
|
||||
"MODEL_NAME = \"churn\"\n",
|
||||
"IMAGE = \"us-docker.pkg.dev/cloud-aiplatform/prediction/tf2-cpu.2-5:latest\"\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION)\n",
|
||||
"model = aiplatform.Model.upload(\n",
|
||||
" display_name=MODEL_NAME,\n",
|
||||
" artifact_uri=MODEL_PATH,\n",
|
||||
" serving_container_image_uri=IMAGE,\n",
|
||||
" explanation_parameters=EXPLAIN_PARAMS,\n",
|
||||
" explanation_metadata=EXPLAIN_META,\n",
|
||||
" sync=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model.wait()\n",
|
||||
"MODEL_ID = model.resource_name.split(\"/\")[-1]"
|
||||
]
|
||||
},
|
||||
@@ -782,7 +866,7 @@
|
||||
"source": [
|
||||
"## Deploy your Model resource to an Endpoint resource\n",
|
||||
"\n",
|
||||
"Now that you've imported your model into your project, you need to create an endpoint to serve your model. An endpoint can be thought of as a channel through which your model provides prediction services. Once established, you can make online prediction requests on your model via the public internet. Your endpoint is also serverless, in the sense that Google Cloud ensures high availability by reducing single points of failure, and scalability by dynamically allocating resources to meet the demand for your service. In this way, you are able to focus on your model quality, and freed from adminstrative and infrastructure concerns.\n",
|
||||
"Now that you've imported your model into your project, you need to create an endpoint to serve your model. An endpoint can be thought of as a channel through which your model provides prediction services. Once established, you'll be able to make prediction requests on your model via the public internet. Your endpoint is also serverless, in the sense that Google Cloud ensures high availability by reducing single points of failure, and scalability by dynamically allocating resources to meet the demand for your service. In this way, you are able to focus on your model quality, and freed from adminstrative and infrastructure concerns.\n",
|
||||
"\n",
|
||||
"Run the next cell to deploy your model to an endpoint. **This will take about ten minutes to complete.**"
|
||||
]
|
||||
@@ -834,7 +918,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"try:\n",
|
||||
" resp = endpoint.predict([DEFAULT_INPUT])\n",
|
||||
" resp = send_predict_request(ENDPOINT, DEFAULT_INPUT)\n",
|
||||
" for i in resp.predictions:\n",
|
||||
" vals = i[\"churned_values\"]\n",
|
||||
" probs = i[\"churned_probs\"]\n",
|
||||
@@ -853,8 +937,7 @@
|
||||
},
|
||||
"source": [
|
||||
"### Test results\n",
|
||||
"\n",
|
||||
"Taking a look at the results, you see the following elements:\n",
|
||||
"Taking a closer look at the results, we see the following elements:\n",
|
||||
"\n",
|
||||
"- **churned_values** - a set of possible values (0 and 1) for the target field\n",
|
||||
"- **churned_probs** - a corresponding set of probabilities for each possible target field value (5x10^-40 and 1.0, respectively)\n",
|
||||
@@ -871,7 +954,7 @@
|
||||
"source": [
|
||||
"## Run an explanation test\n",
|
||||
"\n",
|
||||
"You can run a test of Explainable AI on this endpoint. Run the next cell to send a test explanation request. The response you receive encodes the feature importance of this prediction in a text representation called JSON, along with a bar chart summarizing the results.\n",
|
||||
"We can also run a test of Explainable AI on this endpoint. Run the next cell to send a test explanation request. If everything works as expected, you should receive a response encoding the feature importance of this prediction in a text representation called JSON, along with a bar chart summarizing the results.\n",
|
||||
"\n",
|
||||
"**Try this now by running the next cell.**"
|
||||
]
|
||||
@@ -887,7 +970,8 @@
|
||||
"try:\n",
|
||||
" features = []\n",
|
||||
" scores = []\n",
|
||||
" resp = endpoint.explain([DEFAULT_INPUT])\n",
|
||||
" resp = send_predict_request(ENDPOINT, DEFAULT_INPUT, type=\"explain\")\n",
|
||||
" # pp.pprint(resp)\n",
|
||||
" for i in resp.explanations:\n",
|
||||
" for j in i.attributions:\n",
|
||||
" for k in j.feature_attributions:\n",
|
||||
@@ -913,7 +997,7 @@
|
||||
"\n",
|
||||
"Now that you've created an endpoint to serve prediction requests on your model, you're ready to start a monitoring job to keep an eye on model quality and to alert you if and when input begins to deviate in way that may impact your model's prediction quality.\n",
|
||||
"\n",
|
||||
"In this section, you configure and create a model monitoring job based on the churn propensity model you imported from BigQuery ML."
|
||||
"In this section, you will configure and create a model monitoring job based on the churn propensity model you imported from BigQuery ML."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -955,35 +1039,17 @@
|
||||
"# Prediction target column name in training dataset.\n",
|
||||
"TARGET = \"churned\"\n",
|
||||
"\n",
|
||||
"# # Skew and drift thresholds.\n",
|
||||
"\n",
|
||||
"DEFAULT_THRESHOLD_VALUE = 0.001\n",
|
||||
"\n",
|
||||
"SKEW_THRESHOLDS = {\n",
|
||||
" \"country\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
" \"cnt_user_engagement\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
"}\n",
|
||||
"DRIFT_THRESHOLDS = {\n",
|
||||
" \"country\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
" \"cnt_user_engagement\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
"}\n",
|
||||
"ATTRIB_SKEW_THRESHOLDS = {\n",
|
||||
" \"country\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
" \"cnt_user_engagement\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
"}\n",
|
||||
"ATTRIB_DRIFT_THRESHOLDS = {\n",
|
||||
" \"country\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
" \"cnt_user_engagement\": DEFAULT_THRESHOLD_VALUE,\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e10f3d0fa538"
|
||||
},
|
||||
"source": [
|
||||
"You can change the threshold values and the configuration settings, so that you can monitor other features in the model as well."
|
||||
"# Skew and drift thresholds.\n",
|
||||
"SKEW_DEFAULT_THRESHOLDS = \"country,cnt_user_engagement\" # @param {type:\"string\"}\n",
|
||||
"SKEW_CUSTOM_THRESHOLDS = \"cnt_level_start_quickplay:.01\" # @param {type:\"string\"}\n",
|
||||
"DRIFT_DEFAULT_THRESHOLDS = \"country,cnt_user_engagement\" # @param {type:\"string\"}\n",
|
||||
"DRIFT_CUSTOM_THRESHOLDS = \"cnt_level_start_quickplay:.01\" # @param {type:\"string\"}\n",
|
||||
"ATTRIB_SKEW_DEFAULT_THRESHOLDS = \"country,cnt_user_engagement\" # @param {type:\"string\"}\n",
|
||||
"# fmt: off\n",
|
||||
"ATTRIB_SKEW_CUSTOM_THRESHOLDS = \"cnt_level_start_quickplay:.01\" # @param {type:\"string\"}\n",
|
||||
"ATTRIB_DRIFT_DEFAULT_THRESHOLDS = \"country,cnt_user_engagement\" # @param {type:\"string\"}\n",
|
||||
"ATTRIB_DRIFT_CUSTOM_THRESHOLDS = \"cnt_level_start_quickplay:.01\" # @param {type:\"string\"}\n",
|
||||
"# fmt: on"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1005,46 +1071,58 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"skew_config = model_monitoring.SkewDetectionConfig(\n",
|
||||
" data_source=DATASET_BQ_URI,\n",
|
||||
" skew_thresholds=SKEW_THRESHOLDS,\n",
|
||||
" attribute_skew_thresholds=ATTRIB_SKEW_THRESHOLDS,\n",
|
||||
" target_field=TARGET,\n",
|
||||
"skew_thresholds = get_thresholds(SKEW_DEFAULT_THRESHOLDS, SKEW_CUSTOM_THRESHOLDS)\n",
|
||||
"drift_thresholds = get_thresholds(DRIFT_DEFAULT_THRESHOLDS, DRIFT_CUSTOM_THRESHOLDS)\n",
|
||||
"attrib_skew_thresholds = get_thresholds(\n",
|
||||
" ATTRIB_SKEW_DEFAULT_THRESHOLDS, ATTRIB_SKEW_CUSTOM_THRESHOLDS\n",
|
||||
")\n",
|
||||
"attrib_drift_thresholds = get_thresholds(\n",
|
||||
" ATTRIB_DRIFT_DEFAULT_THRESHOLDS, ATTRIB_DRIFT_CUSTOM_THRESHOLDS\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"drift_config = model_monitoring.DriftDetectionConfig(\n",
|
||||
" drift_thresholds=DRIFT_THRESHOLDS,\n",
|
||||
" attribute_drift_thresholds=ATTRIB_DRIFT_THRESHOLDS,\n",
|
||||
"skew_config = ModelMonitoringObjectiveConfig.TrainingPredictionSkewDetectionConfig(\n",
|
||||
" skew_thresholds=skew_thresholds,\n",
|
||||
" attribution_score_skew_thresholds=attrib_skew_thresholds,\n",
|
||||
")\n",
|
||||
"drift_config = ModelMonitoringObjectiveConfig.PredictionDriftDetectionConfig(\n",
|
||||
" drift_thresholds=drift_thresholds,\n",
|
||||
" attribution_score_drift_thresholds=attrib_drift_thresholds,\n",
|
||||
")\n",
|
||||
"explanation_config = ModelMonitoringObjectiveConfig.ExplanationConfig(\n",
|
||||
" enable_feature_attributes=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"explanation_config = model_monitoring.ExplanationConfig()\n",
|
||||
"objective_config = model_monitoring.ObjectiveConfig(\n",
|
||||
" skew_config, drift_config, explanation_config\n",
|
||||
"training_dataset = ModelMonitoringObjectiveConfig.TrainingDataset(target_field=TARGET)\n",
|
||||
"training_dataset.bigquery_source = BigQuerySource(input_uri=DATASET_BQ_URI)\n",
|
||||
"objective_config = ModelMonitoringObjectiveConfig(\n",
|
||||
" training_dataset=training_dataset,\n",
|
||||
" training_prediction_skew_detection_config=skew_config,\n",
|
||||
" prediction_drift_detection_config=drift_config,\n",
|
||||
" explanation_config=explanation_config,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Create sampling configuration\n",
|
||||
"random_sampling = model_monitoring.RandomSampleConfig(sample_rate=LOG_SAMPLE_RATE)\n",
|
||||
"\n",
|
||||
"# Create schedule configuration\n",
|
||||
"schedule_config = model_monitoring.ScheduleConfig(monitor_interval=MONITOR_INTERVAL)\n",
|
||||
"\n",
|
||||
"# Create alerting configuration.\n",
|
||||
"emails = [USER_EMAIL]\n",
|
||||
"alerting_config = model_monitoring.EmailAlertConfig(\n",
|
||||
" user_emails=emails, enable_logging=True\n",
|
||||
"model_ids = get_deployed_model_ids(ENDPOINT_ID)\n",
|
||||
"objective_template = ModelDeploymentMonitoringObjectiveConfig(\n",
|
||||
" objective_config=objective_config\n",
|
||||
")\n",
|
||||
"objective_configs = set_objectives(model_ids, objective_template)\n",
|
||||
"\n",
|
||||
"# Create the monitoring job.\n",
|
||||
"job = aiplatform.ModelDeploymentMonitoringJob.create(\n",
|
||||
" display_name=JOB_NAME,\n",
|
||||
" logging_sampling_strategy=random_sampling,\n",
|
||||
" schedule_config=schedule_config,\n",
|
||||
" alert_config=alerting_config,\n",
|
||||
" objective_configs=objective_config,\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" endpoint=endpoint,\n",
|
||||
")"
|
||||
"monitoring_job = create_monitoring_job(objective_configs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "OiwOVR4D_xhl"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Run a prediction request to generate schema, if necessary.\n",
|
||||
"try:\n",
|
||||
" _ = send_predict_request(ENDPOINT, DEFAULT_INPUT)\n",
|
||||
" print(\"prediction succeeded\")\n",
|
||||
"except Exception:\n",
|
||||
" print(\"prediction failed\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1062,35 +1140,6 @@
|
||||
"As your monitoring job collects data, measurements are stored in Cloud Storage and you are free to examine your data at any time. The \"Statistics and Anomalies Root Path\" specifies the location of your measurements in Cloud Storage. Run the following cell to see an example of the layout of these measurements in Cloud Storage. If you substitute the Cloud Storage URL in your job creation email, you can view the structure and content of the data files for your own monitoring job."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6f38e8423bce"
|
||||
},
|
||||
"source": [
|
||||
"### Create the sampling distribution\n",
|
||||
"\n",
|
||||
"Next, you send a first test prediction request. The model monitoring service will analyze the distribution of features and automatically create a baseline to monitor deviations from the baseline.\n",
|
||||
"\n",
|
||||
"*Note:* You need to wait for the email notification before making the first prediction request."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "OiwOVR4D_xhl"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Run a prediction request to generate schema, if necessary.\n",
|
||||
"try:\n",
|
||||
" _ = endpoint.predict([DEFAULT_INPUT])\n",
|
||||
" print(\"prediction succeeded\")\n",
|
||||
"except Exception as e:\n",
|
||||
" print(f\"prediction failed: {e}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -1128,7 +1177,7 @@
|
||||
"source": [
|
||||
"### You can create monitoring jobs with other user interfaces\n",
|
||||
"\n",
|
||||
"In the previous cells, you created a monitoring job using the Python client library. Alternatively, you can use the *gcloud* command line tool or the Cloud Console to create a model monitoring job. \n"
|
||||
"In the previous cells, you created a monitoring job using the Python client library. You can also use the *gcloud* command line tool or the Cloud Console to create a model monitoring job. \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1139,9 +1188,9 @@
|
||||
"source": [
|
||||
"## Interpret your results\n",
|
||||
"\n",
|
||||
"Vertex AI Model Monitoring detects an anomaly when the threshold set for a feature is exceeded. The following cells give you a sense of the alerting and reporting experience after model monitoring anomalies have been detected.\n",
|
||||
"Model Monitoring detects an anomaly when the threshold set for a feature is exceeded. The following cells give you a sense of the alerting and reporting experience after model monitoring anomalies have been detected.\n",
|
||||
"\n",
|
||||
"Vertex AI Model Monitoring automatically notifies you of detected anomalies through email, but you can also [set up alerts through Cloud Logging](https://cloud.google.com/vertex-ai/docs/model-monitoring/using-model-monitoring#monitor-job)."
|
||||
"Model Monitoring automatically notifies you of detected anomalies through email, but you can also [set up alerts through Cloud Logging](https://cloud.google.com/vertex-ai/docs/model-monitoring/using-model-monitoring#monitor-job)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1222,15 +1271,20 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "d6cc924aa1fb"
|
||||
"id": "TPP_ImwJDFJf"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Undeploy the model and delete the endpoint\n",
|
||||
"endpoint.undeploy_all()\n",
|
||||
"endpoint.delete()\n",
|
||||
"# Delete endpoint resource\n",
|
||||
"tmp = ! gcloud ai endpoints describe --region=$REGION $ENDPOINT_ID --quiet \\\n",
|
||||
" | grep \" id:\"\n",
|
||||
"DEPLOYED_MODEL_ID = tmp[1][7:-1]\n",
|
||||
"! gcloud ai endpoints undeploy-model --deployed-model-id=$DEPLOYED_MODEL_ID \\\n",
|
||||
" $ENDPOINT_ID --quiet\n",
|
||||
"! gcloud ai endpoints delete $ENDPOINT --quiet\n",
|
||||
"\n",
|
||||
"model.delete()\n",
|
||||
"# Delete model resource\n",
|
||||
"! gcloud ai models delete $MODEL_ID --quiet\n",
|
||||
"\n",
|
||||
"# Delete BQ table and dataset\n",
|
||||
"rmtable = f\"bq rm -f model_deployment_monitoring_{ENDPOINT_ID}.serving_predict\"\n",
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "b0b4f2bf"
|
||||
},
|
||||
@@ -32,7 +32,6 @@
|
||||
"# Vertex AI Pipelines: Custom training with pre-built Google Cloud Pipeline Components\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/custom_model_training_and_batch_prediction.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
@@ -45,11 +44,11 @@
|
||||
" </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/notebooksofficial/pipelines/custom_model_training_and_batch_prediction.ipynb\">\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/pipelines/custom_model_training_and_batch_prediction.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",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/><br/><br/>"
|
||||
]
|
||||
@@ -66,6 +65,17 @@
|
||||
"This tutorial demonstrates how to use Vertex AI Pipelines with pre-built Google Cloud Pipeline Components for custom training."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "57139e75264f"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial is the [CIFAR10 dataset](https://www.tensorflow.org/datasets/catalog/cifar10) from [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/overview). The version of the dataset you will use is built into TensorFlow. The trained model predicts which type of class an image is from ten classes: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, or truck."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -97,17 +107,6 @@
|
||||
"Learn more about [Google Cloud Pipeline Components](https://cloud.google.com/vertex-ai/docs/pipelines/build-pipeline)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "57139e75264f"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial is the [CIFAR10 dataset](https://www.tensorflow.org/datasets/catalog/cifar10) from [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/overview). The version of the dataset you will use is built into TensorFlow. The trained model predicts which type of class an image is from ten classes: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, or truck."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -197,8 +196,8 @@
|
||||
"! pip3 install -U google-cloud-storage {USER_FLAG} -q\n",
|
||||
"! pip3 install {USER_FLAG} kfp google-cloud-pipeline-components --upgrade -q\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade --force-reinstall $USER_FLAG tensorflow kfp google-cloud-aiplatform google-cloud-storage google-cloud-pipeline-components -q"
|
||||
"if os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! pip3 install --upgrade --force-reinstall $USER_FLAG tensorflow==2.5 kfp google-cloud-aiplatform google-cloud-storage google-cloud-pipeline-components"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -214,7 +213,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "restart"
|
||||
},
|
||||
@@ -262,7 +261,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "set_project_id"
|
||||
},
|
||||
@@ -319,16 +318,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "region"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
|
||||
"\n",
|
||||
"if REGION == \"[your-region]\":\n",
|
||||
" REGION = \"us-central1\""
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -337,29 +333,22 @@
|
||||
"id": "timestamp"
|
||||
},
|
||||
"source": [
|
||||
"#### UUID\n",
|
||||
"#### Timestamp\n",
|
||||
"\n",
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, create a timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "wJjft8z1IA81"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import string\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Generate a uuid of a specifed length(default=8)\n",
|
||||
"def generate_uuid(length: int = 8) -> str:\n",
|
||||
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"UUID = generate_uuid()"
|
||||
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -446,7 +435,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "bucket"
|
||||
},
|
||||
@@ -458,14 +447,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "autoset_bucket"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
|
||||
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
|
||||
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
|
||||
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
@@ -500,7 +489,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "validate_bucket"
|
||||
},
|
||||
@@ -522,7 +511,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "set_service_account"
|
||||
},
|
||||
@@ -570,7 +559,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "set_service_account:pipelines"
|
||||
},
|
||||
@@ -602,6 +591,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import google.cloud.aiplatform as aip\n",
|
||||
"import tensorflow as tf\n",
|
||||
"from google_cloud_pipeline_components.experimental.custom_job import utils\n",
|
||||
"from kfp.v2 import compiler, dsl\n",
|
||||
"from kfp.v2.dsl import component"
|
||||
@@ -620,7 +610,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "pipeline_constants"
|
||||
},
|
||||
@@ -642,7 +632,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "yX-aimhGRRRl"
|
||||
},
|
||||
@@ -675,7 +665,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ipG9uBUDRRRm"
|
||||
},
|
||||
@@ -845,12 +835,12 @@
|
||||
"\n",
|
||||
"#### Package Assembly\n",
|
||||
"\n",
|
||||
"In the following cells, you assemble the training package."
|
||||
"In the following cells, you will assemble the training package."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "YpA6MFcLRRRn"
|
||||
},
|
||||
@@ -890,7 +880,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "2etUCVVMRRRo"
|
||||
},
|
||||
@@ -1106,14 +1096,12 @@
|
||||
"source": [
|
||||
"### Convert the component to a Vertex AI Custom Job\n",
|
||||
"\n",
|
||||
"Next, use the `create_custom_training_job_op_from_component` method to convert the custom component into a Vertex AI Custom Job pre-built component.\n",
|
||||
"\n",
|
||||
"**replica_count :** The number of machine replicas the batch operation may be scaled to. Only used if machine_type is set. Default is 10."
|
||||
"Next, use the `create_custom_training_job_op_from_component` method to convert the custom component into a Vertex AI Custom Job pre-built component."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "deb6c1cc"
|
||||
},
|
||||
@@ -1141,7 +1129,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "5R23d2J_HJr-"
|
||||
},
|
||||
@@ -1231,7 +1219,7 @@
|
||||
" pipeline_func=pipeline, package_path=\"custom_model_training_spec.json\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"DISPLAY_NAME = \"cifar10_\" + UUID\n",
|
||||
"DISPLAY_NAME = \"cifar10_\" + TIMESTAMP\n",
|
||||
"\n",
|
||||
"job = aip.PipelineJob(\n",
|
||||
" display_name=DISPLAY_NAME,\n",
|
||||
@@ -1252,7 +1240,7 @@
|
||||
"source": [
|
||||
"### View custom training pipeline results\n",
|
||||
"\n",
|
||||
"Finally, you view the artifact outputs of each task in the pipeline."
|
||||
"Finally, you will view the artifact outputs of each task in the pipeline."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1265,8 +1253,6 @@
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"import tensorflow as tf\n",
|
||||
"\n",
|
||||
"PROJECT_NUMBER = job.gca_resource.name.split(\"/\")[1]\n",
|
||||
"print(PROJECT_NUMBER)\n",
|
||||
"\n",
|
||||
@@ -1372,7 +1358,6 @@
|
||||
"batch_job = aip.BatchPredictionJob(batch_job_id)\n",
|
||||
"batch_job.delete()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil rm -rf {BUCKET_URI}"
|
||||
|
||||
@@ -29,24 +29,21 @@
|
||||
"id": "JAPoU8Sm5E6e"
|
||||
},
|
||||
"source": [
|
||||
"# Training an acquisition-prediction model using Swivel, BigQuery ML and Vertex AI Pipelines\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/google_cloud_pipeline_components_bqml_text.ipynb\"\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/pipelines/google_cloud_pipeline_components_bqml_text.ipynb\"\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/google_cloud_pipeline_components_bqml_text.ipynb\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/notebooks/official/pipelines/google_cloud_pipeline_components_bqml_text.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/google_cloud_pipeline_components_bqml_text.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/notebooks/official/pipelines/google_cloud_pipeline_components_bqml_text.ipynb\">\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
@@ -58,67 +55,33 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "991ab00f3d75"
|
||||
"id": "tvgnzT1CKxrO"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates the usage of `DataflowPythonJobOp` and BigQuery ML components through buidling a Text Categorization model and running it on Vertex AI Pipelines. \n",
|
||||
"This notebooks shows the DataflowPythonJobOp and the main BQML components in a Text Categorization Vertex AI Pipeline. \n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"The pipeline will \n",
|
||||
"\n",
|
||||
"1. Read raw text (HTML) documents stored in Google Cloud Storage.\n",
|
||||
"2. Extract title, content and topic of (HTML) documents using Dataflow and ingest into BigQuery.\n",
|
||||
"3. Apply the Swivel model to generate embeddings of your document’s content.\n",
|
||||
"1. Read raw text (HTML) documents stored in Google Cloud Storage\n",
|
||||
"2. Extract title, content and topic of (HTML) documents using Dataflow and ingest into BigQuery\n",
|
||||
"3. Apply the Swivel model to generate embeddings of our document’s content\n",
|
||||
"4. Train a Logistic regression model to classify if an article is about corporate acquisitions (`acq` category). \n",
|
||||
"5. Evaluate the model.\n",
|
||||
"6. Apply the model to a dataset in order to generate predictions."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "acc98a3361cc"
|
||||
},
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"5. Evaluate the model \n",
|
||||
"6. Apply the model to a dataset in order to generate predictions\n",
|
||||
"\n",
|
||||
"In this notebook, you learn how to build a simple BigQuery ML pipeline using Vertex AI pipelines in order to calculate text embeddings of content from articles and classify them\n",
|
||||
"into the *corporate acquisitions* category.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"\n",
|
||||
"- Vertex AI Pipelines\n",
|
||||
"- BigQuery ML\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Creating a component for Dataflow job that ingests data to BigQuery.\n",
|
||||
"- Creating a component for preprocessing steps to run on the data in BigQuery.\n",
|
||||
"- Creating a component for training a logistic regression model using BigQuery ML.\n",
|
||||
"- Building and configuring a Kubeflow DSL pipeline with all the created components.\n",
|
||||
"- Compiling and running the pipeline in Vertex AI Pipelines."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "9bc6d52899ba"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used in this notebook is the [Reuters-21578 Text Categorization Collection Data Set](https://archive.ics.uci.edu/ml/datasets/reuters-21578+text+categorization+collection). This dataset is a collection of publicly available news articles appeared on the Reuters newswire in 1987. They were assembled and indexed with categories by personnel from Reuters Ltd. and Carnegie Group, Inc. in 1987."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "tvgnzT1CKxrO"
|
||||
},
|
||||
"source": [
|
||||
"The dataset is [Reuters-21578 Text Categorization Collection Data Set](https://archive.ics.uci.edu/ml/datasets/reuters-21578+text+categorization+collection).\n",
|
||||
"\n",
|
||||
"The dataset is a collection of publicly available news articles appeared on the Reuters newswire in 1987. They were assembled and indexed with categories by personnel from Reuters Ltd. and Carnegie Group, Inc. in 1987.\n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this notebook, you will learn how to build a simple BigQuery ML pipeline on Vertex AI pipeline in order to calculate text embeddings of articles' content and classify them\n",
|
||||
"depending the *corporate acquisitions* category.\n",
|
||||
"\n",
|
||||
"### Costs \n",
|
||||
"\n",
|
||||
"This tutorial uses billable components of Google Cloud:\n",
|
||||
@@ -126,15 +89,19 @@
|
||||
"* Vertex AI\n",
|
||||
"* Cloud Storage\n",
|
||||
"* BigQuery\n",
|
||||
"* Dataflow\n",
|
||||
"* Dataflow"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ze4-nDLfK4pw"
|
||||
},
|
||||
"source": [
|
||||
"### Set up your local development environment\n",
|
||||
"\n",
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing), [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), [BigQuery\n",
|
||||
"pricing](https://cloud.google.com/bigquery/pricing), [Dataflow\n",
|
||||
"pricing](https://cloud.google.com/dataflow/pricing) and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
|
||||
"all the requirements to run this notebook. You can skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -143,11 +110,6 @@
|
||||
"id": "gCuSR8GkAgzl"
|
||||
},
|
||||
"source": [
|
||||
"### Set up your local development environment\n",
|
||||
"\n",
|
||||
"**If you are using Colab or Vertex AI Workbench Notebooks**, your environment already meets\n",
|
||||
"all the requirements to run this notebook. You can skip this step.\n",
|
||||
"\n",
|
||||
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
|
||||
"You need the following:\n",
|
||||
"\n",
|
||||
@@ -185,7 +147,7 @@
|
||||
"id": "i7EUnXsZhAGF"
|
||||
},
|
||||
"source": [
|
||||
"## Install additional packages\n",
|
||||
"### Install additional packages\n",
|
||||
"\n",
|
||||
"Install additional package dependencies not installed in your notebook environment, such as Vertex AI SDK. Use the latest major GA version of each package."
|
||||
]
|
||||
@@ -200,26 +162,43 @@
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# The Vertex AI Workbench Notebook product has specific requirements\n",
|
||||
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
|
||||
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
|
||||
" \"/opt/deeplearning/metadata/env_version\"\n",
|
||||
")\n",
|
||||
"# The Google Cloud Notebook product has specific requirements\n",
|
||||
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
|
||||
"\n",
|
||||
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
|
||||
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
|
||||
"USER_FLAG = \"\"\n",
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
" \n",
|
||||
"# Install dependencies\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform==1.10.0 \\\n",
|
||||
" google_cloud_pipeline_components==1.0.1 \\\n",
|
||||
" google-api-core==2.8.2 \\\n",
|
||||
" google-auth==1.35.0 -q\n",
|
||||
"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade tensorflow==2.8.0 \\\n",
|
||||
" tensorflow-hub==0.12.0 \\\n",
|
||||
" kfp==1.8.9 -q"
|
||||
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "e6d33c55a3c5"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! touch /builder/home/.local/lib/python3.9/site-packages/google_api_core-2.7.1.dist-info/METADATA"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "wyy5Lbnzg5fi"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip3 install {USER_FLAG} --upgrade \"apache-beam[gcp]==2.36.0\"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade \"bs4==0.0.1\"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade \"nltk==3.7\"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade \"tensorflow<2.8.0\"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade \"tensorflow-hub==0.12.0\"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade \"kfp==1.8.2\"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade \"google-cloud-aiplatform==1.10.0\"\n",
|
||||
"! pip3 install {USER_FLAG} --upgrade \"google_cloud_pipeline_components==1.0.1\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -275,9 +254,9 @@
|
||||
"\n",
|
||||
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"1. [Enable the required APIs](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,bigquery.googleapis.com,dataflow.googleapis.com,storage-component.googleapis.com).\n",
|
||||
"1. [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com)\n",
|
||||
"\n",
|
||||
"1. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
|
||||
"1. If you are running this notebook locally, you will need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
|
||||
"\n",
|
||||
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
|
||||
"Cloud SDK uses the right project for all the commands in this notebook.\n",
|
||||
@@ -300,26 +279,40 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "cd85f5c794e5"
|
||||
"id": "oM1iC_MfAts1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
|
||||
"import os\n",
|
||||
"\n",
|
||||
"PROJECT_ID = \"\"\n",
|
||||
"\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",
|
||||
" PROJECT_ID = shell_output[0]\n",
|
||||
" print(\"Project ID: \", PROJECT_ID)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "qJYoRfYng0XZ"
|
||||
},
|
||||
"source": [
|
||||
"Otherwise, set your project ID here."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "oM1iC_MfAts1"
|
||||
"id": "riG_qUokg0XZ"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
|
||||
" # Get your GCP project id from gcloud\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)"
|
||||
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
|
||||
" PROJECT_ID = \"\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -330,41 +323,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gcloud config set project $PROJECT_ID"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "264543a144ad"
|
||||
},
|
||||
"source": [
|
||||
"#### Region\n",
|
||||
"\n",
|
||||
"You can also change the `REGION` variable, which is used for operations\n",
|
||||
"throughout the rest of this notebook. Below are regions supported for Vertex AI. It is recommended that you choose the region closest to you.\n",
|
||||
"\n",
|
||||
"- Americas: `us-central1`\n",
|
||||
"- Europe: `europe-west4`\n",
|
||||
"- Asia Pacific: `asia-east1`\n",
|
||||
"\n",
|
||||
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
|
||||
"\n",
|
||||
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "3281bedf6d3c"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
|
||||
"\n",
|
||||
"if REGION == \"[your-region]\":\n",
|
||||
" REGION = \"us-central1\""
|
||||
"!gcloud config set project $PROJECT_ID"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -373,9 +332,9 @@
|
||||
"id": "06571eb4063b"
|
||||
},
|
||||
"source": [
|
||||
"#### UUID\n",
|
||||
"#### Timestamp\n",
|
||||
"\n",
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -386,16 +345,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import string\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Generate a uuid of length 8\n",
|
||||
"def generate_uuid():\n",
|
||||
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=8))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"UUID = generate_uuid()"
|
||||
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -406,9 +358,16 @@
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
|
||||
"authenticated.\n",
|
||||
"\n",
|
||||
"**If you are using Google Cloud Notebooks**, your environment is already\n",
|
||||
"authenticated. Skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "sBCra4QMA2wR"
|
||||
},
|
||||
"source": [
|
||||
"**If you are using Colab**, run the cell below and follow the instructions\n",
|
||||
"when prompted to authenticate your account via oAuth.\n",
|
||||
"\n",
|
||||
@@ -441,19 +400,19 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"# If you are running this notebook in Colab, run this cell and follow the\n",
|
||||
"# instructions to authenticate your GCP account. This provides access to your\n",
|
||||
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
|
||||
"# requests.\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"# The Google Cloud Notebook product has specific requirements\n",
|
||||
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
|
||||
"\n",
|
||||
"# If on Vertex AI Workbench, then don't execute this code\n",
|
||||
"IS_COLAB = \"google.colab\" in sys.modules\n",
|
||||
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
|
||||
" \"DL_ANACONDA_HOME\"\n",
|
||||
"):\n",
|
||||
"# If on Google Cloud Notebooks, then don't execute this code\n",
|
||||
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
|
||||
" if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth as google_auth\n",
|
||||
"\n",
|
||||
@@ -476,10 +435,12 @@
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"\n",
|
||||
"When you use services like Vertex AI and Dataflow, you need to specify Cloud Storage bucket paths as staging locations. Cloud Storage bucket is used to save the artifacts that are required or that are generated while using the services.\n",
|
||||
"\n",
|
||||
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
|
||||
"Cloud Storage buckets."
|
||||
"Cloud Storage buckets.\n",
|
||||
"\n",
|
||||
"You may also change the `REGION` variable, which is used for operations\n",
|
||||
"throughout the rest of this notebook. We suggest that you [choose a region where Vertex AI services are\n",
|
||||
"available](https://cloud.google.com/vertex-ai/docs/general/locations#available_regions)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -490,8 +451,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
|
||||
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
|
||||
"BUCKET_URI = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
|
||||
"REGION = \"[your-region]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -502,9 +463,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
|
||||
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
|
||||
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
|
||||
"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\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -547,78 +510,6 @@
|
||||
"! gsutil ls -al $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "3708bd0b1855"
|
||||
},
|
||||
"source": [
|
||||
"#### Service Account\n",
|
||||
"\n",
|
||||
"You use a service account to create Vertex AI Pipeline jobs. If you do not want to use your project's Compute Engine service account, set `SERVICE_ACCOUNT` to another service account ID."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "199a32a35466"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"SERVICE_ACCOUNT = \"[your-service-account]\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "abb872bb98c1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if (\n",
|
||||
" SERVICE_ACCOUNT == \"\"\n",
|
||||
" or SERVICE_ACCOUNT is None\n",
|
||||
" or SERVICE_ACCOUNT == \"[your-service-account]\"\n",
|
||||
"):\n",
|
||||
" # Get your service account from gcloud\n",
|
||||
" if not IS_COLAB:\n",
|
||||
" shell_output = !gcloud auth list 2>/dev/null\n",
|
||||
" SERVICE_ACCOUNT = shell_output[2].replace(\"*\", \"\").strip()\n",
|
||||
"\n",
|
||||
" else: # IS_COLAB:\n",
|
||||
" shell_output = ! gcloud projects describe $PROJECT_ID\n",
|
||||
" project_number = shell_output[-1].split(\":\")[1].strip().replace(\"'\", \"\")\n",
|
||||
" SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
|
||||
"\n",
|
||||
" print(\"Service Account:\", SERVICE_ACCOUNT)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4a63f2d1cd52"
|
||||
},
|
||||
"source": [
|
||||
"#### Set service account access for Vertex AI Pipelines\n",
|
||||
"\n",
|
||||
"Run the following commands to grant your service account access to read and write pipeline artifacts in the bucket that you created in the previous step. You only need to run this step once per service account."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "749c598c5f5d"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectCreator $BUCKET_URI\n",
|
||||
"\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectViewer $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -661,11 +552,11 @@
|
||||
"source": [
|
||||
"### Prepare input data\n",
|
||||
"\n",
|
||||
"In the following cell, you:\n",
|
||||
"In the following code, you will \n",
|
||||
"\n",
|
||||
"1) Get the dataset from UCI archive.\n",
|
||||
"2) Untar the dataset.\n",
|
||||
"3) Copy the dataset to the Cloud Storage location."
|
||||
"1) Get dataset from UCI archive.\n",
|
||||
"2) Untar the dataset\n",
|
||||
"3) Copy the dataset to a Cloud Storage location."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -710,16 +601,18 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"from pathlib import Path as path\n",
|
||||
"from urllib.parse import urlparse\n",
|
||||
"\n",
|
||||
"import tensorflow_hub as hub\n",
|
||||
"from google.cloud import aiplatform as vertex_ai\n",
|
||||
"\n",
|
||||
"os.environ[\"TFHUB_MODEL_LOAD_FORMAT\"] = \"UNCOMPRESSED\"\n",
|
||||
"\n",
|
||||
"import google.cloud.aiplatform as vertex_ai\n",
|
||||
"from kfp import dsl\n",
|
||||
"from kfp.v2 import compiler\n",
|
||||
"from kfp.v2.dsl import component\n",
|
||||
"\n",
|
||||
"os.environ[\"TFHUB_MODEL_LOAD_FORMAT\"] = \"UNCOMPRESSED\""
|
||||
"from kfp.v2.dsl import component"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -730,7 +623,7 @@
|
||||
"source": [
|
||||
"### Define constants\n",
|
||||
"\n",
|
||||
"About the model you are going to use in preprocessing, you use the [Swivel](https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1) embedding which was trained on English Google News 130GB corpus and has 20 dimensions."
|
||||
"About the model we are going to use in preprocessing, we use the [Swivel](https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1) embedding which was trained on English Google News 130GB corpus and has 20 dimensions."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -741,7 +634,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"JOB_NAME = f\"reuters-ingest-{UUID}\"\n",
|
||||
"JOB_NAME = f\"reuters-ingest-{TIMESTAMP}\"\n",
|
||||
"SETUP_FILE_URI = urlparse(BUCKET_URI)._replace(path=\"setup.py\").geturl()\n",
|
||||
"RUNNER = \"DataflowRunner\"\n",
|
||||
"STAGING_LOCATION_URI = urlparse(BUCKET_URI)._replace(path=\"staging\").geturl()\n",
|
||||
@@ -750,13 +643,13 @@
|
||||
"BQ_DATASET = \"mlops_bqml_text_analyisis\"\n",
|
||||
"BQ_TABLE = \"reuters_ingested\"\n",
|
||||
"MODEL_NAME = \"swivel_text_embedding_model\"\n",
|
||||
"EMBEDDINGS_TABLE = f\"reuters_text_embeddings_{UUID}\"\n",
|
||||
"EMBEDDINGS_TABLE = f\"reuters_text_embeddings_{TIMESTAMP}\"\n",
|
||||
"MODEL_PATH = (\n",
|
||||
" f'{hub.resolve(\"https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1\")}/*'\n",
|
||||
")\n",
|
||||
"PREPROCESSED_TABLE = f\"reuters_text_preprocessed_{UUID}\"\n",
|
||||
"PREPROCESSED_TABLE = f\"reuters_text_preprocessed_{TIMESTAMP}\"\n",
|
||||
"CLASSIFICATION_MODEL_NAME = \"logistic_reg\"\n",
|
||||
"PREDICT_TABLE = f\"reuters_text_predict_{UUID}\""
|
||||
"PREDICT_TABLE = f\"reuters_text_predict_{TIMESTAMP}\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -765,7 +658,7 @@
|
||||
"id": "NlyOjKrjCXsI"
|
||||
},
|
||||
"source": [
|
||||
"### Initialize Vertex AI SDK client"
|
||||
"### Initialize client"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -785,9 +678,7 @@
|
||||
"id": "ZrgOD30o7HcL"
|
||||
},
|
||||
"source": [
|
||||
"## Pipeline formalization\n",
|
||||
"\n",
|
||||
"In this step, you create various components for the pipeline and build the final pipeline."
|
||||
"## Pipeline formalization"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -866,7 +757,7 @@
|
||||
"from apache_beam.options.pipeline_options import SetupOptions\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Helpers ---------------------------------------------------------------------\n",
|
||||
"# Helpers -------------------------------------------------------- -------------\n",
|
||||
"\n",
|
||||
"def get_args():\n",
|
||||
" \"\"\"\n",
|
||||
@@ -1058,7 +949,7 @@
|
||||
"apache-beam[gcp]==2.36.0\n",
|
||||
"bs4==0.0.1\n",
|
||||
"nltk==3.7\n",
|
||||
"tensorflow==2.8.0"
|
||||
"tensorflow<2.8.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1102,7 +993,7 @@
|
||||
"REQUIRED_PACKAGES = [\n",
|
||||
" 'bs4==0.0.1',\n",
|
||||
" 'nltk==3.7',\n",
|
||||
" 'tensorflow==2.8.0']\n",
|
||||
" 'tensorflow<2.8.0']\n",
|
||||
"\n",
|
||||
"setuptools.setup(\n",
|
||||
" name='ingest',\n",
|
||||
@@ -1119,7 +1010,7 @@
|
||||
"id": "5Nc7ByK1AEe8"
|
||||
},
|
||||
"source": [
|
||||
"#### Copy the setup, module and requirements files to Cloud Storage\n",
|
||||
"#### Copy the setup, the python module and requirements file to Cloud Storage\n",
|
||||
"\n",
|
||||
"Finally, copy the Python module, requirements and setup file to your Cloud Storage bucket."
|
||||
]
|
||||
@@ -1144,15 +1035,15 @@
|
||||
"id": "MI_wYYwdAZZs"
|
||||
},
|
||||
"source": [
|
||||
"### BigQuery ML components\n",
|
||||
"### BQML components\n",
|
||||
"\n",
|
||||
"For the next steps in building the pipeline, you define a set of queries to:\n",
|
||||
"To build the next steps of our pipelines, we define a set of queries to:\n",
|
||||
"\n",
|
||||
"1) Create the BigQuery dataset schema.\n",
|
||||
"2) Preprocess your text data and generate the embeddings using Swivel model.\n",
|
||||
"2) Preprocess our text data and generate the embeddings using Swevel model\n",
|
||||
"2) Train the BigQuery ML Logistic Regression model.\n",
|
||||
"3) Evaluate the model.\n",
|
||||
"4) Run a batch prediction.\n"
|
||||
"4) Run a batch prediction\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1175,9 +1066,9 @@
|
||||
"id": "8tYtZMRiepKe"
|
||||
},
|
||||
"source": [
|
||||
"#### Create BigQuery Dataset query\n",
|
||||
"#### Create BQ Dataset query\n",
|
||||
"\n",
|
||||
"With this query, you create the Bigquery dataset schema that you are going to use to train your model."
|
||||
"With this query, we create the Bigquery dataset schema we are going to use to train our model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1205,9 +1096,9 @@
|
||||
"id": "T2_kuUSZUBDY"
|
||||
},
|
||||
"source": [
|
||||
"#### Create BigQuery Preprocess query\n",
|
||||
"#### Create BQ Preprocess query\n",
|
||||
"\n",
|
||||
"The following query uses the TFHub Swivel model to generate embeddings for your text data and splits the dataset for training and serving purposes."
|
||||
"The following query use the TFHub Swevel model to generate the embedding of our text data and split the dataset for training and serving purposes."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1296,9 +1187,9 @@
|
||||
"id": "yF9W5x4HgUQb"
|
||||
},
|
||||
"source": [
|
||||
"#### Create BigQuery Model query\n",
|
||||
"#### Create BQ Model query\n",
|
||||
"\n",
|
||||
"Below, you have a simple query to build a BigQuery ML Logistic Classifier model for topic's articles classification."
|
||||
"Below you have a simple query to build a BigQuery ML Logistic Classifier model for topic's articles classification."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1333,9 +1224,9 @@
|
||||
"id": "RlLTcuUhdzFU"
|
||||
},
|
||||
"source": [
|
||||
"#### Create BigQuery Prediction query\n",
|
||||
"#### Create BQ Prediction query\n",
|
||||
"\n",
|
||||
"With the following query, you run a prediction job using the table with the preprocessing query."
|
||||
"With the following query, we run a prediction job using the table with the preprocessing query."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1361,14 +1252,7 @@
|
||||
"id": "OxemUVCxAiSo"
|
||||
},
|
||||
"source": [
|
||||
"### Build the pipeline\n",
|
||||
"\n",
|
||||
"In this step, you build the pipeline using the individual components.\n",
|
||||
"\n",
|
||||
"Define the `JOB_NAME` and `JOB_CONFIG` below. `JOB_CONFIG` consists of the following parameters for the destination table:\n",
|
||||
"- `PROJECT_ID`: Id of the project.\n",
|
||||
"- `BQ_DATASET`: Id of the BigQuery dataset.\n",
|
||||
"- `PREDICT_TABLE`: Id of the BigQuery table where predictions are stored."
|
||||
"### Build Pipeline"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1380,7 +1264,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ID = random.randint(1, 10000)\n",
|
||||
"JOB_NAME = f\"reuters-preprocess-{UUID}-{ID}\"\n",
|
||||
"JOB_NAME = f\"reuters-preprocess-{TIMESTAMP}-{ID}\"\n",
|
||||
"JOB_CONFIG = {\n",
|
||||
" \"destinationTable\": {\n",
|
||||
" \"projectId\": PROJECT_ID,\n",
|
||||
@@ -1396,9 +1280,7 @@
|
||||
"id": "mdO8st_gLKBZ"
|
||||
},
|
||||
"source": [
|
||||
"#### Create a custom component for arguments\n",
|
||||
"\n",
|
||||
"Next, you create a component to pass arguments to the `DataflowPythonJobOp` component."
|
||||
"#### Create a custom component to pass `DataflowPythonJobOp` arguments"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1441,26 +1323,7 @@
|
||||
"id": "pcSL1FHk69KT"
|
||||
},
|
||||
"source": [
|
||||
"#### Create the pipeline\n",
|
||||
"\n",
|
||||
"Define the workflow of the pipeline and build the pipeline. The parameters passed to the pipeline include:\n",
|
||||
"\n",
|
||||
"- `create_bq_dataset_query`: SQL query to create the dataset in BigQuery.\n",
|
||||
"- `job_name`: Name of the Cloud Dataflow job to be configured in `PipelineOptions`.\n",
|
||||
"- `inputs_uri`: A directory location of input data.\n",
|
||||
"- `bq_dataset`: Dataset name used in BigQuery.\n",
|
||||
"- `bq_table`: Table name used in BigQuery for ingestion.\n",
|
||||
"- `requirements_file_path`: The GCS path to the pip requirements file.\n",
|
||||
"- `python_file_path`: The GCS path to the python file to run.\n",
|
||||
"- `setup_file_uri`: Path to a Python setup file containing package dependencies.\n",
|
||||
"- `temp_location`: GCS path for Dataflow to stage temporary job files created during the execution of the pipeline.\n",
|
||||
"- `runner`: Pipeline runner used to execute the workflow.\n",
|
||||
"- `create_bq_preprocess_query`: SQL query to preprocess the data in BigQuery.\n",
|
||||
"- `create_bq_model_query`: SQL query to create the BigQuery ML model.\n",
|
||||
"- `create_bq_prediction_query`: SQL query for prediction.\n",
|
||||
"- `job_config`: A json formatted string describing the job configuration. For more information, vist this [page]( https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobConfigurationQuery).\n",
|
||||
"- `project`: Project ID.\n",
|
||||
"- `region`: Selected region to run the Dataflow job."
|
||||
"#### Create the pipeline"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1508,7 +1371,7 @@
|
||||
" project=project,\n",
|
||||
" location=\"US\",\n",
|
||||
" )\n",
|
||||
" # instantiate dataflow args\n",
|
||||
" # instanciate dataflow args\n",
|
||||
" build_dataflow_args_op = build_dataflow_args(\n",
|
||||
" job_name=job_name,\n",
|
||||
" inputs_uri=inputs_uri,\n",
|
||||
@@ -1568,9 +1431,7 @@
|
||||
"id": "nghLONQX7JNg"
|
||||
},
|
||||
"source": [
|
||||
"## Compile and Run the pipeline\n",
|
||||
"\n",
|
||||
"Pass the necessary constants and parameters to the pipeline and compile it to a json file."
|
||||
"## Compile and Run the pipeline"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1590,15 +1451,6 @@
|
||||
"compiler.Compiler().compile(pipeline_func=pipeline, package_path=PIPELINE_PACKAGE)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "b5c1b7b8b290"
|
||||
},
|
||||
"source": [
|
||||
"Using the compiled json file, create Vertex AI Pipeline Job and run it by passing the `SERVICE_ACCOUNT` details configured earlier."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -1608,7 +1460,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pipeline = vertex_ai.PipelineJob(\n",
|
||||
" display_name=f\"data_preprocess_{UUID}\",\n",
|
||||
" display_name=f\"data_preprocess_{TIMESTAMP}\",\n",
|
||||
" template_path=PIPELINE_PACKAGE,\n",
|
||||
" pipeline_root=PIPELINE_ROOT,\n",
|
||||
" parameter_values={\n",
|
||||
@@ -1630,27 +1482,7 @@
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"pipeline.run(service_account=SERVICE_ACCOUNT)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "bcb582e65740"
|
||||
},
|
||||
"source": [
|
||||
"Once the pipeline job gets finished successfully, the trained model can be found created in the BigQuery dataset. Run the following cell to see the model listed in the output."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "c0d194c006ae"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! bq ls $PROJECT_ID:$BQ_DATASET"
|
||||
"pipeline.run()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1662,9 +1494,7 @@
|
||||
"## 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 in the following cell. Set `delete_bucket` and `delete_dataset` to **True** to delete the Cloud Storage bucket and the Bigquery dataset used in this notebook respectively."
|
||||
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1675,19 +1505,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# delete the pipeline job\n",
|
||||
"pipeline.delete()\n",
|
||||
"\n",
|
||||
"delete_bucket = False\n",
|
||||
"delete_dataset = False\n",
|
||||
"\n",
|
||||
"# delete bucket\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI\n",
|
||||
"! gsutil -m rm -r $BUCKET_URI\n",
|
||||
"\n",
|
||||
"# delete dataset\n",
|
||||
"if delete_dataset or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! bq rm -r -f -d $PROJECT_ID:$BQ_DATASET"
|
||||
"! bq rm -r -f -d $PROJECT_ID:$BQ_DATASET"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user