DeepLiftExplainer
DeepLIFT explainer for computing reference-based attributions.
This explainer computes attributions using DeepLIFT (Deep Learning Important FeaTures), which assigns contribution scores based on the difference from a reference baseline. DeepLIFT handles non-linear activations by decomposing them into linear components and properly attributing relevance through the network. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.
The DeepLIFT method provides more stable attributions than simple gradients by using reference baselines and handling activation functions appropriately.
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. Defaults to 64.
-
(grad_batch_sizeint, default:1) –Batch size for gradient computations. Defaults to 64.
Examples:
Single-target usage:
>>> import torch
>>> from torchxai.data_types import SingleTargetAcrossBatch
>>>
>>> model = torch.nn.Linear(10, 2)
>>> explainer = DeepLiftExplainer(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 = DeepLiftExplainer(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 DeepLIFT attributions for the given inputs.
Source code in torchxai/explainers/_grad/_deeplift.py
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 | |
explain
explain(
inputs: TensorOrTupleOfTensorsGeneric,
target: ExplanationTargetType | list[ExplanationTargetType],
baselines: TensorOrTupleOfTensorsGeneric | None = None,
additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]
Compute DeepLIFT 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 representing reference values. If None, uses zero baselines. Should match the structure of inputs.
-
(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
This method temporarily modifies activation functions during computation. Hooks and attributes are automatically removed after attribution computation.
Examples:
>>> # With explicit baselines
>>> 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/_deeplift.py
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 | |