-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added explain tabular samples (#348)
* Added tabular explanation sample * Cleaned up mocks * Ran linter * Fixed mock and added explanation printing * Added more verbose explanations * Fixed endpoint fixture * Fixed linting issues
- Loading branch information
Showing
8 changed files
with
267 additions
and
9 deletions.
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: | ||
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, | ||
) |