mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
17 KiB
17 KiB
In [ ]:
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.In [ ]:
import os
# The Google Cloud Notebook product has specific requirements
IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists("/opt/deeplearning/metadata/env_version")
# Google Cloud Notebook requires dependencies to be installed with '--user'
USER_FLAG = ""
if IS_GOOGLE_CLOUD_NOTEBOOK:
USER_FLAG = "--user"In [ ]:
! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatformIn [ ]:
import os
if not os.getenv("IS_TESTING"):
# Restart the kernel after pip3 installs
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)In [ ]:
import os
import sys
# If you are running this notebook in Colab, run this cell and follow the
# instructions to authenticate your GCP account. This provides access to your
# Cloud Storage bucket and lets you submit training jobs and prediction
# requests.
# If on Google Cloud Notebooks, then don't execute this code
if not os.path.exists("/opt/deeplearning/metadata/env_version"):
if "google.colab" in sys.modules:
from google.colab import auth as google_auth
google_auth.authenticate_user()
# If you are running this notebook locally, replace the string below with the
# path to your service account key and run this cell to authenticate your GCP
# account.
elif not os.getenv("IS_TESTING"):
%env GOOGLE_APPLICATION_CREDENTIALS ''In [ ]:
import datetime
import json
from google.cloud import aiplatform_v1beta1In [ ]:
# Fill in your project ID and region
REGION = "[region]" # @param {type:"string"}
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
# These will be automatically filled in.
STUDY_DISPLAY_NAME = "{}_study_{}".format(
PROJECT_ID.replace("-", ""), datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
) # @param {type: 'string'}
ENDPOINT = REGION + "-aiplatform.googleapis.com"
PARENT = "projects/{}/locations/{}".format(PROJECT_ID, REGION)
print("ENDPOINT: {}".format(ENDPOINT))
print("REGION: {}".format(REGION))
print("PARENT: {}".format(PARENT))
# If you don't know your project ID, you might be able to get your project ID
# using gcloud command by executing the second cell below.
if PROJECT_ID == "" or PROJECT_ID is None or PROJECT_ID == "[your-project-id]":
# Get your GCP project id from gcloud
shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null
PROJECT_ID = shell_output[0]
print("Project ID:", PROJECT_ID)
! gcloud config set project $PROJECT_IDIn [ ]:
# Parameter Configuration
param_r = {"parameter_id": "r", "double_value_spec": {"min_value": 0, "max_value": 1}}
param_theta = {
"parameter_id": "theta",
"double_value_spec": {"min_value": 0, "max_value": 1.57},
}
# Objective Metrics
metric_y1 = {"metric_id": "y1", "goal": "MINIMIZE"}
# Objective Metrics
metric_y2 = {"metric_id": "y2", "goal": "MAXIMIZE"}
# Put it all together in a study configuration
study = {
"display_name": STUDY_DISPLAY_NAME,
"study_spec": {
"algorithm": "RANDOM_SEARCH",
"parameters": [
param_r,
param_theta,
],
"metrics": [metric_y1, metric_y2],
},
}
print(json.dumps(study, indent=2, sort_keys=True))In [ ]:
vizier_client = aiplatform_v1beta1.VizierServiceClient(
client_options=dict(api_endpoint=ENDPOINT)
)
study = vizier_client.create_study(parent=PARENT, study=study)
STUDY_ID = study.name
print("STUDY_ID: {}".format(STUDY_ID))In [ ]:
import math
# r * sin(theta)
def Metric1Evaluation(r, theta):
"""Evaluate the first metric on the trial."""
return r * math.sin(theta)
# r * cos(theta)
def Metric2Evaluation(r, theta):
"""Evaluate the second metric on the trial."""
return r * math.cos(theta)
def CreateMetrics(trial_id, r, theta):
print(("=========== Start Trial: [{}] =============").format(trial_id))
# Evaluate both objective metrics for this trial
y1 = Metric1Evaluation(r, theta)
y2 = Metric2Evaluation(r, theta)
print(
"[r = {}, theta = {}] => y1 = r*sin(theta) = {}, y2 = r*cos(theta) = {}".format(
r, theta, y1, y2
)
)
metric1 = {"metric_id": "y1", "value": y1}
metric2 = {"metric_id": "y2", "value": y2}
# Return the results for this trial
return [metric1, metric2]In [ ]:
client_id = "client1" # @param {type: 'string'}
suggestion_count_per_request = 5 # @param {type: 'integer'}
max_trial_id_to_stop = 4 # @param {type: 'integer'}
print("client_id: {}".format(client_id))
print("suggestion_count_per_request: {}".format(suggestion_count_per_request))
print("max_trial_id_to_stop: {}".format(max_trial_id_to_stop))In [ ]:
trial_id = 0
while int(trial_id) < max_trial_id_to_stop:
suggest_response = vizier_client.suggest_trials(
{
"parent": STUDY_ID,
"suggestion_count": suggestion_count_per_request,
"client_id": client_id,
}
)
for suggested_trial in suggest_response.result().trials:
trial_id = suggested_trial.name.split("/")[-1]
trial = vizier_client.get_trial({"name": suggested_trial.name})
if trial.state in ["COMPLETED", "INFEASIBLE"]:
continue
for param in trial.parameters:
if param.parameter_id == "r":
r = param.value
elif param.parameter_id == "theta":
theta = param.value
print("Trial : r is {}, theta is {}.".format(r, theta))
vizier_client.add_trial_measurement(
{
"trial_name": suggested_trial.name,
"measurement": {
"metrics": CreateMetrics(suggested_trial.name, r, theta)
},
}
)
response = vizier_client.complete_trial(
{"name": suggested_trial.name, "trial_infeasible": False}
)In [ ]:
optimal_trials = vizier_client.list_optimal_trials({"parent": STUDY_ID})
print("optimal_trials: {}".format(optimal_trials))In [ ]:
vizier_client.delete_study({"name": STUDY_ID})
Run in Colab
View on GitHub