LimeExplainer
LIME explainer for local interpretable model-agnostic explanations.
This explainer computes attributions using LIME (Local Interpretable Model-agnostic Explanations), which explains individual predictions by learning locally faithful linear models around the input. LIME perturbs the input and trains a simple interpretable model on the perturbed samples to approximate the model's behavior locally. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.
LIME is particularly useful for understanding complex models by providing locally accurate explanations using interpretable linear models.
Parameters:
-
(modelModule) –The PyTorch model whose output is to be explained.
-
(multi_targetbool, default:False) –Whether to use multi-target mode. When True, can compute attributions for multiple targets simultaneously. Defaults to False.
-
(internal_batch_sizeint, default:1) –Batch size for internal computations (perturbations per evaluation). Defaults to 1.
-
(n_samplesint, default:100) –Number of perturbed samples to generate for LIME. Defaults to 100.
-
(alphafloat, default:0.01) –Regularization parameter for the LASSO interpretable model. Defaults to 0.01.
-
(weight_attributionsbool, default:True) –Whether to weight attributions by feature group sizes when using feature masks. Defaults to True.
Examples:
Single-target usage without mask (feature-level):
>>> import torch
>>> from torchxai.data_types import SingleTargetAcrossBatch
>>>
>>> model = torch.nn.Linear(10, 2)
>>> explainer = LimeExplainer(model)
>>> attributions = explainer.explain(
... inputs=torch.randn(1, 10),
... target=SingleTargetAcrossBatch(index=0),
... )
>>> attributions.shape # (1, 10)
With a feature mask (group-level attribution):
>>> feature_mask = torch.tensor([[0, 0, 1, 1, 2, 2, 2, 3, 3, 4]])
>>> attributions_grouped = explainer.explain(
... inputs=torch.randn(1, 10),
... feature_mask=feature_mask,
... target=SingleTargetAcrossBatch(index=0),
... )
>>> attributions_grouped.shape # (1, 10)
Multi-target usage:
>>> explainer_mt = LimeExplainer(model, multi_target=True)
>>> mt_attributions = explainer_mt.explain(
... inputs=torch.randn(1, 10),
... target=[SingleTargetAcrossBatch(index=0), SingleTargetAcrossBatch(index=1)],
... )
>>> len(mt_attributions), mt_attributions[0].shape # 2, (1, 10)
Methods:
-
explain–Compute LIME attributions for the given inputs.
Source code in torchxai/explainers/_perturbation/_lime.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 | |
explain
explain(
inputs: TensorOrTupleOfTensorsGeneric,
target: ExplanationTargetType | list[ExplanationTargetType],
baselines: TensorOrTupleOfTensorsGeneric | None = None,
feature_mask: TensorOrTupleOfTensorsGeneric | None = None,
additional_forward_args: tuple[Any, ...] | None = None,
frozen_features: list[Tensor] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]
Compute LIME attributions for the given inputs.
Parameters:
-
(inputsTensorOrTupleOfTensorsGeneric) –Input tensor(s) for attribution computation.
-
(targetExplanationTargetType | list[ExplanationTargetType]) –An
ExplanationTargetType(e.g.SingleTargetAcrossBatch) for single-target mode, or a list of them for multi-target mode. -
(baselinesTensorOrTupleOfTensorsGeneric | None, default:None) –Baseline tensors for perturbation (typically zeros). If None, uses zero baselines matching input shape.
-
(feature_maskTensorOrTupleOfTensorsGeneric | None, default:None) –Masks representing feature groups for aggregation. If provided, features with the same mask value are grouped together.
-
(additional_forward_argstuple[Any, ...] | None, default:None) –Additional arguments for model forward pass.
-
(frozen_featureslist[Tensor] | None, default:None) –List of feature indices to keep unchanged during perturbation. Useful for special tokens like CLS, SEP in NLP models.
Returns:
-
TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]–Tensor in single-target mode. List of Tensors, one per target, in multi-target mode.
Note
LIME trains a local linear model on perturbed samples. The number of samples and regularization strength are controlled by initialization parameters.
Examples:
>>> # For tabular data with feature grouping
>>> feature_mask = torch.tensor([[0, 0, 1, 1, 2, 2, 2, 3, 3, 4]])
>>> attributions = explainer.explain(
... inputs=OrderedDict({"features": torch.randn(1, 10)}),
... target=torch.tensor([1]),
... baselines=OrderedDict({"features": torch.zeros(1, 10)}),
... feature_mask=feature_mask,
... )
Source code in torchxai/explainers/_perturbation/_lime.py
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 | |