mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-27 15:42:05 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ebb74e97a | ||
|
|
a992a5530d | ||
|
|
24e0e92f8d | ||
|
|
bc4ec36914 | ||
|
|
5387799f32 |
@@ -17,16 +17,13 @@ import concurrent
|
||||
import dataclasses
|
||||
import datetime
|
||||
import functools
|
||||
import json
|
||||
import git
|
||||
import operator
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import utils
|
||||
from typing import List, Optional
|
||||
from utils import util
|
||||
|
||||
import execute_notebook_helper
|
||||
import execute_notebook_remote
|
||||
@@ -38,7 +35,6 @@ from utils import NotebookProcessors, util
|
||||
|
||||
# A buffer so that workers finish before the orchestrating job
|
||||
WORKER_TIMEOUT_BUFFER_IN_SECONDS: int = 60 * 60
|
||||
PYTHON_VERSION = "3.9" # Set default python version
|
||||
|
||||
|
||||
def format_timedelta(delta: datetime.timedelta) -> str:
|
||||
@@ -70,7 +66,6 @@ class NotebookExecutionResult:
|
||||
log_url: str
|
||||
output_uri: str
|
||||
build_id: str
|
||||
logs_bucket: str
|
||||
error_message: Optional[str]
|
||||
|
||||
@property
|
||||
@@ -115,33 +110,6 @@ def _process_notebook(
|
||||
nbformat.write(nb, new_file)
|
||||
|
||||
|
||||
def _get_notebook_python_version(notebook_path: str) -> str:
|
||||
"""
|
||||
Get the python version for running the notebook if it is specified in
|
||||
the notebook.
|
||||
"""
|
||||
python_version = PYTHON_VERSION
|
||||
|
||||
# Load the notebook
|
||||
file = open(notebook_path)
|
||||
src = file.read()
|
||||
nb_json = json.loads(src)
|
||||
|
||||
#Iterate over the cells in the ipynb
|
||||
for cell in nb_json['cells']:
|
||||
if cell['cell_type'] == 'markdown':
|
||||
markdown = str.join('', cell['source'])
|
||||
|
||||
# Look for the python version specification pattern
|
||||
re_match = re.search('python version = (\d\.\d)', markdown, flags=re.IGNORECASE)
|
||||
if re_match:
|
||||
# get the version number
|
||||
python_version = re_match.group(1)
|
||||
break
|
||||
|
||||
return python_version
|
||||
|
||||
|
||||
def _create_tag(filepath: str) -> str:
|
||||
tag = os.path.basename(os.path.normpath(filepath))
|
||||
tag = re.sub("[^0-9a-zA-Z_.-]+", "-", tag)
|
||||
@@ -192,7 +160,6 @@ def process_and_execute_notebook(
|
||||
output_uri=notebook_output_uri,
|
||||
log_url="",
|
||||
build_id="",
|
||||
logs_bucket="",
|
||||
error_message=None,
|
||||
)
|
||||
|
||||
@@ -200,10 +167,6 @@ def process_and_execute_notebook(
|
||||
time_start = datetime.datetime.now()
|
||||
operation = None
|
||||
try:
|
||||
# Get the python version for running the notebook if specified
|
||||
notebook_exec_python_version = _get_notebook_python_version(notebook_path=notebook)
|
||||
print(f"Running notebook with python {notebook_exec_python_version}")
|
||||
|
||||
# Pre-process notebook by substituting variable names
|
||||
_process_notebook(
|
||||
notebook_path=notebook,
|
||||
@@ -230,13 +193,11 @@ def process_and_execute_notebook(
|
||||
private_pool_id=private_pool_id,
|
||||
private_pool_region=variable_region,
|
||||
timeout_in_seconds=timeout_in_seconds,
|
||||
python_version=notebook_exec_python_version
|
||||
)
|
||||
|
||||
operation_metadata = BuildOperationMetadata(mapping=operation.metadata)
|
||||
result.build_id = operation_metadata.build.id
|
||||
result.log_url = operation_metadata.build.log_url
|
||||
result.logs_bucket = operation_metadata.build.logs_bucket
|
||||
|
||||
# Block and wait for the result
|
||||
operation_result = operation.result()
|
||||
@@ -378,7 +339,7 @@ def process_and_execute_notebooks(
|
||||
seconds=max(timeout - WORKER_TIMEOUT_BUFFER_IN_SECONDS, 0)
|
||||
)
|
||||
|
||||
if len(notebooks) >= 1:
|
||||
if len(notebooks) > 1:
|
||||
notebook_execution_results: List[NotebookExecutionResult] = []
|
||||
|
||||
print(f"Found {len(notebooks)} modified notebooks: {notebooks}")
|
||||
@@ -443,7 +404,6 @@ def process_and_execute_notebooks(
|
||||
result.log_url,
|
||||
result.output_uri,
|
||||
result.output_uri_web,
|
||||
result.logs_bucket
|
||||
]
|
||||
for result in results_sorted
|
||||
],
|
||||
@@ -454,35 +414,10 @@ def process_and_execute_notebooks(
|
||||
"log_url",
|
||||
"output_uri",
|
||||
"output_uri_web",
|
||||
"logs_bucket"
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
if len(notebooks) == 1:
|
||||
print("="*100)
|
||||
print("The notebook execution build log:\n")
|
||||
print("="*100)
|
||||
|
||||
build_id = results_sorted[0].build_id
|
||||
logs_bucket_name = (results_sorted[0].logs_bucket).removeprefix("gs://")
|
||||
log_file_name = f"log-{build_id}.txt"
|
||||
|
||||
log_contents = util.download_blob_into_memory(
|
||||
bucket_name=logs_bucket_name,
|
||||
blob_name=log_file_name,
|
||||
download_as_text=True
|
||||
)
|
||||
|
||||
# Remove extra steps from the log
|
||||
match = re.search("starting Step #4", log_contents, flags=re.IGNORECASE)
|
||||
|
||||
if match is not None:
|
||||
match_index = match.span()[0]
|
||||
print(log_contents[match_index:])
|
||||
else:
|
||||
print(log_contents)
|
||||
|
||||
print("\n=== END RESULTS===\n")
|
||||
|
||||
total_notebook_duration = functools.reduce(
|
||||
@@ -498,5 +433,25 @@ def process_and_execute_notebooks(
|
||||
# Raise error if any notebooks failed
|
||||
if not all([result.is_pass for result in results_sorted]):
|
||||
raise RuntimeError("Notebook failures detected. See logs for details")
|
||||
|
||||
elif len(notebooks) == 1:
|
||||
notebook = notebooks[0]
|
||||
|
||||
# Pre-process notebook by substituting variable names
|
||||
_process_notebook(
|
||||
notebook_path=notebook,
|
||||
variable_project_id=variable_project_id,
|
||||
variable_region=variable_region,
|
||||
variable_service_account=variable_service_account,
|
||||
variable_vpc_network=variable_vpc_network,
|
||||
)
|
||||
|
||||
execute_notebook_helper.execute_notebook(
|
||||
notebook_source=notebook,
|
||||
output_file_or_uri="/".join(
|
||||
[artifacts_bucket, pathlib.Path(notebook).name]
|
||||
),
|
||||
should_log_output=True,
|
||||
)
|
||||
else:
|
||||
print("No notebooks modified in this pull request.")
|
||||
|
||||
@@ -40,7 +40,6 @@ def execute_notebook_remote(
|
||||
private_pool_region: Optional[str],
|
||||
tag: Optional[str],
|
||||
timeout_in_seconds: Optional[int] = None,
|
||||
python_version: Optional[str] = None
|
||||
) -> operation.Operation:
|
||||
"""Create and execute a single notebook on Google Cloud Build"""
|
||||
# Load build steps from YAML
|
||||
@@ -51,12 +50,8 @@ def execute_notebook_remote(
|
||||
"_PYTHON_IMAGE": container_uri,
|
||||
"_NOTEBOOK_GCS_URI": notebook_uri,
|
||||
"_NOTEBOOK_OUTPUT_GCS_URI": notebook_output_uri,
|
||||
"_PYTHON_VERSION" : f"python{python_version}"
|
||||
}
|
||||
|
||||
if python_version is not None:
|
||||
substitutions["_PYTHON_VERSION"] = "python" + python_version
|
||||
|
||||
build = cloudbuild_v1.Build()
|
||||
|
||||
options: Optional[client_options.ClientOptions] = None
|
||||
|
||||
@@ -10,21 +10,21 @@ steps:
|
||||
entrypoint: /bin/sh
|
||||
args:
|
||||
- -c
|
||||
- ${_PYTHON_VERSION} .cloud-build/CheckPythonVersion.py -q
|
||||
- python3 .cloud-build/CheckPythonVersion.py -q
|
||||
# Create a virtual environment
|
||||
- name: ${_PYTHON_IMAGE}
|
||||
entrypoint: /bin/sh
|
||||
args:
|
||||
- -c
|
||||
- ${_PYTHON_VERSION} -m venv workspace/env
|
||||
- python3 -m venv workspace/env
|
||||
# Install Python dependencies
|
||||
- name: ${_PYTHON_IMAGE}
|
||||
entrypoint: /bin/sh
|
||||
args:
|
||||
- -c
|
||||
- . workspace/env/bin/activate &&
|
||||
python -m pip -q install -U pip &&
|
||||
python -m pip -q install -U -r .cloud-build/requirements.txt
|
||||
python3 -m pip -q install -U pip &&
|
||||
python3 -m pip -q install -U -r .cloud-build/requirements.txt
|
||||
# Install Python dependencies and run testing script
|
||||
- name: ${_PYTHON_IMAGE}
|
||||
entrypoint: /bin/sh
|
||||
@@ -32,7 +32,7 @@ steps:
|
||||
- -c
|
||||
- |
|
||||
. workspace/env/bin/activate &&
|
||||
python .cloud-build/execute_notebook_cli.py --notebook_source "${_NOTEBOOK_GCS_URI}" --output_file_or_uri "${_NOTEBOOK_OUTPUT_GCS_URI}"
|
||||
python3 .cloud-build/execute_notebook_cli.py --notebook_source "${_NOTEBOOK_GCS_URI}" --output_file_or_uri "${_NOTEBOOK_OUTPUT_GCS_URI}"
|
||||
env:
|
||||
- 'IS_TESTING=1'
|
||||
timeout: 86400s
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
notebooks/official/vizier/gapic-vizier-multi-objective-optimization.ipynb
|
||||
notebooks/official/pipelines/lightweight_functions_component_io_kfp.ipynb
|
||||
notebooks/official/ml_metadata/sdk-metric-parameter-tracking-for-locally-trained-models.ipynb
|
||||
notebooks/official/custom/custom-tabular-bq-managed-dataset.ipynb
|
||||
notebooks/official/matching_engine/sdk_matching_engine_for_indexing.ipynb
|
||||
.cloud-build/tests/python_version_test.ipynb
|
||||
notebooks/official/pipelines/metrics_viz_run_compare_kfp.ipynb
|
||||
notebooks/official/matching_engine/sdk_matching_engine_for_indexing.ipynb
|
||||
@@ -1 +1 @@
|
||||
notebooks/official/custom/custom-tabular-bq-managed-dataset.ipynb
|
||||
notebooks/official/pipelines/metrics_viz_run_compare_kfp.ipynb
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "57a3d44ed8a8"
|
||||
},
|
||||
"source": [
|
||||
"### Set up your Google Cloud project\n",
|
||||
"\n",
|
||||
"**_NOTE_**: This notebook has been tested in the following environment:\n",
|
||||
"\n",
|
||||
"* Python version = 3.7\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 and Compute Engine API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,compute_component).\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",
|
||||
"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": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "c6516f90311b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# test if the right python version is being used\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"actual_python_version = f\"{sys.version_info.major}.{sys.version_info.minor}\"\n",
|
||||
"print(f\"Runtime python version: {actual_python_version}\")\n",
|
||||
"\n",
|
||||
"assert actual_python_version == \"3.7\", \"Wrong python version!\""
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "python_version_test.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import subprocess
|
||||
import tarfile
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Optional, Union
|
||||
from typing import Optional
|
||||
|
||||
from google.auth import credentials as auth_credentials
|
||||
from google.cloud import storage
|
||||
@@ -58,34 +58,3 @@ def archive_code_and_upload(staging_bucket: str):
|
||||
print(f"Uploaded source code archive to {source_archived_file_gcs}")
|
||||
|
||||
return source_archived_file_gcs
|
||||
|
||||
|
||||
def download_blob_into_memory(
|
||||
bucket_name: str,
|
||||
blob_name: str,
|
||||
download_as_text: Optional[bool]=False
|
||||
) -> Union[bytes, str]:
|
||||
"""
|
||||
Downloads a blob into memory as byte or as text if
|
||||
download_as_text is set to True.
|
||||
"""
|
||||
|
||||
storage_client = storage.Client()
|
||||
|
||||
bucket = storage_client.bucket(bucket_name)
|
||||
|
||||
# Construct a client side representation of a blob.
|
||||
blob = bucket.blob(blob_name)
|
||||
|
||||
# Download the blob content
|
||||
if download_as_text:
|
||||
contents = blob.download_as_text()
|
||||
else:
|
||||
contents = blob.download_as_bytes()
|
||||
|
||||
print(
|
||||
f"Downloaded storage object {blob_name} from bucket {bucket_name}."
|
||||
)
|
||||
|
||||
return contents
|
||||
|
||||
|
||||
@@ -39,15 +39,18 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage1/mlops_data_management.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://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/community/ml_ops/stage1/mlops_data_management.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",
|
||||
"*Note: This notebook is not supported for execution in Colab*"
|
||||
"<br/><br/><br/>"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -166,20 +169,21 @@
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"\n",
|
||||
"ONCE_ONLY = False\n",
|
||||
"ONCE_ONLY = True\n",
|
||||
"if ONCE_ONLY:\n",
|
||||
" ! pip3 install -U {USER_FLAG} -q tensorflow==2.5 \\\n",
|
||||
" tensorflow-data-validation==1.2 \\\n",
|
||||
" tensorflow-transform==1.2 \\\n",
|
||||
" tensorflow-io==0.18 \n",
|
||||
" \n",
|
||||
" ! pip3 install --upgrade {USER_FLAG} -q google-cloud-aiplatform[tensorboard] \\\n",
|
||||
" google-cloud-pipeline-components \\\n",
|
||||
" google-cloud-bigquery \\\n",
|
||||
" google-cloud-logging \\\n",
|
||||
" apache-beam[gcp] \\\n",
|
||||
" pyarrow \\\n",
|
||||
" cloudml-hypertune\n"
|
||||
" ! pip3 install -U tensorflow==2.5 $USER_FLAG -q\n",
|
||||
" ! pip3 install -U tensorflow-data-validation==1.2 $USER_FLAG -q\n",
|
||||
" ! pip3 install -U tensorflow-transform==1.2 $USER_FLAG -q\n",
|
||||
" ! pip3 install -U tensorflow-io==0.18 $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade google-cloud-aiplatform[tensorboard] $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade google-cloud-pipeline-components $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade google-cloud-bigquery $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade google-cloud-logging $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade apache-beam[gcp]==2.33.0 $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade pyarrow $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade cloudml-hypertune $USER_FLAG -q\n",
|
||||
" ! pip3 install --upgrade kfp $USER_FLAG -q\n",
|
||||
" ! pip3 install future $USER_FLAG -q"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -351,7 +355,7 @@
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already authenticated. \n",
|
||||
"**If you are using Vertex AI Workbench 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",
|
||||
@@ -413,7 +417,7 @@
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"\n",
|
||||
"When you submit a custom training job using the Vertex AI SDK, you upload a Python package\n",
|
||||
"When you submit a custom training job using the Vertex SDK, you upload a Python package\n",
|
||||
"containing your training code to a Cloud Storage bucket. Vertex AI runs\n",
|
||||
"the code from this package. In this tutorial, Vertex AI also saves the\n",
|
||||
"trained model that results from your job in the same bucket. You can then\n",
|
||||
|
||||
@@ -84,8 +84,7 @@
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Hyperparameter tuning with Random algorithm.\n",
|
||||
"- Hyperparameter tuning with Vizier (Bayesian) algorithm.\n",
|
||||
"- Suggesting trials and updating results for Vizier study"
|
||||
"- Hyperparameter tuning with Vizier (Bayesian) algorithm."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -188,8 +187,7 @@
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade $USER_FLAG -q google-cloud-aiplatform \\\n",
|
||||
" google-vizier==0.0.4"
|
||||
"! pip3 install --upgrade google-cloud-aiplatform[tensorboard] $USER_FLAG -q"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -331,32 +329,25 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "06571eb4063b"
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "4e166d927e36"
|
||||
"id": "timestamp"
|
||||
},
|
||||
"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\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -367,7 +358,7 @@
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already authenticated. \n",
|
||||
"**If you are using Vertex AI Workbench 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",
|
||||
@@ -455,7 +446,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
|
||||
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"aip-\" + UUID"
|
||||
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -518,8 +509,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import google.cloud.aiplatform as aip\n",
|
||||
"from google.cloud.aiplatform.vizier import Study, pyvizier"
|
||||
"import google.cloud.aiplatform as aip"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -544,6 +534,35 @@
|
||||
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "aip_constants"
|
||||
},
|
||||
"source": [
|
||||
"#### Vertex AI constants\n",
|
||||
"\n",
|
||||
"Setup up the following constants for Vertex AI:\n",
|
||||
"\n",
|
||||
"- `API_ENDPOINT`: The Vertex AI API service endpoint for `Dataset`, `Model`, `Job`, `Pipeline` and `Endpoint` services.\n",
|
||||
"- `PARENT`: The Vertex AI location root path for `Dataset`, `Model`, `Job`, `Pipeline` and `Endpoint` resources."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "aip_constants"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# API service endpoint\n",
|
||||
"API_ENDPOINT = \"{}-aiplatform.googleapis.com\".format(REGION)\n",
|
||||
"\n",
|
||||
"# Vertex location root path for your dataset, model and endpoint resources\n",
|
||||
"PARENT = \"projects/\" + PROJECT_ID + \"/locations/\" + REGION"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -607,7 +626,7 @@
|
||||
"if os.getenv(\"IS_TESTING_TF\"):\n",
|
||||
" TF = os.getenv(\"IS_TESTING_TF\")\n",
|
||||
"else:\n",
|
||||
" TF = \"2.5\".replace(\".\", \"-\")\n",
|
||||
" TF = \"2.1\".replace(\".\", \"-\")\n",
|
||||
"\n",
|
||||
"if TF[0] == \"2\":\n",
|
||||
" if TRAIN_GPU:\n",
|
||||
@@ -1012,7 +1031,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"JOB_NAME = \"custom_job_\" + UUID\n",
|
||||
"JOB_NAME = \"custom_job_\" + TIMESTAMP\n",
|
||||
"MODEL_DIR = \"{}/{}\".format(BUCKET_URI, JOB_NAME)\n",
|
||||
"\n",
|
||||
"if not TRAIN_NGPU or TRAIN_NGPU < 2:\n",
|
||||
@@ -1075,7 +1094,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job = aip.CustomJob(display_name=\"boston_\" + UUID, worker_pool_specs=worker_pool_spec)"
|
||||
"job = aip.CustomJob(\n",
|
||||
" display_name=\"boston_\" + TIMESTAMP, worker_pool_specs=worker_pool_spec\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1107,7 +1128,7 @@
|
||||
"from google.cloud.aiplatform import hyperparameter_tuning as hpt\n",
|
||||
"\n",
|
||||
"hpt_job = aip.HyperparameterTuningJob(\n",
|
||||
" display_name=\"boston_\" + UUID,\n",
|
||||
" display_name=\"boston_\" + TIMESTAMP,\n",
|
||||
" custom_job=job,\n",
|
||||
" metric_spec={\n",
|
||||
" \"val_loss\": \"minimize\",\n",
|
||||
@@ -1288,7 +1309,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job = aip.CustomJob(\n",
|
||||
" display_name=\"boston_\" + UUID,\n",
|
||||
" display_name=\"boston_\" + TIMESTAMP,\n",
|
||||
" worker_pool_specs=worker_pool_spec,\n",
|
||||
" base_output_dir=MODEL_DIR,\n",
|
||||
")"
|
||||
@@ -1323,7 +1344,7 @@
|
||||
"from google.cloud.aiplatform import hyperparameter_tuning as hpt\n",
|
||||
"\n",
|
||||
"hpt_job = aip.HyperparameterTuningJob(\n",
|
||||
" display_name=\"boston_\" + UUID,\n",
|
||||
" display_name=\"boston_\" + TIMESTAMP,\n",
|
||||
" custom_job=job,\n",
|
||||
" metric_spec={\n",
|
||||
" \"val_loss\": \"minimize\",\n",
|
||||
@@ -1492,25 +1513,22 @@
|
||||
"id": "vizier_client"
|
||||
},
|
||||
"source": [
|
||||
"### Specify the algorithm used to suggest trial parameters\n",
|
||||
"### Create Vizier client\n",
|
||||
"\n",
|
||||
"First, you create a `StudyConfig`, and specify the algorithm to suggest the next trial.\n",
|
||||
"\n",
|
||||
" GRID_SEARCH: grid search\n",
|
||||
" RANDOM_SEARCH: random search\n",
|
||||
" ALGORIGTHM_UNSPECIFIED: Vizier bayesian algorithm"
|
||||
"Create a client side connection to the Vertex AI Vizier service."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "d7dd26490358"
|
||||
"id": "vizier_client"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"problem = pyvizier.StudyConfig()\n",
|
||||
"problem.algorithm = pyvizier.Algorithm.RANDOM_SEARCH"
|
||||
"vizier_client = aip.gapic.VizierServiceClient(\n",
|
||||
" client_options=dict(api_endpoint=API_ENDPOINT)\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1525,15 +1543,7 @@
|
||||
"\n",
|
||||
"In the following example, the goal is to maximize y = x^2 with x in the range of \\[-10. 10\\]. This example has only one parameter and uses an easily calculated function to help demonstrate how to use Vizier.\n",
|
||||
"\n",
|
||||
"First, you specify the metrics to minimize or maximize in the study as a list to the property `metric_information`. Then you specify the parameters to the study using the `add_XXX_params()` method for the corresponding data type:\n",
|
||||
"\n",
|
||||
" - add_bool_param\n",
|
||||
" - add_categorical_param\n",
|
||||
" - add_discrete_param\n",
|
||||
" - add_float_param\n",
|
||||
" - add_int_param\n",
|
||||
"\n",
|
||||
"You create the study using the `create_or_load()` method."
|
||||
"First, you will create the study using the `create_study()` method."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1544,19 +1554,28 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"STUDY_DISPLAY_NAME = \"xpow2\" + UUID\n",
|
||||
"STUDY_DISPLAY_NAME = \"xpow2\" + TIMESTAMP\n",
|
||||
"\n",
|
||||
"problem.metric_information.append(\n",
|
||||
" pyvizier.MetricInformation(name=\"y\", goal=pyvizier.ObjectiveMetricGoal.MAXIMIZE)\n",
|
||||
")\n",
|
||||
"param_x = {\n",
|
||||
" \"parameter_id\": \"x\",\n",
|
||||
" \"double_value_spec\": {\"min_value\": -10.0, \"max_value\": 10.0},\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"params = problem.search_space.select_root()\n",
|
||||
"params.add_float_param(\"x\", -10.0, 10.0, scale_type=pyvizier.ScaleType.LINEAR)\n",
|
||||
"metric_y = {\"metric_id\": \"y\", \"goal\": \"MAXIMIZE\"}\n",
|
||||
"\n",
|
||||
"study = Study.create_or_load(display_name=STUDY_DISPLAY_NAME, problem=problem)\n",
|
||||
"study = {\n",
|
||||
" \"display_name\": STUDY_DISPLAY_NAME,\n",
|
||||
" \"study_spec\": {\n",
|
||||
" \"algorithm\": \"RANDOM_SEARCH\",\n",
|
||||
" \"parameters\": [param_x],\n",
|
||||
" \"metrics\": [metric_y],\n",
|
||||
" },\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"study = vizier_client.create_study(parent=PARENT, study=study)\n",
|
||||
"STUDY_NAME = study.name\n",
|
||||
"print(\"STUDY_NAME: {}\".format(STUDY_NAME))"
|
||||
"\n",
|
||||
"print(STUDY_NAME)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1567,7 +1586,9 @@
|
||||
"source": [
|
||||
"### Get Vizier study\n",
|
||||
"\n",
|
||||
"You can get a study using the method `list()`."
|
||||
"You can get a study using the method `get_study()`, with the following key/value pairs:\n",
|
||||
"\n",
|
||||
"- `name`: The name of the study."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1578,8 +1599,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"studies = Study.list()\n",
|
||||
"print(studies[0].gca_resource)"
|
||||
"study = vizier_client.get_study({\"name\": STUDY_NAME})\n",
|
||||
"\n",
|
||||
"print(study)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1590,9 +1612,11 @@
|
||||
"source": [
|
||||
"### Get suggested trial\n",
|
||||
"\n",
|
||||
"Next, query the Vizier service for a suggested trial(s) using the method `suggest()`, with the following key/value pairs:\n",
|
||||
"Next, query the Vizier service for a suggested trial(s) using the method `suggest_trials`, with the following key/value pairs:\n",
|
||||
"\n",
|
||||
"- `count`: The number of trials to suggest.\n",
|
||||
"- `parent`: The name of the study.\n",
|
||||
"- `suggestion_count`: The number of trials to suggest.\n",
|
||||
"- `client_id`: blah\n",
|
||||
"\n",
|
||||
"This call is a long running operation. The method `result()` from the response object will wait until the call has completed."
|
||||
]
|
||||
@@ -1601,13 +1625,18 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "11ff2c4562cb"
|
||||
"id": "vizier_suggest_trial"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"SUGGEST_COUNT = 1\n",
|
||||
"CLIENT_ID = \"1001\"\n",
|
||||
"\n",
|
||||
"trials = study.suggest(count=SUGGEST_COUNT)\n",
|
||||
"response = vizier_client.suggest_trials(\n",
|
||||
" {\"parent\": STUDY_NAME, \"suggestion_count\": SUGGEST_COUNT, \"client_id\": CLIENT_ID}\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"trials = response.result().trials\n",
|
||||
"\n",
|
||||
"print(trials)\n",
|
||||
"\n",
|
||||
@@ -1650,10 +1679,12 @@
|
||||
"source": [
|
||||
"RESULT = 0.01\n",
|
||||
"\n",
|
||||
"measurement = pyvizier.Measurement()\n",
|
||||
"measurement.metrics[\"y\"] = RESULT\n",
|
||||
"\n",
|
||||
"trials[0].add_measurement(measurement)"
|
||||
"vizier_client.add_trial_measurement(\n",
|
||||
" {\n",
|
||||
" \"trial_name\": TRIAL_ID,\n",
|
||||
" \"measurement\": {\"metrics\": [{\"metric_id\": \"y\", \"value\": RESULT}]},\n",
|
||||
" }\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1664,7 +1695,7 @@
|
||||
"source": [
|
||||
"### Delete the Vizier study\n",
|
||||
"\n",
|
||||
"The method 'delete()' will delete the study."
|
||||
"The method 'delete_study()' will delete the study."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1675,7 +1706,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"study.delete()"
|
||||
"vizier_client.delete_study({\"name\": STUDY_NAME})"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -216,10 +216,6 @@
|
||||
"source": [
|
||||
"### Set up your Google Cloud project\n",
|
||||
"\n",
|
||||
"**_NOTE_**: This notebook has been tested in the following environment:\n",
|
||||
"\n",
|
||||
"* Python version = 3.7\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",
|
||||
|
||||
@@ -134,30 +134,16 @@
|
||||
"### 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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "24743cf4a1e1"
|
||||
},
|
||||
"source": [
|
||||
"**_NOTE_**: This notebook has been tested in the following environment:\n",
|
||||
"all the requirements to run this notebook. You can skip this step.\n",
|
||||
"\n",
|
||||
"* Python version = 3.9"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "gCuSR8GkAgzl"
|
||||
},
|
||||
"source": [
|
||||
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
|
||||
"You need the following:\n",
|
||||
"\n",
|
||||
"* The Google Cloud SDK\n",
|
||||
"* Git\n",
|
||||
"* Python 3\n",
|
||||
"* virtualenv\n",
|
||||
"* Jupyter notebook running in a virtual environment with Python 3\n",
|
||||
"\n",
|
||||
"The Google Cloud guide to [Setting up a Python development\n",
|
||||
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
"id": "title"
|
||||
},
|
||||
"source": [
|
||||
"# Compare Vertex AI Forecasting and BigQuery ML ARIMA_PLUS\n",
|
||||
"# Compare Vertex Forecasting and BQML ARIMA_PLUS\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
@@ -61,28 +61,7 @@
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"In this tutorial, you take on the role of a store planner who must determine how much inventory they will need to order for each of their products and stores for November 2019. You will accomplish this by training forecasting models using historical sales data. You will start with a baseline model using BigQuery ML (BQML) [ARIMA_PLUS](https://cloud.google.com/bigquery-ml/docs/reference/standard-sql/bigqueryml-syntax-create-time-series) and then compare it against a [Vertex AI Forecasting](https://cloud.google.com/vertex-ai/docs/tabular-data/forecasting/overview) model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "objective:automl,training,batch_prediction"
|
||||
},
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you create an BQML ARIMA_PLUS model using a training [Vertex AI Pipeline](https://cloud.google.com/vertex-ai/docs/pipelines/introduction) from [Google Cloud Pipeline Components](https://cloud.google.com/vertex-ai/docs/pipelines/components-introduction) (GCPC), and then do a batch prediction using the corresponding prediction pipeline. You then train a Vertex AI Forecasting model using the same data and compare the evaluation metrics.\n",
|
||||
"\n",
|
||||
"The steps performed are:\n",
|
||||
"\n",
|
||||
"- Train the BQML ARIMA_PLUS model.\n",
|
||||
"- View BQML model evaluation.\n",
|
||||
"- Make a batch prediction with the BQML model.\n",
|
||||
"- Create a Vertex AI `Dataset` resource.\n",
|
||||
"- Train the Vertex AI Forecasting model.\n",
|
||||
"- View the Model evaluation.\n",
|
||||
"- Make a batch prediction with the Model.\n"
|
||||
"In this tutorial, you will take on the role of a store planner who must determine how much inventory they will need to order for each of their products and stores for November 2019. You will accomplish this by training forecasting models using historical sales data. You will start with a baseline model using [BQML ARIMA_PLUS](https://cloud.google.com/bigquery-ml/docs/reference/standard-sql/bigqueryml-syntax-create-time-series) and then compare it against a [Vertex Forecasting](https://cloud.google.com/vertex-ai/docs/tabular-data/forecasting/overview) model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -93,7 +72,28 @@
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"To demonstrate the tradeoffs between using BQML and Vertex AI Forecasting, this tutorial will use a synthetic dataset where product sales are dependent on a variety of factors such as advertisements, holidays, and locations. You will see how well a univariate model like ARIMA_PLUS can forecast future sales without knowing information about these factors explicitly, and how well a multivariate model like Vertex AI Forecasting can perform when these factors are known."
|
||||
"To demonstrate the tradeoffs between using BQML and Vertex Forecasting, this tutorial will use a synthetic dataset where product sales are dependent on a variety of factors such as advertisements, holidays, and locations. You will see how well a univariate model like ARIMA_PLUS can forecast future sales without knowing information about these factors explicitly, and how well a multivariate model like Vertex Forecasting can perform when these factors are known."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "objective:automl,training,batch_prediction"
|
||||
},
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you create an BQML ARIMA_PLUS model using a training [Vertex Pipeline](https://cloud.google.com/vertex-ai/docs/pipelines/introduction) from [Google Cloud Pipeline Components](https://cloud.google.com/vertex-ai/docs/pipelines/components-introduction) (GCPC), and then do a batch prediction using the corresponding prediction pipeline. You then train a Vertex Forecasting model using the same data and compare the evaluation metrics.\n",
|
||||
"\n",
|
||||
"The steps performed are:\n",
|
||||
"\n",
|
||||
"- Train the BQML ARIMA_PLUS model.\n",
|
||||
"- View BQML model evaluation.\n",
|
||||
"- Make a batch prediction with the BQML model.\n",
|
||||
"- Create a Vertex `Dataset` resource.\n",
|
||||
"- Train the Vertex Forecasting model.\n",
|
||||
"- View the Model evaluation.\n",
|
||||
"- Make a batch prediction with the Model.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -155,9 +155,9 @@
|
||||
"id": "install_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"## Install additional packages\n",
|
||||
"\n",
|
||||
"Install the following packages required to execute this notebook."
|
||||
"Install the latest version of the Google Cloud Pipeline Components (GCPC) SDK."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -170,14 +170,8 @@
|
||||
"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",
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
"# Google Cloud Notebook\n",
|
||||
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"else:\n",
|
||||
" USER_FLAG = \"\"\n",
|
||||
@@ -185,7 +179,7 @@
|
||||
"! (pip3 install --upgrade $USER_FLAG \\\n",
|
||||
" google-cloud-bigquery[pandas]==2.34.4 \\\n",
|
||||
" google-cloud-aiplatform==1.16.1 \\\n",
|
||||
" google-cloud-pipeline-components==1.0.23)"
|
||||
" google-cloud-pipeline-components==1.0.18)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -246,7 +240,7 @@
|
||||
"5. 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",
|
||||
"6. (optional) You may also specify a service account to use to run Vertex AI Pipelines in the project.\n",
|
||||
"6. (optional) You may also specify a service account to use to run Vertex Pipelines in the project.\n",
|
||||
"\n",
|
||||
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$`."
|
||||
]
|
||||
@@ -310,7 +304,7 @@
|
||||
"#### Region\n",
|
||||
"All BigQuery operations (`DATA_REGION`) are set to run in the `US` multi-region. This is required by the ARIMA pipeline because the data you will be using is stored in this region. All destination tables will also be stored in this region.\n",
|
||||
"\n",
|
||||
"You may change the `REGION` variable, which is used for Vertex AI Forecasting operations\n",
|
||||
"You may change the `REGION` variable, which is used for Vertex Forecasting operations\n",
|
||||
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
|
||||
"\n",
|
||||
"- Americas: `us-central1`\n",
|
||||
@@ -330,11 +324,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
|
||||
"DATA_REGION = \"US\" # @param {type: \"string\"}\n",
|
||||
"\n",
|
||||
"if REGION == \"[your-region]\":\n",
|
||||
" REGION = \"us-central1\""
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -343,9 +334,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."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -356,16 +347,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\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -376,7 +360,7 @@
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already authenticated.\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",
|
||||
@@ -411,11 +395,8 @@
|
||||
"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 on Google Cloud Notebook, then don't execute this code\n",
|
||||
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
|
||||
" if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth as google_auth\n",
|
||||
"\n",
|
||||
@@ -423,9 +404,10 @@
|
||||
"\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",
|
||||
" # account. Alternatively, you may edit this notebook to authenticate using\n",
|
||||
" # gcloud.\n",
|
||||
" elif not os.getenv(\"IS_TESTING\"):\n",
|
||||
" %env GOOGLE_APPLICATION_CREDENTIALS '[your-service-account-key-path]'"
|
||||
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -438,7 +420,7 @@
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"\n",
|
||||
"When you initialize the Vertex AI SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
|
||||
"When you initialize the Vertex SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
|
||||
"\n",
|
||||
"Set the name of your Cloud Storage bucket below. Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization."
|
||||
]
|
||||
@@ -472,9 +454,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
|
||||
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"aip-\" + UUID\n",
|
||||
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP\n",
|
||||
"\n",
|
||||
"! gsutil ls -b $BUCKET_URI || gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil ls -b $BUCKET_URI || gsutil mb -l $DATA_REGION $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -532,9 +514,9 @@
|
||||
"id": "init_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex AI SDK for Python\n",
|
||||
"## Initialize Vertex SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
|
||||
"Initialize the Vertex SDK for Python for your project and corresponding bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -576,8 +558,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"arima_dataset_name = f\"forecasting_demo_arima_{UUID}\"\n",
|
||||
"vertex_dataset_name = f\"forecasting_demo_vertex_{UUID}\"\n",
|
||||
"arima_dataset_name = f\"forecasting_demo_arima_{TIMESTAMP}\"\n",
|
||||
"vertex_dataset_name = f\"forecasting_demo_vertex_{TIMESTAMP}\"\n",
|
||||
"\n",
|
||||
"arima_dataset_path = \".\".join([PROJECT_ID, arima_dataset_name])\n",
|
||||
"vertex_dataset_path = \".\".join([PROJECT_ID, vertex_dataset_name])\n",
|
||||
@@ -810,15 +792,15 @@
|
||||
"\n",
|
||||
"Now you are ready to start creating your own BQML ARIMA_PLUS model.\n",
|
||||
"\n",
|
||||
"Like with Vertex AI Forecasting, the pipeline you will run will train evaluation models using the training and validation sets and use backtesting to create evaluation metrics on the test set. Finally, a serving model will be produced that uses all available data.\n",
|
||||
"Like with Vertex Forecasting, the pipeline you will run will train evaluation models using the training and validation sets and use backtesting to create evaluation metrics on the test set. Finally, a serving model will be produced that uses all available data.\n",
|
||||
"\n",
|
||||
"**How do you estimate the cost?**\n",
|
||||
"\n",
|
||||
"Backtesting involves training a single BQML model for each period in the test set, so the cost is a function of the length of the test set after any downsampling done by the windowing strategy. The cost is also multiplied by the number of candidate models trained, which is determined by `max_order`.\n",
|
||||
"Backtesting will involve training a single BQML model for each period in the test set, so the cost will be a function of the length of the test set. The cost is also multiplied by the number of candidate models trained, which is determined by `max_order`.\n",
|
||||
"\n",
|
||||
"According to [BQ pricing](https://cloud.google.com/bigquery-ml/pricing), BQML model creation costs $250 per TB. We'll use a max order of 3, which translates to 20 candidate models when there are multiple time series. Our demo dataset is 3 MB in size, and includes 31 test periods. We window with a stride length of 1, so all periods are used for evaluation.\n",
|
||||
"According to [BQ pricing](https://cloud.google.com/bigquery-ml/pricing), BQML model creation costs $250 per TB. We'll use a max order of 3, which translates to 20 candidate models when there are multiple time series. Our demo dataset is 3 MB in size, and includes 31 test periods.\n",
|
||||
"\n",
|
||||
"In this tutorial, the model create stage of the pipeline costs `3 MB * ($250 / 1024^2) * (31 / 1) periods * 20 candidates = $0.44`."
|
||||
"In this tutorial, the model create stage of the pipeline costs `3 MB * ($250 / 1024^2) * 31 periods * 20 candidates = $0.44`."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -839,23 +821,47 @@
|
||||
"\n",
|
||||
"- `bigquery_destination_uri`: (optional) BigQuery Dataset URI. Used to export the metrics table and model. If not given, we will create one for the user.\n",
|
||||
"- `data_granularity_unit`: Enum used to specify the time granularity (hour, day, week, month, etc).\n",
|
||||
"- `data_source_csv_filenames` or `data_source_bigquery_table_path`: A URI for either a CSV stored in GCR or a BigQuery table, respectively.\n",
|
||||
"- `data_source`: JSON for specifying the input data source URI and type. Similar to a Vertex Dataset. Currently supports BigQuery and CSV (in GCS) data sources.\n",
|
||||
"\n",
|
||||
" It can look like either:\n",
|
||||
" ```json\n",
|
||||
" {\n",
|
||||
" \"big_query_data_source\": {\n",
|
||||
" \"big_query_table_path\": \"bq://[PROJECT].[DATASET].[TABLE]\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
" or\n",
|
||||
" ```json\n",
|
||||
" {\n",
|
||||
" \"csv_data_source\": {\n",
|
||||
" \"csv_filenames\": [ [GCS_PATHS] ],\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
"- `evaluated_examples_destination_uri\t`: (optional) BigQuery Dataset URI OR Table URI. Used to export the evaluated examples table. Will use bigquery_destination_uri if not provided.\n",
|
||||
"- `forecast_horizon`: Integer number of periods to predict.\n",
|
||||
"- A data splitting strategy of either:\n",
|
||||
" - `predefined_split_key`: A column containing `TRAIN`, `VALIDATE`, or `TEST` to denote the splits for each row.\n",
|
||||
" - `training_fraction`, `validation_fraction`, and `test_fraction` to set the fractions to split on chronologically on the time column.\n",
|
||||
" - `timestamp_split_key` plus the fractions in the previous option to perform fractional splitting on a column other than the time column.\n",
|
||||
"- A windowing strategy of either:\n",
|
||||
" - `window_column`: A boolean column decides whether or now each row gets considered when calculating the evaluation metrics.\n",
|
||||
" - `window_stride_length`: Every N rows will be used to compute the evaluation metrics.\n",
|
||||
" - `window_max_count`: Downsample rows such that only the given number are used to calculate the evaluation metrics.\n",
|
||||
"- `target_column`: Name of target column.\n",
|
||||
"- `split_spec`: JSON for specifying how data should be split. Supports predefined split and fractional split.\n",
|
||||
" \n",
|
||||
" It can look like either:\n",
|
||||
" ```json\n",
|
||||
" {\"predefined_split\": {\"key\": \"[SPLIT_COLUMN]\"}}\n",
|
||||
" ```\n",
|
||||
" or\n",
|
||||
" ```json\n",
|
||||
" {\n",
|
||||
" \"fraction_split\": {\n",
|
||||
" \"training_fraction\": 0.8,\n",
|
||||
" \"validation_fraction\": 0.1,\n",
|
||||
" \"test_fraction\": 0.1,\n",
|
||||
" },\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
"- `target_column_name`: Name of target column.\n",
|
||||
"- `time_column`: Name of time column.\n",
|
||||
"- `time_series_identifier_column`: Name of id column.\n",
|
||||
"- `max_order`: Integer between 1 and 5 representing the size of the parameter search space for ARIMA_PLUS. 5 would result in the highest accuracy model, but also the longest training runtime/cost.\n",
|
||||
"\n",
|
||||
"The execution of the training pipeline may take around **20 minutes**."
|
||||
"The execution of the training pipeline will take around **20 minutes**."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -872,24 +878,31 @@
|
||||
"forecast_horizon = 30 # @param {type: \"integer\"}\n",
|
||||
"data_granularity_unit = \"day\" # @param {type: \"string\"}\n",
|
||||
"split_column = \"split\" # @param {type: \"string\"}\n",
|
||||
"window_stride_length = 1 # @param {type: \"integer\"}\n",
|
||||
"max_order = 3 # @param {type: \"integer\"}\n",
|
||||
"override_destination = True # @param {type: \"boolean\"}\n",
|
||||
"\n",
|
||||
"split_spec = {\"predefined_split\": {\"key\": split_column}}\n",
|
||||
"data_source = {\n",
|
||||
" \"big_query_data_source\": {\n",
|
||||
" \"big_query_table_path\": TRAINING_DATASET_BQ_PATH,\n",
|
||||
" },\n",
|
||||
"}\n",
|
||||
"window_config = {\"stride\": 1}\n",
|
||||
"\n",
|
||||
"(\n",
|
||||
" train_job_spec_path,\n",
|
||||
" train_parameter_values,\n",
|
||||
") = utils.get_bqml_arima_train_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=DATA_REGION,\n",
|
||||
" time_column=time_column,\n",
|
||||
" time_series_identifier_column=time_series_identifier_column,\n",
|
||||
" target_column=target_column,\n",
|
||||
" target_column_name=target_column,\n",
|
||||
" forecast_horizon=forecast_horizon,\n",
|
||||
" data_granularity_unit=data_granularity_unit,\n",
|
||||
" predefined_split_key=split_column,\n",
|
||||
" data_source_bigquery_table_path=TRAINING_DATASET_BQ_PATH,\n",
|
||||
" window_stride_length=window_stride_length,\n",
|
||||
" split_spec=split_spec,\n",
|
||||
" data_source=data_source,\n",
|
||||
" window_config=window_config,\n",
|
||||
" bigquery_destination_uri=arima_dataset_path,\n",
|
||||
" override_destination=override_destination,\n",
|
||||
" max_order=max_order,\n",
|
||||
@@ -904,9 +917,9 @@
|
||||
"source": [
|
||||
"### Run the training pipeline\n",
|
||||
"\n",
|
||||
"Use the Vertex AI Python SDK to kick off a training pipeline run. Once the run has started, the following cell will output a link that will allow you to monitor the run. The link should look like this: \n",
|
||||
"Use the Vertex Python SDK to kick off a training pipeline run. Once the run has started, the following cell will output a link that will allow you to monitor the run. The link should look like this: \n",
|
||||
"\n",
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[REGION]/pipelines/runs/[DISPLAY_NAME]`"
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[DATA_REGION]/pipelines/runs/[DISPLAY_NAME]`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -918,7 +931,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# The display name should be unique even if this cell is rerun.\n",
|
||||
"DISPLAY_NAME = f\"forecasting-demo-train-{generate_uuid()}\"\n",
|
||||
"now = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
"DISPLAY_NAME = f\"forecasting-demo-train-{now}\"\n",
|
||||
"\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
" job_id=DISPLAY_NAME,\n",
|
||||
@@ -989,11 +1003,27 @@
|
||||
"Now that your Model resource is trained, you can make a batch prediction using the prediction pipeline, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `bigquery_destination_uri`: (optional) BigQuery Dataset URI. Used to export the metrics table and model. If not given, we will create one for the user.\n",
|
||||
"- `data_source_csv_filenames` or `data_source_bigquery_table_path`: A URI for either a CSV stored in GCR or a BigQuery table, respectively.\n",
|
||||
"- `data_source`: JSON for specifying the input data source URI and type. Similar to a Vertex Dataset. Currently supports BigQuery and CSV (in GCS) data sources.\n",
|
||||
"\n",
|
||||
" It can look like either:\n",
|
||||
" ```json\n",
|
||||
" {\n",
|
||||
" \"big_query_data_source\": {\n",
|
||||
" \"big_query_table_path\": \"bq://[PROJECT].[DATASET].[TABLE]\"\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
" or\n",
|
||||
" ```json\n",
|
||||
" {\n",
|
||||
" \"csv_data_source\": {\n",
|
||||
" \"csv_filenames\": [ [GCS_PATHS] ],\n",
|
||||
" }\n",
|
||||
" ```\n",
|
||||
"- `generate_explanation`: If True, the predictions table will have some extra xAI columns.\n",
|
||||
"- `model_name`: Name of an existing BQML ARIMA_PLUS model to use for predictions.\n",
|
||||
"\n",
|
||||
"The execution of the prediction pipeline may take around **5 minutes**."
|
||||
"The execution of the prediction pipeline will take around **5 minutes**."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1004,8 +1034,14 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"data_source = {\n",
|
||||
" \"big_query_data_source\": {\n",
|
||||
" \"big_query_table_path\": PREDICTION_DATASET_BQ_PATH,\n",
|
||||
" },\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Get the model name programmatically, you can also find this by looking at the\n",
|
||||
"# execution graph in Vertex AI Pipelines.\n",
|
||||
"# execution graph in Vertex Pipelines.\n",
|
||||
"for task_detail in job.gca_resource.job_detail.task_details:\n",
|
||||
" if task_detail.task_name == \"bigquery-create-model-job\":\n",
|
||||
" model_name = task_detail.outputs[\"model\"].artifacts[0].metadata[\"modelId\"]\n",
|
||||
@@ -1019,9 +1055,9 @@
|
||||
" predict_parameter_values,\n",
|
||||
") = utils.get_bqml_arima_predict_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=DATA_REGION,\n",
|
||||
" model_name=f\"{arima_dataset_path}.{model_name}\",\n",
|
||||
" data_source_bigquery_table_path=PREDICTION_DATASET_BQ_PATH,\n",
|
||||
" data_source=data_source,\n",
|
||||
" bigquery_destination_uri=arima_dataset_path,\n",
|
||||
")"
|
||||
]
|
||||
@@ -1034,9 +1070,9 @@
|
||||
"source": [
|
||||
"### Run the prediction pipeline\n",
|
||||
"\n",
|
||||
"Use the Vertex AI Python SDK to kick off a prediction pipeline run. Once the run has started, the following cell will output a link that will allow you to monitor the run. The link should look like this: \n",
|
||||
"Use the Vertex Python SDK to kick off a prediction pipeline run. Once the run has started, the following cell will output a link that will allow you to monitor the run. The link should look like this: \n",
|
||||
"\n",
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[REGION]/pipelines/runs/[DISPLAY_NAME]`"
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[DATA_REGION]/pipelines/runs/[DISPLAY_NAME]`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1048,7 +1084,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# The display name should be unique even if this cell is rerun.\n",
|
||||
"DISPLAY_NAME = f\"forecasting-demo-predict-{generate_uuid()}\"\n",
|
||||
"now = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
"DISPLAY_NAME = f\"forecasting-demo-predict-{now}\"\n",
|
||||
"\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
" job_id=DISPLAY_NAME,\n",
|
||||
@@ -1080,7 +1117,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the prediction table programmatically, you can also find this by looking at the\n",
|
||||
"# execution graph in Vertex AI Pipelines.\n",
|
||||
"# execution graph in Vertex Pipelines.\n",
|
||||
"for task_detail in job.gca_resource.job_detail.task_details:\n",
|
||||
" if task_detail.task_name == \"bigquery-query-job\":\n",
|
||||
" pred_table = (\n",
|
||||
@@ -1205,7 +1242,7 @@
|
||||
"id": "59qKXL9ARO97"
|
||||
},
|
||||
"source": [
|
||||
"# Compare Against Vertex AI Forecasting"
|
||||
"# Compare Against Vertex Forecasting"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1234,7 +1271,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset = aiplatform.TimeSeriesDataset.create(\n",
|
||||
" display_name=\"forecasting_demo_train\" + \"_\" + UUID,\n",
|
||||
" display_name=\"forecasting_demo_train\" + \"_\" + TIMESTAMP,\n",
|
||||
" bq_source=[TRAINING_DATASET_BQ_PATH],\n",
|
||||
")\n",
|
||||
"print(dataset.resource_name)"
|
||||
@@ -1280,12 +1317,11 @@
|
||||
" \"product\": \"categorical\",\n",
|
||||
" \"holiday\": \"categorical\",\n",
|
||||
"}\n",
|
||||
"available_at_forecast_columns_ = [\n",
|
||||
"available_at_forecast_columns = [\n",
|
||||
" \"date\",\n",
|
||||
" \"advertisement\",\n",
|
||||
" \"holiday\",\n",
|
||||
"]\n",
|
||||
"available_at_forecast_columns = available_at_forecast_columns_ # @param {type: \"raw\"}\n",
|
||||
"] # @param {type: \"raw\"}\n",
|
||||
"unavailable_at_forecast_columns = [\"sales\"] # @param {type: \"raw\"}\n",
|
||||
"time_series_attribute_columns = [\"store\", \"product\"] # @param {type: \"raw\"}\n",
|
||||
"context_window = 30 # @param {type: \"integer\"}\n",
|
||||
@@ -1301,7 +1337,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL_DISPLAY_NAME = f\"forecasting-demo-model_{UUID}\"\n",
|
||||
"MODEL_DISPLAY_NAME = f\"forecasting-demo-model_{TIMESTAMP}\"\n",
|
||||
"\n",
|
||||
"training_job = aiplatform.AutoMLForecastingTrainingJob(\n",
|
||||
" display_name=MODEL_DISPLAY_NAME,\n",
|
||||
@@ -1337,7 +1373,7 @@
|
||||
"\n",
|
||||
"The `run` method, when completed, returns the `Model` resource.\n",
|
||||
"\n",
|
||||
"The execution of the training pipeline may take up to **one hour**. You can learn about the pricing for Vertex AI Forecasting [here](https://cloud.google.com/vertex-ai/pricing#tabular-data)."
|
||||
"The execution of the training pipeline will take up to **one hour**. You can learn about the pricing for Vertex Forecasting [here](https://cloud.google.com/vertex-ai/pricing#tabular-data)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1364,7 +1400,6 @@
|
||||
" budget_milli_node_hours=budget_milli_node_hours,\n",
|
||||
" model_display_name=MODEL_DISPLAY_NAME,\n",
|
||||
" predefined_split_column_name=split_column,\n",
|
||||
" window_stride_length=window_stride_length,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -1421,7 +1456,7 @@
|
||||
"\n",
|
||||
"Now that you have backtesting metrics from both models, you can compare the two side-by-side.\n",
|
||||
"\n",
|
||||
"Since the sales in this dataset were a function of covariates, we should expect the MAE, RMSE, and MAPE to be lower when using Vertex AI Forecasting. The BQML ARIMA_PLUS evaluation metrics show the relative impact of including these additional features in a model."
|
||||
"Since the sales in this dataset were a function of covariates, we should expect the MAE, RMSE, and MAPE to be lower when using Vertex Forecasting. The BQML ARIMA_PLUS evaluation metrics show the relative impact of including these additional features in a model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1443,7 +1478,7 @@
|
||||
"source": [
|
||||
"## Send a batch prediction request\n",
|
||||
"\n",
|
||||
"The following section shows how you can send a batch prediction to your Vertex AI Forecasting model in case you want to compare the models at serving time."
|
||||
"The following section shows how you can send a batch prediction to your Vertex Forecasting model in case you want to compare the models at serving time."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1475,7 +1510,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_prediction_job = model.batch_predict(\n",
|
||||
" job_display_name=f\"forecasting_demo_predictions_{UUID}\",\n",
|
||||
" job_display_name=f\"forecasting_demo_predictions_{TIMESTAMP}\",\n",
|
||||
" bigquery_source=PREDICTION_DATASET_BQ_PATH,\n",
|
||||
" instances_format=\"bigquery\",\n",
|
||||
" bigquery_destination_prefix=f\"bq://{vertex_dataset_path}\",\n",
|
||||
@@ -1496,7 +1531,7 @@
|
||||
"\n",
|
||||
"Next, wait for the batch job to complete. Alternatively, you can set the parameter `sync` to `True` in the `batch_predict()` method to block until the batch prediction job is completed.\n",
|
||||
"\n",
|
||||
"The execution of the prediction pipeline may take up to 30 minutes.\n"
|
||||
"The execution of the prediction pipeline will take up to 30 minutes.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1563,7 +1598,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"Click the link below to view Vertex AI Forecasting predictions:\")\n",
|
||||
"print(\"Click the link below to view Vertex Forecasting predictions:\")\n",
|
||||
"print(\n",
|
||||
" get_data_studio_link(\n",
|
||||
" batch_prediction_bq_input_uri=actuals_table,\n",
|
||||
@@ -1581,7 +1616,7 @@
|
||||
"id": "cleanup:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Clean up Vertex AI and BigQuery resources\n",
|
||||
"## Clean up Vertex and BigQuery resources\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",
|
||||
|
||||
@@ -68,7 +68,18 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "02b9af111927"
|
||||
"id": "dataset:covid,forecast"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial a time series dataset containing samples drawn from the Iowa Liquor Retail Sales dataset. Data were made available by the Iowa Department of Commerce. It is provided under the Creative Commons Zero v1.0 Universal license. For more details, see: https://console.cloud.google.com/marketplace/product/iowa-department-of-commerce/iowa-liquor-sales. This dataset does not require any feature engineering. The version of the dataset you will use in this tutorial is stored in BigQuery."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "objective:automl,training,batch_prediction"
|
||||
},
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
@@ -86,18 +97,7 @@
|
||||
"- Create a `Vertex AI Dataset` resource.\n",
|
||||
"- Train an `AutoML` tabular forecasting `Model` resource.\n",
|
||||
"- Obtain the evaluation metrics for the `Model` resource.\n",
|
||||
"- Make a batch prediction."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "dataset:covid,forecast"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial is a time series dataset containing samples drawn from the Iowa Liquor Retail Sales dataset. Data is made available by the Iowa Department of Commerce. It is provided under the Creative Commons Zero v1.0 Universal license. For more details, see: https://console.cloud.google.com/marketplace/product/iowa-department-of-commerce/iowa-liquor-sales. This dataset does not require any feature engineering. The version of the dataset you use in this tutorial is stored in BigQuery."
|
||||
"- Make a batch prediction.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -128,38 +128,29 @@
|
||||
"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",
|
||||
"If you are using Colab or Workbench AI Notebooks, your environment already meets 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",
|
||||
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
|
||||
"\n",
|
||||
"* The Google Cloud SDK\n",
|
||||
"* Git\n",
|
||||
"* Python 3\n",
|
||||
"* virtualenv\n",
|
||||
"* Jupyter notebook running in a virtual environment with Python 3\n",
|
||||
"- The Cloud Storage SDK\n",
|
||||
"- Git\n",
|
||||
"- Python 3\n",
|
||||
"- virtualenv\n",
|
||||
"- Jupyter notebook running in a virtual environment with Python 3\n",
|
||||
"\n",
|
||||
"The Google Cloud guide to [Setting up a Python development\n",
|
||||
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
|
||||
"installation guide](https://jupyter.org/install) provide detailed instructions\n",
|
||||
"for meeting these requirements. The following steps provide a condensed set of\n",
|
||||
"instructions:\n",
|
||||
"The Cloud Storage guide to [Setting up a Python development environment](https://cloud.google.com/python/setup) and the [Jupyter installation guide](https://jupyter.org/install) provide detailed instructions for meeting these requirements. The following steps provide a condensed set of instructions:\n",
|
||||
"\n",
|
||||
"1. [Install and initialize the Cloud SDK.](https://cloud.google.com/sdk/docs/)\n",
|
||||
"1. [Install and initialize the SDK](https://cloud.google.com/sdk/docs/).\n",
|
||||
"\n",
|
||||
"1. [Install Python 3.](https://cloud.google.com/python/setup#installing_python)\n",
|
||||
"2. [Install Python 3](https://cloud.google.com/python/setup#installing_python).\n",
|
||||
"\n",
|
||||
"1. [Install\n",
|
||||
" virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv)\n",
|
||||
" and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
|
||||
"3. [Install virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv) and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
|
||||
"\n",
|
||||
"1. To install Jupyter, run `pip3 install jupyter` on the\n",
|
||||
"command-line in a terminal shell.\n",
|
||||
"4. To install Jupyter, run `pip3 install jupyter` on the command-line in a terminal shell.\n",
|
||||
"\n",
|
||||
"1. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
|
||||
"5. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
|
||||
"\n",
|
||||
"1. Open this notebook in the Jupyter Notebook Dashboard."
|
||||
"6. Open this notebook in the Jupyter Notebook Dashboard.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -170,7 +161,7 @@
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the following packages required to execute this notebook. "
|
||||
"Install the latest version of Vertex AI SDK for Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -184,7 +175,7 @@
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# The Vertex AI Workbench Notebook product has specific requirements\n",
|
||||
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
|
||||
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
|
||||
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
|
||||
" \"/opt/deeplearning/metadata/env_version\"\n",
|
||||
")\n",
|
||||
@@ -194,7 +185,8 @@
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q"
|
||||
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
|
||||
"! pip3 install --upgrade tensorflow $USER_FLAG -q"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -205,7 +197,7 @@
|
||||
"source": [
|
||||
"### Restart the kernel\n",
|
||||
"\n",
|
||||
"After you install the additional packages, you need to restart the notebook kernel so it can find the packages."
|
||||
"Once you've installed the additional packages, you need to restart the notebook kernel so it can find the packages."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -216,7 +208,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
@@ -227,48 +218,34 @@
|
||||
" app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0e3cab0cc491"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "before_you_begin:nogpu"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### GPU runtime\n",
|
||||
"\n",
|
||||
"This tutorial does not require a GPU runtime.\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",
|
||||
"2. [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",
|
||||
"3. [Enable the following APIs: Vertex AI APIs, Compute Engine APIs, and Cloud Storage.](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com,compute_component,storage-component.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",
|
||||
"4. 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",
|
||||
"5. 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": "1460fd744366"
|
||||
},
|
||||
"source": [
|
||||
"#### 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`."
|
||||
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$`."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -317,7 +294,7 @@
|
||||
"#### 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",
|
||||
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
|
||||
"\n",
|
||||
"- Americas: `us-central1`\n",
|
||||
"- Europe: `europe-west4`\n",
|
||||
@@ -325,7 +302,7 @@
|
||||
"\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)."
|
||||
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -348,9 +325,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."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -361,16 +338,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\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -381,31 +351,23 @@
|
||||
"source": [
|
||||
"### 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",
|
||||
"**If you are using Workbench AI Notebooks**, your environment is already authenticated. Skip this step.\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",
|
||||
"**If you are using Colab**, run the cell below and follow the instructions 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",
|
||||
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
|
||||
"\n",
|
||||
"2. Click **Create service account**.\n",
|
||||
"**Click Create service account**.\n",
|
||||
"\n",
|
||||
"3. In the **Service account name** field, enter a name, and\n",
|
||||
" click **Create**.\n",
|
||||
"In the **Service account name** field, enter a name, and 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",
|
||||
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex 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",
|
||||
"Click Create. A JSON file that contains your key downloads to your 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.\n"
|
||||
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -476,9 +438,9 @@
|
||||
},
|
||||
"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_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
|
||||
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -498,7 +460,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION -p $PROJECT_ID $BUCKET_URI"
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -527,6 +489,9 @@
|
||||
"id": "setup_vars"
|
||||
},
|
||||
"source": [
|
||||
"### Set up variables\n",
|
||||
"\n",
|
||||
"Next, set up some variables used throughout the tutorial.\n",
|
||||
"### Import libraries and define constants"
|
||||
]
|
||||
},
|
||||
@@ -538,10 +503,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import urllib\n",
|
||||
"\n",
|
||||
"import google.cloud.aiplatform as aiplatform\n",
|
||||
"from google.cloud import bigquery"
|
||||
"import google.cloud.aiplatform as aiplatform"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -627,7 +589,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset = aiplatform.TimeSeriesDataset.create(\n",
|
||||
" display_name=\"iowa_liquor_sales_train\" + \"_\" + UUID,\n",
|
||||
" display_name=\"iowa_liquor_sales_train\" + \"_\" + TIMESTAMP,\n",
|
||||
" bq_source=[TRAINING_DATASET_BQ_PATH],\n",
|
||||
")\n",
|
||||
"\n",
|
||||
@@ -687,7 +649,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL_DISPLAY_NAME = f\"iowa-liquor-sales-forecast-model_{UUID}\"\n",
|
||||
"MODEL_DISPLAY_NAME = f\"iowa-liquor-sales-forecast-model_{TIMESTAMP}\"\n",
|
||||
"\n",
|
||||
"training_job = aiplatform.AutoMLForecastingTrainingJob(\n",
|
||||
" display_name=MODEL_DISPLAY_NAME,\n",
|
||||
@@ -810,7 +772,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"batch_predict_bq_output_dataset_name = f\"iowa_liquor_sales_predictions_{UUID}\"\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from google.cloud import bigquery\n",
|
||||
"\n",
|
||||
"batch_predict_bq_output_dataset_name = f\"iowa_liquor_sales_predictions_{TIMESTAMP}\"\n",
|
||||
"batch_predict_bq_output_dataset_path = \"{}.{}\".format(\n",
|
||||
" PROJECT_ID, batch_predict_bq_output_dataset_name\n",
|
||||
")\n",
|
||||
@@ -854,7 +820,7 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"batch_prediction_job = model.batch_predict(\n",
|
||||
" job_display_name=f\"iowa_liquor_sales_forecasting_predictions_{UUID}\",\n",
|
||||
" job_display_name=f\"iowa_liquor_sales_forecasting_predictions_{TIMESTAMP}\",\n",
|
||||
" bigquery_source=PREDICTION_DATASET_BQ_PATH,\n",
|
||||
" instances_format=\"bigquery\",\n",
|
||||
" bigquery_destination_prefix=batch_predict_bq_output_uri_prefix,\n",
|
||||
@@ -935,6 +901,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import urllib\n",
|
||||
"\n",
|
||||
"tables = client.list_tables(batch_predict_bq_output_dataset_path)\n",
|
||||
"\n",
|
||||
"prediction_table_id = \"\"\n",
|
||||
@@ -1044,6 +1012,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set this to true only if you'd like to delete your bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"\n",
|
||||
"# Delete dataset\n",
|
||||
"dataset.delete()\n",
|
||||
"\n",
|
||||
@@ -1056,9 +1027,6 @@
|
||||
"# Delete batch prediction job\n",
|
||||
"batch_prediction_job.delete()\n",
|
||||
"\n",
|
||||
"# Set this to true only if you'd like to delete your bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil rm -r $BUCKET_URI"
|
||||
]
|
||||
|
||||
@@ -33,18 +33,16 @@
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/feature_store/sdk-feature-store.ipynb\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/feature_store/sdk-feature-store.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/feature_store/sdk-feature-store.ipynb\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/feature_store/sdk-feature-store.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/feature_store/sdk-feature-store.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/notebook_template.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",
|
||||
@@ -79,6 +77,8 @@
|
||||
"\n",
|
||||
"- `Vertex AI Feature Store`\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Create featurestore, entity type, and feature resources.\n",
|
||||
@@ -393,7 +393,7 @@
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench notebooks**, your environment is already\n",
|
||||
"authenticated."
|
||||
"authenticated. Skip this step."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -822,7 +822,7 @@
|
||||
"source": [
|
||||
"## Import Feature Values\n",
|
||||
"\n",
|
||||
"You need to import feature values before you can use them for online/offline serving. In this step, you learn how to import feature values by ingesting the values from Cloud Storage. You can also import feature values from BigQuery or a Pandas dataframe.\n"
|
||||
"You need to import feature values before you can use them for online/offline serving. In this step, you learn how to import feature values by ingesting the values from GCS (Google Cloud Storage). You can also import feature values from BigQuery or a Pandas dataframe.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1025,7 +1025,7 @@
|
||||
"source": [
|
||||
"### Read one entity per request\n",
|
||||
"\n",
|
||||
"With the Vertex AI SDK, it is easy to read feature values of one entity. By default, the SDK will return the latest value of each feature, meaning the feature values with the most recent timestamp.\n",
|
||||
"With the Python SDK, it is easy to read feature values of one entity. By default, the SDK will return the latest value of each feature, meaning the feature values with the most recent timestamp.\n",
|
||||
"\n",
|
||||
"To read feature values, specify the entity type ID and features to read. By default all the features of an entity type will be selected. The response will output and display the selected entity type ID and the selected feature values as a Pandas dataframe."
|
||||
]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -32,24 +32,17 @@
|
||||
"# Vertex AI: Vertex AI Migration: AutoML Text Sentiment Analysis\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/UJ8 Vertex SDK AutoML Text Sentiment Analysis.ipynb\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/ai-platform-samples/blob/master/vertex-ai-samples/tree/master/notebooks/official/migration/UJ8%20Vertex%20SDK%20AutoML%20Text%20Sentiment%20Analysis.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/UJ8 Vertex SDK AutoML Text Sentiment Analysis.ipynb\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/ai-platform-samples/blob/master/vertex-ai-samples/tree/master/notebooks/official/migration/UJ8%20Vertex%20SDK%20AutoML%20Text%20Sentiment%20Analysis.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/UJ8 Vertex SDK AutoML Text Sentiment Analysis.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/>"
|
||||
]
|
||||
@@ -176,7 +169,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip3 install --upgrade tensorflow $USER_FLAG"
|
||||
"if os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! pip3 install --upgrade tensorflow $USER_FLAG"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -61,7 +61,7 @@
|
||||
"\n",
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This tutorial demonstrates how to train a model with BigQuery ML and upload it on Vertex AI Model Registry, then make batch predictions.\n"
|
||||
"This tutorial demonstrates how to train a model with BigQuery ML and upload it on Vertex AI model registry, then make batch predictions.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -77,13 +77,15 @@
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"\n",
|
||||
"- `Vertex AI Model Registry`\n",
|
||||
"- `Vertex AI Model` resources \n",
|
||||
"- `Vertex AI Endpoint` resources\n",
|
||||
"- `Vertex AI Prediction`\n",
|
||||
"- `BigQuery ML`\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Train a model with `BigQuery ML`\n",
|
||||
"- Train a model with `BQML`\n",
|
||||
"- Upload the model to `Vertex AI Model Registry` \n",
|
||||
"- Create a `Vertex AI Endpoint` resource\n",
|
||||
"- Deploy the `Model` resource to the `Endpoint` resource\n",
|
||||
@@ -130,7 +132,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",
|
||||
"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",
|
||||
@@ -175,7 +185,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "2b4ef9b72d43"
|
||||
},
|
||||
@@ -194,7 +204,8 @@
|
||||
"if IS_WORKBENCH_NOTEBOOK:\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade google-cloud-aiplatform google-cloud-bigquery pyarrow {USER_FLAG} -q"
|
||||
"! pip3 install --upgrade google-cloud-aiplatform {USER_FLAG} -q\n",
|
||||
"! pip3 install --upgrade google-cloud-bigquery {USER_FLAG} -q"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -382,7 +393,15 @@
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
|
||||
"authenticated.\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",
|
||||
@@ -524,7 +543,15 @@
|
||||
"id": "PKQD2e0eMg3M"
|
||||
},
|
||||
"source": [
|
||||
"### Create BigQuery dataset resource\n",
|
||||
"### Create BigQuery dataset resource"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "BnXOpvs2MmzF"
|
||||
},
|
||||
"source": [
|
||||
"First, you create an empty dataset resource in your project."
|
||||
]
|
||||
},
|
||||
@@ -539,7 +566,17 @@
|
||||
"BQ_DATASET_NAME = \"penguins\" + UUID\n",
|
||||
"DATASET_QUERY = f\"\"\"CREATE SCHEMA {BQ_DATASET_NAME}\"\"\"\n",
|
||||
"\n",
|
||||
"job = bqclient.query(DATASET_QUERY)\n",
|
||||
"job = bqclient.query(DATASET_QUERY)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "59bf85366baf"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"job.result()\n",
|
||||
"print(job.state)"
|
||||
]
|
||||
@@ -551,7 +588,7 @@
|
||||
},
|
||||
"source": [
|
||||
"## Train BigQuery ML model and upload it to Vertex AI Model Registry\n",
|
||||
"Next, you create and train a `BigQuery ML` tabular regression model from the public dataset penguins and store the model in your project `Vertex AI Model Registry` using the `CREATE MODEL` statement. The model configuration is specified in the `OPTIONS` statement as follows:\n",
|
||||
"Next, you create and train a BQML tabular regression model from the public dataset penguins and store the model in your project `Vertex AI Model Registry` using the `CREATE MODEL` statement. The model configuration is specified in the `OPTIONS` statement as follows:\n",
|
||||
"\n",
|
||||
"- `model_type`: The type and archictecture of tabular model to train, e.g., LOGISTIC_REG.\n",
|
||||
"\n",
|
||||
@@ -594,7 +631,6 @@
|
||||
"id": "eee158e2a375"
|
||||
},
|
||||
"source": [
|
||||
"### Create BigQuery ML Model\n",
|
||||
"Create the BigQuery ML model using the query above and the BigQuery client that you created previously:"
|
||||
]
|
||||
},
|
||||
@@ -769,7 +805,15 @@
|
||||
"id": "C39qOaBHZI1G"
|
||||
},
|
||||
"source": [
|
||||
"## Batch Prediction on the BigQuery ML model\n",
|
||||
"## Batch Prediction on the BQML model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "UBffk3GyaPY3"
|
||||
},
|
||||
"source": [
|
||||
"Here you request batch predictions directly from the BigQuery ML model; you don't need to deploy the model to an endpoint. For data types that support both batch and online predictions, use batch predictions when you don't require an immediate response and want to process accumulated data by using a single request.\n",
|
||||
"\n",
|
||||
"Learn more abount <a href=\"https://cloud.google.com/bigquery-ml/docs/reference/standard-sql/bigqueryml-syntax-predict\" target=\"_blank\">The ML.PREDICT function</a>"
|
||||
|
||||
-2191
File diff suppressed because it is too large
Load Diff
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 38 KiB |
+169
-196
@@ -38,13 +38,13 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/notebooks/blob/main/official/pipelines/google_cloud_pipeline_components_model_upload_predict_evaluate.ipynb\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/notebooks/blob/master/official/pipelines/google_cloud_pipeline_components_model_upload_predict_evaluate.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/ai/platform/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/notebooks/blob/main/official/pipelines/google_cloud_pipeline_components_model_upload_predict_evaluate.ipynb\"><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://github.com/GoogleCloudPlatform/vertex-ai-samples/notebooks/blob/master/official/pipelines/google_cloud_pipeline_components_model_upload_predict_evaluate.ipynb\">\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
@@ -63,6 +63,19 @@
|
||||
"This notebook shows how to use the components defined in [`google_cloud_pipeline_components`](https://github.com/kubeflow/pipelines/tree/master/components/google-cloud) in conjunction with an experimental `evaluation` method, to build a [Vertex AI Pipelines](https://cloud.google.com/vertex-ai/docs/pipelines) workflow that uploads a tabular custom model as a `Model` resource, creates a `BatchPredictionJob` resource, and evaluates the `Model` resource with the `BatchPredictionJob` results to create an evaluation `system.Metrics` artifact."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "dataset:bikes_weather,lrg"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial is part of the [safe driver prediction Kaggle competition](https://www.kaggle.com/c/porto-seguro-safe-driver-prediction/overview). The model has been trained on this data, and ground truth will be used for evaluation.\n",
|
||||
"\n",
|
||||
"The dataset predicts the whether or not a claim was filed for the policy holder."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -73,12 +86,6 @@
|
||||
"\n",
|
||||
"In this tutorial, you evaluate a custom model using a pipeline with components from `google_cloud_pipeline_components` and a custom pipeline component you build.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"\n",
|
||||
"- Vertex AI Pipelines\n",
|
||||
"- Vertex AI Model Registry\n",
|
||||
"- Vertex AI Batch Prediction\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Upload a pre-trained model as a `Model` resource.\n",
|
||||
@@ -87,19 +94,6 @@
|
||||
"- Compare the evaluation metrics to a threshold.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "dataset:bikes_weather,lrg"
|
||||
},
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial is part of the [safe driver prediction Kaggle competition](https://www.kaggle.com/c/porto-seguro-safe-driver-prediction/overview). The model has been trained on this data, and ground truth is used for evaluation.\n",
|
||||
"\n",
|
||||
"The dataset predicts the whether or not a claim was filed for the policy holder."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -128,38 +122,29 @@
|
||||
"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.\n",
|
||||
"If you are using Colab or Google Cloud Notebook, your environment already meets 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",
|
||||
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
|
||||
"\n",
|
||||
"* The Google Cloud SDK\n",
|
||||
"* Git\n",
|
||||
"* Python 3\n",
|
||||
"* virtualenv\n",
|
||||
"* Jupyter notebook running in a virtual environment with Python 3\n",
|
||||
"- The Cloud Storage SDK\n",
|
||||
"- Git\n",
|
||||
"- Python 3\n",
|
||||
"- virtualenv\n",
|
||||
"- Jupyter notebook running in a virtual environment with Python 3\n",
|
||||
"\n",
|
||||
"The Google Cloud guide to [Setting up a Python development\n",
|
||||
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
|
||||
"installation guide](https://jupyter.org/install) provide detailed instructions\n",
|
||||
"for meeting these requirements. The following steps provide a condensed set of\n",
|
||||
"instructions:\n",
|
||||
"The Cloud Storage guide to [Setting up a Python development environment](https://cloud.google.com/python/setup) and the [Jupyter installation guide](https://jupyter.org/install) provide detailed instructions for meeting these requirements. The following steps provide a condensed set of instructions:\n",
|
||||
"\n",
|
||||
"1. [Install and initialize the Cloud SDK.](https://cloud.google.com/sdk/docs/)\n",
|
||||
"1. [Install and initialize the SDK](https://cloud.google.com/sdk/docs/).\n",
|
||||
"\n",
|
||||
"1. [Install Python 3.](https://cloud.google.com/python/setup#installing_python)\n",
|
||||
"2. [Install Python 3](https://cloud.google.com/python/setup#installing_python).\n",
|
||||
"\n",
|
||||
"1. [Install\n",
|
||||
" virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv)\n",
|
||||
" and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
|
||||
"3. [Install virtualenv](Ihttps://cloud.google.com/python/setup#installing_and_using_virtualenv) and create a virtual environment that uses Python 3.\n",
|
||||
"\n",
|
||||
"1. To install Jupyter, run `pip3 install jupyter` on the\n",
|
||||
"command-line in a terminal shell.\n",
|
||||
"4. Activate that environment and run `pip3 install Jupyter` in a terminal shell to install Jupyter.\n",
|
||||
"\n",
|
||||
"1. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
|
||||
"5. Run `jupyter notebook` on the command line in a terminal shell to launch Jupyter.\n",
|
||||
"\n",
|
||||
"1. Open this notebook in the Jupyter Notebook Dashboard."
|
||||
"6. Open this notebook in the Jupyter Notebook Dashboard.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -170,7 +155,7 @@
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the latest version of Vertex AI and google-cloud-pipeline-components SDK for Python."
|
||||
"Install the latest version of Vertex AI SDK for Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -183,21 +168,34 @@
|
||||
"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",
|
||||
"# Google Cloud Notebook\n",
|
||||
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
|
||||
" USER_FLAG = \"--user\"\n",
|
||||
"else:\n",
|
||||
" USER_FLAG = \"\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade google-cloud-aiplatform \\\n",
|
||||
" google-cloud-pipeline-components \\\n",
|
||||
" kfp $USER_FLAG -q"
|
||||
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "install_gcpc"
|
||||
},
|
||||
"source": [
|
||||
"Install the latest GA version of *google-cloud-pipeline-components* library as well."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "wJkgLcdCBfb8"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip3 install --upgrade google-cloud-pipeline-components $USER_FLAG\n",
|
||||
"! pip3 install --upgrade kfp $USER_FLAG"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -208,7 +206,7 @@
|
||||
"source": [
|
||||
"### Restart the kernel\n",
|
||||
"\n",
|
||||
"After you install the additional packages, you need to restart the notebook kernel so it can find the packages."
|
||||
"Once you've installed the additional packages, you need to restart the notebook kernel so it can find the packages."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -219,7 +217,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
@@ -236,8 +233,6 @@
|
||||
"id": "check_versions"
|
||||
},
|
||||
"source": [
|
||||
"### Check package versions\n",
|
||||
"\n",
|
||||
"Check the versions of the packages you installed. "
|
||||
]
|
||||
},
|
||||
@@ -253,48 +248,34 @@
|
||||
"! python3 -c \"import google_cloud_pipeline_components; print('google_cloud_pipeline_components version: {}'.format(google_cloud_pipeline_components.__version__))\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "d6a00c14b087"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "before_you_begin:nogpu"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### GPU runtime\n",
|
||||
"\n",
|
||||
"This tutorial does not require a GPU runtime.\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",
|
||||
"2. [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 APIs, Compute Engine APIs, and Cloud Storage.](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com,compute_component,storage-component.googleapis.com)\n",
|
||||
"3. [Enable the Vertex AI APIs, Compute Engine APIs, and Cloud Storage.](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com,compute_component,storage-component.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",
|
||||
"4. [The Google Cloud SDK](https://cloud.google.com/sdk) is already installed in Google Cloud Notebook.\n",
|
||||
"\n",
|
||||
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
|
||||
"5. 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": "50756f65354f"
|
||||
},
|
||||
"source": [
|
||||
"#### 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`."
|
||||
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$`."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -343,7 +324,7 @@
|
||||
"#### 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",
|
||||
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
|
||||
"\n",
|
||||
"- Americas: `us-central1`\n",
|
||||
"- Europe: `europe-west4`\n",
|
||||
@@ -351,7 +332,7 @@
|
||||
"\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)."
|
||||
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -362,10 +343,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\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -374,9 +352,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."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -387,16 +365,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\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -407,31 +378,23 @@
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
|
||||
"authenticated. \n",
|
||||
"**If you are using Google Cloud Notebook**, your environment is already authenticated. Skip this step.\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",
|
||||
"**If you are using Colab**, run the cell below and follow the instructions 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",
|
||||
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
|
||||
"\n",
|
||||
"2. Click **Create service account**.\n",
|
||||
"**Click Create service account**.\n",
|
||||
"\n",
|
||||
"3. In the **Service account name** field, enter a name, and\n",
|
||||
" click **Create**.\n",
|
||||
"In the **Service account name** field, enter a name, and 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",
|
||||
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex 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",
|
||||
"Click Create. A JSON file that contains your key downloads to your 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."
|
||||
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -450,11 +413,8 @@
|
||||
"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 on Google Cloud Notebook, then don't execute this code\n",
|
||||
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
|
||||
" if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth as google_auth\n",
|
||||
"\n",
|
||||
@@ -490,8 +450,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\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -502,9 +461,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"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -524,7 +482,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION -p $PROJECT_ID $BUCKET_URI"
|
||||
"! gsutil mb -l $REGION $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -544,7 +502,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil ls -al $BUCKET_URI"
|
||||
"! gsutil ls -al $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -553,11 +511,9 @@
|
||||
"id": "set_service_account"
|
||||
},
|
||||
"source": [
|
||||
"### Service Account\n",
|
||||
"#### Service Account\n",
|
||||
"\n",
|
||||
"You use a service account to create Vertex AI Pipeline jobs.\n",
|
||||
"\n",
|
||||
"If you do not want to use your project's Compute Engine service account, set `SERVICE_ACCOUNT` to another service account ID."
|
||||
"**If you don't know your service account**, try to get your service account using `gcloud` command by executing the second cell below."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -584,19 +540,23 @@
|
||||
" 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",
|
||||
" # Get your GCP project id from gcloud\n",
|
||||
" shell_output = !gcloud auth list 2>/dev/null\n",
|
||||
" SERVICE_ACCOUNT = shell_output[2].replace(\"*\", \"\").strip()\n",
|
||||
" print(\"Service Account:\", SERVICE_ACCOUNT)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "9ca2fb92cb31"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"shell_output[2].replace(\"*\", \"\").strip()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -616,9 +576,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectCreator $BUCKET_URI\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectCreator $BUCKET_NAME\n",
|
||||
"\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectViewer $BUCKET_URI"
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectViewer $BUCKET_NAME"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -627,6 +587,9 @@
|
||||
"id": "setup_vars"
|
||||
},
|
||||
"source": [
|
||||
"### Set up variables\n",
|
||||
"\n",
|
||||
"Next, set up some variables used throughout the tutorial.\n",
|
||||
"### Import libraries and define constants"
|
||||
]
|
||||
},
|
||||
@@ -638,17 +601,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import kfp\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"from google_cloud_pipeline_components.experimental.evaluation import \\\n",
|
||||
" ModelEvaluationOp as evaluation_op\n",
|
||||
"from google_cloud_pipeline_components.types import artifact_types\n",
|
||||
"from google_cloud_pipeline_components.v1.batch_predict_job import \\\n",
|
||||
" ModelBatchPredictOp as batch_prediction_op\n",
|
||||
"from google_cloud_pipeline_components.v1.model import \\\n",
|
||||
" ModelUploadOp as model_upload_op\n",
|
||||
"from kfp.v2 import compiler\n",
|
||||
"from kfp.v2.components import importer_node\n",
|
||||
"import google.cloud.aiplatform as aip\n",
|
||||
"from kfp.v2.dsl import Input, Metrics, component"
|
||||
]
|
||||
},
|
||||
@@ -658,6 +611,8 @@
|
||||
"id": "pipeline_constants"
|
||||
},
|
||||
"source": [
|
||||
"#### Vertex AI Pipelines constants\n",
|
||||
"\n",
|
||||
"Setup up the following constant for Vertex AI Pipelines:"
|
||||
]
|
||||
},
|
||||
@@ -669,7 +624,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PIPELINE_ROOT = \"{}/pipeline_root/safe_driver\".format(BUCKET_URI)"
|
||||
"PIPELINE_ROOT = \"{}/pipeline_root/safe_driver\".format(BUCKET_NAME)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -678,7 +633,7 @@
|
||||
"id": "init_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"### Initialize Vertex AI SDK for Python\n",
|
||||
"## Initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
|
||||
]
|
||||
@@ -691,7 +646,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI, location=REGION)"
|
||||
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME, location=REGION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -702,7 +657,7 @@
|
||||
"source": [
|
||||
"## Create component for comparing evalution metrics to a threshold\n",
|
||||
"\n",
|
||||
"First, you create your own component that takes the evaluation metrics artifact as input, checks the threshold and return yes/no decision. It is used in a subsequent dsl.Condition() to decide whether the model should proceed to the next step i.e., online deployment.\n",
|
||||
"First, you create your own component that will take as input the evaluation metrics artifact and make a comparison to a threshold and return a yes/no decision that could be used in a subsequent dsl.Condition() to decide whether the model should proceed to the next step -- e.g., online deployment.\n",
|
||||
"\n",
|
||||
"The component takes the following parameters:\n",
|
||||
"\n",
|
||||
@@ -735,6 +690,7 @@
|
||||
" data = json.load(f)\n",
|
||||
"\n",
|
||||
" slices = data[\"slicedMetrics\"]\n",
|
||||
" # print(\"# slices\", len(slices))\n",
|
||||
"\n",
|
||||
" metrics = slices[0][\"metrics\"][\"classification\"]\n",
|
||||
" # print(\"METRIC KEYS\", metrics.keys())\n",
|
||||
@@ -756,7 +712,7 @@
|
||||
"\n",
|
||||
"Next, define the pipeline.\n",
|
||||
"\n",
|
||||
"[`google_cloud_pipeline_components`](https://github.com/kubeflow/pipelines/tree/master/components/google-cloud) components used to define the pipeline are: upload the model, run batch prediction, and evaluate the model with the given predictions.\n",
|
||||
"Then, [`google_cloud_pipeline_components`](https://github.com/kubeflow/pipelines/tree/master/components/google-cloud) components are used to define the rest of the pipeline: upload the model, run batch prediction, and evaluate the model with the given predictions.\n",
|
||||
"\n",
|
||||
"View the definition of the [upload model component](https://github.com/kubeflow/pipelines/blob/master/components/google-cloud/google_cloud_pipeline_components/aiplatform/model/upload_model/component.yaml).\n",
|
||||
"\n",
|
||||
@@ -773,17 +729,27 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import kfp\n",
|
||||
"from google_cloud_pipeline_components.experimental.evaluation import \\\n",
|
||||
" ModelEvaluationOp as evaluation_op\n",
|
||||
"from google_cloud_pipeline_components.types import artifact_types\n",
|
||||
"from google_cloud_pipeline_components.v1.batch_predict_job import \\\n",
|
||||
" ModelBatchPredictOp as batch_prediction_op\n",
|
||||
"from google_cloud_pipeline_components.v1.model import \\\n",
|
||||
" ModelUploadOp as model_upload_op\n",
|
||||
"from kfp.v2.components import importer_node\n",
|
||||
"\n",
|
||||
"DATA_URIS = [\n",
|
||||
" \"gs://cloud-samples-data/vertex-ai/dataset-management/datasets/safe_driver/dataset_safe_driver_train_10k.csv\"\n",
|
||||
"]\n",
|
||||
"MODEL_URI = \"gs://cloud-samples-data/vertex-ai/google-cloud-aiplatform-ci-artifacts/models/safe_driver/model\"\n",
|
||||
"# Create working dir\n",
|
||||
"WORKING_DIR = f\"{PIPELINE_ROOT}/{UUID}\"\n",
|
||||
"MODEL_DISPLAY_NAME = f\"safe-driver-{UUID}\"\n",
|
||||
"BATCH_PREDICTION_DISPLAY_NAME = f\"batch-prediction-on-pipelines-model-{UUID}\"\n",
|
||||
"WORKING_DIR = f\"{PIPELINE_ROOT}/{TIMESTAMP}\"\n",
|
||||
"MODEL_DISPLAY_NAME = f\"safe-driver-{TIMESTAMP}\"\n",
|
||||
"BATCH_PREDICTION_DISPLAY_NAME = f\"batch-prediction-on-pipelines-model-{TIMESTAMP}\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@kfp.dsl.pipeline(name=\"upload-evaluate-\" + UUID)\n",
|
||||
"@kfp.dsl.pipeline(name=\"upload-evaluate-\" + TIMESTAMP)\n",
|
||||
"def pipeline(\n",
|
||||
" metric: str,\n",
|
||||
" threshold: float,\n",
|
||||
@@ -863,6 +829,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from kfp.v2 import compiler # noqa: F811\n",
|
||||
"\n",
|
||||
"compiler.Compiler().compile(\n",
|
||||
" pipeline_func=pipeline,\n",
|
||||
" package_path=\"evaluation_demo_pipeline.json\",\n",
|
||||
@@ -888,9 +856,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"DISPLAY_NAME = \"safe_driver\" + UUID\n",
|
||||
"DISPLAY_NAME = \"safe_driver\" + TIMESTAMP\n",
|
||||
"\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
"job = aip.PipelineJob(\n",
|
||||
" display_name=DISPLAY_NAME,\n",
|
||||
" template_path=\"evaluation_demo_pipeline.json\",\n",
|
||||
" pipeline_root=PIPELINE_ROOT,\n",
|
||||
@@ -911,7 +879,7 @@
|
||||
"source": [
|
||||
"Click on the generated link to see your run in the Cloud Console.\n",
|
||||
"\n",
|
||||
"In the UI, the nodes of pipeline DAG expand or collapse when you click on them."
|
||||
"In the UI, many of the pipeline DAG nodes will expand or collapse when you click on them."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1016,7 +984,7 @@
|
||||
"id": "delete_pipeline"
|
||||
},
|
||||
"source": [
|
||||
"### Delete pipeline job\n",
|
||||
"### Delete a pipeline job\n",
|
||||
"\n",
|
||||
"After a pipeline job is completed, you can delete the pipeline job with the method `delete()`. Prior to completion, a pipeline job can be canceled with the method `cancel()`."
|
||||
]
|
||||
@@ -1038,16 +1006,16 @@
|
||||
"id": "cleanup:pipelines"
|
||||
},
|
||||
"source": [
|
||||
"## Cleaning up\n",
|
||||
"# Cleaning up\n",
|
||||
"\n",
|
||||
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
|
||||
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
|
||||
"\n",
|
||||
"Otherwise, you can delete the individual resources you created in this tutorial.\n",
|
||||
"Otherwise, you can delete the individual resources you created in this tutorial -- *Note:* this is auto-generated and not all resources may be applicable for this tutorial:\n",
|
||||
"\n",
|
||||
"- Model\n",
|
||||
"- Batch Job\n",
|
||||
"- Cloud Storage Bucket (Set `delete_bucket` to **True** to delete the Cloud Storage bucket)."
|
||||
"- Cloud Storage Bucket"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1058,30 +1026,35 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"delete_bucket = False\n",
|
||||
"delete_model = True\n",
|
||||
"delete_batchjob = True\n",
|
||||
"delete_bucket = True\n",
|
||||
"\n",
|
||||
"# Delete the created model\n",
|
||||
"models = aiplatform.Model.list(\n",
|
||||
" filter=f\"display_name={MODEL_DISPLAY_NAME}\", order_by=\"create_time\"\n",
|
||||
")\n",
|
||||
"if len(models) > 0:\n",
|
||||
" model = models[0]\n",
|
||||
" model.delete()\n",
|
||||
" print(\"Deleted model:\", model)\n",
|
||||
"try:\n",
|
||||
" if delete_model and \"MODEL_DISPLAY_NAME\" in globals():\n",
|
||||
" models = aip.Model.list(\n",
|
||||
" filter=f\"display_name={MODEL_DISPLAY_NAME}\", order_by=\"create_time\"\n",
|
||||
" )\n",
|
||||
" model = models[0]\n",
|
||||
" aip.Model.delete(model)\n",
|
||||
" print(\"Deleted model:\", model)\n",
|
||||
"except Exception as e:\n",
|
||||
" print(e)\n",
|
||||
"\n",
|
||||
"# Delete the created batch-prediction job\n",
|
||||
"batch_predictions = aiplatform.BatchPredictionJob.list(\n",
|
||||
" filter=f\"display_name={BATCH_PREDICTION_DISPLAY_NAME}\",\n",
|
||||
" order_by=\"create_time\",\n",
|
||||
")\n",
|
||||
"if len(batch_predictions) > 0:\n",
|
||||
" batch_prediction = batch_predictions[0]\n",
|
||||
" batch_prediction.delete()\n",
|
||||
" print(\"Deleted batch prediction job:\", batch_prediction)\n",
|
||||
"try:\n",
|
||||
" if delete_batchjob and \"BATCH_PREDICTION_DISPLAY_NAME\" in globals():\n",
|
||||
" batch_predictions = aip.BatchPredictionJob.list(\n",
|
||||
" filter=f\"display_name={BATCH_PREDICTION_DISPLAY_NAME}\",\n",
|
||||
" order_by=\"create_time\",\n",
|
||||
" )\n",
|
||||
" batch_prediction = batch_predictions[0]\n",
|
||||
" aip.BatchPredictionJob.delete(batch_prediction)\n",
|
||||
" print(\"Deleted batch prediction job:\", batch_prediction)\n",
|
||||
"except Exception as e:\n",
|
||||
" print(e)\n",
|
||||
"\n",
|
||||
"# Delete the Cloud Storage bucket\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
"if delete_bucket and \"BUCKET_NAME\" in globals():\n",
|
||||
" ! gsutil rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user