RandomExplainer
Random explainer for generating baseline attributions using random noise.
This explainer generates random attributions with the same shape as the input tensors. It serves as a baseline method for comparison with other attribution techniques, helping to establish whether other methods provide meaningful signal above random noise. This is particularly useful for sanity checks and statistical significance testing of attribution methods. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.
Random attributions help establish baseline performance and can be used to validate that other attribution methods provide meaningful explanations.
Parameters:
-
(modelModule) –The PyTorch model whose output is to be explained (not used for computation but required for API consistency).
-
(multi_targetbool, default:False) –Whether to use multi-target mode. When True, can generate random attributions for multiple targets simultaneously. Defaults to False.
-
(internal_batch_sizeint, default:64) –Batch size for internal computations (not used but maintained for API consistency). Defaults to 64.
-
(random_seedint | None, default:None) –Random seed for reproducible random attributions. If None, uses PyTorch's current random state. Defaults to None.
Examples:
Single-target usage:
>>> import torch
>>> from torchxai.data_types import SingleTargetAcrossBatch
>>>
>>> model = torch.nn.Linear(10, 2)
>>> explainer = RandomExplainer(model)
>>> attributions = explainer.explain(
... inputs=torch.randn(1, 10),
... target=SingleTargetAcrossBatch(index=0),
... )
>>> attributions.shape # (1, 10)
Multi-target usage:
>>> explainer_mt = RandomExplainer(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–Generate random attributions for the given inputs.
Source code in torchxai/explainers/_random.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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 | |
explain
explain(
inputs: TensorOrTupleOfTensorsGeneric,
target: ExplanationTargetType | list[ExplanationTargetType],
additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]
Generate random 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 (ignored).
Returns:
-
TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]–For single-target mode: OrderedDict mapping feature names to random attribution tensors.
-
TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]–For multi-target mode: List of OrderedDicts, one per target.
Note
Random attributions are generated with the same shape as input tensors. If a random seed was provided during initialization, the results will be reproducible across calls with the same inputs.
Examples:
>>> # Generate random baseline for comparison
>>> random_attributions = explainer.explain(
... inputs=OrderedDict({"features": torch.randn(1, 10)}),
... target=torch.tensor([1]),
... )
>>> print(
... f"Random attribution range: {random_attributions['features'].min():.3f} to {random_attributions['features'].max():.3f}"
... )
Source code in torchxai/explainers/_random.py
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 | |