Skip to content

KernelSHAP

Bases: MultitaskExplainerMixin, AttributionExplainer

KernelSHAP is a model‑agnostic Shapley value estimator that interprets predictions by computing Shapley values through a weighted linear regression in the space of feature coalitions.

By unifying ideas from LIME and Shapley value theory, KernelSHAP provides additive feature attributions with strong consistency guarantees.

Reference: Lundberg and Lee (2017). A Unified Approach to Interpreting Model Predictions. Paper

Examples:

>>> from interpreto import Granularity, KernelShap
>>> from interpreto.attributions import InferenceModes
>>> method = KernelShap(model, tokenizer, batch_size=4,
>>>                     inference_mode=InferenceModes.SOFTMAX,
>>>                     n_perturbations=20,
>>>                     granularity=Granularity.WORD)
>>> explanations = method(text)

Parameters:

Name Type Description Default

model

PreTrainedModel

model to explain

required

tokenizer

PreTrainedTokenizer

Hugging Face tokenizer associated with the model

required

batch_size

int

batch size for the attribution method

4

granularity

Granularity

granularity level of the perturbations (token, word, sentence, etc.)

WORD

inference_mode

Callable[[Tensor], Tensor]

The mode used for inference. It can be either one of LOGITS, SOFTMAX, or LOG_SOFTMAX. Use InferenceModes to choose the appropriate mode.

LOGITS

n_perturbations

int

the number of perturbations to generate

1000

distance_function

DistancesFromMaskProtocol

distance function used to compute weights of perturbed samples in the linear model training.

required

similarity_kernel

SimilarityKernelProtocol

similarity kernel used to compute weights of perturbed samples in the linear model training.

required

kernel_width

float | Callable

kernel width used in the similarity_kernel

required

device

device

device on which the attribution method will be run

None