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 |
---|---|---|---|
|
PreTrainedModel
|
model to explain |
required |
|
PreTrainedTokenizer
|
Hugging Face tokenizer associated with the model |
required |
|
int
|
batch size for the attribution method |
4
|
|
Granularity
|
granularity level of the perturbations (token, word, sentence, etc.) |
WORD
|
|
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
|
|
int
|
the number of perturbations to generate |
1000
|
|
DistancesFromMaskProtocol
|
distance function used to compute weights of perturbed samples in the linear model training. |
required |
|
SimilarityKernelProtocol
|
similarity kernel used to compute weights of perturbed samples in the linear model training. |
required |
|
float | Callable
|
kernel width used in the |
required |
|
device
|
device on which the attribution method will be run |
None
|