IntegratedGradientsExplainer

Integrated Gradients explainer for computing path-integrated attributions.

This explainer computes attributions using Integrated Gradients, which integrates gradients along a straight path from a baseline input to the actual input. This method satisfies important axioms including sensitivity and implementation invariance, making it a robust attribution method. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.

The Integrated Gradients method provides theoretically grounded attributions by computing the integral of gradients along the path from baseline to input.

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: 50 ) –

    Batch size for internal computations. Defaults to 64.

  • grad_batch_size

    (int, default: 64 ) –

    Batch size for gradient computations. Defaults to 64.

  • n_steps

    (int, default: 50 ) –

    Number of steps for the integral approximation. Defaults to 50.

Examples:

Single-target usage:

>>> import torch
>>> from torchxai.data_types import SingleTargetAcrossBatch
>>>
>>> model = torch.nn.Linear(10, 2)
>>> explainer = IntegratedGradientsExplainer(model)
>>> inputs   = torch.randn(1, 10)
>>> baseline = torch.zeros(1, 10)
>>> attributions = explainer.explain(
...     inputs=inputs,
...     baselines=baseline,
...     target=SingleTargetAcrossBatch(index=0),
... )
>>> attributions.shape   # (1, 10)

Multi-target usage:

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

Methods:

  • explain

    Compute Integrated Gradients attributions for the given inputs.

Source code in torchxai/explainers/_grad/_integrated_gradients.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
class IntegratedGradientsExplainer(FeatureAttributionExplainer):
    """Integrated Gradients explainer for computing path-integrated attributions.

    This explainer computes attributions using Integrated Gradients, which integrates
    gradients along a straight path from a baseline input to the actual input.
    This method satisfies important axioms including sensitivity and implementation
    invariance, making it a robust attribution method. Supports both single-target
    and multi-target modes for both single-target and multi-target scenarios.

    The Integrated Gradients method provides theoretically grounded attributions
    by computing the integral of gradients along the path from baseline to input.

    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.
        n_steps: Number of steps for the integral approximation. Defaults to 50.

    Examples:
        Single-target usage:
        >>> import torch
        >>> from torchxai.data_types import SingleTargetAcrossBatch
        >>>
        >>> model = torch.nn.Linear(10, 2)
        >>> explainer = IntegratedGradientsExplainer(model)
        >>> inputs   = torch.randn(1, 10)
        >>> baseline = torch.zeros(1, 10)
        >>> attributions = explainer.explain(
        ...     inputs=inputs,
        ...     baselines=baseline,
        ...     target=SingleTargetAcrossBatch(index=0),
        ... )
        >>> attributions.shape   # (1, 10)

        Multi-target usage:
        >>> explainer_mt = IntegratedGradientsExplainer(model, multi_target=True)
        >>> mt_attributions = explainer_mt.explain(
        ...     inputs=inputs,
        ...     baselines=baseline,
        ...     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",
        "n_steps",
        "return_convergence_delta",
    ]

    def __init__(
        self,
        model: Module,
        multi_target: bool = False,
        internal_batch_size: int = 50,
        grad_batch_size: int = 64,
        n_steps: int = 50,
        return_convergence_delta: bool = False,
    ) -> None:
        """Initialize the IntegratedGradientsExplainer.

        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 50.
            grad_batch_size: Batch size for gradient computations. Defaults to 64.
            n_steps: Number of steps for the integral approximation. Defaults to 50.
            return_convergence_delta: Whether to return convergence delta for
                completeness check. Defaults to False.
        """
        self.n_steps = n_steps
        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 Integrated Gradients attribution function.

        Returns:
            Captum IntegratedGradients attribution function for single targets.
        """
        return partial(
            IntegratedGradients(self._model).attribute,
            n_steps=self.n_steps,
            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 Integrated Gradients attribution function.

        Returns:
            MultiTargetIntegratedGradients attribution function for multiple targets.
        """
        return partial(
            MultiTargetIntegratedGradients(
                self._model, grad_batch_size=self._grad_batch_size
            ).attribute,
            n_steps=self.n_steps,
            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 Integrated Gradients 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: Baseline tensors representing reference values. If None,
                uses zero baselines. Should match the structure of inputs.
            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.

            If return_convergence_delta was set to True during initialization, the return
            format may include convergence delta information depending on the underlying
            implementation.

        Note:
            The number of integration steps and convergence delta behavior are controlled
            by parameters set during initialization (n_steps and return_convergence_delta).
            More steps generally provide more accurate approximations but increase computation time.

        Examples:
            >>> # With convergence delta enabled at initialization
            >>> explainer = IntegratedGradientsExplainer(
            ...     model, n_steps=100, return_convergence_delta=True
            ... )
            >>> attributions = explainer.explain(
            ...     inputs=OrderedDict({"input": torch.randn(2, 10)}),
            ...     target=torch.tensor([0, 1]),
            ...     baselines=OrderedDict({"input": torch.zeros(2, 10)}),
            ... )
            >>>
            >>> # With automatic zero baselines
            >>> attributions = explainer.explain(
            ...     inputs=torch.randn(2, 10), target=torch.tensor([0, 1])
            ... )
        """
        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 Integrated Gradients 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 ) –

    Baseline tensors representing reference values. If None, uses zero baselines. Should match the structure of inputs.

  • 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.

  • TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]

    If return_convergence_delta was set to True during initialization, the return

  • TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]

    format may include convergence delta information depending on the underlying

  • TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]

    implementation.

Note

The number of integration steps and convergence delta behavior are controlled by parameters set during initialization (n_steps and return_convergence_delta). More steps generally provide more accurate approximations but increase computation time.

Examples:

>>> # With convergence delta enabled at initialization
>>> explainer = IntegratedGradientsExplainer(
...     model, n_steps=100, return_convergence_delta=True
... )
>>> attributions = explainer.explain(
...     inputs=OrderedDict({"input": torch.randn(2, 10)}),
...     target=torch.tensor([0, 1]),
...     baselines=OrderedDict({"input": torch.zeros(2, 10)}),
... )
>>>
>>> # With automatic zero baselines
>>> attributions = explainer.explain(
...     inputs=torch.randn(2, 10), target=torch.tensor([0, 1])
... )
Source code in torchxai/explainers/_grad/_integrated_gradients.py
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
def explain(
    self,
    inputs: TensorOrTupleOfTensorsGeneric,
    target: ExplanationTargetType | list[ExplanationTargetType],
    baselines: TensorOrTupleOfTensorsGeneric | None = None,
    additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]:
    """Compute Integrated Gradients 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: Baseline tensors representing reference values. If None,
            uses zero baselines. Should match the structure of inputs.
        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.

        If return_convergence_delta was set to True during initialization, the return
        format may include convergence delta information depending on the underlying
        implementation.

    Note:
        The number of integration steps and convergence delta behavior are controlled
        by parameters set during initialization (n_steps and return_convergence_delta).
        More steps generally provide more accurate approximations but increase computation time.

    Examples:
        >>> # With convergence delta enabled at initialization
        >>> explainer = IntegratedGradientsExplainer(
        ...     model, n_steps=100, return_convergence_delta=True
        ... )
        >>> attributions = explainer.explain(
        ...     inputs=OrderedDict({"input": torch.randn(2, 10)}),
        ...     target=torch.tensor([0, 1]),
        ...     baselines=OrderedDict({"input": torch.zeros(2, 10)}),
        ... )
        >>>
        >>> # With automatic zero baselines
        >>> attributions = explainer.explain(
        ...     inputs=torch.randn(2, 10), target=torch.tensor([0, 1])
        ... )
    """
    return self._default_explain(
        inputs=inputs,
        target=target,
        baselines=baselines,
        additional_forward_args=additional_forward_args,
    )