-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update the samples of hyperparameter tuning in the public doc (#…
…1600) * Update Hyperparameter tuning job samples * Update Hyperparameter tuning job samples 2 * feat: fix linting problems in the hyperparamter tuning job samples * feat: resolve conflict for the hyperparameter tuning samples * feat: fix lint problems Co-authored-by: Ivan Cheung <[email protected]>
- Loading branch information
Showing
10 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
samples/model-builder/cancel_hyperparameter_tuning_job_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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 | ||
# | ||
# 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. | ||
|
||
# [START aiplatform_sdk_cancel_hyperparameter_tuning_job_sample] | ||
from google.cloud import aiplatform | ||
|
||
|
||
def cancel_hyperparameter_tuning_job_sample( | ||
project: str, | ||
hyperparameter_tuning_job_id: str, | ||
location: str = "us-central1", | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
hpt_job = aiplatform.HyperparameterTuningJob.get( | ||
resource_name=hyperparameter_tuning_job_id, | ||
) | ||
|
||
hpt_job.cancel() | ||
|
||
|
||
# [END aiplatform_sdk_cancel_hyperparameter_tuning_job_sample] |
40 changes: 40 additions & 0 deletions
40
samples/model-builder/cancel_hyperparameter_tuning_job_sample_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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 | ||
# | ||
# 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. | ||
|
||
import cancel_hyperparameter_tuning_job_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_cancel_hyperparameter_tuning_job_sample( | ||
mock_sdk_init, | ||
mock_hyperparameter_tuning_job_get, | ||
mock_hyperparameter_tuning_job_cancel, | ||
): | ||
|
||
cancel_hyperparameter_tuning_job_sample.cancel_hyperparameter_tuning_job_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
hyperparameter_tuning_job_id=constants.HYPERPARAMETER_TUNING_JOB_ID, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
) | ||
|
||
mock_hyperparameter_tuning_job_get.assert_called_once_with( | ||
resource_name=constants.HYPERPARAMETER_TUNING_JOB_ID, | ||
) | ||
|
||
mock_hyperparameter_tuning_job_cancel.assert_called_once_with() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
samples/model-builder/create_hyperparameter_tuning_job_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# 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 | ||
# | ||
# 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. | ||
|
||
# [START aiplatform_sdk_create_hyperparameter_tuning_job_sample] | ||
from google.cloud import aiplatform | ||
|
||
from google.cloud.aiplatform import hyperparameter_tuning as hpt | ||
|
||
|
||
def create_hyperparameter_tuning_job_sample( | ||
project: str, | ||
location: str, | ||
staging_bucket: str, | ||
display_name: str, | ||
container_uri: str, | ||
): | ||
aiplatform.init(project=project, location=location, staging_bucket=staging_bucket) | ||
|
||
worker_pool_specs = [ | ||
{ | ||
"machine_spec": { | ||
"machine_type": "n1-standard-4", | ||
"accelerator_type": "NVIDIA_TESLA_K80", | ||
"accelerator_count": 1, | ||
}, | ||
"replica_count": 1, | ||
"container_spec": { | ||
"image_uri": container_uri, | ||
"command": [], | ||
"args": [], | ||
}, | ||
} | ||
] | ||
|
||
custom_job = aiplatform.CustomJob( | ||
display_name='custom_job', | ||
worker_pool_specs=worker_pool_specs, | ||
) | ||
|
||
hpt_job = aiplatform.HyperparameterTuningJob( | ||
display_name=display_name, | ||
custom_job=custom_job, | ||
metric_spec={ | ||
'loss': 'minimize', | ||
}, | ||
parameter_spec={ | ||
'lr': hpt.DoubleParameterSpec(min=0.001, max=0.1, scale='log'), | ||
'units': hpt.IntegerParameterSpec(min=4, max=128, scale='linear'), | ||
'activation': hpt.CategoricalParameterSpec(values=['relu', 'selu']), | ||
'batch_size': hpt.DiscreteParameterSpec(values=[128, 256], scale='linear') | ||
}, | ||
max_trial_count=128, | ||
parallel_trial_count=8, | ||
labels={'my_key': 'my_value'}, | ||
) | ||
|
||
hpt_job.run() | ||
|
||
print(hpt_job.resource_name) | ||
return hpt_job | ||
|
||
|
||
# [END aiplatform_sdk_create_hyperparameter_tuning_job_sample] |
59 changes: 59 additions & 0 deletions
59
samples/model-builder/create_hyperparameter_tuning_job_sample_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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 | ||
# | ||
# 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. | ||
|
||
from unittest.mock import ANY | ||
|
||
import create_hyperparameter_tuning_job_sample | ||
|
||
import test_constants as constants | ||
|
||
|
||
def test_create_hyperparameter_tuning_job_sample( | ||
mock_sdk_init, | ||
mock_custom_job, | ||
mock_get_custom_job, | ||
mock_get_hyperparameter_tuning_job, | ||
mock_run_hyperparameter_tuning_job, | ||
): | ||
|
||
create_hyperparameter_tuning_job_sample.create_hyperparameter_tuning_job_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
staging_bucket=constants.STAGING_BUCKET, | ||
display_name=constants.HYPERPARAMETER_TUNING_JOB_DISPLAY_NAME, | ||
container_uri=constants.CONTAINER_URI, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
staging_bucket=constants.STAGING_BUCKET, | ||
) | ||
|
||
mock_get_custom_job.assert_called_once_with( | ||
display_name=constants.CUSTOM_JOB_DISPLAY_NAME, | ||
worker_pool_specs=constants.CUSTOM_JOB_WORKER_POOL_SPECS, | ||
) | ||
|
||
mock_get_hyperparameter_tuning_job.assert_called_once_with( | ||
display_name=constants.HYPERPARAMETER_TUNING_JOB_DISPLAY_NAME, | ||
custom_job=mock_custom_job, | ||
metric_spec=constants.HYPERPARAMETER_TUNING_JOB_METRIC_SPEC, | ||
parameter_spec=ANY, | ||
max_trial_count=constants.HYPERPARAMETER_TUNING_JOB_MAX_TRIAL_COUNT, | ||
parallel_trial_count=constants.HYPERPARAMETER_TUNING_JOB_PARALLEL_TRIAL_COUNT, | ||
labels=constants.HYPERPARAMETER_TUNING_JOB_LABELS, | ||
) | ||
|
||
mock_run_hyperparameter_tuning_job.assert_called_once() |
34 changes: 34 additions & 0 deletions
34
samples/model-builder/delete_hyperparameter_tuning_job_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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 | ||
# | ||
# 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. | ||
|
||
# [START aiplatform_sdk_delete_hyperparameter_tuning_job_sample] | ||
from google.cloud import aiplatform | ||
|
||
|
||
def delete_hyperparameter_tuning_job_sample( | ||
project: str, | ||
hyperparameter_tuning_job_id: str, | ||
location: str = "us-central1", | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
hpt_job = aiplatform.HyperparameterTuningJob.get( | ||
resource_name=hyperparameter_tuning_job_id, | ||
) | ||
|
||
hpt_job.delete() | ||
|
||
|
||
# [END aiplatform_sdk_delete_hyperparameter_tuning_job_sample] |
40 changes: 40 additions & 0 deletions
40
samples/model-builder/delete_hyperparameter_tuning_job_sample_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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 | ||
# | ||
# 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. | ||
|
||
import delete_hyperparameter_tuning_job_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_delete_hyperparameter_tuning_job_sample( | ||
mock_sdk_init, | ||
mock_hyperparameter_tuning_job_get, | ||
mock_hyperparameter_tuning_job_delete, | ||
): | ||
|
||
delete_hyperparameter_tuning_job_sample.delete_hyperparameter_tuning_job_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
hyperparameter_tuning_job_id=constants.HYPERPARAMETER_TUNING_JOB_ID, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
) | ||
|
||
mock_hyperparameter_tuning_job_get.assert_called_once_with( | ||
resource_name=constants.HYPERPARAMETER_TUNING_JOB_ID, | ||
) | ||
|
||
mock_hyperparameter_tuning_job_delete.assert_called_once_with() |
34 changes: 34 additions & 0 deletions
34
samples/model-builder/get_hyperparameter_tuning_job_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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 | ||
# | ||
# 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. | ||
|
||
# [START aiplatform_sdk_get_hyperparameter_tuning_job_sample] | ||
from google.cloud import aiplatform | ||
|
||
|
||
def get_hyperparameter_tuning_job_sample( | ||
project: str, | ||
hyperparameter_tuning_job_id: str, | ||
location: str = "us-central1", | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
hpt_job = aiplatform.HyperparameterTuningJob.get( | ||
resource_name=hyperparameter_tuning_job_id, | ||
) | ||
|
||
return hpt_job | ||
|
||
|
||
# [END aiplatform_sdk_get_hyperparameter_tuning_job_sample] |
Oops, something went wrong.