בדף הזה מוסבר איך לשלוח בקשות API ל-Agent Platform Vizier באמצעות Python. מידע על אופן הפעולה של Agent Platform Vizier זמין במאמר סקירה כללית על Agent Platform Vizier.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
אם אתם משתמשים במעטפת מקומית, צריך ליצור פרטי כניסה לאימות מקומי עבור חשבון המשתמש:
gcloud auth application-default login
אם אתם משתמשים ב-Cloud Shell, אתם לא צריכים לעשות את זה.
אם מוחזרת שגיאת אימות ואתם משתמשים בספק זהויות חיצוני (IdP), ודאו ש נכנסתם ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
- מתקינים את Agent Platform SDK ל-Python.
הגדרת קבועים
כדי להגדיר קבועים, מריצים את הפקודות הבאות ומחליפים את REGION ואת PROJECT_ID באזור ובמזהה הפרויקט.
יוצרים שם משלכם למחקר או משתמשים בערכים המוצעים.
import json
import datetime
from google.cloud import aiplatform
REGION = "REGION"
PROJECT_ID = "PROJECT_ID"
# The following placeholder variables are 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)
יצירת בקשות API
בקשות ה-API הבאות בשורת הפקודה כתובות ב-Python.
יצירת מחקר
מחקר הוא סדרה של ניסויים, או ניסיונות, שעוזרים לכם לבצע אופטימיזציה של היפרפרמטרים או פרמטרים.
בדוגמה הבאה, המטרה היא להגדיל את y = x^2
עם x בטווח [-10. 10]. בדוגמה הזו יש רק פרמטר אחד, והיא משתמשת בפונקציה מחושבת כדי להמחיש איך משתמשים ב-Agent Platform Vizier.
param_x = {
'parameter_id': 'x',
'double_value_spec': {
'min_value': -10.0,
'max_value': 10.0
}
}
metric_y = {
'metric_id': 'y',
'goal': 'MAXIMIZE'
}
study = {
'display_name': STUDY_DISPLAY_NAME,
'study_spec': {
'algorithm': 'RANDOM_SEARCH',
'parameters': [param_x],
'metrics': [metric_y],
}
}
כדי ליצור את המחקר באמצעות הגדרת המחקר, שולחים את הבקשה הבאה דרך VizierServiceClient. משתמשים בערך שמוחזר STUDY_NAME
כדי לשלוח שאילתה למחקר.
vizier_client = aiplatform.gapic.VizierServiceClient(client_options=dict(api_endpoint=ENDPOINT))
study = vizier_client.create_study(parent=PARENT, study=study)
STUDY_NAME = study.name
צפייה במחקר
אחרי שיוצרים את המחקר, אפשר למצוא אותו במסוףGoogle Cloud , בקטע Gemini Enterprise Agent Platform, בדף Experiments.
קבלת מחקר
כדי לקבל מחקר, שולחים את הבקשה הבאה.
vizier_client.get_study({'name': STUDY_NAME})
הצגת רשימת מחקרים
כדי להציג רשימה של מחקרים בפרויקט ובאזור ספציפיים, שולחים את הבקשה הבאה:
vizier_client.list_studies({'parent': PARENT})
קבלת הצעות לתקופות ניסיון
כדי לקבל הצעה לתקופת ניסיון מ-Agent Platform Vizier, צריך ליצור בקשה שמכילה SUGGEST_COUNT ו-CLIENT_ID. כדי להעביר את המידע הזה ל-Agent Platform Vizier, צריך לשלוח את הבקשה.
יוצרים את הבקשה באמצעות הפקודות הבאות. משנים את SUGGEST_COUNT למספר ההצעות שרוצים לקבל מכל בקשה.
suggest_response = vizier_client.suggest_trials({
'parent': STUDY_NAME,
'suggestion_count': SUGGEST_COUNT,
'client_id': CLIENT_ID
})
suggest_trials מתחילה פעולה ממושכת ליצירת תקופת הניסיון.
בתגובה נאמר ש-Agent Platform Vizier עובד על ההצעות לתקופת הניסיון.
כדי לחכות לתוצאה שמוחזרת, משתמשים בפונקציה result() .
suggest_response.result().trials
הפורמט הבא מציג דוגמה לניסיון. בגרסת הניסיון הזו מוצע להשתמש בערך 0.1 עבור הפרמטר x.
name: "TRIAL_ID"
state: ACTIVE
parameters {
parameter_id: "x"
value {
number_value: 0.1
}
}
start_time {
seconds: 1618011215
}
כדי לקבל את תקופת הניסיון, משתמשים בערך TRIAL_ID מהתגובה הקודמת:
vizier_client.get_trial({
'name': TRIAL_ID
})
הערכת התוצאות
אחרי שמקבלים את ההצעות לניסויים, צריך לבדוק כל ניסוי ולתעד כל תוצאה כמדידה.
לדוגמה, אם הפונקציה שאתם מנסים לבצע אופטימיזציה שלה היא y = x^2, אתם מעריכים את הפונקציה באמצעות הערך המוצע של הניסוי x.
אם משתמשים בערך המוצע 0.1, הפונקציה מחזירה את הערך y = 0.1 * 0.1, וכתוצאה מכך מתקבל הערך 0.01.
הוספת מדידה
אחרי שנבדוק את ההצעה שלך להגדרת מדידה בתקופת הניסיון, נוסיף את המדידה הזו לתקופת הניסיון.
משתמשים בפקודות הבאות כדי לאחסן את המדידה ולשלוח את הבקשה.
בדוגמה הזו, מחליפים את RESULT במדידה. אם הפונקציה שמבצעים לה אופטימיזציה היא y = x^2, והערך המוצע של x הוא 0.1, התוצאה היא 0.01.
vizier_client.add_trial_measurement({
'trial_name': TRIAL_ID,
'measurement': {
'metrics': [{'metric_id': 'y', 'value':RESULT }]
}
})
השלמת תקופת ניסיון
אחרי שמוסיפים את כל המדידות לתקופת הניסיון, צריך להשלים את תקופת הניסיון על ידי שליחת פקודה.
בסיום תקופת הניסיון, אפשר לשלוח פקודה להשלמת תקופת הניסיון בלבד, או לשלוח פקודה להוספת מדידה סופית ולהשלמת תקופת הניסיון.
ללא מדידה סופית
כדי להשלים תקופת ניסיון בלי להוסיף מדידה סופית, שולחים את הבקשה הבאה:
vizier_client.complete_trial({
'name': TRIAL_ID
})
עם מדידה סופית
כדי לסיים את תקופת הניסיון ולכלול מדידה סופית, משתמשים בפקודות הבאות ומחליפים את RESULT במדידה הסופית.
vizier_client.complete_trial({
'name': TRIAL_ID
'final_measurement': {
'metrics': [{'metric_id': 'y', 'value': RESULT}]
}
})
תעשה לי רשימת תקופות ניסיון
כדי להציג רשימה של ניסויים במחקר ספציפי, שולחים את הבקשה הבאה:
vizier_client.list_trials({
'parent': STUDY_NAME
})
אחרי שתסיימו את כל תקופות הניסיון שבהמתנה, תוכלו להתקשר למספר suggestTrials כדי לקבל הצעות נוספות ולחזור על תהליך ההערכה של תקופת הניסיון.
רשימת תקופות ניסיון אופטימליות
בדוגמה הבאה מוצג list_optimal_trials, שמחזיר את הניסויים האופטימליים לפי פרטו במחקר עם כמה יעדים, או את הניסויים האופטימליים במחקר עם יעד אחד:
vizier_client.list_optimal_trials({
'parent': STUDY_NAME
})
המאמרים הבאים
- אפשר לעיין בהפניית REST בנושא מחקרים.