gradients
gradients
¶
Gradient-based visual attribution methods.
compute_gradient_maps_for_policy
¶
compute_gradient_maps_for_policy(policy, observation, actions=None, explanation_type=value, output_selector=None, target_camera=None, target_vision_module_names=None, preprocess_observation=True)
Compute gradient visual maps for policy visual modules.
This is the policy-level entry point for gradient explanations. CNN feature maps and ViT patch-token activations use the same public method; token activations are reshaped to a patch grid before Grad-CAM weighting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Policy
|
Policy instance to explain. |
required |
observation
|
ObservationBatch
|
Raw observation tensors keyed by camera names. Camera
tensors must have shape |
required |
actions
|
ActionBatch | None
|
Optional action tensors. Discrete predictors use tokenized actions when available; otherwise pseudo-target tokens are generated before attribution hooks are registered. |
None
|
explanation_type
|
str
|
Either |
value
|
output_selector
|
PolicyPredictionSelector | None
|
Optional function selecting the prediction tensor to explain for continuous predictors. |
None
|
target_camera
|
str | None
|
Optional camera key to explain. If omitted, every camera exposed by a visual module is explained. |
None
|
target_vision_module_names
|
list[str] | None
|
Optional visual module allowlist. |
None
|
preprocess_observation
|
bool
|
Whether to normalize/tokenize |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Heatmaps keyed by camera with shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If a camera tensor has an unsupported rank. |
RuntimeError
|
If the selected visual module does not expose a compatible explainability target. |
RuntimeError
|
If target-layer hooks do not capture activation or gradient tensors. |
Source code in src/versatil/explainability/attribution/gradients.py
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 147 | |