Skip to content

LIME

Bases: MultitaskExplainerMixin, AttributionExplainer

Local Interpretable Model-agnostic Explanations (LIME) is a perturbation‑based approach that explains individual predictions by fitting a simple, interpretable surrogate model locally around the prediction of interest. By sampling perturbed versions of the input and weighting them by their proximity to the original instance, LIME learns per‑feature importance scores that approximate the behaviour of the underlying black‑box model in that local region.

Reference: Ribeiro et al. (2016). "Why Should I Trust You?": Explaining the Predictions of Any Classifier. Paper

Examples:

>>> from interpreto import Granularity, Lime
>>> from interpreto.attributions import InferenceModes
>>> method = Lime(model, tokenizer, batch_size=4,
>>>               inference_mode=InferenceModes.LOG_SOFTMAX,
>>>               n_perturbations=20,
>>>               granularity=Granularity.WORD,
>>>               distance_function=Lime.distance_functions.HAMMING)
>>> 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.

100

perturb_probability

float

probability of perturbation.

0.5

distance_function

DistancesFromMaskProtocol

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

COSINE

kernel_width

float | Callable | None

kernel width used in the similarity_kernel. If None, the kernel width is computed using the default_kernel_width_fn function.

None

device

device

device on which the attribution method will be run

None