SpRAy API¶
SpRAy performs spectral clustering of explanation heatmaps to reveal systematic attention artifacts (Clever Hans behavior).
Class Reference¶
SpRAyDetector
¶
SpRAyDetector(
*,
n_clusters: int | None = None,
cluster_selection: str = "auto",
affinity: str = "cosine",
nearest_neighbors: int = 10,
rbf_gamma: float | None = None,
min_clusters: int = 2,
max_clusters: int = 10,
downsample_size: int | tuple[int, int] | None = 32,
random_state: int = 42,
small_cluster_threshold: float = 0.05,
purity_threshold: float = 0.8,
focus_threshold: float = 0.6,
separation_threshold: float = 0.3,
alignment_threshold: float = 0.6,
heatmap_generator: HeatmapGenerator | None = None
)
Bases: DetectorBase
Spectral clustering of explanation heatmaps for Clever Hans detection.
Source code in shortcut_detect/xai/spray_detector.py
Functions¶
fit
¶
fit(
heatmaps: ndarray | None = None,
*,
labels: ndarray | None = None,
group_labels: ndarray | None = None,
inputs: Any | None = None,
heatmap_generator: HeatmapGenerator | None = None,
model: Any | None = None,
target_layer: str | Any | None = None,
head: str | int = "logits",
target_index: int | None = None,
batch_size: int = 16
) -> SpRAyDetector
Fit SpRAy detector on heatmaps or raw inputs.
Parameters¶
heatmaps: Precomputed heatmaps shaped (N,H,W). If provided, inputs and model are ignored. labels: Optional task labels for cluster purity analysis. group_labels: Optional protected attribute labels for cluster purity analysis. inputs: Raw inputs to generate heatmaps from if heatmaps are not provided. heatmap_generator: Optional callable or GradCAMHeatmapGenerator used to produce heatmaps from inputs. model / target_layer: If provided (and heatmaps is None), a GradCAMHeatmapGenerator will be created internally. head / target_index: Parameters forwarded to GradCAM when generating heatmaps. batch_size: Batch size for heatmap generation when inputs are provided.
Source code in shortcut_detect/xai/spray_detector.py
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 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |