Skip to content

pydvl.influence.base_influence_function_model

ComposableInfluence

Bases: InfluenceFunctionModel, Generic[TensorType, BatchType, DataLoaderType, BlockMapperType], ABC

Generic abstract base class, that allow for block-wise computation of influence quantities. Inherit from this base class for specific influence algorithms and tensor frameworks.

is_thread_safe abstractmethod property

is_thread_safe: bool

Whether the influence computation is thread safe

n_parameters abstractmethod property

n_parameters

Number of trainable parameters of the underlying model

_concat abstractmethod

_concat(tensors: Iterable[TensorType], dim: int)

Implement this to concat tensors at a specified dimension

Source code in src/pydvl/influence/base_influence_function_model.py
@abstractmethod
def _concat(self, tensors: Iterable[TensorType], dim: int):
    """Implement this to concat tensors at a specified dimension"""

_create_batch abstractmethod staticmethod

_create_batch(x: TensorType, y: TensorType) -> BatchType

Implement this method to provide the creation of a subtype of Batch for a specific framework

Source code in src/pydvl/influence/base_influence_function_model.py
@staticmethod
@abstractmethod
def _create_batch(x: TensorType, y: TensorType) -> BatchType:
    """Implement this method to provide the creation of a subtype of
    [Batch][pydvl.influence.types.Batch] for a specific framework
    """

_create_block_mapper abstractmethod

_create_block_mapper(data: DataLoaderType) -> BlockMapperType

Override this method to create a block mapper instance, that can be used to compute block-wise influence quantities.

PARAMETER DESCRIPTION
data

iterable of tensors

TYPE: DataLoaderType

RETURNS DESCRIPTION
BlockMapperType

BlockMapper instance

Source code in src/pydvl/influence/base_influence_function_model.py
@abstractmethod
def _create_block_mapper(self, data: DataLoaderType) -> BlockMapperType:
    """
    Override this method to create a block mapper instance, that can be used
    to compute block-wise influence quantities.

    Args:
        data: iterable of tensors

    Returns:
        BlockMapper instance
    """
    pass

_flatten_trailing_dim abstractmethod

_flatten_trailing_dim(tensor: TensorType)

Implement this to flatten all but the first dimension

Source code in src/pydvl/influence/base_influence_function_model.py
@abstractmethod
def _flatten_trailing_dim(self, tensor: TensorType):
    """Implement this to flatten all but the first dimension"""

fit

fit(data: DataLoaderType) -> InfluenceFunctionModel

Fitting to provided data, by internally creating a block mapper instance from it. Args: data: iterable of tensors

RETURNS DESCRIPTION
InfluenceFunctionModel

Fitted instance

Source code in src/pydvl/influence/base_influence_function_model.py
@log_duration(log_level=logging.INFO)
def fit(self, data: DataLoaderType) -> InfluenceFunctionModel:
    """
    Fitting to provided data, by internally creating a block mapper instance from
    it.
    Args:
        data: iterable of tensors

    Returns:
        Fitted instance
    """
    self.block_mapper = self._create_block_mapper(data)
    return self

fit_required staticmethod

fit_required(method)

Decorator to enforce the fitted check

Source code in src/pydvl/influence/base_influence_function_model.py
@staticmethod
def fit_required(method):
    """Decorator to enforce the fitted check"""

    @wraps(method)
    def wrapper(self, *args, **kwargs):
        if not self.is_fitted:
            raise NotFittedException(type(self))
        return method(self, *args, **kwargs)

    return wrapper

influence_factors

influence_factors(x: TensorType, y: TensorType) -> TensorType

Computes the approximation of

\[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

where the gradient is meant to be per sample of the batch \((x, y)\). For all input tensors it is assumed, that the first dimension is the batch dimension.

PARAMETER DESCRIPTION
x

model input to use in the gradient computations

TYPE: TensorType

y

label tensor to compute gradients

TYPE: TensorType

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise inverse Hessian matrix vector products

Source code in src/pydvl/influence/base_influence_function_model.py
def influence_factors(self, x: TensorType, y: TensorType) -> TensorType:
    r"""
    Computes the approximation of

    \[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

    where the gradient is meant to be per sample of the batch $(x, y)$.
    For all input tensors it is assumed,
    that the first dimension is the batch dimension.

    Args:
        x: model input to use in the gradient computations
        y: label tensor to compute gradients

    Returns:
        Tensor representing the element-wise inverse Hessian matrix vector products

    """
    if not self.is_fitted:
        raise NotFittedException(type(self))
    return self._influence_factors(x, y)

influence_factors_by_block

influence_factors_by_block(
    x: TensorType, y: TensorType
) -> OrderedDict[str, TensorType]

Compute the block-wise approximation of

\[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

where the gradient is meant to be per sample of the batch \((x, y)\).

PARAMETER DESCRIPTION
x

model input to use in the gradient computations

TYPE: TensorType

y

label tensor to compute gradients

TYPE: TensorType

RETURNS DESCRIPTION
OrderedDict[str, TensorType]

Ordered dictionary of tensors representing the element-wise

OrderedDict[str, TensorType]

approximate inverse Hessian matrix vector products per block.

Source code in src/pydvl/influence/base_influence_function_model.py
@InfluenceFunctionModel.fit_required
def influence_factors_by_block(
    self, x: TensorType, y: TensorType
) -> OrderedDict[str, TensorType]:
    r"""
    Compute the block-wise approximation of

    \[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

    where the gradient is meant to be per sample of the batch $(x, y)$.

    Args:
        x: model input to use in the gradient computations
        y: label tensor to compute gradients

    Returns:
        Ordered dictionary of tensors representing the element-wise
        approximate inverse Hessian matrix vector products per block.

    """
    return self.block_mapper.transformed_grads(self._create_batch(x, y))

influences

influences(
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = Up,
) -> TensorType

Computes the approximation of

\[ \langle H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}})), \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the case of up-weighting influence, resp.

\[ \langle H^{-1}\nabla_{\theta} \ell(y_{test}, f_{\theta}(x_{test})), \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the perturbation type influence case.

PARAMETER DESCRIPTION
x_test

model input to use in the gradient computations of \(H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))\)

TYPE: TensorType

y_test

label tensor to compute gradients

TYPE: TensorType

x

optional model input to use in the gradient computations \(\nabla_{theta}\ell(y, f_{\theta}(x))\), resp. \(\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))\), if None, use \(x=x_{test}\)

TYPE: Optional[TensorType] DEFAULT: None

y

optional label tensor to compute gradients

TYPE: Optional[TensorType] DEFAULT: None

mode

enum value of InfluenceMode

TYPE: InfluenceMode DEFAULT: Up

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise scalar products for the provided batch

Source code in src/pydvl/influence/base_influence_function_model.py
def influences(
    self,
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = InfluenceMode.Up,
) -> TensorType:
    r"""
    Computes the approximation of

    \[ \langle H^{-1}\nabla_{\theta} \ell(y_{\text{test}},
        f_{\theta}(x_{\text{test}})),
        \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the case of up-weighting influence, resp.

    \[ \langle H^{-1}\nabla_{\theta} \ell(y_{test}, f_{\theta}(x_{test})),
        \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the perturbation type influence case.

    Args:
        x_test: model input to use in the gradient computations
            of $H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))$
        y_test: label tensor to compute gradients
        x: optional model input to use in the gradient computations
            $\nabla_{theta}\ell(y, f_{\theta}(x))$,
            resp. $\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))$,
            if None, use $x=x_{test}$
        y: optional label tensor to compute gradients
        mode: enum value of [InfluenceMode]
            [pydvl.influence.base_influence_function_model.InfluenceMode]

    Returns:
        Tensor representing the element-wise scalar products for the provided batch

    """
    if not self.is_fitted:
        raise NotFittedException(type(self))

    if x is None and y is not None:
        raise ValueError(
            "Providing labels y, without providing model input x is not supported"
        )

    if x is not None and y is None:
        raise ValueError(
            "Providing model input x, without providing labels y is not supported"
        )

    return self._influences(x_test, y_test, x, y, mode)

influences_by_block

influences_by_block(
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = Up,
) -> OrderedDict[str, TensorType]

Compute the block-wise influence values for the provided data, i.e. an approximation of

\[ \langle H^{-1}\nabla_{theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}})), \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the case of up-weighting influence, resp.

\[ \langle H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test})), \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the perturbation type influence case.

PARAMETER DESCRIPTION
x_test

model input to use in the gradient computations of the approximation of \(H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))\)

TYPE: TensorType

y_test

label tensor to compute gradients

TYPE: TensorType

x

optional model input to use in the gradient computations \(\nabla_{theta}\ell(y, f_{\theta}(x))\), resp. \(\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))\), if None, use \(x=x_{test}\)

TYPE: Optional[TensorType] DEFAULT: None

y

optional label tensor to compute gradients

TYPE: Optional[TensorType] DEFAULT: None

mode

enum value of InfluenceMode

TYPE: InfluenceMode DEFAULT: Up

RETURNS DESCRIPTION
OrderedDict[str, TensorType]

Ordered dictionary of tensors representing the element-wise scalar products

OrderedDict[str, TensorType]

for the provided batch per block.

Source code in src/pydvl/influence/base_influence_function_model.py
@InfluenceFunctionModel.fit_required
def influences_by_block(
    self,
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = InfluenceMode.Up,
) -> OrderedDict[str, TensorType]:
    r"""
    Compute the block-wise influence values for the provided data, i.e. an
    approximation of

    \[ \langle H^{-1}\nabla_{theta} \ell(y_{\text{test}},
        f_{\theta}(x_{\text{test}})),
        \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the case of up-weighting influence, resp.

    \[ \langle H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test})),
        \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the perturbation type influence case.

    Args:
        x_test: model input to use in the gradient computations
            of the approximation of
            $H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))$
        y_test: label tensor to compute gradients
        x: optional model input to use in the gradient computations
            $\nabla_{theta}\ell(y, f_{\theta}(x))$,
            resp. $\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))$,
            if None, use $x=x_{test}$
        y: optional label tensor to compute gradients
        mode: enum value of [InfluenceMode]
            [pydvl.influence.base_influence_function_model.InfluenceMode]

    Returns:
        Ordered dictionary of tensors representing the element-wise scalar products
        for the provided batch per block.

    """
    left_batch = self._create_batch(x_test, y_test)

    if x is None:
        if y is not None:
            raise ValueError(
                "Providing labels y, without providing model input x "
                "is not supported"
            )
        right_batch = left_batch
    else:
        if y is None:
            raise ValueError(
                "Providing model input x, without providing labels y "
                "is not supported"
            )
        right_batch = self._create_batch(x, y)

    return self.block_mapper.interactions(left_batch, right_batch, mode)

influences_from_factors

influences_from_factors(
    z_test_factors: TensorType,
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = Up,
) -> TensorType

Computation of

\[ \langle z_{\text{test_factors}}, \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the case of up-weighting influence, resp.

\[ \langle z_{\text{test_factors}}, \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the perturbation type influence case. The gradient is meant to be per sample of the batch \((x, y)\).

PARAMETER DESCRIPTION
z_test_factors

pre-computed array, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)

TYPE: TensorType

x

model input to use in the gradient computations \(\nabla_{\theta}\ell(y, f_{\theta}(x))\), resp. \(\nabla_{x}\nabla_{\theta}\ell(y, f_{\theta}(x))\), if None, use \(x=x_{\text{test}}\)

TYPE: TensorType

y

label tensor to compute gradients

TYPE: TensorType

mode

enum value of InfluenceMode

TYPE: InfluenceMode DEFAULT: Up

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise scalar products for the provided batch

Source code in src/pydvl/influence/base_influence_function_model.py
@InfluenceFunctionModel.fit_required
def influences_from_factors(
    self,
    z_test_factors: TensorType,
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = InfluenceMode.Up,
) -> TensorType:
    r"""
    Computation of

    \[ \langle z_{\text{test_factors}},
        \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the case of up-weighting influence, resp.

    \[ \langle z_{\text{test_factors}},
        \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the perturbation type influence case. The gradient is meant to be per sample
    of the batch $(x, y)$.

    Args:
        z_test_factors: pre-computed array, approximating
            $H^{-1}\nabla_{\theta} \ell(y_{\text{test}},
            f_{\theta}(x_{\text{test}}))$
        x: model input to use in the gradient computations
            $\nabla_{\theta}\ell(y, f_{\theta}(x))$,
            resp. $\nabla_{x}\nabla_{\theta}\ell(y, f_{\theta}(x))$,
            if None, use $x=x_{\text{test}}$
        y: label tensor to compute gradients
        mode: enum value of [InfluenceMode]
            [pydvl.influence.base_influence_function_model.InfluenceMode]

    Returns:
        Tensor representing the element-wise scalar products for the provided batch

    """
    tensors = self.block_mapper.generate_interactions_from_transformed_grads(
        z_test_factors,
        self._create_batch(x, y),
        mode,
    )
    result: TensorType = next(tensors)
    for tensor in tensors:
        result = result + tensor
    return result

influences_from_factors_by_block

influences_from_factors_by_block(
    z_test_factors: OrderedDict[str, TensorType],
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = Up,
) -> OrderedDict[str, TensorType]

Block-wise computation of

\[ \langle z_{\text{test_factors}}, \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the case of up-weighting influence, resp.

\[ \langle z_{\text{test_factors}}, \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the perturbation type influence case. The gradient is meant to be per sample of the batch \((x, y)\).

PARAMETER DESCRIPTION
z_test_factors

pre-computed array, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)

TYPE: OrderedDict[str, TensorType]

x

model input to use in the gradient computations \(\nabla_{\theta}\ell(y, f_{\theta}(x))\), resp. \(\nabla_{x}\nabla_{\theta}\ell(y, f_{\theta}(x))\), if None, use \(x=x_{\text{test}}\)

TYPE: TensorType

y

label tensor to compute gradients

TYPE: TensorType

mode

enum value of InfluenceMode

TYPE: InfluenceMode DEFAULT: Up

RETURNS DESCRIPTION
OrderedDict[str, TensorType]

Ordered dictionary of tensors representing the element-wise scalar products

OrderedDict[str, TensorType]

for the provided batch per block

Source code in src/pydvl/influence/base_influence_function_model.py
@InfluenceFunctionModel.fit_required
def influences_from_factors_by_block(
    self,
    z_test_factors: OrderedDict[str, TensorType],
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = InfluenceMode.Up,
) -> OrderedDict[str, TensorType]:
    r"""
    Block-wise computation of

    \[ \langle z_{\text{test_factors}},
        \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the case of up-weighting influence, resp.

    \[ \langle z_{\text{test_factors}},
        \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the perturbation type influence case. The gradient is meant to be per sample
    of the batch $(x, y)$.

    Args:
        z_test_factors: pre-computed array, approximating
            $H^{-1}\nabla_{\theta} \ell(y_{\text{test}},
            f_{\theta}(x_{\text{test}}))$
        x: model input to use in the gradient computations
            $\nabla_{\theta}\ell(y, f_{\theta}(x))$,
            resp. $\nabla_{x}\nabla_{\theta}\ell(y, f_{\theta}(x))$,
            if None, use $x=x_{\text{test}}$
        y: label tensor to compute gradients
        mode: enum value of [InfluenceMode]
            [pydvl.influence.base_influence_function_model.InfluenceMode]

    Returns:
        Ordered dictionary of tensors representing the element-wise scalar products
        for the provided batch per block

    """
    return self.block_mapper.interactions_from_transformed_grads(
        z_test_factors, self._create_batch(x, y), mode
    )

InfluenceFunctionModel

Bases: Generic[TensorType, DataLoaderType], ABC

Generic abstract base class for computing influence related quantities. For a specific influence algorithm and tensor framework, inherit from this base class

is_fitted abstractmethod property

is_fitted

Override this, to expose the fitting status of the instance.

is_thread_safe abstractmethod property

is_thread_safe: bool

Whether the influence computation is thread safe

n_parameters abstractmethod property

n_parameters

Number of trainable parameters of the underlying model

_influence_factors abstractmethod

_influence_factors(x: TensorType, y: TensorType) -> TensorType

Override this method to implement the approximation of

\[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

where the gradient is meant to be per sample of the batch \((x, y)\).

PARAMETER DESCRIPTION
x

model input to use in the gradient computations

TYPE: TensorType

y

label tensor to compute gradients

TYPE: TensorType

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise inverse Hessian matrix vector products

Source code in src/pydvl/influence/base_influence_function_model.py
@abstractmethod
def _influence_factors(self, x: TensorType, y: TensorType) -> TensorType:
    r"""
    Override this method to implement the approximation of

    \[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

    where the gradient is meant to be per sample of the batch $(x, y)$.

    Args:
        x: model input to use in the gradient computations
        y: label tensor to compute gradients

    Returns:
        Tensor representing the element-wise inverse Hessian matrix vector products

    """

_influences abstractmethod

_influences(
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = Up,
) -> TensorType

Override this method to implement the approximation of

\[ \langle H^{-1}\nabla_{theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}})), \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the case of up-weighting influence, resp.

\[ \langle H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test})), \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the perturbation type influence case.

PARAMETER DESCRIPTION
x_test

model input to use in the gradient computations of \(H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))\)

TYPE: TensorType

y_test

label tensor to compute gradients

TYPE: TensorType

x

optional model input to use in the gradient computations \(\nabla_{theta}\ell(y, f_{\theta}(x))\), resp. \(\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))\), if None, use \(x=x_{test}\)

TYPE: Optional[TensorType] DEFAULT: None

y

optional label tensor to compute gradients

TYPE: Optional[TensorType] DEFAULT: None

mode

enum value of InfluenceMode

TYPE: InfluenceMode DEFAULT: Up

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise scalar products for the provided batch

Source code in src/pydvl/influence/base_influence_function_model.py
@abstractmethod
def _influences(
    self,
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = InfluenceMode.Up,
) -> TensorType:
    r"""
    Override this method to implement the approximation of

    \[ \langle H^{-1}\nabla_{theta} \ell(y_{\text{test}},
        f_{\theta}(x_{\text{test}})),
        \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the case of up-weighting influence, resp.

    \[ \langle H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test})),
        \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the perturbation type influence case.

    Args:
        x_test: model input to use in the gradient computations
            of $H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))$
        y_test: label tensor to compute gradients
        x: optional model input to use in the gradient computations
            $\nabla_{theta}\ell(y, f_{\theta}(x))$,
            resp. $\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))$,
            if None, use $x=x_{test}$
        y: optional label tensor to compute gradients
        mode: enum value of [InfluenceMode]
            [pydvl.influence.base_influence_function_model.InfluenceMode]

    Returns:
        Tensor representing the element-wise scalar products for the provided batch

    """

fit abstractmethod

fit(data: DataLoaderType) -> InfluenceFunctionModel

Override this method to fit the influence function model to training data, e.g. pre-compute hessian matrix or matrix decompositions

PARAMETER DESCRIPTION
data

TYPE: DataLoaderType

RETURNS DESCRIPTION
InfluenceFunctionModel

The fitted instance

Source code in src/pydvl/influence/base_influence_function_model.py
@abstractmethod
def fit(self, data: DataLoaderType) -> InfluenceFunctionModel:
    """
    Override this method to fit the influence function model to training data,
    e.g. pre-compute hessian matrix or matrix decompositions

    Args:
        data:

    Returns:
        The fitted instance
    """

fit_required staticmethod

fit_required(method)

Decorator to enforce the fitted check

Source code in src/pydvl/influence/base_influence_function_model.py
@staticmethod
def fit_required(method):
    """Decorator to enforce the fitted check"""

    @wraps(method)
    def wrapper(self, *args, **kwargs):
        if not self.is_fitted:
            raise NotFittedException(type(self))
        return method(self, *args, **kwargs)

    return wrapper

influence_factors

influence_factors(x: TensorType, y: TensorType) -> TensorType

Computes the approximation of

\[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

where the gradient is meant to be per sample of the batch \((x, y)\). For all input tensors it is assumed, that the first dimension is the batch dimension.

PARAMETER DESCRIPTION
x

model input to use in the gradient computations

TYPE: TensorType

y

label tensor to compute gradients

TYPE: TensorType

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise inverse Hessian matrix vector products

Source code in src/pydvl/influence/base_influence_function_model.py
def influence_factors(self, x: TensorType, y: TensorType) -> TensorType:
    r"""
    Computes the approximation of

    \[ H^{-1}\nabla_{\theta} \ell(y, f_{\theta}(x)) \]

    where the gradient is meant to be per sample of the batch $(x, y)$.
    For all input tensors it is assumed,
    that the first dimension is the batch dimension.

    Args:
        x: model input to use in the gradient computations
        y: label tensor to compute gradients

    Returns:
        Tensor representing the element-wise inverse Hessian matrix vector products

    """
    if not self.is_fitted:
        raise NotFittedException(type(self))
    return self._influence_factors(x, y)

influences

influences(
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = Up,
) -> TensorType

Computes the approximation of

\[ \langle H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}})), \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the case of up-weighting influence, resp.

\[ \langle H^{-1}\nabla_{\theta} \ell(y_{test}, f_{\theta}(x_{test})), \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the perturbation type influence case.

PARAMETER DESCRIPTION
x_test

model input to use in the gradient computations of \(H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))\)

TYPE: TensorType

y_test

label tensor to compute gradients

TYPE: TensorType

x

optional model input to use in the gradient computations \(\nabla_{theta}\ell(y, f_{\theta}(x))\), resp. \(\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))\), if None, use \(x=x_{test}\)

TYPE: Optional[TensorType] DEFAULT: None

y

optional label tensor to compute gradients

TYPE: Optional[TensorType] DEFAULT: None

mode

enum value of InfluenceMode

TYPE: InfluenceMode DEFAULT: Up

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise scalar products for the provided batch

Source code in src/pydvl/influence/base_influence_function_model.py
def influences(
    self,
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = InfluenceMode.Up,
) -> TensorType:
    r"""
    Computes the approximation of

    \[ \langle H^{-1}\nabla_{\theta} \ell(y_{\text{test}},
        f_{\theta}(x_{\text{test}})),
        \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the case of up-weighting influence, resp.

    \[ \langle H^{-1}\nabla_{\theta} \ell(y_{test}, f_{\theta}(x_{test})),
        \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the perturbation type influence case.

    Args:
        x_test: model input to use in the gradient computations
            of $H^{-1}\nabla_{theta} \ell(y_{test}, f_{\theta}(x_{test}))$
        y_test: label tensor to compute gradients
        x: optional model input to use in the gradient computations
            $\nabla_{theta}\ell(y, f_{\theta}(x))$,
            resp. $\nabla_{x}\nabla_{theta}\ell(y, f_{\theta}(x))$,
            if None, use $x=x_{test}$
        y: optional label tensor to compute gradients
        mode: enum value of [InfluenceMode]
            [pydvl.influence.base_influence_function_model.InfluenceMode]

    Returns:
        Tensor representing the element-wise scalar products for the provided batch

    """
    if not self.is_fitted:
        raise NotFittedException(type(self))

    if x is None and y is not None:
        raise ValueError(
            "Providing labels y, without providing model input x is not supported"
        )

    if x is not None and y is None:
        raise ValueError(
            "Providing model input x, without providing labels y is not supported"
        )

    return self._influences(x_test, y_test, x, y, mode)

influences_from_factors abstractmethod

influences_from_factors(
    z_test_factors: TensorType,
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = Up,
) -> TensorType

Override this method to implement the computation of

\[ \langle z_{\text{test_factors}}, \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the case of up-weighting influence, resp.

\[ \langle z_{\text{test_factors}}, \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

for the perturbation type influence case. The gradient is meant to be per sample of the batch \((x, y)\).

PARAMETER DESCRIPTION
z_test_factors

pre-computed array, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)

TYPE: TensorType

x

model input to use in the gradient computations \(\nabla_{\theta}\ell(y, f_{\theta}(x))\), resp. \(\nabla_{x}\nabla_{\theta}\ell(y, f_{\theta}(x))\), if None, use \(x=x_{\text{test}}\)

TYPE: TensorType

y

label tensor to compute gradients

TYPE: TensorType

mode

enum value of InfluenceMode

TYPE: InfluenceMode DEFAULT: Up

RETURNS DESCRIPTION
TensorType

Tensor representing the element-wise scalar products for the provided batch

Source code in src/pydvl/influence/base_influence_function_model.py
@abstractmethod
def influences_from_factors(
    self,
    z_test_factors: TensorType,
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = InfluenceMode.Up,
) -> TensorType:
    r"""
    Override this method to implement the computation of

    \[ \langle z_{\text{test_factors}},
        \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the case of up-weighting influence, resp.

    \[ \langle z_{\text{test_factors}},
        \nabla_{x} \nabla_{\theta} \ell(y, f_{\theta}(x)) \rangle \]

    for the perturbation type influence case. The gradient is meant to be per sample
    of the batch $(x, y)$.

    Args:
        z_test_factors: pre-computed array, approximating
            $H^{-1}\nabla_{\theta} \ell(y_{\text{test}},
            f_{\theta}(x_{\text{test}}))$
        x: model input to use in the gradient computations
            $\nabla_{\theta}\ell(y, f_{\theta}(x))$,
            resp. $\nabla_{x}\nabla_{\theta}\ell(y, f_{\theta}(x))$,
            if None, use $x=x_{\text{test}}$
        y: label tensor to compute gradients
        mode: enum value of [InfluenceMode]
            [pydvl.influence.base_influence_function_model.InfluenceMode]

    Returns:
        Tensor representing the element-wise scalar products for the provided batch

    """