forked from googleapis/python-aiplatform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy.py
More file actions
300 lines (267 loc) · 11.3 KB
/
study.py
File metadata and controls
300 lines (267 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# -*- coding: utf-8 -*-
# Copyright 2022 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
#
# http://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.
import copy
from typing import Optional, Collection, Type, TypeVar
from google.api_core import exceptions
from google.auth import credentials as auth_credentials
from google.cloud.aiplatform import base
from google.cloud.aiplatform import utils
from google.cloud.aiplatform import initializer
from google.cloud.aiplatform.vizier import client_abc
from google.cloud.aiplatform.vizier import pyvizier as vz
from google.cloud.aiplatform.vizier.trial import Trial
from google.cloud.aiplatform.compat.types import study as gca_study
_T = TypeVar("_T")
_LOGGER = base.Logger(__name__)
class Study(base.VertexAiResourceNounWithFutureManager, client_abc.StudyInterface):
"""Manage Study resource for Vertex Vizier."""
client_class = utils.VizierClientWithOverride
_resource_noun = "study"
_getter_method = "get_study"
_list_method = "list_studies"
_delete_method = "delete_study"
_parse_resource_name_method = "parse_study_path"
_format_resource_name_method = "study_path"
def __init__(
self,
study_id: str,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
):
"""Retrieves an existing managed study given a study resource name or a study id.
Example Usage:
study = aiplatform.Study(study_id = '12345678')
or
study = aiplatform.Study(study_id = 'projects/123/locations/us-central1/studies/12345678')
Args:
study_id (str):
Required. A fully-qualified study resource name or a study ID.
Example: "projects/123/locations/us-central1/studies/12345678" or "12345678" when
project and location are initialized or passed.
project (str):
Optional. Project to retrieve study from. If not set, project
set in aiplatform.init will be used.
location (str):
Optional. Location to retrieve study from. If not set, location
set in aiplatform.init will be used.
credentials (auth_credentials.Credentials):
Optional. Custom credentials to use to retrieve this Feature. Overrides
credentials set in aiplatform.init.
"""
base.VertexAiResourceNounWithFutureManager.__init__(
self,
project=project,
location=location,
credentials=credentials,
resource_name=study_id,
)
self._gca_resource = self._get_gca_resource(resource_name=study_id)
@classmethod
@base.optional_sync()
def create_or_load(
cls,
display_name: str,
problem: vz.ProblemStatement,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> client_abc.StudyInterface:
"""Creates a Study resource.
Example Usage:
sc = pyvizier.StudyConfig()
sc.algorithm = pyvizier.Algorithm.RANDOM_SEARCH
sc.metric_information.append(
pyvizier.MetricInformation(
name='pr-auc', goal=pyvizier.ObjectiveMetricGoal.MAXIMIZE))
root = sc.search_space.select_root()
root.add_float_param(
'learning_rate', 0.00001, 1.0, scale_type=pyvizier.ScaleType.LINEAR)
root.add_categorical_param('optimizer', ['adagrad', 'adam', 'experimental'])
study = aiplatform.Study.create_or_load(display_name='tuning_study', problem=sc)
Args:
display_name (str):
Required. A name to describe the Study. It's unique per study. An existing study
will be returned if the study has the same display name.
problem (vz.ProblemStatement):
Required. Configurations of the study. It defines the problem to create the study.
project (str):
Optional. Project to retrieve study from. If not set, project
set in aiplatform.init will be used.
location (str):
Optional. Location to retrieve study from. If not set, location
set in aiplatform.init will be used.
credentials (auth_credentials.Credentials):
Optional. Custom credentials to use to retrieve this Feature. Overrides
credentials set in aiplatform.init.
Returns:
StudyInterface - The created study resource object.
"""
project = initializer.global_config.project if not project else project
location = initializer.global_config.location if not location else location
credentials = (
initializer.global_config.credentials if not credentials else credentials
)
api_client = cls._instantiate_client(
location=location,
credentials=credentials,
)
study = gca_study.Study(
display_name=display_name, study_spec=problem.to_proto()
)
try:
study = api_client.create_study(
parent=initializer.global_config.common_location_path(
project,
location,
),
study=study,
)
except exceptions.AlreadyExists:
_LOGGER.info("The study is already created. Using existing study.")
study = api_client.lookup_study(
request={
"parent": initializer.global_config.common_location_path(
project,
location,
),
"display_name": display_name,
},
)
return Study(study.name)
def get_trial(self, uid: int) -> client_abc.TrialInterface:
"""Retrieves the trial under the study by given trial id.
Args:
uid (int): Required. Unique identifier of the trial to search.
Returns:
TrialInterface - The trial resource object.
"""
study_path_components = self._parse_resource_name(self.resource_name)
return Trial(
Trial._format_resource_name(
project=study_path_components["project"],
location=study_path_components["location"],
study=study_path_components["study"],
trial=uid,
),
credentials=self.credentials,
)
def trials(
self, trial_filter: Optional[vz.TrialFilter] = None
) -> Collection[client_abc.TrialInterface]:
"""Fetches a collection of trials.
Args:
trial_filter (int): Optional. A filter for the trials.
Returns:
Collection[TrialInterface] - A list of trials resource object belonging
to the study.
"""
list_trials_request = {"parent": self.resource_name}
trials_response = self.api_client.list_trials(request=list_trials_request)
return [
Trial._construct_sdk_resource_from_gapic(
trial,
project=self.project,
location=self.location,
credentials=self.credentials,
)
for trial in trials_response.trials
]
def optimal_trials(self) -> Collection[client_abc.TrialInterface]:
"""Returns optimal trial(s).
Returns:
Collection[TrialInterface] - A list of optimal trials resource object.
"""
list_optimal_trials_request = {"parent": self.resource_name}
optimal_trials_response = self.api_client.list_optimal_trials(
request=list_optimal_trials_request
)
return [
Trial._construct_sdk_resource_from_gapic(
trial,
project=self.project,
location=self.location,
credentials=self.credentials,
)
for trial in optimal_trials_response.optimal_trials
]
def materialize_study_config(self) -> vz.StudyConfig:
"""#Materializes the study config.
Returns:
StudyConfig - A deepcopy of StudyConfig from the study.
"""
study = self.api_client.get_study(
name=self.resource_name, credentials=self.credentials
)
return copy.deepcopy(vz.StudyConfig.from_proto(study.study_spec))
@classmethod
def from_uid(
cls: Type[_T],
uid: str,
project: Optional[str] = None,
location: Optional[str] = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> _T:
"""Fetches an existing study from the Vizier service.
Args:
uid (str): Required. Unique identifier of the study.
Returns:
StudyInterface - The study resource object.
"""
project = initializer.global_config.project if not project else project
location = initializer.global_config.location if not location else location
credentials = (
initializer.global_config.credentials if not credentials else credentials
)
return Study(
study_id=uid, project=project, location=location, credentials=credentials
)
def suggest(
self, *, count: Optional[int] = None, worker: str = ""
) -> Collection[client_abc.TrialInterface]:
"""Returns Trials to be evaluated by worker.
Args:
count (int): Optional. Number of suggestions.
worker (str): When new Trials are generated, their `assigned_worker` field is
populated with this worker. suggest() first looks for existing Trials
that are assigned to `worker`, before generating new ones.
Returns:
Collection[TrialInterface] - A list of suggested trial resource objects.
"""
suggest_trials_lro = self.api_client.suggest_trials(
request={
"parent": self.resource_name,
"suggestion_count": count,
"client_id": worker,
},
)
_LOGGER.log_action_started_against_resource_with_lro(
"Suggest", "study", self.__class__, suggest_trials_lro
)
_LOGGER.info(self.client_class.get_gapic_client_class())
trials = suggest_trials_lro.result()
_LOGGER.log_action_completed_against_resource("study", "suggested", self)
return [
Trial._construct_sdk_resource_from_gapic(
trial,
project=self.project,
location=self.location,
credentials=self.credentials,
)
for trial in trials.trials
]
def delete(self) -> None:
"""Deletes the study."""
self.api_client.delete_study(name=self.resource_name)