Metrics
Complexity
Bases: Metric
Computes the complexity of attributions.
Given attributions
, calculates a fractional contribution distribution prob_mass
,
prob_mass[i] = hist[i] / sum(hist)
. where hist[i] = histogram(attributions[i])
.
The complexity is defined by the entropy,
evaluation = -sum(hist * ln(hist))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model used for evaluation |
required |
explainer |
Optional[Explainer]
|
The explainer used for evaluation. |
None
|
n_bins |
int
|
The number of bins for histogram computation. |
10
|
Reference
U. Bhatt, A. Weller, and J. M. F. Moura. Evaluating and aggregating feature-based model attributions. In Proceedings of the IJCAI (2020).
Source code in pnpxai/evaluator/metrics/complexity.py
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 |
|
evaluate(inputs, targets, attributions=None)
Evaluate the explainer's complexity based on their probability masses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Optional[Tensor]
|
The input tensors to the model. |
required |
targets |
Optional[Tensor]
|
The target labels for the inputs. |
required |
attributions |
Optional[Tensor]
|
The attributions for the inputs. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
A tensor of the complexity evaluations. |
Source code in pnpxai/evaluator/metrics/complexity.py
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 |
|
MuFidelity
Bases: Metric
Computes the MuFidelity metric for attributions.
Given a model
and inputs
, mufidelity of model
to an explainer at inputs
is calculated by
a correlation between difference of predictions and attributions of maked inputs,
evaluation = corr(pred_diff, masked_attr)
.
The masked inputs are generated by masking subset_mask
to noised inputs
,
masked = perturbed * subset_mask + (1.0 - subset_mask) * baseline
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model to evaluate. |
required |
explainer |
Optional[Explainer]
|
The explainer to evaluate. |
None
|
n_perturb |
int
|
Number of perturbations to generate. |
150
|
noise_scale |
int
|
Scale factor for Gaussian random noise. |
0.2
|
batch_size |
int
|
Batch size for model evaluation. |
32
|
grid_size |
int
|
Size of the grid for creating subsets. |
9
|
baseline |
Union[float, Tensor]
|
Baseline value for masked subsets. |
0.0
|
mask_agg_dim |
Optional[int]
|
Dimension to aggregate masks. |
None
|
**kwargs |
Additional kwargs to compute metric in an evaluator. Not required for single usage. |
required |
Reference
U. Bhatt, A. Weller, and J. M. F. Moura. Evaluating and aggregating feature-based model attributions. In Proceedings of the IJCAI (2020).
Source code in pnpxai/evaluator/metrics/mu_fidelity.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 |
|
evaluate(inputs, targets, attributions=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Tensor
|
The input data (N x C x H x W). |
required |
targets |
Tensor
|
The target labels for the inputs (N x 1). |
required |
attributions |
Tensor
|
The attributions of the inputs. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The result of the metric evaluation. |
Source code in pnpxai/evaluator/metrics/mu_fidelity.py
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 |
|
Sensitivity
Bases: Metric
Computes the complexity of attributions.
Given attributions
, calculates a fractional contribution distribution prob_mass
,
prob_mass[i] = hist[i] / sum(hist)
. where hist[i] = histogram(attributions[i])
.
The complexity is defined by the entropy,
evaluation = -sum(hist * ln(hist))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Model
|
The model used for evaluation |
required |
explainer |
Optional[Union[Explainer, Callable]]
|
The explainer used for evaluation. It can be an instance of |
None
|
n_iter |
Optional[int]
|
The number of iterations for perturbation. |
8
|
epsilon |
Optional[float]
|
The magnitude of random uniform noise. |
0.2
|
Source code in pnpxai/evaluator/metrics/sensitivity.py
11 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 |
|
evaluate(inputs, targets, attributions=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Tensor
|
The input data. |
required |
targets |
Tensor
|
The target labels for the inputs. |
required |
attributions |
Optional[Tensor]
|
The attributions of the inputs. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The result of the metric evaluation. |
Source code in pnpxai/evaluator/metrics/sensitivity.py
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 |
|
AbPC
Bases: PixelFlipping
A metric class for evaluating the correctness of explanations or attributions using the Area between Perturbation Curves (AbPC) technique.
This class inherits from the PixelFlipping class and assesses the quality of attributions by comparing the area between the perturbation curves obtained by perturbing input features (e.g., pixels) in both ascending and descending order of their attributed importance. The average probability change is measured, providing a comprehensive evaluation of the explainer's correctness.
Attributes:
Name | Type | Description |
---|---|---|
model |
Module
|
The model. |
explainer |
Optional[Explainer]=None
|
The explainer whose explanations are being evaluated. |
channel_dim |
int
|
Target channel dimension. |
n_steps |
int
|
The number of perturbation steps. |
baseline_fn |
Optional[BaselineFunction]
|
Function to generate baseline inputs for perturbation. |
prob_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute probabilities from model outputs. |
pred_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute predictions from model outputs. |
forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract forward arguments from inputs. |
additional_forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract additional forward arguments. |
lb |
float
|
The lower bound for clamping the probability differences. |
Methods:
Name | Description |
---|---|
evaluate |
Evaluate the explainer's correctness using the AbPC technique by observing changes in model predictions. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
evaluate(inputs, targets, attributions, attention_mask=None, return_pf=False)
Evaluate the explainer's correctness using the AbPC technique by observing changes in model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorOrTupleOfTensors
|
The input tensors to the model. |
required |
targets |
Tensor
|
The target labels for the inputs. |
required |
attributions |
TensorOrTupleOfTensors
|
The attributions for the inputs. |
required |
attention_mask |
Optional[TensorOrTupleOfTensors]
|
Attention masks for the inputs. |
None
|
return_pf |
Optional[bool]
|
Whether to return the perturbation curves for ascending and descending orders. |
False
|
Returns:
Name | Type | Description |
---|---|---|
TensorOrTupleOfTensors |
TensorOrTupleOfTensors
|
The mean clamped differences in probabilities at each perturbation step, indicating the impact of perturbing the most and least relevant features. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
LeRF
Bases: PixelFlipping
A metric class for evaluating the correctness of explanations or attributions using the Least Relevant First (LeRF) pixel flipping technique.
This class inherits from the PixelFlipping class and evaluates the quality of attributions by perturbing input features (e.g., pixels) in ascending order of their attributed importance. The average probability change is measured to assess the explainer's correctness.
Attributes:
Name | Type | Description |
---|---|---|
model |
Module
|
The model. |
explainer |
Optional[Explainer]=None
|
The explainer whose explanations are being evaluated. |
channel_dim |
int
|
Target channel dimension. |
n_steps |
int
|
The number of perturbation steps. |
baseline_fn |
Optional[BaselineFunction]
|
Function to generate baseline inputs for perturbation. |
prob_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute probabilities from model outputs. |
pred_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute predictions from model outputs. |
forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract forward arguments from inputs. |
additional_forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract additional forward arguments. |
Methods:
Name | Description |
---|---|
evaluate |
Evaluate the explainer's correctness using the LeRF technique by observing changes in model predictions. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
evaluate(inputs, targets, attributions, attention_mask=None)
Evaluate the explainer's correctness using the LeRF technique by observing changes in model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorOrTupleOfTensors
|
The input tensors to the model. |
required |
targets |
Tensor
|
The target labels for the inputs. |
required |
attributions |
TensorOrTupleOfTensors
|
The attributions for the inputs. |
required |
attention_mask |
Optional[TensorOrTupleOfTensors]
|
Attention masks for the inputs. Default is None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
TensorOrTupleOfTensors |
TensorOrTupleOfTensors
|
The mean probabilities at each perturbation step, indicating the impact of perturbing the least relevant features first. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
|
MoRF
Bases: PixelFlipping
A metric class for evaluating the correctness of explanations or attributions using the Most Relevant First (MoRF) pixel flipping technique.
This class inherits from the PixelFlipping class and evaluates the quality of attributions by perturbing input features (e.g., pixels) in descending order of their attributed importance. The average probability change is measured to assess the explainer's correctness (lower better).
Attributes:
Name | Type | Description |
---|---|---|
model |
Module
|
The model. |
explainer |
Optional[Explainer]=None
|
The explainer whose explanations are being evaluated. |
channel_dim |
int
|
Target channel dimension. |
n_steps |
int
|
The number of perturbation steps. |
baseline_fn |
Optional[BaselineFunction]
|
Function to generate baseline inputs for perturbation. |
prob_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute probabilities from model outputs. |
pred_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute predictions from model outputs. |
forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract forward arguments from inputs. |
additional_forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract additional forward arguments. |
Methods:
Name | Description |
---|---|
evaluate |
Evaluate the explainer's correctness using the MoRF technique by observing changes in model predictions. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
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 |
|
evaluate(inputs, targets, attributions, attention_mask=None)
Evaluate the explainer's correctness using the MoRF technique by observing changes in model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorOrTupleOfTensors
|
The input tensors to the model. |
required |
targets |
Tensor
|
The target labels for the inputs. |
required |
attributions |
TensorOrTupleOfTensors
|
The attributions for the inputs. |
required |
attention_mask |
Optional[TensorOrTupleOfTensors]
|
Attention masks for the inputs. Default is None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
TensorOrTupleOfTensors |
TensorOrTupleOfTensors
|
The mean probabilities at each perturbation step, indicating the impact of perturbing the most relevant features first. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
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 |
|
PixelFlipping
Bases: Metric
A metric class for evaluating the correctness of explanations or attributions provided by an explainer using the pixel flipping technique.
This class assesses the quality of attributions by perturbing input features (e.g., pixels) in the order of their attributed importance and measuring the resulting change in the model's predictions. Correct attributions should lead to significant changes in model predictions when the most important features are perturbed.
Attributes:
Name | Type | Description |
---|---|---|
model |
Module
|
The model. |
explainer |
Optional[Explainer]=None
|
The explainer whose explanations are being evaluated. |
channel_dim |
int
|
Target channel dimension. |
n_steps |
int
|
The number of perturbation steps. |
baseline_fn |
Optional[BaselineFunction]
|
Function to generate baseline inputs for perturbation. |
prob_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute probabilities from model outputs. |
pred_fn |
Optional[Callable[[Tensor], Tensor]]
|
Function to compute predictions from model outputs. |
forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract forward arguments from inputs. |
additional_forward_arg_extractor |
Optional[Callable[[Tuple[Tensor]], Union[Tensor, Tuple[Tensor]]]]
|
Optional function to extract additional forward arguments. |
Methods:
Name | Description |
---|---|
evaluate |
Evaluate the explainer's correctness based on the attributions by observing changes in model predictions. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
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 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 |
|
evaluate(inputs, targets, attributions, attention_mask=None, descending=True)
Evaluate the explainer's correctness based on the attributions by observing changes in model predictions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
TensorOrTupleOfTensors
|
The input tensors to the model. |
required |
targets |
Tensor
|
The target labels for the inputs. |
required |
attributions |
TensorOrTupleOfTensors
|
The attributions for the inputs. |
required |
attention_mask |
Optional[TensorOrTupleOfTensors]
|
Attention masks for the inputs. Default is None. |
None
|
descending |
bool
|
Whether to flip pixels in descending order of attribution. Default is True. |
True
|
Returns:
Type | Description |
---|---|
Union[Dict[str, Tensor], Tuple[Dict[str, Tensor]]]
|
Union[Dict[str, Tensor], Tuple[Dict[str, Tensor]]]: A dictionary or tuple of dictionaries containing the probabilities and predictions at each perturbation step. |
Source code in pnpxai/evaluator/metrics/pixel_flipping.py
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|