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