Deprecation notice
This module is deprecated since v0.10.0 in favor of pydvl.valuation.
pydvl.value.shapley.classwise
¶
Class-wise Shapley (Schoch et al., 2022)1 offers a Shapley framework tailored
for classification problems. Let
where
Analysis of Class-wise Shapley
For a detailed analysis of the method, with comparison to other valuation techniques, please refer to the main documentation.
In practice, the quantity above is estimated using Monte Carlo sampling of the powerset and the set of index permutations. This results in the estimator
with
Notes for derivation of test cases
The unit tests include the following manually constructed data:
Let
in closed form
References¶
-
Schoch, Stephanie, Haifeng Xu, and Yangfeng Ji. CS-Shapley: Class-wise Shapley Values for Data Valuation in Classification. In Proc. of the Thirty-Sixth Conference on Neural Information Processing Systems (NeurIPS). New Orleans, Louisiana, USA, 2022. ↩
ClasswiseScorer
¶
ClasswiseScorer(
scoring: Union[str, ScorerCallable] = "accuracy",
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] = exp,
initial_label: Optional[int] = None,
name: Optional[str] = None,
)
Bases: Scorer
A Scorer designed for evaluation in classification problems. Its value
is computed from an in-class and an out-of-class "inner score" (Schoch et
al., 2022) 1. Let
where
Warning
Metrics must support multiple class labels if you intend to apply them
to a multi-class problem. For instance, the metric 'accuracy' supports
multiple classes, but the metric 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 Scorer.
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. |
initial_label
|
Set initial label (for the first iteration) |
name
|
Name of the scorer. If not provided, the name of the inner scoring
function will be prefixed by |
New in version 0.7.1
Source code in src/pydvl/value/shapley/classwise.py
estimate_in_class_and_out_of_class_score
¶
estimate_in_class_and_out_of_class_score(
model: SupervisedModel,
x_test: NDArray[float64],
y_test: NDArray[int_],
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
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
TYPE:
|
Source code in src/pydvl/value/shapley/classwise.py
compute_classwise_shapley_values
¶
compute_classwise_shapley_values(
u: Utility,
*,
done: StoppingCriterion,
truncation: TruncationPolicy,
done_sample_complements: Optional[StoppingCriterion] = None,
normalize_values: bool = True,
use_default_scorer_value: bool = True,
min_elements_per_label: int = 1,
n_jobs: int = 1,
parallel_backend: Optional[ParallelBackend] = None,
config: Optional[ParallelConfig] = None,
progress: bool = False,
seed: Optional[Seed] = None,
) -> ValuationResult
Computes an approximate Class-wise Shapley value by sampling independent permutations of the index set for each label and index sets sampled from the powerset of the complement (with respect to the currently evaluated label), approximating the sum:
where
PARAMETER | DESCRIPTION |
---|---|
u
|
Utility object containing model, data, and scoring function. The scorer must be of type ClasswiseScorer.
TYPE:
|
done
|
Function that checks whether the computation needs to stop.
TYPE:
|
truncation
|
Callable function that decides whether to interrupt processing a permutation and set subsequent marginals to zero.
TYPE:
|
done_sample_complements
|
Function checking whether computation needs to stop. Otherwise, it will resample conditional sets until the stopping criterion is met.
TYPE:
|
normalize_values
|
Indicates whether to normalize the values by the variation in each class times their in-class accuracy.
TYPE:
|
done_sample_complements
|
Number of times to resample the complement set for each permutation.
TYPE:
|
use_default_scorer_value
|
The first set of indices is the sampled complement set. Unless not otherwise specified, the default scorer value is used for this. If it is set to false, the base score is calculated from the utility.
TYPE:
|
min_elements_per_label
|
The minimum number of elements for each opposite label.
TYPE:
|
n_jobs
|
Number of parallel jobs to run.
TYPE:
|
parallel_backend
|
Parallel backend instance to use
for parallelizing computations. If
TYPE:
|
config
|
(DEPRECATED) Object configuring parallel computation, with cluster address, number of cpus, etc.
TYPE:
|
progress
|
Whether to display a progress bar.
TYPE:
|
seed
|
Either an instance of a numpy random number generator or a seed for it.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ValuationResult
|
ValuationResult object containing computed data values. |
New in version 0.7.1
Source code in src/pydvl/value/shapley/classwise.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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 325 326 327 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 |
|