<jupyter_start><jupyter_text>Copyright (c) Microsoft Corporation. All rights reserved.Licensed under the MIT License.  Example of integration with pytorch/captum_**This notebook showcases how create an interpret-community style explanation using captum to view it in the dashboard.**_ Table of Contents1. [Introduction](Introduction)1. [Setup](Setup)1. [Project](Project)1. [Run model explainer locally at training time](Explain)    1. Train a binary classification model    1. Explain the model        1. Generate global explanations        1. Generate local explanations1. [Visualize results](Visualize)1. [Next steps](Next)  1. IntroductionThis notebook illustrates how to integrate captum explanations with intepret-community visualization.        2. ProjectThe goal of this project is to run an IntegratedGradients explainer from captum and visualize it in the ExplanationDashboard.  3. Create a captum model (taken from their main page)<jupyter_code># Example taken from captum's main page
# https://captum.ai/
import numpy as np

import torch
import torch.nn as nn

from captum.attr import IntegratedGradients

class ToyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.lin1 = nn.Linear(3, 3)
        self.relu = nn.ReLU()
        self.lin2 = nn.Linear(3, 2)
        self.output = nn.Linear(2, 1)

        # initialize weights and biases
        self.lin1.weight = nn.Parameter(torch.arange(-4.0, 5.0).view(3, 3))
        self.lin1.bias = nn.Parameter(torch.zeros(1,3))
        self.lin2.weight = nn.Parameter(torch.arange(-3.0, 3.0).view(2, 3))
        self.lin2.bias = nn.Parameter(torch.ones(1,2))

    def forward(self, input):
        return self.output(self.lin2(self.relu(self.lin1(input))))


model = ToyModel()
model.eval()

# Fix the random seed to make computations deterministic

torch.manual_seed(123)
np.random.seed(123)<jupyter_output><empty_output><jupyter_text>Define input and baseline tensors<jupyter_code>input = torch.rand(100, 3)
baseline = torch.zeros(100, 3)
labels = np.random.rand(100)<jupyter_output><empty_output><jupyter_text>Run integrated gradients to get an explanation<jupyter_code>ig = IntegratedGradients(model)
attributions, delta = ig.attribute(input, baseline, target=0, return_convergence_delta=True)
# optionally print feature attributions
# print('IG Attributions:', attributions)<jupyter_output><empty_output><jupyter_text>Create an interpret-community style explanation<jupyter_code>from interpret_community.adapter import ExplanationAdapter
adapter = ExplanationAdapter(features=['A', 'B', 'C'])
global_explanation = adapter.create_global(attributions, evaluation_examples=np.array(input))
# Sorted SHAP values
print('ranked global importance values: {}'.format(global_explanation.get_ranked_global_values()))
# Corresponding feature names
print('ranked global importance names: {}'.format(global_explanation.get_ranked_global_names()))
# Feature ranks (based on original order of features)
print('global importance rank: {}'.format(global_explanation.global_importance_rank))
# Print out a dictionary that holds the sorted feature importance names and values
print('global importance rank: {}'.format(global_explanation.get_feature_importance_dict()))<jupyter_output><empty_output><jupyter_text>Explain overall model predictions as a collection of local (instance-level) explanations<jupyter_code># feature shap values for all features and all data points in the training data
# uncomment to view all local importance values:
# print('local importance values: {}'.format(global_explanation.local_importance_values))

# view ranked local importance values for the first row:
sorted_local_importance_values = global_explanation.get_ranked_local_values()[0]
sorted_local_importance_names = global_explanation.get_ranked_local_names()[0]

print('local importance values: {}'.format(sorted_local_importance_values))
print('local importance names: {}'.format(sorted_local_importance_names))<jupyter_output><empty_output><jupyter_text>4. VisualizeLoad the visualization dashboard<jupyter_code>from raiwidgets import ExplanationDashboard
from interpret_community.common.model_wrapper import wrap_model
from interpret_community.dataset.dataset_wrapper import DatasetWrapper
import pandas as pd
wrapped_model = wrap_model(model, DatasetWrapper(input), model_task='regression')
dataset = pd.DataFrame(np.array(input))
ExplanationDashboard(global_explanation, wrapped_model, dataset=dataset, true_y=labels)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09582963620230701, 'fraction_numerical': 0.01419698314108252, 'mean_word_length': 6.2144, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '283511', 'license': 'MIT', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1287, 'n_words': 355, 'path': 'notebooks/captum-integration-example.ipynb', 'repo_name': 'gaugup/interpret-community', 'size': '4508'}