InputXGradientExplainer
Input × Gradient explainer for computing input-scaled gradient attributions.
This explainer computes attributions by multiplying input features with their gradients, providing a measure that considers both the magnitude of the input and its sensitivity to the output. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.
The Input × Gradient method combines the input magnitude with gradient information, making it useful for understanding feature importance in the context of actual input values.
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:64) –Batch size for internal computations. Defaults to 64.
-
(grad_batch_sizeint, default:64) –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 = InputXGradientExplainer(model)
>>> attributions = explainer.explain(
... inputs=torch.randn(1, 10),
... target=SingleTargetAcrossBatch(index=0),
... )
>>> attributions.shape # (1, 10)
Multi-target usage:
>>> explainer_mt = InputXGradientExplainer(model, multi_target=True)
>>> mt_attributions = explainer_mt.explain(
... inputs=torch.randn(1, 10),
... target=[SingleTargetAcrossBatch(index=0), SingleTargetAcrossBatch(index=1)],
... )
>>> len(mt_attributions), mt_attributions[0].shape # 2, (1, 10)
Methods:
-
explain–Compute Input × Gradient attributions for the given inputs.
Source code in torchxai/explainers/_grad/_input_x_gradient.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
explain
explain(
inputs: TensorOrTupleOfTensorsGeneric,
target: ExplanationTargetType | list[ExplanationTargetType],
additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]
Compute Input × Gradient 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. -
(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.
Examples:
>>> # Single tensor input (wrapped automatically)
>>> attributions = explainer.explain(
... inputs=torch.randn(2, 10), target=torch.tensor([0, 1])
... )
>>>
>>> # Multiple features (use OrderedDict)
>>> attributions = explainer.explain(
... inputs=OrderedDict(
... {"feat1": torch.randn(2, 5), "feat2": torch.randn(2, 5)}
... ),
... target=torch.tensor([0, 1]),
... )
Source code in torchxai/explainers/_grad/_input_x_gradient.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |