Package: explainable-ai-sdk
Explainable AI SDKThis is a Python SDK forGoogle Cloud Explainable AI, an
explanation service that provides insight into machine learning models deployed
onAI Platform. The Explainable AI SDK
helps to visualize explanation results, and to defineexplanation metadatafor
the explanation service.Explanation metadata tells the explanation service which of your model's inputs
and outputs to use for your explanation request. The SDK has metadata builders
that help you to build and save an explanation metadata file before you deploy
your model to AI Platform.The Explainable AI SDK also helps you to visualize feature attribution results
on models deployed to AI Platform.InstallationThe Explainable AI SDK supports models built with:Python 3.7 and laterTensorFlow 1.15 or TensorFlow 2.x.The Explainable AI SDK is preinstalled onGoogle Cloud AI Platform Notebooksimages.For other platforms:Make sure that you haveinstalled Cloud SDK. In
order to communicate with Cloud AI Platform, the Explainable AI SDK requires
a shell environment with Cloud SDK installed.Install the Explainable AI SDK:pipinstallexplainable-ai-sdkMetadata BuildersAfter you build your model, you use a metadata builder to create your
explanation metadata. This produces a JSON file that is used for model
deployment on AI Platform.There are different metadata builders for TensorFlow 1.x and 2.x in
their respective folders.TensorFlow 2.xFor TensorFlow 2.x, there is one metadata builder that takes aSavedModel, and uploads both your model and metadata file to Cloud Storage.For example:fromexplainable_ai_sdk.metadata.tf.v2importSavedModelMetadataBuilderbuilder=SavedModelMetadataBuilder(model_path)builder.save_model_with_metadata('gs://my_bucket/model')# Save the model and the metadata.TensorFlow 1.xFor TensorFlow 1.x, the Explainable AI SDK supports models built with Keras,
Estimator and the low-level TensorFlow API. There is a different metadata
builder for each of these three TensorFlow APIs. An example usage for a Keras
model would be as follows:fromexplainable_ai_sdk.metadata.tf.v1importKerasGraphMetadataBuildermy_model=keras.models.Sequential()my_model.add(keras.layers.Dense(32,activation='relu',input_dim=10))my_model.add(keras.layers.Dense(32,activation='relu'))my_model.add(keras.layers.Dense(1,activation='sigmoid'))builder=KerasGraphMetadataBuilder(my_model)builder.save_model_with_metadata('gs://my_bucket/model')# Save the model and the metadata.For examples using the Estimator and TensorFlow Core builders, refer to thev1 README file.Making Predict and Explain CallsThe Explainable AI SDK includes a model interface to help you communicate with
the deployed model more easily. With this interface, you can callpredict()andexplain()functions to get predictions and explanations for the provided
data points, respectively.Here is an example snippet for using the model interface:project_id="example_project"model_name="example_model"version_name="v1"m=explainable_ai_sdk.load_model_from_ai_platform(project_id,model_name,version_name)instances=[]# ... steps for preparing instancespredictions=m.predict(instances)explanations=m.explain(instances)Explanation, Attribution, and VisualizationTheexplain()function returns a list ofExplanationobjects --
oneExplanationper input instance. This object makes it easier to interact
with returned attributions. You can use theExplanationobject to get
feature importance and raw attributions, and to visualize attributions.Note: Currently, thefeature_importance()andas_tensors()functions
only work on tabular models, due to the limited payload size. We are working on
making both functions available for image models.**Get feature importanceThefeature_importance()function returns the imporance of each feature
based on feature attributions. Note that if a feature has more than one
dimension, the importance is calculated based on the aggregation.explanations[0].feature_importance()Get raw attributionsTo get feature attributions over each dimension, use theas_tensors()function to return the raw attributions as tensors.explanations[0].as_tensors()Visualize attributionsTheExplanationclass allows you to visualize feature attributions directly.
For both image and tabular models, you can callvisualize_attributions()to see feature attributions.explantions[0].visualize_attributions()Here is an example visualization:CaveatsThis library works with (and depends) on either major version of TensorFlow.Do not import themetadata/tf/v1andmetadata/tf/v2folders in the
same Python runtime. If you do, there may be unintended side effects of mixing
TensorFlow 1.x and 2.x behavior.Explainable AI documentationFor more information about Explainable AI, refer to theExplainable AI documentation.LicenseAll files in this repository are under theApache License, Version 2.0unless noted otherwise.Note:We are not accepting contributions at this time.