InputXBaselineGradientExplainer
Input × Baseline Gradient explainer for computing scaled baseline-gradient attributions.
This explainer computes attributions by multiplying (input - baseline) with their gradients, providing a measure that considers both the deviation from baseline and gradient sensitivity. This method is particularly useful when you have meaningful baseline references. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.
The Input × Baseline Gradient method provides attributions that are grounded in both the input magnitude relative to a baseline and gradient information.
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 = InputXBaselineGradientExplainer(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 = InputXBaselineGradientExplainer(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 Input × Baseline Gradient attributions for the given inputs.
Source code in torchxai/explainers/_grad/_input_x_baseline_gradient.py
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
explain
explain(
inputs: TensorOrTupleOfTensorsGeneric,
target: ExplanationTargetType | list[ExplanationTargetType],
baselines: TensorOrTupleOfTensorsGeneric | None = None,
additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]
Compute Input × Baseline 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. -
(baselinesTensorOrTupleOfTensorsGeneric | None, default:None) –Baseline tensors representing reference values. Required for this method as it computes (input - baseline) × gradient.
-
(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:
>>> # 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)}),
... )
>>>
>>> # Multiple features with baselines
>>> attributions = explainer.explain(
... inputs=OrderedDict(
... {"feat1": torch.randn(2, 5), "feat2": torch.randn(2, 5)}
... ),
... target=torch.tensor([0, 1]),
... baselines=OrderedDict(
... {"feat1": torch.zeros(2, 5), "feat2": torch.zeros(2, 5)}
... ),
... )
Source code in torchxai/explainers/_grad/_input_x_baseline_gradient.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |