Sparseness¶
Sparseness measures the concentration of attribution maps using the Gini index. Higher sparseness values indicate that importance is concentrated in fewer features, which is often desirable for interpretability.
Quote
We use the Gini Index to measure the sparseness of explanations. Sparser explanations are often more interpretable as they highlight fewer, more important features.
-- Concise Explanations of Neural Networks using Adversarial Training (2020)1
Formally, the Sparseness is computed as the Gini coefficient of the L1-normalized absolute attributions:
Where \(x_{(i)}\) is the \(i\)-th smallest component and \(n\) is the number of features.
Info
The better the explanation, the higher the Sparseness score. Higher values indicate sparser explanations where importance is concentrated in fewer features.
Score Interpretation¶
- Higher scores are better: A high Sparseness score indicates that the explanation focuses on a small subset of features.
- Values range from 0 (perfectly uniform distribution, low sparseness) to ~1 (maximally concentrated, high sparseness).
- For image explanations with 4D tensors
(B, H, W, C), channels are averaged before computing sparseness.
Example¶
from xplique.metrics import Sparseness
from xplique.attributions import Saliency
# load images, labels and model
# ...
explainer = Saliency(model)
explanations = explainer(inputs, labels)
metric = Sparseness()
score = metric.evaluate(explanations)
Note
Unlike fidelity metrics, Sparseness does not require the model or targets—it only evaluates the explanation itself.
Sparseness¶
Gini-index-based sparseness of attribution maps.
__init__(self,
batch_size: Optional[int] = 32)¶
batch_size: Optional[int] = 32)
detailed_evaluate(self,
explanations: tensorflow.python.framework.tensor.Tensor) -> numpy.ndarray¶
explanations: tensorflow.python.framework.tensor.Tensor) -> numpy.ndarray
Compute the Gini coefficient for each explanation in the batch.
Parameters
-
explanations : tf.Tensor
Attribution maps of shape (B, H, W) or (B, H, W, C) where: - B: batch size - H, W: spatial dimensions - C: channels (optional, will be averaged if present)
Return
-
np.ndarray : numpy.ndarray
Gini coefficients of shape (B,), one per sample. Values are in the range [0, 1] where: - 0: perfectly uniform distribution (low sparseness) - 1: maximally concentrated distribution (high sparseness)