Skip to content

pydvl.influence.torch.base

TorchBatch dataclass

TorchBatch(x: Tensor, y: Tensor)

Bases: Batch

A convenience class for handling batches of data. Validates, the alignment of the first dimension (batch dimension) of the input and target tensor

ATTRIBUTE DESCRIPTION
x

The input tensor that contains features or data points.

TYPE: Tensor

y

The target tensor that contains labels corresponding to the inputs.

TYPE: Tensor

TorchGradientProvider

TorchGradientProvider(
    model: Module, loss: LossType, restrict_to: Optional[Dict[str, Parameter]]
)

Bases: GradientProvider[TorchBatch, Tensor]

Compute per-sample gradients of a function defined by a torch.nn.Module and a loss function using torch.func.

Consider a function

\[ \ell: \mathbb{R}^{d_1} \times \mathbb{R}^{d_2} \times \mathbb{R}^{n} \times \mathbb{R}^{n}, \quad \ell(\omega_1, \omega_2, x, y) = \operatorname{loss}(f(\omega_1, \omega_2; x), y) \]

e.g. a two layer neural network \(f\) with a loss function, then this object should compute the expressions:

\[ \nabla_{\omega_{i}}\ell(\omega_1, \omega_2, x, y), \nabla_{\omega_{i}}\nabla_{x}\ell(\omega_1, \omega_2, x, y), \nabla_{\omega}\ell(\omega_1, \omega_2, x, y) \cdot v\]
Source code in src/pydvl/influence/torch/base.py
def __init__(
    self,
    model: torch.nn.Module,
    loss: LossType,
    restrict_to: Optional[Dict[str, torch.nn.Parameter]],
):
    self.model = model
    self.loss = loss

    if restrict_to is None:
        restrict_to = ModelParameterDictBuilder(model).build_from_block_mode(
            BlockMode.FULL
        )

    self.params_to_restrict_to = restrict_to

grads

grads(batch: TorchBatch) -> Dict[str, Tensor]

Computes and returns a dictionary mapping parameter names to their respective per-sample gradients. Given the example in the class docstring, this means

\[ \text{result}[\omega_i] = \nabla_{\omega_{i}}\ell(\omega_1, \omega_2, \text{batch.x}, \text{batch.y}), \]

where the first dimension of the resulting tensors is always considered to be the batch dimension, so the shape of the resulting tensors are \((N, d_i)\), where \(N\) is the number of samples in the batch.

PARAMETER DESCRIPTION
batch

The batch of data for which to compute gradients.

TYPE: TorchBatch

RETURNS DESCRIPTION
Dict[str, Tensor]

A dictionary where keys are gradient identifiers and values are the gradients computed per sample.

Source code in src/pydvl/influence/torch/base.py
def grads(self, batch: TorchBatch) -> Dict[str, torch.Tensor]:
    r"""
    Computes and returns a dictionary mapping parameter names to their respective
    per-sample gradients. Given the example in the class docstring, this means

    $$ \text{result}[\omega_i] = \nabla_{\omega_{i}}\ell(\omega_1, \omega_2,
        \text{batch.x}, \text{batch.y}), $$

    where the first dimension of the resulting tensors is always considered to be
    the batch dimension, so the shape of the resulting tensors are $(N, d_i)$,
    where $N$ is the number of samples in the batch.

    Args:
        batch: The batch of data for which to compute gradients.

    Returns:
        A dictionary where keys are gradient identifiers and values are the
            gradients computed per sample.
    """
    gradient_dict = self._grads(batch.to(self.device))
    return self._detach_dict(gradient_dict)

mixed_grads

mixed_grads(batch: TorchBatch) -> Dict[str, Tensor]

Computes and returns a dictionary mapping gradient names to their respective per-sample mixed gradients. In this context, mixed gradients refer to computing gradients with respect to the instance definition in addition to compute derivatives with respect to the input batch. Given the example in the class docstring, this means

\[ \text{result}[\omega_i] = \nabla_{\omega_{i}}\nabla_{x}\ell(\omega_1, \omega_2, \text{batch.x}, \text{batch.y}), \]

where the first dimension of the resulting tensors is always considered to be the batch dimension and the last to be the non-batch input related derivatives. So the shape of the resulting tensors are \((N, n, d_i)\), where \(N\) is the number of samples in the batch.

PARAMETER DESCRIPTION
batch

The batch of data for which to compute mixed gradients.

TYPE: TorchBatch

RETURNS DESCRIPTION
Dict[str, Tensor]

A dictionary where keys are gradient identifiers and values are the mixed gradients computed per sample.

Source code in src/pydvl/influence/torch/base.py
def mixed_grads(self, batch: TorchBatch) -> Dict[str, torch.Tensor]:
    r"""
    Computes and returns a dictionary mapping gradient names to their respective
    per-sample mixed gradients. In this context, mixed gradients refer to computing
    gradients with respect to the instance definition in addition to
    compute derivatives with respect to the input batch.
    Given the example in the class docstring, this means

    $$ \text{result}[\omega_i] = \nabla_{\omega_{i}}\nabla_{x}\ell(\omega_1,
        \omega_2, \text{batch.x}, \text{batch.y}), $$

    where the first dimension of the resulting tensors is always considered to be
    the batch dimension and the last to be the non-batch input related derivatives.
    So the shape of the resulting tensors are $(N, n, d_i)$,
    where $N$ is the number of samples in the batch.

    Args:
        batch: The batch of data for which to compute mixed gradients.

    Returns:
        A dictionary where keys are gradient identifiers and values are the
            mixed gradients computed per sample.
    """
    gradient_dict = self._mixed_grads(batch.to(self.device))
    return self._detach_dict(gradient_dict)

jacobian_prod

jacobian_prod(batch: TorchBatch, g: Tensor) -> Tensor

Computes the matrix-Jacobian product for the provided batch and input tensor. Given the example in the class docstring, this means

\[ (\nabla_{\omega_{1}}\ell(\omega_1, \omega_2, \text{batch.x}, \text{batch.y}), \nabla_{\omega_{2}}\ell(\omega_1, \omega_2, \text{batch.x}, \text{batch.y})) \cdot g^T\]

where g must be a tensor of shape \((K, d_1+d_2)\), so the resulting tensor is of shape \((N, K)\).

PARAMETER DESCRIPTION
batch

The batch of data for which to compute the Jacobian.

TYPE: TorchBatch

g

The tensor to be used in the matrix-Jacobian product calculation.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

The resulting tensor from the matrix-Jacobian product computation.

Source code in src/pydvl/influence/torch/base.py
def jacobian_prod(
    self,
    batch: TorchBatch,
    g: torch.Tensor,
) -> torch.Tensor:
    r"""
    Computes the matrix-Jacobian product for the provided batch and input tensor.
    Given the example in the class docstring, this means

    $$ (\nabla_{\omega_{1}}\ell(\omega_1, \omega_2,
        \text{batch.x}, \text{batch.y}),
        \nabla_{\omega_{2}}\ell(\omega_1, \omega_2,
        \text{batch.x}, \text{batch.y})) \cdot g^T$$

    where g must be a tensor of shape $(K, d_1+d_2)$, so the resulting tensor
    is of shape $(N, K)$.

    Args:
        batch: The batch of data for which to compute the Jacobian.
        g: The tensor to be used in the matrix-Jacobian product
            calculation.

    Returns:
        The resulting tensor from the matrix-Jacobian product computation.
    """
    result = self._jacobian_prod(batch.to(self.device), g.to(self.device))
    if result.requires_grad:
        result = result.detach()
    return result

OperatorBilinearForm

OperatorBilinearForm(operator: TorchOperatorType)

Bases: BilinearForm[Tensor, TorchBatch, TorchGradientProvider]

Base class for bilinear forms based on an instance of [TorchOperator][pydvl.influence.torch.operator.base.TorchOperator]. This means it computes weighted inner products of the form:

\[ \langle \operatorname{Op}(x), y \rangle \]
Source code in src/pydvl/influence/torch/base.py
def __init__(
    self,
    operator: TorchOperatorType,
):
    self.operator = operator

grads_inner_prod

grads_inner_prod(
    left: BatchType,
    right: Optional[BatchType],
    gradient_provider: GradientProviderType,
) -> TensorType

Computes the gradient inner product of two batches of data, i.e.

\[ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}), \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}\]

where \(\nabla_{\omega}\ell(\omega, \cdot, \cdot)\) is represented by the gradient_provider and the expression must be understood sample-wise.

PARAMETER DESCRIPTION
left

The first batch for gradient and inner product computation

TYPE: BatchType

right

The second batch for gradient and inner product computation, optional; if not provided, the inner product will use the gradient computed for left for both arguments.

TYPE: Optional[BatchType]

gradient_provider

The gradient provider to compute the gradients.

TYPE: GradientProviderType

RETURNS DESCRIPTION
TensorType

A tensor representing the inner products of the per-sample gradients

Source code in src/pydvl/influence/types.py
def grads_inner_prod(
    self,
    left: BatchType,
    right: Optional[BatchType],
    gradient_provider: GradientProviderType,
) -> TensorType:
    r"""
    Computes the gradient inner product of two batches of data, i.e.

    $$ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}),
    \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}$$

    where $\nabla_{\omega}\ell(\omega, \cdot, \cdot)$ is represented by the
    `gradient_provider` and the expression must be understood sample-wise.

    Args:
        left: The first batch for gradient and inner product computation
        right: The second batch for gradient and inner product computation,
            optional; if not provided, the inner product will use the gradient
            computed for `left` for both arguments.
        gradient_provider: The gradient provider to compute the gradients.

    Returns:
        A tensor representing the inner products of the per-sample gradients
    """
    left_grad = gradient_provider.flat_grads(left)
    if right is None:
        right_grad = left_grad
    else:
        right_grad = gradient_provider.flat_grads(right)
    return self.inner_prod(left_grad, right_grad)

mixed_grads_inner_prod

mixed_grads_inner_prod(
    left: BatchType,
    right: Optional[BatchType],
    gradient_provider: GradientProviderType,
) -> TensorType

Computes the mixed gradient inner product of two batches of data, i.e.

\[ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}), \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}\]

where \(\nabla_{\omega}\ell(\omega, \cdot)\) and \(\nabla_{\omega}\nabla_{x}\ell(\omega, \cdot)\) are represented by the gradient_provider. The expression must be understood sample-wise.

PARAMETER DESCRIPTION
left

The first batch for gradient and inner product computation

TYPE: BatchType

right

The second batch for gradient and inner product computation

TYPE: Optional[BatchType]

gradient_provider

The gradient provider to compute the gradients.

TYPE: GradientProviderType

RETURNS DESCRIPTION
TensorType

A tensor representing the inner products of the mixed per-sample gradients

Source code in src/pydvl/influence/types.py
def mixed_grads_inner_prod(
    self,
    left: BatchType,
    right: Optional[BatchType],
    gradient_provider: GradientProviderType,
) -> TensorType:
    r"""
    Computes the mixed gradient inner product of two batches of data, i.e.

    $$ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}),
    \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y})
    \rangle_{B}$$

    where $\nabla_{\omega}\ell(\omega, \cdot)$ and
    $\nabla_{\omega}\nabla_{x}\ell(\omega, \cdot)$ are represented by the
    `gradient_provider`. The expression must be understood sample-wise.

    Args:
        left: The first batch for gradient and inner product computation
        right: The second batch for gradient and inner product computation
        gradient_provider: The gradient provider to compute the gradients.

    Returns:
        A tensor representing the inner products of the mixed per-sample gradients
    """
    left_grad = gradient_provider.flat_grads(left)
    if right is None:
        right = left
    right_mixed_grad = gradient_provider.flat_mixed_grads(right)
    return self.inner_prod(left_grad, right_mixed_grad)

inner_prod

inner_prod(left: Tensor, right: Optional[Tensor]) -> Tensor

Computes the weighted inner product of two vectors, i.e.

\[ \langle \operatorname{Op}(\text{left}), \text{right} \rangle \]
PARAMETER DESCRIPTION
left

The first tensor in the inner product computation.

TYPE: Tensor

right

The second tensor, optional; if not provided, the inner product will use left tensor for both arguments.

TYPE: Optional[Tensor]

RETURNS DESCRIPTION
Tensor

A tensor representing the inner product.

Source code in src/pydvl/influence/torch/base.py
def inner_prod(
    self, left: torch.Tensor, right: Optional[torch.Tensor]
) -> torch.Tensor:
    r"""
    Computes the weighted inner product of two vectors, i.e.

    $$ \langle \operatorname{Op}(\text{left}), \text{right} \rangle $$

    Args:
        left: The first tensor in the inner product computation.
        right: The second tensor, optional; if not provided, the inner product will
            use `left` tensor for both arguments.

    Returns:
        A tensor representing the inner product.
    """
    if right is None:
        right = left
    if left.shape[0] <= right.shape[0]:
        return self._inner_product(left, right)
    return self._inner_product(right, left).T

DictBilinearForm

DictBilinearForm(operator: 'TensorDictOperator')

Bases: OperatorBilinearForm

Base class for bilinear forms based on an instance of [TorchOperator][pydvl.influence.torch.operator.base.TorchOperator]. This means it computes weighted inner products of the form:

\[ \langle \operatorname{Op}(x), y \rangle \]
Source code in src/pydvl/influence/torch/base.py
def __init__(
    self,
    operator: "TensorDictOperator",
):
    super().__init__(operator)

inner_prod

inner_prod(left: Tensor, right: Optional[Tensor]) -> Tensor

Computes the weighted inner product of two vectors, i.e.

\[ \langle \operatorname{Op}(\text{left}), \text{right} \rangle \]
PARAMETER DESCRIPTION
left

The first tensor in the inner product computation.

TYPE: Tensor

right

The second tensor, optional; if not provided, the inner product will use left tensor for both arguments.

TYPE: Optional[Tensor]

RETURNS DESCRIPTION
Tensor

A tensor representing the inner product.

Source code in src/pydvl/influence/torch/base.py
def inner_prod(
    self, left: torch.Tensor, right: Optional[torch.Tensor]
) -> torch.Tensor:
    r"""
    Computes the weighted inner product of two vectors, i.e.

    $$ \langle \operatorname{Op}(\text{left}), \text{right} \rangle $$

    Args:
        left: The first tensor in the inner product computation.
        right: The second tensor, optional; if not provided, the inner product will
            use `left` tensor for both arguments.

    Returns:
        A tensor representing the inner product.
    """
    if right is None:
        right = left
    if left.shape[0] <= right.shape[0]:
        return self._inner_product(left, right)
    return self._inner_product(right, left).T

grads_inner_prod

grads_inner_prod(
    left: TorchBatch,
    right: Optional[TorchBatch],
    gradient_provider: TorchGradientProvider,
) -> Tensor

Computes the gradient inner product of two batches of data, i.e.

\[ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}), \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}\]

where \(\nabla_{\omega}\ell(\omega, \cdot, \cdot)\) is represented by the gradient_provider and the expression must be understood sample-wise.

PARAMETER DESCRIPTION
left

The first batch for gradient and inner product computation

TYPE: TorchBatch

right

The second batch for gradient and inner product computation, optional; if not provided, the inner product will use the gradient computed for left for both arguments.

TYPE: Optional[TorchBatch]

gradient_provider

The gradient provider to compute the gradients.

TYPE: TorchGradientProvider

RETURNS DESCRIPTION
Tensor

A tensor representing the inner products of the per-sample gradients

Source code in src/pydvl/influence/torch/base.py
def grads_inner_prod(
    self,
    left: TorchBatch,
    right: Optional[TorchBatch],
    gradient_provider: TorchGradientProvider,
) -> torch.Tensor:
    r"""
    Computes the gradient inner product of two batches of data, i.e.

    $$ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}),
    \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}$$

    where $\nabla_{\omega}\ell(\omega, \cdot, \cdot)$ is represented by the
    `gradient_provider` and the expression must be understood sample-wise.

    Args:
        left: The first batch for gradient and inner product computation
        right: The second batch for gradient and inner product computation,
            optional; if not provided, the inner product will use the gradient
            computed for `left` for both arguments.
        gradient_provider: The gradient provider to compute the gradients.

    Returns:
        A tensor representing the inner products of the per-sample gradients
    """
    operator = cast(TensorDictOperator, self.operator)
    left_grads = gradient_provider.grads(left)
    if right is None:
        right_grads = left_grads
    else:
        right_grads = gradient_provider.grads(right)

    left_batch_size, right_batch_size = next(
        (
            (l.shape[0], r.shape[0])
            for r, l in zip(left_grads.values(), right_grads.values())
        )
    )

    if left_batch_size <= right_batch_size:
        left_grads = operator.apply_to_dict(left_grads)
        tensor_pairs = zip(left_grads.values(), right_grads.values())
    else:
        right_grads = operator.apply_to_dict(right_grads)
        tensor_pairs = zip(left_grads.values(), right_grads.values())

    tensors_to_reduce = (
        self._aggregate_grads(left, right) for left, right in tensor_pairs
    )

    return cast(torch.Tensor, sum(tensors_to_reduce))

mixed_grads_inner_prod

mixed_grads_inner_prod(
    left: TorchBatch,
    right: Optional[TorchBatch],
    gradient_provider: TorchGradientProvider,
) -> Tensor

Computes the mixed gradient inner product of two batches of data, i.e.

\[ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}), \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}\]

where \(\nabla_{\omega}\ell(\omega, \cdot)\) and \(\nabla_{\omega}\nabla_{x}\ell(\omega, \cdot)\) are represented by the gradient_provider. The expression must be understood sample-wise.

PARAMETER DESCRIPTION
left

The first batch for gradient and inner product computation

TYPE: TorchBatch

right

The second batch for gradient and inner product computation

TYPE: Optional[TorchBatch]

gradient_provider

The gradient provider to compute the gradients.

TYPE: TorchGradientProvider

RETURNS DESCRIPTION
Tensor

A tensor representing the inner products of the mixed per-sample gradients

Source code in src/pydvl/influence/torch/base.py
def mixed_grads_inner_prod(
    self,
    left: TorchBatch,
    right: Optional[TorchBatch],
    gradient_provider: TorchGradientProvider,
) -> torch.Tensor:
    r"""
    Computes the mixed gradient inner product of two batches of data, i.e.

    $$ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}),
    \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y})
    \rangle_{B}$$

    where $\nabla_{\omega}\ell(\omega, \cdot)$ and
    $\nabla_{\omega}\nabla_{x}\ell(\omega, \cdot)$ are represented by the
    `gradient_provider`. The expression must be understood sample-wise.

    Args:
        left: The first batch for gradient and inner product computation
        right: The second batch for gradient and inner product computation
        gradient_provider: The gradient provider to compute the gradients.

    Returns:
        A tensor representing the inner products of the mixed per-sample gradients
    """
    operator = cast(TensorDictOperator, self.operator)
    if right is None:
        right = left
    right_grads = gradient_provider.mixed_grads(right)
    left_grads = gradient_provider.grads(left)
    left_grads = operator.apply_to_dict(left_grads)
    left_grads_views = (t.reshape(t.shape[0], -1) for t in left_grads.values())
    right_grads_views = (
        t.reshape(*right.x.shape, -1) for t in right_grads.values()
    )
    tensor_pairs = zip(left_grads_views, right_grads_views)
    tensors_to_reduce = (
        self._aggregate_mixed_grads(left, right) for left, right in tensor_pairs
    )
    return cast(torch.Tensor, sum(tensors_to_reduce))

LowRankBilinearForm

LowRankBilinearForm(operator: 'LowRankOperator')

Bases: OperatorBilinearForm

Specialized bilinear form for operators of the type

\[ \operatorname{Op}(b) = V D^{-1}V^Tb.\]

It computes the expressions

\[ \langle \operatorname{Op}(\nabla_{\theta} \ell(z, \theta)), \nabla_{\theta} \ell(z^{\prime}, \theta) \rangle = \langle V\nabla_{\theta} \ell(z, \theta), D^{-1}V\nabla_{\theta} \ell(z^{\prime}, \theta) \rangle\]

in an efficient way using torch.autograd functionality.

Source code in src/pydvl/influence/torch/base.py
def __init__(self, operator: "LowRankOperator"):
    super().__init__(operator)

inner_prod

inner_prod(left: Tensor, right: Optional[Tensor]) -> Tensor

Computes the weighted inner product of two vectors, i.e.

\[ \langle \operatorname{Op}(\text{left}), \text{right} \rangle \]
PARAMETER DESCRIPTION
left

The first tensor in the inner product computation.

TYPE: Tensor

right

The second tensor, optional; if not provided, the inner product will use left tensor for both arguments.

TYPE: Optional[Tensor]

RETURNS DESCRIPTION
Tensor

A tensor representing the inner product.

Source code in src/pydvl/influence/torch/base.py
def inner_prod(
    self, left: torch.Tensor, right: Optional[torch.Tensor]
) -> torch.Tensor:
    r"""
    Computes the weighted inner product of two vectors, i.e.

    $$ \langle \operatorname{Op}(\text{left}), \text{right} \rangle $$

    Args:
        left: The first tensor in the inner product computation.
        right: The second tensor, optional; if not provided, the inner product will
            use `left` tensor for both arguments.

    Returns:
        A tensor representing the inner product.
    """
    if right is None:
        right = left
    if left.shape[0] <= right.shape[0]:
        return self._inner_product(left, right)
    return self._inner_product(right, left).T

mixed_grads_inner_prod

mixed_grads_inner_prod(
    left: BatchType,
    right: Optional[BatchType],
    gradient_provider: GradientProviderType,
) -> TensorType

Computes the mixed gradient inner product of two batches of data, i.e.

\[ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}), \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}\]

where \(\nabla_{\omega}\ell(\omega, \cdot)\) and \(\nabla_{\omega}\nabla_{x}\ell(\omega, \cdot)\) are represented by the gradient_provider. The expression must be understood sample-wise.

PARAMETER DESCRIPTION
left

The first batch for gradient and inner product computation

TYPE: BatchType

right

The second batch for gradient and inner product computation

TYPE: Optional[BatchType]

gradient_provider

The gradient provider to compute the gradients.

TYPE: GradientProviderType

RETURNS DESCRIPTION
TensorType

A tensor representing the inner products of the mixed per-sample gradients

Source code in src/pydvl/influence/types.py
def mixed_grads_inner_prod(
    self,
    left: BatchType,
    right: Optional[BatchType],
    gradient_provider: GradientProviderType,
) -> TensorType:
    r"""
    Computes the mixed gradient inner product of two batches of data, i.e.

    $$ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}),
    \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y})
    \rangle_{B}$$

    where $\nabla_{\omega}\ell(\omega, \cdot)$ and
    $\nabla_{\omega}\nabla_{x}\ell(\omega, \cdot)$ are represented by the
    `gradient_provider`. The expression must be understood sample-wise.

    Args:
        left: The first batch for gradient and inner product computation
        right: The second batch for gradient and inner product computation
        gradient_provider: The gradient provider to compute the gradients.

    Returns:
        A tensor representing the inner products of the mixed per-sample gradients
    """
    left_grad = gradient_provider.flat_grads(left)
    if right is None:
        right = left
    right_mixed_grad = gradient_provider.flat_mixed_grads(right)
    return self.inner_prod(left_grad, right_mixed_grad)

grads_inner_prod

grads_inner_prod(
    left: TorchBatch,
    right: Optional[TorchBatch],
    gradient_provider: TorchGradientProvider,
) -> Tensor

Computes the gradient inner product of two batches of data, i.e.

\[ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}), \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}\]

where \(\nabla_{\omega}\ell(\omega, \cdot, \cdot)\) is represented by the gradient_provider and the expression must be understood sample-wise.

PARAMETER DESCRIPTION
left

The first batch for gradient and inner product computation

TYPE: TorchBatch

right

The second batch for gradient and inner product computation, optional; if not provided, the inner product will use the gradient computed for left for both arguments.

TYPE: Optional[TorchBatch]

gradient_provider

The gradient provider to compute the gradients.

TYPE: TorchGradientProvider

RETURNS DESCRIPTION
Tensor

A tensor representing the inner products of the per-sample gradients

Source code in src/pydvl/influence/torch/base.py
def grads_inner_prod(
    self,
    left: TorchBatch,
    right: Optional[TorchBatch],
    gradient_provider: TorchGradientProvider,
) -> torch.Tensor:
    r"""
    Computes the gradient inner product of two batches of data, i.e.

    $$ \langle \nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y}),
    \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle_{B}$$

    where $\nabla_{\omega}\ell(\omega, \cdot, \cdot)$ is represented by the
    `gradient_provider` and the expression must be understood sample-wise.

    Args:
        left: The first batch for gradient and inner product computation
        right: The second batch for gradient and inner product computation,
            optional; if not provided, the inner product will use the gradient
            computed for `left` for both arguments.
        gradient_provider: The gradient provider to compute the gradients.

    Returns:
        A tensor representing the inner products of the per-sample gradients
    """
    op = cast("LowRankOperator", self.operator)

    if op.exact:
        return super().grads_inner_prod(left, right, gradient_provider)

    V = op.low_rank_representation.projections
    D = op.low_rank_representation.eigen_vals.clone()
    regularization = op.regularization

    if regularization is not None:
        D += regularization

    V_left = gradient_provider.jacobian_prod(left, V.t())
    D_inv = 1.0 / D

    if right is None:
        V_right = V_left
    else:
        V_right = gradient_provider.jacobian_prod(right, V.t())

    V_right = V_right * D_inv.unsqueeze(-1)

    return torch.einsum("ij, ik -> jk", V_left, V_right)

TensorOperator

Bases: Operator[Tensor, OperatorBilinearForm], ABC

Abstract base class for operators that can be applied to instances of torch.Tensor.

input_size abstractmethod property

input_size: int

Abstract property to get the needed size for inputs to the operator instance

RETURNS DESCRIPTION
int

An integer representing the input size.

apply

apply(tensor: TensorType) -> TensorType

Applies the operator to a tensor.

PARAMETER DESCRIPTION
tensor

A tensor, whose tailing dimension must conform to the operator's input size

TYPE: TensorType

RETURNS DESCRIPTION
TensorType

A tensor representing the result of the operator application.

Source code in src/pydvl/influence/types.py
def apply(self, tensor: TensorType) -> TensorType:
    """
    Applies the operator to a tensor.

    Args:
        tensor: A tensor, whose tailing dimension must conform to the
            operator's input size

    Returns:
        A tensor representing the result of the operator application.
    """
    self._validate_tensor_input(tensor)
    return self._apply(tensor)

TensorDictOperator

Bases: TensorOperator, ABC

Abstract base class for operators that can be applied to instances of torch.Tensor and compatible dictionaries mapping strings to tensors. Input dictionaries must conform to the structure defined by the property input_dict_structure. Useful for operators involving autograd functionality to avoid intermediate flattening and concatenating of gradient inputs.

input_size abstractmethod property

input_size: int

Abstract property to get the needed size for inputs to the operator instance

RETURNS DESCRIPTION
int

An integer representing the input size.

input_dict_structure abstractmethod property

input_dict_structure: Dict[str, Tuple[int, ...]]

Implement this to expose the expected structure of the input tensor dict, i.e. a dictionary of shapes (excluding the first batch dimension), in order to validate the input tensor dicts.

apply

apply(tensor: TensorType) -> TensorType

Applies the operator to a tensor.

PARAMETER DESCRIPTION
tensor

A tensor, whose tailing dimension must conform to the operator's input size

TYPE: TensorType

RETURNS DESCRIPTION
TensorType

A tensor representing the result of the operator application.

Source code in src/pydvl/influence/types.py
def apply(self, tensor: TensorType) -> TensorType:
    """
    Applies the operator to a tensor.

    Args:
        tensor: A tensor, whose tailing dimension must conform to the
            operator's input size

    Returns:
        A tensor representing the result of the operator application.
    """
    self._validate_tensor_input(tensor)
    return self._apply(tensor)

apply_to_dict

apply_to_dict(mat: Dict[str, Tensor]) -> Dict[str, Tensor]

Applies the operator to a dictionary of tensors, compatible to the structure defined by the property input_dict_structure.

PARAMETER DESCRIPTION
mat

dictionary of tensors, whose keys and shapes match the property input_dict_structure.

TYPE: Dict[str, Tensor]

RETURNS DESCRIPTION
Dict[str, Tensor]

A dictionary of tensors after applying the operator

Source code in src/pydvl/influence/torch/base.py
def apply_to_dict(self, mat: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
    """
    Applies the operator to a dictionary of tensors, compatible to the structure
    defined by the property `input_dict_structure`.

    Args:
        mat: dictionary of tensors, whose keys and shapes match the property
            `input_dict_structure`.

    Returns:
        A dictionary of tensors after applying the operator
    """

    if not self._validate_mat_dict(mat):
        raise ValueError(
            f"Incompatible input structure, expected (excluding batch"
            f"dimension): \n {self.input_dict_structure}"
        )

    return self._apply_to_dict(self._dict_to_device(mat))

TorchOperatorGradientComposition

TorchOperatorGradientComposition(
    op: TorchOperatorType, gp: TorchGradientProvider
)

Bases: OperatorGradientComposition[Tensor, TorchBatch, TorchOperatorType, TorchGradientProvider]

Representing a composable block that integrates an [TorchOperator][pydvl.influence.torch.operator.base.TorchOperator] and a [TorchPerSampleGradientProvider][pydvl.influence.torch.operator.gradient_provider.TorchPerSampleGradientProvider]

This block is designed to be flexible, handling different computational modes via an abstract operator and gradient provider.

Source code in src/pydvl/influence/torch/base.py
def __init__(self, op: TorchOperatorType, gp: TorchGradientProvider):
    super().__init__(op, gp)

interactions

interactions(
    left_batch: BatchType, right_batch: Optional[BatchType], mode: InfluenceMode
)

Computes the interaction between the gradients on two batches of data based on the specified mode weighted by the operator action, i.e.

\[ \langle \operatorname{Op}(\nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y})), \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle\]

for the case InfluenceMode.Up and

\[ \langle \operatorname{Op}(\nabla_{\omega}\ell(\omega, \text{left.x}, \text{left.y})), \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y}) \rangle \]

for the case InfluenceMode.Perturbation.

PARAMETER DESCRIPTION
left_batch

The left data batch for gradient computation.

TYPE: BatchType

right_batch

The right data batch for gradient computation.

TYPE: Optional[BatchType]

mode

An instance of InfluenceMode determining the type of influence computation.

TYPE: InfluenceMode

RETURNS DESCRIPTION

The result of the influence computation as dictated by the mode.

Source code in src/pydvl/influence/types.py
def interactions(
    self,
    left_batch: BatchType,
    right_batch: Optional[BatchType],
    mode: InfluenceMode,
):
    r"""
    Computes the interaction between the gradients on two batches of data based on
    the specified mode weighted by the operator action,
    i.e.

    $$ \langle \operatorname{Op}(\nabla_{\omega}\ell(\omega, \text{left.x},
    \text{left.y})),
    \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle$$

    for the case `InfluenceMode.Up` and

    $$ \langle \operatorname{Op}(\nabla_{\omega}\ell(\omega, \text{left.x},
    \text{left.y})),
    \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y}) \rangle $$

    for the case `InfluenceMode.Perturbation`.

    Args:
        left_batch: The left data batch for gradient computation.
        right_batch: The right data batch for gradient computation.
        mode: An instance of InfluenceMode determining the type of influence
            computation.

    Returns:
        The result of the influence computation as dictated by the mode.
    """
    bilinear_form = self.op.as_bilinear_form()
    if mode == InfluenceMode.Up:
        return bilinear_form.grads_inner_prod(left_batch, right_batch, self.gp)
    elif mode == InfluenceMode.Perturbation:
        return bilinear_form.mixed_grads_inner_prod(
            left_batch, right_batch, self.gp
        )
    else:
        raise UnsupportedInfluenceModeException(mode)

transformed_grads

transformed_grads(batch: BatchType)

Computes the gradients of a data batch, transformed by the operator application , i.e. the expressions

\[ \operatorname{Op}(\nabla_{\omega}\ell(\omega, \text{batch.x}, \text{batch.y})) \]
PARAMETER DESCRIPTION
batch

The data batch for gradient computation.

TYPE: BatchType

RETURNS DESCRIPTION

A tensor representing the application of the operator to the gradients.

Source code in src/pydvl/influence/types.py
def transformed_grads(self, batch: BatchType):
    r"""
    Computes the gradients of a data batch, transformed by the operator application
    , i.e. the expressions

    $$ \operatorname{Op}(\nabla_{\omega}\ell(\omega, \text{batch.x},
        \text{batch.y})) $$

    Args:
        batch: The data batch for gradient computation.

    Returns:
        A tensor representing the application of the operator to the gradients.

    """
    grads = self.gp.flat_grads(batch)
    return self.op.apply(grads)

interactions_from_transformed_grads

interactions_from_transformed_grads(
    left_factors: TensorType, right_batch: BatchType, mode: InfluenceMode
)

Computes the interaction between the transformed gradients on two batches of data using pre-computed factors and a batch of data, based on the specified mode. This means

\[ \langle \text{left_factors}, \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle\]

for the case InfluenceMode.Up and

\[ \langle \text{left_factors}, \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y}) \rangle \]

for the case InfluenceMode.Perturbation.

PARAMETER DESCRIPTION
left_factors

Pre-computed tensor factors from a left batch.

TYPE: TensorType

right_batch

The right data batch for influence computation.

TYPE: BatchType

mode

An instance of InfluenceMode determining the type of influence computation.

TYPE: InfluenceMode

RETURNS DESCRIPTION

The result of the interaction computation using the provided factors and batch gradients.

Source code in src/pydvl/influence/types.py
def interactions_from_transformed_grads(
    self, left_factors: TensorType, right_batch: BatchType, mode: InfluenceMode
):
    r"""
    Computes the interaction between the transformed gradients on two batches of
    data using pre-computed factors and a batch of data,
    based on the specified mode. This means

    $$ \langle \text{left_factors},
    \nabla_{\omega}\ell(\omega, \text{right.x}, \text{right.y}) \rangle$$

    for the case `InfluenceMode.Up` and

    $$ \langle \text{left_factors},
    \nabla_{\omega}\nabla_{x}\ell(\omega, \text{right.x}, \text{right.y}) \rangle $$

    for the case `InfluenceMode.Perturbation`.

    Args:
        left_factors: Pre-computed tensor factors from a left batch.
        right_batch: The right data batch for influence computation.
        mode: An instance of InfluenceMode determining the type of influence
            computation.

    Returns:
        The result of the interaction computation using the provided factors and
            batch gradients.
    """
    if mode is InfluenceMode.Up:
        right_grads = self.gp.flat_grads(right_batch)
    else:
        right_grads = self.gp.flat_mixed_grads(right_batch)
    return self._tensor_inner_product(left_factors, right_grads)

TorchBlockMapper

TorchBlockMapper(
    composable_block_dict: OrderedDict[str, TorchOperatorGradientComposition]
)

Bases: BlockMapper[Tensor, TorchBatch, TorchOperatorGradientComposition[TorchOperatorType]]

Class for mapping operations across multiple compositional blocks represented by instances of TorchOperatorGradientComposition.

This class takes a dictionary of compositional blocks and applies their methods to batches or tensors, and aggregates the results.

Source code in src/pydvl/influence/torch/base.py
def __init__(
    self, composable_block_dict: OrderedDict[str, TorchOperatorGradientComposition]
):
    super().__init__(composable_block_dict)

transformed_grads

transformed_grads(batch: BatchType) -> OrderedDict[str, TensorType]

Computes and returns the transformed gradients for a batch in dictionary with the keys defined by the block names.

PARAMETER DESCRIPTION
batch

The batch of data for which to compute transformed gradients.

TYPE: BatchType

RETURNS DESCRIPTION
OrderedDict[str, TensorType]

An ordered dictionary of transformed gradients by block.

Source code in src/pydvl/influence/types.py
def transformed_grads(
    self,
    batch: BatchType,
) -> OrderedDict[str, TensorType]:
    """
    Computes and returns the transformed gradients for a batch in dictionary
    with the keys defined by the block names.

    Args:
        batch: The batch of data for which to compute transformed gradients.

    Returns:
        An ordered dictionary of transformed gradients by block.
    """
    tensor_gen = self.generate_transformed_grads(batch)
    return self._to_ordered_dict(tensor_gen)

interactions

interactions(
    left_batch: BatchType, right_batch: BatchType, mode: InfluenceMode
) -> OrderedDict[str, TensorType]

Computes interactions between two batches, aggregated by block, based on a specified mode.

PARAMETER DESCRIPTION
left_batch

The left batch for interaction computation.

TYPE: BatchType

right_batch

The right batch for interaction computation.

TYPE: BatchType

mode

The mode determining the type of interactions.

TYPE: InfluenceMode

RETURNS DESCRIPTION
OrderedDict[str, TensorType]

An ordered dictionary of gradient interactions by block.

Source code in src/pydvl/influence/types.py
def interactions(
    self, left_batch: BatchType, right_batch: BatchType, mode: InfluenceMode
) -> OrderedDict[str, TensorType]:
    """
    Computes interactions between two batches, aggregated by block,
    based on a specified mode.

    Args:
        left_batch: The left batch for interaction computation.
        right_batch: The right batch for interaction computation.
        mode: The mode determining the type of interactions.

    Returns:
        An ordered dictionary of gradient interactions by block.
    """
    tensor_gen = self.generate_interactions(left_batch, right_batch, mode)
    return self._to_ordered_dict(tensor_gen)

interactions_from_transformed_grads

interactions_from_transformed_grads(
    left_factors: OrderedDict[str, TensorType],
    right_batch: BatchType,
    mode: InfluenceMode,
) -> OrderedDict[str, TensorType]

Computes interactions from transformed gradients and a right batch, aggregated by block and based on a mode.

PARAMETER DESCRIPTION
left_factors

Pre-computed factors as a tensor or an ordered dictionary of tensors by block. If the input is a tensor, it is split into blocks according to the ordering in the composable_block_dict attribute.

TYPE: OrderedDict[str, TensorType]

right_batch

The right batch for interaction computation.

TYPE: BatchType

mode

The mode determining the type of interactions.

TYPE: InfluenceMode

RETURNS DESCRIPTION
OrderedDict[str, TensorType]

An ordered dictionary of interactions from transformed gradients by block.

Source code in src/pydvl/influence/types.py
def interactions_from_transformed_grads(
    self,
    left_factors: OrderedDict[str, TensorType],
    right_batch: BatchType,
    mode: InfluenceMode,
) -> OrderedDict[str, TensorType]:
    """
    Computes interactions from transformed gradients and a right batch,
    aggregated by block and based on a mode.

    Args:
        left_factors: Pre-computed factors as a tensor or an ordered dictionary of
            tensors by block. If the input is a tensor, it is split into blocks
            according to the ordering in the `composable_block_dict` attribute.
        right_batch: The right batch for interaction computation.
        mode: The mode determining the type of interactions.

    Returns:
        An ordered dictionary of interactions from transformed gradients by block.
    """
    tensor_gen = self.generate_interactions_from_transformed_grads(
        left_factors, right_batch, mode
    )
    return self._to_ordered_dict(tensor_gen)

generate_transformed_grads

generate_transformed_grads(
    batch: BatchType,
) -> Generator[TensorType, None, None]

Generator that yields transformed gradients for a given batch, processed by each block.

PARAMETER DESCRIPTION
batch

The batch of data for which to generate transformed gradients.

TYPE: BatchType

YIELDS DESCRIPTION
TensorType

Transformed gradients for each block.

Source code in src/pydvl/influence/types.py
def generate_transformed_grads(
    self, batch: BatchType
) -> Generator[TensorType, None, None]:
    """
    Generator that yields transformed gradients for a given batch,
    processed by each block.

    Args:
        batch: The batch of data for which to generate transformed gradients.

    Yields:
        Transformed gradients for each block.
    """
    for comp_block in self.composable_block_dict.values():
        yield comp_block.transformed_grads(batch)

generate_interactions

generate_interactions(
    left_batch: BatchType, right_batch: Optional[BatchType], mode: InfluenceMode
) -> Generator[TensorType, None, None]

Generator that yields gradient interactions between two batches, processed by each block based on a mode.

PARAMETER DESCRIPTION
left_batch

The left batch for interaction computation.

TYPE: BatchType

right_batch

The right batch for interaction computation.

TYPE: Optional[BatchType]

mode

The mode determining the type of interactions.

TYPE: InfluenceMode

YIELDS DESCRIPTION
TensorType

Gradient interactions for each block.

TYPE:: TensorType

Source code in src/pydvl/influence/types.py
def generate_interactions(
    self,
    left_batch: BatchType,
    right_batch: Optional[BatchType],
    mode: InfluenceMode,
) -> Generator[TensorType, None, None]:
    """
    Generator that yields gradient interactions between two batches, processed by
    each block based on a mode.

    Args:
        left_batch: The left batch for interaction computation.
        right_batch: The right batch for interaction computation.
        mode: The mode determining the type of interactions.

    Yields:
        TensorType: Gradient interactions for each block.
    """
    for comp_block in self.composable_block_dict.values():
        yield comp_block.interactions(left_batch, right_batch, mode)

generate_interactions_from_transformed_grads

generate_interactions_from_transformed_grads(
    left_factors: Union[TensorType, OrderedDict[str, TensorType]],
    right_batch: BatchType,
    mode: InfluenceMode,
) -> Generator[TensorType, None, None]

Generator that yields interactions computed from pre-computed factors and a right batch, processed by each block based on a mode.

PARAMETER DESCRIPTION
left_factors

Pre-computed factors as a tensor or an ordered dictionary of tensors by block.

TYPE: Union[TensorType, OrderedDict[str, TensorType]]

right_batch

The right batch for interaction computation.

TYPE: BatchType

mode

The mode determining the type of interactions.

TYPE: InfluenceMode

YIELDS DESCRIPTION
TensorType

Interactions for each block.

TYPE:: TensorType

Source code in src/pydvl/influence/types.py
def generate_interactions_from_transformed_grads(
    self,
    left_factors: Union[TensorType, OrderedDict[str, TensorType]],
    right_batch: BatchType,
    mode: InfluenceMode,
) -> Generator[TensorType, None, None]:
    """
    Generator that yields interactions computed from pre-computed factors and a
    right batch, processed by each block based on a mode.

    Args:
        left_factors: Pre-computed factors as a tensor or an ordered dictionary of
            tensors by block.
        right_batch: The right batch for interaction computation.
        mode: The mode determining the type of interactions.

    Yields:
        TensorType: Interactions for each block.
    """
    if not isinstance(left_factors, dict):
        left_factors_dict = self._split_to_blocks(left_factors)
    else:
        left_factors_dict = cast(OrderedDict[str, TensorType], left_factors)
    for k, comp_block in self.composable_block_dict.items():
        yield comp_block.interactions_from_transformed_grads(
            left_factors_dict[k], right_batch, mode
        )

TorchComposableInfluence

TorchComposableInfluence(
    model: Module,
    block_structure: Union[
        BlockMode, OrderedDict[str, List[str]]
    ] = BlockMode.FULL,
    regularization: Optional[Union[float, Dict[str, Optional[float]]]] = None,
)

Bases: ComposableInfluence[Tensor, TorchBatch, DataLoader, TorchBlockMapper[TorchOperatorType]], ModelInfoMixin, ABC

Abstract base class, that allow for block-wise computation of influence quantities with the torch framework. Inherit from this base class for specific influence algorithms.

Source code in src/pydvl/influence/torch/base.py
def __init__(
    self,
    model: torch.nn.Module,
    block_structure: Union[BlockMode, OrderedDict[str, List[str]]] = BlockMode.FULL,
    regularization: Optional[Union[float, Dict[str, Optional[float]]]] = None,
):
    parameter_dict_builder = ModelParameterDictBuilder(model)
    if isinstance(block_structure, BlockMode):
        self.parameter_dict = parameter_dict_builder.build_from_block_mode(
            block_structure
        )
    else:
        self.parameter_dict = parameter_dict_builder.build(block_structure)

    self._regularization_dict = self._build_regularization_dict(regularization)

    super().__init__(model)

is_thread_safe abstractmethod property

is_thread_safe: bool

Whether the influence computation is thread safe

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)

influences

influences(
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = 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

influences_from_factors(
    z_test_factors: TensorType,
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = 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_by_block

influences_by_block(
    x_test: TensorType,
    y_test: TensorType,
    x: Optional[TensorType] = None,
    y: Optional[TensorType] = None,
    mode: InfluenceMode = 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)

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_from_factors_by_block

influences_from_factors_by_block(
    z_test_factors: OrderedDict[str, TensorType],
    x: TensorType,
    y: TensorType,
    mode: InfluenceMode = 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
    )