FeatureAblationExplainer
Feature Ablation explainer for computing systematic feature removal attributions.
This explainer computes attributions using Feature Ablation, which systematically removes features or feature groups and measures the resulting change in model output. This direct approach provides intuitive explanations by showing exactly how much each feature contributes to the prediction. The method supports feature grouping through masks, allowing for hierarchical ablation studies. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.
Feature Ablation provides the most direct measure of feature importance through systematic removal and impact measurement.
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:64) –Batch size for internal computations (perturbations per evaluation). Defaults to 64.
-
(weight_attributionsbool, default:False) –Whether to weight attributions by feature group sizes when using feature masks. Defaults to False.
Examples:
Single-target usage without mask (feature-level):
>>> import torch
>>> from torchxai.data_types import SingleTargetAcrossBatch
>>>
>>> model = torch.nn.Linear(10, 2)
>>> explainer = FeatureAblationExplainer(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 = FeatureAblationExplainer(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 Feature Ablation attributions for the given inputs.
Source code in torchxai/explainers/_perturbation/_feature_ablation.py
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 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | |
explain
explain(
inputs: TensorOrTupleOfTensorsGeneric,
target: ExplanationTargetType | list[ExplanationTargetType],
baselines: TensorOrTupleOfTensorsGeneric | None = None,
feature_mask: TensorOrTupleOfTensorsGeneric | None = None,
additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]
Compute Feature Ablation 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 ablation (typically zeros). If None, uses zero baselines matching input shape.
-
(feature_maskTensorOrTupleOfTensorsGeneric | None, default:None) –Masks representing feature groups for ablation. Features with the same mask value are ablated together as a group.
-
(additional_forward_argstuple[Any, ...] | None, default:None) –Additional arguments for model forward pass.
Returns:
-
TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]–Tensor in single-target mode. List of Tensors, one per target, in multi-target mode.
Note
Feature Ablation directly measures feature importance through systematic removal. The internal_batch_size parameter controls how many features are ablated simultaneously for computational efficiency.
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/_feature_ablation.py
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | |