pydvl.valuation.scorers.classwise
¶
This module contains the implementation of the [ClasswiseScorer][pydvl.valuation.scorers.classwise.ClasswiseScorer] class for Class-wise Shapley values.
Its value is computed from an in-class and an out-of-class "inner score" (Schoch et al., 2022) 1. Let \(S\) be the training set and \(D\) be the valuation set. For each label \(c\), \(D\) is factorized into two disjoint sets: \(D_c\) for in-class instances and \(D_{-c}\) for out-of-class instances. The score combines an in-class metric of performance, adjusted by a discounted out-of-class metric. These inner scores must be provided upon construction or default to accuracy. They are combined into:
where \(f\) and \(g\) are continuous, monotonic functions. For a detailed explanation, refer to section four of (Schoch et al., 2022)1 .
ClasswiseSupervisedScorer
¶
ClasswiseSupervisedScorer(
scoring: str | SupervisedScorerCallable | SupervisedModel,
test_data: Dataset,
default: float = 0.0,
range: tuple[float, float] = (0, 1),
in_class_discount_fn: Callable[[float], float] = lambda x: x,
out_of_class_discount_fn: Callable[[float], float] = np.exp,
rescale_scores: bool = True,
name: str | None = None,
)
Bases: SupervisedScorer
A Scorer designed for evaluation in classification problems.
The final score is the combination of the in-class and out-of-class scores, which are e.g. the accuracy of the trained model over the instances of the test set with the same, and different, labels, respectively. See the module's documentation for more on this.
These two scores are computed with an "inner" scoring function, which must be provided upon construction.
Multi-class support
The inner score must support multiple class labels if you intend to apply them
to a multi-class problem. For instance, 'accuracy' supports multiple classes,
but f1
does not. For a two-class classification problem, using f1_weighted
is essentially equivalent to using accuracy
.
PARAMETER | DESCRIPTION |
---|---|
scoring |
Name of the scoring function or a callable that can be passed to SupervisedScorer.
TYPE:
|
default |
Score to use when a model fails to provide a number, e.g. when too little was used to train it, or errors arise.
TYPE:
|
range |
Numerical range of the score function. Some Monte Carlo methods
can use this to estimate the number of samples required for a
certain quality of approximation. If not provided, it can be read
from the |
in_class_discount_fn |
Continuous, monotonic increasing function used to discount the in-class score. |
out_of_class_discount_fn |
Continuous, monotonic increasing function used to discount the out-of-class score. |
rescale_scores |
If set to True, the scores will be denormalized. This is particularly useful when the inner score function \(a_S\) is calculated by an estimator of the form $rac{1}{N} \sum_i x_i$.
TYPE:
|
name |
Name of the scorer. If not provided, the name of the inner scoring
function will be prefixed by
TYPE:
|
New in version 0.7.1
Source code in src/pydvl/valuation/scorers/classwise.py
compute_in_and_out_of_class_scores
¶
compute_in_and_out_of_class_scores(
model: SupervisedModel, rescale_scores: bool = True
) -> tuple[float, float]
Computes in-class and out-of-class scores using the provided inner scoring function. The result is
In this context, for label \(c\) calculations are executed twice: once for \(D_c\) and once for \(D_{-c}\) to determine the in-class and out-of-class scores, respectively. By default, the raw scores are multiplied by \(\frac{|D_c|}{|D|}\) and \(\frac{|D_{-c}|}{|D|}\), respectively. This is done to ensure that both scores are of the same order of magnitude. This normalization is particularly useful when the inner score function \(a_S\) is calculated by an estimator of the form \(\frac{1}{N} \sum_i x_i\), e.g. the accuracy.
PARAMETER | DESCRIPTION |
---|---|
model |
Model used for computing the score on the validation set.
TYPE:
|
x_test |
Array containing the features of the classification problem.
|
y_test |
Array containing the labels of the classification problem.
|
rescale_scores |
If set to True, the scores will be denormalized. This is particularly useful when the inner score function \(a_S\) is calculated by an estimator of the form \(\frac{1}{N} \sum_i x_i\).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[float, float]
|
Tuple containing the in-class and out-of-class scores. |