-
Notifications
You must be signed in to change notification settings - Fork 354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added explain tabular samples #348
Merged
ivanmkc
merged 7 commits into
googleapis:master
from
ivanmkc:imkc--explain-tabular-sample
May 4, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f7b2d9f
Added tabular explanation sample
ivanmkc 76116c7
Cleaned up mocks
ivanmkc 8fb4947
Ran linter
ivanmkc 70ed28c
Fixed mock and added explanation printing
ivanmkc 8200fce
Added more verbose explanations
ivanmkc e526b5f
Fixed endpoint fixture
ivanmkc 3d7baa2
Fixed linting issues
ivanmkc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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,50 @@ | ||
# 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. | ||
|
||
from typing import Dict | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
# [START aiplatform_sdk_explain_tabular_sample] | ||
def explain_tabular_sample( | ||
project: str, location: str, endpoint_id: str, instance_dict: Dict | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
endpoint = aiplatform.Endpoint(endpoint_id) | ||
|
||
response = endpoint.explain(instances=[instance_dict], parameters={}) | ||
|
||
for explanation in response.explanations: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as GAPIC sample for consistency. |
||
print(" explanation") | ||
# Feature attributions. | ||
attributions = explanation.attributions | ||
for attribution in attributions: | ||
print(" attribution") | ||
print(" baseline_output_value:", attribution.baseline_output_value) | ||
print(" instance_output_value:", attribution.instance_output_value) | ||
print(" output_display_name:", attribution.output_display_name) | ||
print(" approximation_error:", attribution.approximation_error) | ||
print(" output_name:", attribution.output_name) | ||
output_index = attribution.output_index | ||
for output_index in output_index: | ||
print(" output_index:", output_index) | ||
|
||
for prediction in response.predictions: | ||
print(prediction) | ||
|
||
|
||
# [END aiplatform_sdk_explain_tabular_sample] |
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,39 @@ | ||
# 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. | ||
|
||
|
||
import explain_tabular_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_explain_tabular_sample( | ||
mock_sdk_init, mock_endpoint, mock_get_endpoint, mock_endpoint_explain | ||
): | ||
|
||
explain_tabular_sample.explain_tabular_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
endpoint_id=constants.ENDPOINT_NAME, | ||
instance_dict=constants.PREDICTION_TABULAR_INSTANCE, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
|
||
mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME,) | ||
|
||
mock_endpoint_explain.assert_called_once_with( | ||
instances=[constants.PREDICTION_TABULAR_INSTANCE], parameters={} | ||
) |
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 |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
import pytest | ||
|
||
import import_data_video_classification_sample | ||
|
||
import test_constants as constants | ||
|
||
|
||
|
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
70 changes: 70 additions & 0 deletions
70
samples/model-builder/upload_model_explain_tabular_managed_container_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,70 @@ | ||
# 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. | ||
|
||
from typing import Dict, Optional, Sequence | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
# [START aiplatform_sdk_upload_model_explain_tabular_managed_container_sample] | ||
def upload_model_explain_tabular_managed_container_sample( | ||
project, | ||
location, | ||
model_display_name: str, | ||
serving_container_image_uri: str, | ||
artifact_uri: Optional[str] = None, | ||
serving_container_predict_route: Optional[str] = None, | ||
serving_container_health_route: Optional[str] = None, | ||
description: Optional[str] = None, | ||
serving_container_command: Optional[Sequence[str]] = None, | ||
serving_container_args: Optional[Sequence[str]] = None, | ||
serving_container_environment_variables: Optional[Dict[str, str]] = None, | ||
serving_container_ports: Optional[Sequence[int]] = None, | ||
instance_schema_uri: Optional[str] = None, | ||
parameters_schema_uri: Optional[str] = None, | ||
prediction_schema_uri: Optional[str] = None, | ||
explanation_metadata: Optional[aiplatform.explain.ExplanationMetadata] = None, | ||
explanation_parameters: Optional[aiplatform.explain.ExplanationParameters] = None, | ||
sync: bool = True, | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
model = aiplatform.Model.upload( | ||
display_name=model_display_name, | ||
serving_container_image_uri=serving_container_image_uri, | ||
artifact_uri=artifact_uri, | ||
serving_container_predict_route=serving_container_predict_route, | ||
serving_container_health_route=serving_container_health_route, | ||
description=description, | ||
serving_container_command=serving_container_command, | ||
serving_container_args=serving_container_args, | ||
serving_container_environment_variables=serving_container_environment_variables, | ||
serving_container_ports=serving_container_ports, | ||
instance_schema_uri=instance_schema_uri, | ||
parameters_schema_uri=parameters_schema_uri, | ||
prediction_schema_uri=prediction_schema_uri, | ||
explanation_metadata=explanation_metadata, | ||
explanation_parameters=explanation_parameters, | ||
sync=sync, | ||
) | ||
|
||
model.wait() | ||
|
||
print(model.display_name) | ||
print(model.resource_name) | ||
return model | ||
|
||
|
||
# [END aiplatform_sdk_upload_model_explain_tabular_managed_container_sample] |
65 changes: 65 additions & 0 deletions
65
samples/model-builder/upload_model_explain_tabular_managed_container_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,65 @@ | ||
# 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. | ||
|
||
import test_constants as constants | ||
|
||
import upload_model_explain_tabular_managed_container_sample | ||
|
||
|
||
def test_upload_model_explain_tabular_managed_container_sample( | ||
mock_sdk_init, mock_model, mock_init_model, mock_upload_model | ||
): | ||
|
||
upload_model_explain_tabular_managed_container_sample.upload_model_explain_tabular_managed_container_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
model_display_name=constants.MODEL_NAME, | ||
serving_container_image_uri=constants.SERVING_CONTAINER_IMAGE_URI, | ||
artifact_uri=constants.MODEL_ARTIFACT_URI, | ||
serving_container_predict_route=constants.SERVING_CONTAINER_PREDICT_ROUTE, | ||
serving_container_health_route=constants.SERVING_CONTAINER_HEALTH_ROUTE, | ||
description=constants.DESCRIPTION, | ||
serving_container_command=constants.SERVING_CONTAINER_COMMAND, | ||
serving_container_args=constants.SERVING_CONTAINER_ARGS, | ||
serving_container_environment_variables=constants.SERVING_CONTAINER_ENVIRONMENT_VARIABLES, | ||
serving_container_ports=constants.SERVING_CONTAINER_PORTS, | ||
instance_schema_uri=constants.INSTANCE_SCHEMA_URI, | ||
parameters_schema_uri=constants.PARAMETERS_SCHEMA_URI, | ||
prediction_schema_uri=constants.PREDICTION_SCHEMA_URI, | ||
explanation_metadata=constants.EXPLANATION_METADATA, | ||
explanation_parameters=constants.EXPLANATION_PARAMETERS, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
|
||
mock_upload_model.assert_called_once_with( | ||
display_name=constants.MODEL_NAME, | ||
serving_container_image_uri=constants.SERVING_CONTAINER_IMAGE_URI, | ||
artifact_uri=constants.MODEL_ARTIFACT_URI, | ||
serving_container_predict_route=constants.SERVING_CONTAINER_PREDICT_ROUTE, | ||
serving_container_health_route=constants.SERVING_CONTAINER_HEALTH_ROUTE, | ||
description=constants.DESCRIPTION, | ||
serving_container_command=constants.SERVING_CONTAINER_COMMAND, | ||
serving_container_args=constants.SERVING_CONTAINER_ARGS, | ||
serving_container_environment_variables=constants.SERVING_CONTAINER_ENVIRONMENT_VARIABLES, | ||
serving_container_ports=constants.SERVING_CONTAINER_PORTS, | ||
instance_schema_uri=constants.INSTANCE_SCHEMA_URI, | ||
parameters_schema_uri=constants.PARAMETERS_SCHEMA_URI, | ||
prediction_schema_uri=constants.PREDICTION_SCHEMA_URI, | ||
explanation_metadata=constants.EXPLANATION_METADATA, | ||
explanation_parameters=constants.EXPLANATION_PARAMETERS, | ||
sync=True, | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a generic explain sample and doesn't necessarily need to be qualified as tabular.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sheet has me doing explain_tabular_sample, I assume it's used by the tech writers in such a sample.