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:
-
(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:6) –Batch size for internal computations. Defaults to 64.
-
(grad_batch_sizeint, default:16) –Batch size for gradient computations. Defaults to 64.
-
(return_convergence_deltabool, 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 | |
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:
-
(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) –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_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
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 | |