Skip to content

pydvl.valuation.utility.base

This module defines the base class for all utilities.

UtilityBase

Bases: Generic[SampleT], ABC

Base class for all utilities.

A utility is a scalar-valued set function which will be evaluated over subsets of the training set.

training_data property

training_data: Dataset | None

Retrieves the training data used by this utility.

This property is read-only. In order to set it, use with_dataset().

__call__ abstractmethod

__call__(sample: SampleT | None) -> float

Note

Calls with empty samples or None must always return the same valid value, e.g. 0, or whatever makes sense for the utility. Some samplers (e.g. permutations) depend on this.

PARAMETER DESCRIPTION
sample

TYPE: SampleT | None

RETURNS DESCRIPTION
float

The evaluation of the utility for the sample

Source code in src/pydvl/valuation/utility/base.py
@abstractmethod
def __call__(self, sample: SampleT | None) -> float:
    """
    !!! Note
        Calls with empty samples or None must always return the same valid value,
        e.g. 0, or whatever makes sense for the utility. Some samplers (e.g.
        permutations) depend on this.

    Args:
        sample:

    Returns:
        The evaluation of the utility for the sample
    """
    ...

__str__

__str__()

Returns a string representation of the utility. Subclasses should override this method to provide a more informative string

Source code in src/pydvl/valuation/utility/base.py
def __str__(self):
    """Returns a string representation of the utility.
    Subclasses should override this method to provide a more informative string
    """
    return f"{self.__class__.__name__}"

with_dataset

with_dataset(data: Dataset, copy: bool = True) -> Self

Returns the utility, or a copy of it, with the given dataset. Args: data: The dataset to use for utility fitting (training data) copy: Whether to copy the utility object or not. Valuation methods should always make copies to avoid unexpected side effects. Returns: The utility object.

Source code in src/pydvl/valuation/utility/base.py
def with_dataset(self, data: Dataset, copy: bool = True) -> Self:
    """Returns the utility, or a copy of it, with the given dataset.
    Args:
        data: The dataset to use for utility fitting (training data)
        copy: Whether to copy the utility object or not. Valuation methods should
            always make copies to avoid unexpected side effects.
    Returns:
        The utility object.
    """
    utility = cp.copy(self) if copy else self
    utility._training_data = data
    return utility