Explainable AI — AI Explanation Service by google cloud (Part 1)

Nazrul Miya
7 min readJun 30, 2021

This is my 4th story and this time is about ‘explainable AI’. The story will demonstrate using google cloud-backed explainable ai. I have separated the article into two parts — the first starts with practical implementation and the second part explains the concept behind google cloud’s explainable AI. By the end of these stories, one can get a clear idea about what is explainable AI ? how google cloud’s AI explanation service helps to explain your model.

If you like the story please hit a clap and share :) Also, follow me for more such stories. :).

High-level steps

I will start with building a TensorFlow classification model, which will then be deployed into the google cloud’s AI platform. Finally, will get predictions and explanations from the model.

Example Dataset: Loan prediction dataset. The dataset captures information on loans taken by customers. The task is to build a binary classification model to predict if a loan can be granted, given the customer’s details.

1. Building the TensorFlow model

Note: Main focus is on explanations of the model, hence just built a basic TensorFlow model without much emphasizing preprocessing, analysis, hyperparameter tuning, etc.

Now a TensorFlow model is ready to predict loan status when customer details are provided.

The next step is to deploy the model into google cloud service — AI Platform.

2. Deploy TensorFlow model to google AI platform

Prerequisites:

  1. One must have a valid google cloud(GC) Account. ( create a gcp account )
  2. A project must be created in GC. (create a project )
  3. A billing account must be linked to the project. ( link billing to project )
  4. Enable ‘AI Platform prediction & Training’ API service for the created project, so that the AI platform can be used in the project. ( enable API service )
  5. A storage bucket is created for storing the TensorFlow model. (create a google cloud storage)

I have followed the above prerequisite steps and finally, I have a project id (nm-xai-demo) and storage bucket (xai-demo-bucket) to proceed with the deployment.

The following steps are involved in deploying a model to the AI platform.

a. Set project resource

In google cloud, the root of the resource's hierarchy is a project, all other resources are mapped to the root. To deploy the TensorFlow model, I will access the google ai platform service from the notebook, and the ai platform service is enabled for the project I created in prerequisite step 2. Hence I need to set the right project in the notebook as an environment variable, it ensures that all the google cloud resources are being accessed from the notebook mapped to the set project.

a. Authenticate a google cloud account from the notebook

To access google cloud resources such as AI platform, storage, etc, the google cloud account needs to be authenticated. Below code, snippet authenticates google cloud account through google oAth access token.

Verification code can be generated upon clicking the link and then by authenticating through a google cloud account

c. Save the model into the storage bucket

The next step is to save the TensorFlow model into the created storage bucket ‘xai-demo-bucket’.

Now the TensorFlow model is stored at the storage bucket location ‘gs://xai-demo-bucket/xai/models/assets’

d. Create a model resource in the AI platform

The next step is to create a model resource in the AI platform which is a container of all versions of the trained models. Each version is linked to the model file saved in the storage bucket.

Model resource creation in AI platform

d. Create a version resource in the AI platform

A version is to be linked to a model saved in a storage bucket. Will create a version providing the model path.

The region, model path in google cloud storage, and model name are to be provided while creating a version of the model. Optionally frameworks such as TensorFlow/scikit-learn, python version, framework version can be set while creating the version.

Creating a version

Google AI explanations use different types of explanations such as “sampled-shapley”, “integrated-gradients”, or “xrai”. This explanation type is provided while creating the version, through the parameter ‘explanation-method’. I will explain these explanation methods theory in the next story. For the practical demo, I used the explanation method “integrated-gradients”.

For more run time versions check here

For more machine types check here

Now, let's verify the model resource and version resource created in the last two steps,

Version consfiguartions

Let's summarise what I did so far — created a TensorFlow model to predict loan status given customer details, the model is then saved in google cloud storage bucket, next a model resource named ‘loan_pred_model’ is created in google cloud AI platform, and a version ‘v1’ is linked to the model resource.

The next step is to get an explanation from the TensorFlow model which deployed in the AI platform.

3. Get an explanation from the model

Explanation from the TensorFlow model deployed in AI platform can be achieved through google provided python sdk ‘explainable-ai-sdk’

a. Download explainable ai SDK

explainable-ai-sdk 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. it is an interface which helps to communicate more easily with models deployed in AI platform.

The Explainable AI SDK supports models built with:

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

b. Build model metadata

an explanation metadata JSON file ‘explanation_metadata.json’ must be created and saved in the same path where the TensorFlow model is saved in google cloud storage. The JSON should contain information about the TensorFlow model such as the input of the model, output by the model, any baseline, etc. explainable-ai-sdk uses the configurations in the metadata file to interact with the model and provide explanations.

the explainable-ai-sdk also helps to create this explanation metadata file for us.

importing explainable_ai_sdk
Generating model metadata

c. Save model metadata

The next step is to save the generated model metadata into a cloud storage bucket, where the TensorFlow model is saved.

saving metadata to the bucket

The function save_metadata() creates an explanation_metadata.json file and writes metadata configurations into that file.

d. Configure explanation type

Google AI explanations use different types of explanations such as “sampled-shapley”, “integrated-gradients”, or “xrai”. I will explain these explanation methods theory in the next story.

For the practical demo, let's use the explanation method “integrated-gradients”. The method has to be updated in the version resource ‘v1’.

If the explanation method has not been set while creating the version, then the parameter can be updated through an update request. The parameter has to be written in a YAML file and the file path to be provided in the update request.

Updating explanation method

e. Prepare test data

The aim is to get explanations from the model for given test data instances. Now it is time to prepare the test data instances for getting the explanations.

Loading and preprocessing test data instances

f. Load model resource from AI platform

The TensorFlow model was deployed in the AI platform by creating a model resource and a version resource. The version resource internally points to the actual TensorFlow model stored in the bucket.

To get predictions or explanations, this model resource has to be exported to the notebook instance.

loading ai platform model resource

g. Send explanation request

Next will send test data instances to get explanations objects. Sending 50 data instances hence will receive 50 explanations object.

explanation request

4. Understand explanations

Now I have received 50 explanations objects corresponds to 50 input test data instances. Let see how these explanations can be consumed.

I am stopping this story here. In the next story, I will explain how to use the explanations object and the core concept behind such explanations. The next story will give a clear idea about explaining your model by these explanations.

Thanks to Google Cloud AI Explanation Service

--

--