DeepLiftShapExplainer

DeepLIFT SHAP explainer for computing Shapley values with DeepLIFT.

This explainer computes attributions using DeepLIFT SHAP, which combines DeepLIFT with Shapley value computation by using a distribution of training baselines. This approach provides theoretically grounded attributions that satisfy Shapley value axioms while leveraging DeepLIFT's efficient computation. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.

DeepLIFT SHAP is particularly effective when you have representative training baselines, as it averages attributions across multiple reference points.

Parameters:

  • model

    (Module) –

    The PyTorch model whose output is to be explained.

  • multi_target

    (bool, default: False ) –

    Whether to use multi-target mode. When True, can compute attributions for multiple targets simultaneously. Defaults to False.

  • internal_batch_size

    (int, default: 6 ) –

    Batch size for internal computations. Defaults to 64.

  • grad_batch_size

    (int, default: 16 ) –

    Batch size for gradient computations. Defaults to 64.

  • return_convergence_delta

    (bool, default: False ) –

    Whether to return convergence delta for completeness check. Defaults to False.

Examples:

Single-target usage:

>>> import torch
>>> from torchxai.data_types import SingleTargetAcrossBatch
>>>
>>> model = torch.nn.Linear(10, 2)
>>> explainer = DeepLiftShapExplainer(model)
>>> inputs        = torch.randn(1, 10)
>>> baselines_dist = torch.zeros(1, 10).expand(5, -1)   # 5-sample reference distribution
>>> attributions = explainer.explain(
...     inputs=inputs,
...     baselines=baselines_dist,
...     target=SingleTargetAcrossBatch(index=0),
... )
>>> attributions.shape   # (1, 10)

Multi-target usage:

>>> explainer_mt = DeepLiftShapExplainer(model, multi_target=True)
>>> mt_attributions = explainer_mt.explain(
...     inputs=inputs,
...     baselines=baselines_dist,
...     target=[SingleTargetAcrossBatch(index=0), SingleTargetAcrossBatch(index=1)],
... )
>>> len(mt_attributions), mt_attributions[0].shape   # 2, (1, 10)

Methods:

  • explain

    Compute DeepLIFT SHAP attributions for the given inputs.

Source code in torchxai/explainers/_grad/_deeplift_shap.py
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
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
class DeepLiftShapExplainer(FeatureAttributionExplainer):
    """DeepLIFT SHAP explainer for computing Shapley values with DeepLIFT.

    This explainer computes attributions using DeepLIFT SHAP, which combines DeepLIFT
    with Shapley value computation by using a distribution of training baselines.
    This approach provides theoretically grounded attributions that satisfy Shapley
    value axioms while leveraging DeepLIFT's efficient computation. Supports both
    single-target and multi-target modes for both single-target and multi-target scenarios.

    DeepLIFT SHAP is particularly effective when you have representative training
    baselines, as it averages attributions across multiple reference points.

    Args:
        model: The PyTorch model whose output is to be explained.
        multi_target: Whether to use multi-target mode. When True, can compute
            attributions for multiple targets simultaneously. Defaults to False.
        internal_batch_size: Batch size for internal computations. Defaults to 64.
        grad_batch_size: Batch size for gradient computations. Defaults to 64.
        return_convergence_delta: Whether to return convergence delta for
            completeness check. Defaults to False.

    Examples:
        Single-target usage:
        >>> import torch
        >>> from torchxai.data_types import SingleTargetAcrossBatch
        >>>
        >>> model = torch.nn.Linear(10, 2)
        >>> explainer = DeepLiftShapExplainer(model)
        >>> inputs        = torch.randn(1, 10)
        >>> baselines_dist = torch.zeros(1, 10).expand(5, -1)   # 5-sample reference distribution
        >>> attributions = explainer.explain(
        ...     inputs=inputs,
        ...     baselines=baselines_dist,
        ...     target=SingleTargetAcrossBatch(index=0),
        ... )
        >>> attributions.shape   # (1, 10)

        Multi-target usage:
        >>> explainer_mt = DeepLiftShapExplainer(model, multi_target=True)
        >>> mt_attributions = explainer_mt.explain(
        ...     inputs=inputs,
        ...     baselines=baselines_dist,
        ...     target=[SingleTargetAcrossBatch(index=0), SingleTargetAcrossBatch(index=1)],
        ... )
        >>> len(mt_attributions), mt_attributions[0].shape   # 2, (1, 10)
    """

    __repr_attrs__ = [
        "_multi_target",
        "_internal_batch_size",
        "_grad_batch_size",
        "return_convergence_delta",
    ]

    def __init__(
        self,
        model: Module,
        multi_target: bool = False,
        internal_batch_size: int = 6,
        grad_batch_size: int = 16,
        return_convergence_delta: bool = False,
    ) -> None:
        """Initialize the DeepLiftShapExplainer.

        Args:
            model: The model whose output is to be explained.
            multi_target: Whether to use multi-target mode. Defaults to False.
            internal_batch_size: Batch size for internal computations. Defaults to 64.
            grad_batch_size: Batch size for gradient computations. Defaults to 64.
            return_convergence_delta: Whether to return convergence delta for
                completeness check. Defaults to False.
        """
        self.return_convergence_delta = return_convergence_delta

        super().__init__(
            model, multi_target, internal_batch_size, grad_batch_size=grad_batch_size
        )

    def _init_single_target_explanation_fn(self) -> Callable:
        """Initialize single-target DeepLIFT SHAP attribution function.

        Returns:
            DeepLiftShapBatched attribution function for single targets.
        """
        return partial(
            DeepLiftShapBatched(self._model).attribute,
            return_convergence_delta=self.return_convergence_delta,
            internal_batch_size=self._internal_batch_size,  # type: ignore
        )

    def _init_multi_target_explanation_fn(self) -> Callable:
        """Initialize multi-target DeepLIFT SHAP attribution function.

        Returns:
            MultiTargetDeepLiftShapBatched attribution function for multiple targets.
        """
        return partial(
            MultiTargetDeepLiftShapBatched(
                self._model, grad_batch_size=self._grad_batch_size
            ).attribute,
            return_convergence_delta=self.return_convergence_delta,
            internal_batch_size=self._internal_batch_size,
        )

    def explain(
        self,
        inputs: TensorOrTupleOfTensorsGeneric,
        target: ExplanationTargetType | list[ExplanationTargetType],
        baselines: TensorOrTupleOfTensorsGeneric | None = None,
        additional_forward_args: tuple[Any, ...] | None = None,
    ) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]:
        """Compute DeepLIFT SHAP attributions for the given inputs.

        Args:
            inputs: Input tensor(s) for attribution computation.
            target: An `ExplanationTargetType` (e.g. `SingleTargetAcrossBatch`) for single-target
                mode, or a list of them for multi-target mode.
            baselines: Training baseline distribution for DeepLIFT SHAP.
                Must be provided as a tensor distribution representing training samples.
                The method averages attributions across these baselines.
            additional_forward_args: Additional arguments for model forward pass.

        Returns:
            Tensor in single-target mode. List of Tensors, one per target, in multi-target mode.

        Note:
            DeepLIFT SHAP requires multiple baseline samples (typically from training data)
            rather than a single baseline. The convergence delta behavior is controlled
            by initialization settings.

        Examples:
            >>> # With training baselines
            >>> baselines = torch.randn(100, 10)  # 100 training samples
            >>> attributions = explainer.explain(
            ...     inputs=OrderedDict({"input": torch.randn(2, 10)}),
            ...     target=torch.tensor([0, 1]),
            ...     baselines=OrderedDict({"input": baselines}),
            ... )
        """
        return self._default_explain(
            inputs=inputs,
            target=target,
            baselines=baselines,
            additional_forward_args=additional_forward_args,
        )

explain

explain(
    inputs: TensorOrTupleOfTensorsGeneric,
    target: ExplanationTargetType | list[ExplanationTargetType],
    baselines: TensorOrTupleOfTensorsGeneric | None = None,
    additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]

Compute DeepLIFT SHAP attributions for the given inputs.

Parameters:

  • inputs
    (TensorOrTupleOfTensorsGeneric) –

    Input tensor(s) for attribution computation.

  • target
    (ExplanationTargetType | list[ExplanationTargetType]) –

    An ExplanationTargetType (e.g. SingleTargetAcrossBatch) for single-target mode, or a list of them for multi-target mode.

  • baselines
    (TensorOrTupleOfTensorsGeneric | None, default: None ) –

    Training baseline distribution for DeepLIFT SHAP. Must be provided as a tensor distribution representing training samples. The method averages attributions across these baselines.

  • additional_forward_args
    (tuple[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

DeepLIFT SHAP requires multiple baseline samples (typically from training data) rather than a single baseline. The convergence delta behavior is controlled by initialization settings.

Examples:

>>> # With training baselines
>>> baselines = torch.randn(100, 10)  # 100 training samples
>>> attributions = explainer.explain(
...     inputs=OrderedDict({"input": torch.randn(2, 10)}),
...     target=torch.tensor([0, 1]),
...     baselines=OrderedDict({"input": baselines}),
... )
Source code in torchxai/explainers/_grad/_deeplift_shap.py
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
def explain(
    self,
    inputs: TensorOrTupleOfTensorsGeneric,
    target: ExplanationTargetType | list[ExplanationTargetType],
    baselines: TensorOrTupleOfTensorsGeneric | None = None,
    additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]:
    """Compute DeepLIFT SHAP attributions for the given inputs.

    Args:
        inputs: Input tensor(s) for attribution computation.
        target: An `ExplanationTargetType` (e.g. `SingleTargetAcrossBatch`) for single-target
            mode, or a list of them for multi-target mode.
        baselines: Training baseline distribution for DeepLIFT SHAP.
            Must be provided as a tensor distribution representing training samples.
            The method averages attributions across these baselines.
        additional_forward_args: Additional arguments for model forward pass.

    Returns:
        Tensor in single-target mode. List of Tensors, one per target, in multi-target mode.

    Note:
        DeepLIFT SHAP requires multiple baseline samples (typically from training data)
        rather than a single baseline. The convergence delta behavior is controlled
        by initialization settings.

    Examples:
        >>> # With training baselines
        >>> baselines = torch.randn(100, 10)  # 100 training samples
        >>> attributions = explainer.explain(
        ...     inputs=OrderedDict({"input": torch.randn(2, 10)}),
        ...     target=torch.tensor([0, 1]),
        ...     baselines=OrderedDict({"input": baselines}),
        ... )
    """
    return self._default_explain(
        inputs=inputs,
        target=target,
        baselines=baselines,
        additional_forward_args=additional_forward_args,
    )