Skip to main content

Helper library for CAIP explanations.

Project description

Explainable AI SDK

This is a Python SDK for Google Cloud Explainable AI, an explanation service that provides insight into machine learning models deployed on AI Platform. The Explainable AI SDK helps to visualize explanation results, and to define explanation metadata for 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.

Installation

The Explainable AI SDK supports models built with:

  • Python 3.7 and later
  • TensorFlow 1.15 or TensorFlow 2.x.

The Explainable AI SDK is preinstalled on Google Cloud AI Platform Notebooks images.

For other platforms:

  1. Make sure that you have installed Cloud SDK. In order to communicate with Cloud AI Platform, the Explainable AI SDK requires a shell environment with Cloud SDK installed.

  2. Install the Explainable AI SDK:

    pip install explainable-ai-sdk
    

Metadata Builders

After 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.x

For TensorFlow 2.x, there is one metadata builder that takes a SavedModel, and uploads both your model and metadata file to Cloud Storage.

For example:

from explainable_ai_sdk.metadata.tf.v2 import SavedModelMetadataBuilder
builder = SavedModelMetadataBuilder(
    model_path)
builder.save_model_with_metadata('gs://my_bucket/model')  # Save the model and the metadata.

TensorFlow 1.x

For 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:

from explainable_ai_sdk.metadata.tf.v1 import KerasGraphMetadataBuilder
my_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 the v1 README file.

Making Predict and Explain Calls

The Explainable AI SDK includes a model interface to help you communicate with the deployed model more easily. With this interface, you can call predict() and explain() 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 instances

predictions = m.predict(instances)
explanations = m.explain(instances)

Explanation, Attribution, and Visualization

The explain() function returns a list of Explanation objects -- one Explanation per input instance. This object makes it easier to interact with returned attributions. You can use the Explanation object to get feature importance and raw attributions, and to visualize attributions.

Note: Currently, the feature_importance() and as_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 importance

The feature_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 attributions

To get feature attributions over each dimension, use the as_tensors() function to return the raw attributions as tensors.

explanations[0].as_tensors()

Visualize attributions

The Explanation class allows you to visualize feature attributions directly. For both image and tabular models, you can call visualize_attributions() to see feature attributions.

explantions[0].visualize_attributions()

Here is an example visualization:

An attribution visualization for a tabular model

Caveats

  • This library works with (and depends) on either major version of TensorFlow.
  • Do not import the metadata/tf/v1 and metadata/tf/v2 folders 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 documentation

For more information about Explainable AI, refer to the Explainable AI documentation.

License

All files in this repository are under the Apache License, Version 2.0 unless noted otherwise.

Note: We are not accepting contributions at this time.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

explainable_ai_sdk-1.2.1.tar.gz (79.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

explainable_ai_sdk-1.2.1-py3-none-any.whl (113.3 kB view details)

Uploaded Python 3

File details

Details for the file explainable_ai_sdk-1.2.1.tar.gz.

File metadata

  • Download URL: explainable_ai_sdk-1.2.1.tar.gz
  • Upload date:
  • Size: 79.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.21.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.8

File hashes

Hashes for explainable_ai_sdk-1.2.1.tar.gz
Algorithm Hash digest
SHA256 3b85382a6222ca715171e278ed3fc92d078d9de5ac2d893dbe7b3b8fb223f85e
MD5 c45012dc6366679863e2aa1bac16f7f8
BLAKE2b-256 a5c5465100d61e953922802406262f48d102cf121ddd227906464a1387c1e89c

See more details on using hashes here.

File details

Details for the file explainable_ai_sdk-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: explainable_ai_sdk-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 113.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.21.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.8

File hashes

Hashes for explainable_ai_sdk-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8805be161315cc7c970bf2555c6799b65098f72babaed0e841d5570278b2247
MD5 651e9108393e1076dd31701daa597c6f
BLAKE2b-256 e75e314e3f3e527fe607d75f249a4071a1f5dd867e16af5130095067abccbe4b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page