pydvl.valuation.samplers.base
¶
Base classes for samplers and evaluation strategies.
See pydvl.valuation.samplers for details.
IndexSampler
¶
IndexSampler(batch_size: int = 1)
Bases: ABC
Samplers are custom iterables over batches of subsets of indices.
Calling from_indices(indexset)
on a sampler returns a generator over batches
of Samples
. A [Sample][pydvl.valuation.samplers.Sample] is a tuple of the form
\((i, S)\), where \(i\) is an index of interest, and \(S \subset I \setminus \{i\}\) is a
subset of the complement of \(i\) in \(I\).
Note
Samplers are not iterators themselves, so that each call to
from_indices(data)
e.g. in a new for loop creates a new iterator.
Derived samplers must implement weight() and [_generate()][pydvl.valuation.samplers.IndexSampler._generate]. See the module's documentation for more on these.
Interrupting samplers¶
Calling [interrupt()][pydvl.valuation.samplers.IndexSampler.interrupt] on a sampler will stop the batched generator after the current batch has been yielded.
PARAMETER | DESCRIPTION |
---|---|
batch_size |
The number of samples to generate per batch. Batches are processed by EvaluationStrategy so that individual valuations in batch are guaranteed to be received in the right sequence.
TYPE:
|
Example
processed by the
[EvaluationStrategy][pydvl.valuation.samplers.base.EvaluationStrategy]
Source code in src/pydvl/valuation/samplers/base.py
generate_batches
¶
Batches the samples and yields them.
Source code in src/pydvl/valuation/samplers/base.py
sample_limit
¶
sample_limit(indices: IndexSetT) -> int | None
Number of samples that can be generated from the indices.
Returns None if the number of samples is infinite, which is the case for most stochastic samplers.
Source code in src/pydvl/valuation/samplers/base.py
weight
abstractmethod
staticmethod
¶
Factor by which to multiply Monte Carlo samples, so that the mean converges to the desired expression.
By the Law of Large Numbers, the sample mean of \(\delta_i(S_j)\) converges to the expectation under the distribution from which \(S_j\) is sampled.
We add a factor \(c(S_j)\) in order to have this expectation coincide with the desired expression.
PARAMETER | DESCRIPTION |
---|---|
n |
The total number of indices in the training data.
TYPE:
|
subset_len |
The size of the subset \(S_j\) for which the marginal is being computed
TYPE:
|
Source code in src/pydvl/valuation/samplers/base.py
make_strategy
abstractmethod
¶
make_strategy(
utility: UtilityBase, coefficient: Callable[[int, int], float] | None = None
) -> EvaluationStrategy
Returns the strategy for this sampler.
Source code in src/pydvl/valuation/samplers/base.py
EvaluationStrategy
¶
EvaluationStrategy(
sampler: SamplerT,
utility: UtilityBase,
coefficient: Callable[[int, int], float] | None = None,
)
Bases: ABC
, Generic[SamplerT, ValueUpdateT]
An evaluation strategy for samplers.
Implements the processing strategy for batches returned by an IndexSampler.
Different sampling schemes require different strategies for the evaluation of the utilities. For instance permutations generated by PermutationSampler must be evaluated in sequence to save computation, see PermutationEvaluationStrategy.
This class defines the common interface.
Usage pattern in valuation methods
def fit(self, data: Dataset):
self.utility.training_data = data
strategy = self.sampler.strategy(self.utility, self.coefficient)
delayed_batches = Parallel()(
delayed(strategy.process)(batch=list(batch), is_interrupted=flag)
for batch in self.sampler
)
for batch in delayed_batches:
for evaluation in batch:
self.result.update(evaluation.idx, evaluation.update)
if self.is_done(self.result):
flag.set()
break
FIXME the coefficient does not belong here, but in the methods. Either we return more information from process() so that it can be used in the methods or we allow for some manipulation of the strategy after it has been created. The latter is rigid but a quick fix, which I need right now.
PARAMETER | DESCRIPTION |
---|---|
sampler |
Required to setup some strategies. Be careful not to store it in the object when subclassing!
TYPE:
|
utility |
Required to setup some strategies. Be careful not to store it in the object when subclassing!
TYPE:
|
coefficient |
An additional coefficient to multiply marginals with. This depends on the valuation method, hence the delayed setup. |
Source code in src/pydvl/valuation/samplers/base.py
process
abstractmethod
¶
process(
batch: SampleBatch, is_interrupted: NullaryPredicate
) -> list[ValueUpdateT]
Processes batches of samples using the evaluator, with the strategy required for the sampler.
Warning
This method is intended to be used by the evaluator to process the samples in one batch, which means it might be sent to another process. Be careful with the objects you use here, as they will be pickled and sent over the wire.
PARAMETER | DESCRIPTION |
---|---|
batch |
TYPE:
|
is_interrupted |
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
list[ValueUpdateT]
|
Updates to values as tuples (idx, update) |