pydvl.value.semivalues
¶
This module provides the core functionality for the computation of generic semi-values. A semi-value is any valuation function with the form:
where the coefficients \(w(k)\) satisfy the property:
Note
For implementation consistency, we slightly depart from the common definition of semi-values, which includes a factor \(1/n\) in the sum over subsets. Instead, we subsume this factor into the coefficient \(w(k)\).
Main components¶
The computation of a semi-value requires two components:
- A subset sampler that generates subsets of the set \(D\) of interest.
- A coefficient \(w(k)\) that assigns a weight to each subset size \(k\).
Samplers can be found in sampler, and can be classified into two categories: powerset samplers and permutation samplers. Powerset samplers generate subsets of \(D_{-i}\), while the permutation sampler generates permutations of \(D\). The former conform to the above definition of semi-values, while the latter reformulates it as:
where \(\sigma_{:i}\) denotes the set of indices in permutation sigma before the position where \(i\) appears (see Data valuation for details), and
is the weight correction due to the reformulation.
Warning
Both PermutationSampler and DeterministicPermutationSampler require caching to be enabled or computation will be doubled wrt. a 'direct' implementation of permutation MC.
Computing semi-values¶
Samplers and coefficients can be arbitrarily mixed by means of the main entry point of this module, compute_generic_semivalues. There are several pre-defined coefficients, including the Shapley value of (Ghorbani and Zou, 2019)1, the Banzhaf index of (Wang and Jia)3, and the Beta coefficient of (Kwon and Zou, 2022)2. For each of these methods, there is a convenience wrapper function. Respectively, these are: compute_shapley_semivalues, compute_banzhaf_semivalues, and compute_beta_shapley_semivalues. instead.
Parallelization and batching
In order to ensure reproducibility and fine-grained control of
parallelization, samples are generated in the main process and then
distributed to worker processes for evaluation. For small sample sizes, this
can lead to a significant overhead. To avoid this, we temporarily provide an
additional argument batch_size
to all methods which can improve
performance with small models up to an order of magnitude. Note that this
argument will be removed before version 1.0 in favour of a more general
solution.
References¶
-
Ghorbani, A., Zou, J., 2019. Data Shapley: Equitable Valuation of Data for Machine Learning. In: Proceedings of the 36th International Conference on Machine Learning, PMLR, pp. 2242–2251. ↩
-
Kwon, Y. and Zou, J., 2022. Beta Shapley: A Unified and Noise-reduced Data Valuation Framework for Machine Learning. In: Proceedings of the 25th International Conference on Artificial Intelligence and Statistics (AISTATS) 2022, Vol. 151. PMLR, Valencia, Spain. ↩
-
Wang, J.T. and Jia, R., 2023. Data Banzhaf: A Robust Data Valuation Framework for Machine Learning. In: Proceedings of The 26th International Conference on Artificial Intelligence and Statistics, pp. 6388-6421. ↩
SVCoefficient
¶
Bases: Protocol
The protocol that coefficients for the computation of semi-values must fulfill.
DefaultMarginal
¶
Bases: MarginalFunction
__call__
¶
__call__(
u: Utility, coefficient: SVCoefficient, samples: Iterable[SampleT]
) -> Tuple[MarginalT, ...]
Computation of marginal utility. This is a helper function for compute_generic_semivalues.
PARAMETER | DESCRIPTION |
---|---|
u |
Utility object with model, data, and scoring function.
TYPE:
|
coefficient |
The semivalue coefficient and sampler weight
TYPE:
|
samples |
A collection of samples. Each sample is a tuple of index and subset of indices to compute a marginal utility.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
MarginalT
|
A collection of marginals. Each marginal is a tuple with index and its marginal |
...
|
utility. |
Source code in src/pydvl/value/semivalues.py
RawUtility
¶
Bases: MarginalFunction
__call__
¶
__call__(
u: Utility, coefficient: SVCoefficient, samples: Iterable[SampleT]
) -> Tuple[MarginalT, ...]
Computation of raw utility without marginalization. This is a helper function for compute_generic_semivalues.
PARAMETER | DESCRIPTION |
---|---|
u |
Utility object with model, data, and scoring function.
TYPE:
|
coefficient |
The semivalue coefficient and sampler weight
TYPE:
|
samples |
A collection of samples. Each sample is a tuple of index and subset of indices to compute a marginal utility.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[MarginalT, ...]
|
A collection of marginals. Each marginal is a tuple with index and its raw utility. |
Source code in src/pydvl/value/semivalues.py
FutureProcessor
¶
The FutureProcessor class used to process the results of the parallel marginal evaluations.
The marginals are evaluated in parallel by n_jobs
threads, but some algorithms require a central
method to postprocess the marginal results. This can be achieved through the future processor.
This base class does not perform any postprocessing, it is a noop used in most data valuation algorithms.
MSRFutureProcessor
¶
MSRFutureProcessor(u: Utility)
Bases: FutureProcessor
This FutureProcessor processes the raw marginals in a way that MSR sampling requires.
MSR sampling evaluates the utility once, and then updates all data semivalues based on this one evaluation. In order to do this, the RawUtility value needs to be postprocessed through this class. For more details on MSR, please refer to the paper (Wang et. al.)3. This processor keeps track of the current values and computes marginals for all data points, so that the values in the ValuationResult can be updated properly down the line.
Source code in src/pydvl/value/semivalues.py
__call__
¶
Computation of marginal utility using Maximum Sample Reuse.
This processor requires the Marginal Function to be set to RawUtility.
Then, this processor computes marginals based on the utility value and the index set provided.
The final formula that gives the Banzhaf semivalue using MSR is:
$$\hat{\phi}_{MSR}(i) = rac{1}{|\mathbf{S}_{
i i}|} \sum_{S \in \mathbf{S}{ i i}} U(S) - rac{1}{|\mathbf{S}{ ot{ i} i}|} \sum_{S \in \mathbf{S}_{ ot{ i} i}} U(S)$$
Args:
future_result: Result of the parallel computing jobs comprised of
a list of indices that were used to evaluate the utility, and the evaluation result (metric).
Returns:
A collection of marginals. Each marginal is a tuple with index and its marginal
utility.
Source code in src/pydvl/value/semivalues.py
SemiValueMode
¶
compute_generic_semivalues
¶
compute_generic_semivalues(
sampler: PowersetSampler[IndexT],
u: Utility,
coefficient: SVCoefficient,
done: StoppingCriterion,
*,
marginal: MarginalFunction = DefaultMarginal(),
future_processor: FutureProcessor = FutureProcessor(),
batch_size: int = 1,
skip_converged: bool = False,
n_jobs: int = 1,
parallel_backend: Optional[ParallelBackend] = None,
config: Optional[ParallelConfig] = None,
progress: bool = False
) -> ValuationResult
Computes semi-values for a given utility function and subset sampler.
PARAMETER | DESCRIPTION |
---|---|
sampler |
The subset sampler to use for utility computations.
TYPE:
|
u |
Utility object with model, data, and scoring function.
TYPE:
|
coefficient |
The semi-value coefficient
TYPE:
|
done |
Stopping criterion.
TYPE:
|
marginal |
Marginal function to be used for computing the semivalues
TYPE:
|
future_processor |
Additional postprocessing steps required for some algorithms
TYPE:
|
batch_size |
Number of marginal evaluations per single parallel job.
TYPE:
|
skip_converged |
Whether to skip marginal evaluations for indices that have already converged. CAUTION: This is only entirely safe if the stopping criterion is MaxUpdates. For any other stopping criterion, the convergence status of indices may change during the computation, or they may be marked as having converged even though in fact the estimated values are far from the true values (e.g. for AbsoluteStandardError, you will probably have to carefully adjust the threshold).
TYPE:
|
n_jobs |
Number of parallel jobs to use.
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:
|
RETURNS | DESCRIPTION |
---|---|
ValuationResult
|
Object with the results. |
Deprecation notice
Parameter batch_size
is for experimental use and will be removed in
future versions.
Changed in version 0.9.0
Deprecated config
argument and added a parallel_backend
argument to allow users to pass the Parallel Backend instance
directly.
Source code in src/pydvl/value/semivalues.py
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 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
compute_shapley_semivalues
¶
compute_shapley_semivalues(
u: Utility,
*,
done: StoppingCriterion,
sampler_t: Type[StochasticSampler] = PermutationSampler,
batch_size: int = 1,
n_jobs: int = 1,
parallel_backend: Optional[ParallelBackend] = None,
config: Optional[ParallelConfig] = None,
progress: bool = False,
seed: Optional[Seed] = None
) -> ValuationResult
Computes Shapley values for a given utility function.
This is a convenience wrapper for compute_generic_semivalues with the Shapley coefficient. Use compute_shapley_values for a more flexible interface and additional methods, including TMCS.
PARAMETER | DESCRIPTION |
---|---|
u |
Utility object with model, data, and scoring function.
TYPE:
|
done |
Stopping criterion.
TYPE:
|
sampler_t |
The sampler type to use. See the sampler module for a list.
TYPE:
|
batch_size |
Number of marginal evaluations per single parallel job.
TYPE:
|
n_jobs |
Number of parallel jobs to use.
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:
|
seed |
Either an instance of a numpy random number generator or a seed for it.
TYPE:
|
progress |
Whether to display a progress bar.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ValuationResult
|
Object with the results. |
Deprecation notice
Parameter batch_size
is for experimental use and will be removed in
future versions.
Changed in version 0.9.0
Deprecated config
argument and added a parallel_backend
argument to allow users to pass the Parallel Backend instance
directly.
Source code in src/pydvl/value/semivalues.py
compute_banzhaf_semivalues
¶
compute_banzhaf_semivalues(
u: Utility,
*,
done: StoppingCriterion,
sampler_t: Type[StochasticSampler] = PermutationSampler,
batch_size: int = 1,
n_jobs: int = 1,
parallel_backend: Optional[ParallelBackend] = None,
config: Optional[ParallelConfig] = None,
progress: bool = False,
seed: Optional[Seed] = None
) -> ValuationResult
Computes Banzhaf values for a given utility function.
This is a convenience wrapper for compute_generic_semivalues with the Banzhaf coefficient.
PARAMETER | DESCRIPTION |
---|---|
u |
Utility object with model, data, and scoring function.
TYPE:
|
done |
Stopping criterion.
TYPE:
|
sampler_t |
The sampler type to use. See the sampler module for a list.
TYPE:
|
batch_size |
Number of marginal evaluations per single parallel job.
TYPE:
|
n_jobs |
Number of parallel jobs to use.
TYPE:
|
seed |
Either an instance of a numpy random number generator or a seed for it.
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:
|
RETURNS | DESCRIPTION |
---|---|
ValuationResult
|
Object with the results. |
Deprecation notice
Parameter batch_size
is for experimental use and will be removed in
future versions.
Changed in version 0.9.0
Deprecated config
argument and added a parallel_backend
argument to allow users to pass the Parallel Backend instance
directly.
Source code in src/pydvl/value/semivalues.py
compute_msr_banzhaf_semivalues
¶
compute_msr_banzhaf_semivalues(
u: Utility,
*,
done: StoppingCriterion,
sampler_t: Type[StochasticSampler] = MSRSampler,
batch_size: int = 1,
n_jobs: int = 1,
parallel_backend: Optional[ParallelBackend] = None,
config: Optional[ParallelConfig] = None,
progress: bool = False,
seed: Optional[Seed] = None
) -> ValuationResult
Computes MSR sampled Banzhaf values for a given utility function.
This is a convenience wrapper for compute_generic_semivalues with the Banzhaf coefficient and MSR sampling.
This algorithm works by sampling random subsets and then evaluating the utility on that subset only once. Based on the evaluation and the subset indices, the MSRFutureProcessor then computes the marginal updates like in the paper (Wang et. al.)3. Their approach updates the semivalues for all data points every time a new evaluation is computed. This increases sample efficiency compared to normal Monte Carlo updates.
PARAMETER | DESCRIPTION |
---|---|
u |
Utility object with model, data, and scoring function.
TYPE:
|
done |
Stopping criterion.
TYPE:
|
sampler_t |
The sampler type to use. See the sampler module for a list.
TYPE:
|
batch_size |
Number of marginal evaluations per single parallel job.
TYPE:
|
n_jobs |
Number of parallel jobs to use.
TYPE:
|
seed |
Either an instance of a numpy random number generator or a seed for it.
TYPE:
|
config |
Object configuring parallel computation, with cluster address, number of cpus, etc.
TYPE:
|
progress |
Whether to display a progress bar.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ValuationResult
|
Object with the results. |
Deprecation notice
Parameter batch_size
is for experimental use and will be removed in
future versions.
Source code in src/pydvl/value/semivalues.py
compute_beta_shapley_semivalues
¶
compute_beta_shapley_semivalues(
u: Utility,
*,
alpha: float = 1,
beta: float = 1,
done: StoppingCriterion,
sampler_t: Type[StochasticSampler] = PermutationSampler,
batch_size: int = 1,
n_jobs: int = 1,
parallel_backend: Optional[ParallelBackend] = None,
config: Optional[ParallelConfig] = None,
progress: bool = False,
seed: Optional[Seed] = None
) -> ValuationResult
Computes Beta Shapley values for a given utility function.
This is a convenience wrapper for compute_generic_semivalues with the Beta Shapley coefficient.
PARAMETER | DESCRIPTION |
---|---|
u |
Utility object with model, data, and scoring function.
TYPE:
|
alpha |
Alpha parameter of the Beta distribution.
TYPE:
|
beta |
Beta parameter of the Beta distribution.
TYPE:
|
done |
Stopping criterion.
TYPE:
|
sampler_t |
The sampler type to use. See the sampler module for a list.
TYPE:
|
batch_size |
Number of marginal evaluations per (parallelized) task.
TYPE:
|
n_jobs |
Number of parallel jobs to use.
TYPE:
|
seed |
Either an instance of a numpy random number generator or a seed for it.
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:
|
RETURNS | DESCRIPTION |
---|---|
ValuationResult
|
Object with the results. |
Deprecation notice
Parameter batch_size
is for experimental use and will be removed in
future versions.
Changed in version 0.9.0
Deprecated config
argument and added a parallel_backend
argument to allow users to pass the Parallel Backend instance
directly.
Source code in src/pydvl/value/semivalues.py
compute_semivalues
¶
compute_semivalues(
u: Utility,
*,
done: StoppingCriterion,
mode: SemiValueMode = SemiValueMode.Shapley,
sampler_t: Type[StochasticSampler] = PermutationSampler,
batch_size: int = 1,
n_jobs: int = 1,
seed: Optional[Seed] = None,
**kwargs
) -> ValuationResult
Convenience entry point for most common semi-value computations.
Deprecation warning
This method is deprecated and will be replaced in 0.8.0 by the more general implementation of compute_generic_semivalues. Use compute_shapley_semivalues, compute_banzhaf_semivalues, or compute_beta_shapley_semivalues instead.
The modes supported with this interface are the following. For greater flexibility use compute_generic_semivalues directly.
- SemiValueMode.Shapley: Shapley values.
- SemiValueMode.BetaShapley:
Implements the Beta Shapley semi-value as introduced in
(Kwon and Zou, 2022)1.
Pass additional keyword arguments
alpha
andbeta
to set the parameters of the Beta distribution (both default to 1). - SemiValueMode.Banzhaf: Implements the Banzhaf semi-value as introduced in (Wang and Jia, 2022)1.
See Data valuation for an overview of valuation.
PARAMETER | DESCRIPTION |
---|---|
u |
Utility object with model, data, and scoring function.
TYPE:
|
done |
Stopping criterion.
TYPE:
|
mode |
The semi-value mode to use. See SemiValueMode for a list.
TYPE:
|
sampler_t |
The sampler type to use. See sampler for a list.
TYPE:
|
batch_size |
Number of marginal evaluations per (parallelized) task.
TYPE:
|
n_jobs |
Number of parallel jobs to use.
TYPE:
|
seed |
Either an instance of a numpy random number generator or a seed for it.
TYPE:
|
kwargs |
Additional keyword arguments passed to compute_generic_semivalues.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
ValuationResult
|
Object with the results. |
Deprecation notice
Parameter batch_size
is for experimental use and will be removed in
future versions.
Source code in src/pydvl/value/semivalues.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|