pydvl.influence.torch.influence_function_model
¶
This module implements several implementations of InfluenceFunctionModel utilizing PyTorch.
TorchInfluenceFunctionModel
¶
Bases: InfluenceFunctionModel[Tensor, DataLoader]
, ABC
Abstract base class for influence computation related to torch models
Source code in src/pydvl/influence/torch/influence_function_model.py
is_fitted
abstractmethod
property
¶
Override this, to expose the fitting status of the instance.
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:
|
RETURNS | DESCRIPTION |
---|---|
InfluenceFunctionModel
|
The fitted instance |
Source code in src/pydvl/influence/base_influence_function_model.py
influences
¶
influences(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Compute the approximation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. For all input tensors it is assumed, that the first dimension is the batch dimension (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors
¶
Compute approximation of
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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise inverse Hessian matrix vector products |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors
¶
influences_from_factors(
z_test_factors: Tensor,
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. 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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
DirectInfluence
¶
DirectInfluence(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
hessian_regularization: float = 0.0,
)
Bases: TorchInfluenceFunctionModel
Given a model and training data, it finds x such that \(Hx = b\), with \(H\) being the model hessian.
PARAMETER | DESCRIPTION |
---|---|
model |
A PyTorch model. The Hessian will be calculated with respect to this model's parameters.
TYPE:
|
loss |
A callable that takes the model's output and target as input and returns the scalar loss. |
hessian_regularization |
Regularization of the hessian.
TYPE:
|
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors
¶
Compute approximation of
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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise inverse Hessian matrix vector products |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors
¶
influences_from_factors(
z_test_factors: Tensor,
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. 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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
fit
¶
fit(data: DataLoader) -> DirectInfluence
Compute the hessian matrix based on a provided dataloader.
PARAMETER | DESCRIPTION |
---|---|
data |
The data to compute the Hessian with.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DirectInfluence
|
The fitted instance. |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences
¶
influences(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Compute approximation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. The action of \(H^{-1}\) is achieved via a direct solver using torch.linalg.solve.
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
A tensor representing the element-wise scalar products for the provided batch. |
Source code in src/pydvl/influence/torch/influence_function_model.py
CgInfluence
¶
CgInfluence(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
hessian_regularization: float = 0.0,
x0: Optional[Tensor] = None,
rtol: float = 1e-07,
atol: float = 1e-07,
maxiter: Optional[int] = None,
progress: bool = False,
precompute_grad: bool = False,
pre_conditioner: Optional[PreConditioner] = None,
use_block_cg: bool = False,
warn_on_max_iteration: bool = True,
)
Bases: TorchInfluenceFunctionModel
Given a model and training data, it uses conjugate gradient to calculate the inverse of the Hessian Vector Product. More precisely, it finds x such that \(Hx = b\), with \(H\) being the model hessian. For more info, see Conjugate Gradient.
PARAMETER | DESCRIPTION |
---|---|
model |
A PyTorch model. The Hessian will be calculated with respect to this model's parameters.
TYPE:
|
loss |
A callable that takes the model's output and target as input and returns the scalar loss. |
hessian_regularization |
Optional regularization parameter added to the Hessian-vector product for numerical stability.
TYPE:
|
x0 |
Initial guess for hvp. If None, defaults to b. |
rtol |
Maximum relative tolerance of result.
TYPE:
|
atol |
Absolute tolerance of result.
TYPE:
|
maxiter |
Maximum number of iterations. If None, defaults to 10*len(b). |
progress |
If True, display progress bars for computing in the non-block mode (use_block_cg=False).
TYPE:
|
precompute_grad |
If True, the full data gradient is precomputed and kept in memory, which can speed up the hessian vector product computation. Set this to False, if you can't afford to keep the full computation graph in memory.
TYPE:
|
pre_conditioner |
Optional pre-conditioner to improve convergence of conjugate gradient method
TYPE:
|
use_block_cg |
If True, use block variant of conjugate gradient method, which solves several right hand sides simultaneously
TYPE:
|
warn_on_max_iteration |
If True, logs a warning, if the desired tolerance is not
achieved within
TYPE:
|
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors
¶
Compute approximation of
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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise inverse Hessian matrix vector products |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors
¶
influences_from_factors(
z_test_factors: Tensor,
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. 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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences
¶
influences(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Compute an approximation of
for the case of up-weighting influence, resp.
for the case of perturbation-type influence. The approximate action of \(H^{-1}\) is achieved via the conjugate gradient method.
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
A tensor representing the element-wise scalar products for the provided batch. |
Source code in src/pydvl/influence/torch/influence_function_model.py
LissaInfluence
¶
LissaInfluence(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
hessian_regularization: float = 0.0,
maxiter: int = 1000,
dampen: float = 0.0,
scale: float = 10.0,
h0: Optional[Tensor] = None,
rtol: float = 0.0001,
progress: bool = False,
warn_on_max_iteration: bool = True,
)
Bases: TorchInfluenceFunctionModel
Uses LISSA, Linear time Stochastic Second-Order Algorithm, to iteratively approximate the inverse Hessian. More precisely, it finds x s.t. \(Hx = b\), with \(H\) being the model's second derivative wrt. the parameters. This is done with the update
where \(I\) is the identity matrix, \(d\) is a dampening term and \(s\) a scaling factor that are applied to help convergence. For details, see Linear time Stochastic Second-Order Approximation (LiSSA)
PARAMETER | DESCRIPTION |
---|---|
model |
A PyTorch model. The Hessian will be calculated with respect to this model's parameters.
TYPE:
|
loss |
A callable that takes the model's output and target as input and returns the scalar loss. |
hessian_regularization |
Optional regularization parameter added to the Hessian-vector product for numerical stability.
TYPE:
|
maxiter |
Maximum number of iterations.
TYPE:
|
dampen |
Dampening factor, defaults to 0 for no dampening.
TYPE:
|
scale |
Scaling factor, defaults to 10.
TYPE:
|
h0 |
Initial guess for hvp. |
rtol |
tolerance to use for early stopping
TYPE:
|
progress |
If True, display progress bars.
TYPE:
|
warn_on_max_iteration |
If True, logs a warning, if the desired tolerance is not
achieved within
TYPE:
|
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors
¶
Compute approximation of
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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise inverse Hessian matrix vector products |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences
¶
influences(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Compute the approximation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. For all input tensors it is assumed, that the first dimension is the batch dimension (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors
¶
influences_from_factors(
z_test_factors: Tensor,
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. 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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
ArnoldiInfluence
¶
ArnoldiInfluence(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
hessian_regularization: float = 0.0,
rank_estimate: int = 10,
krylov_dimension: Optional[int] = None,
tol: float = 1e-06,
max_iter: Optional[int] = None,
eigen_computation_on_gpu: bool = False,
precompute_grad: bool = False,
)
Bases: TorchInfluenceFunctionModel
Solves the linear system Hx = b, where H is the Hessian of the model's loss function and b is the given right-hand side vector. It employs the [implicitly restarted Arnoldi method] (https://en.wikipedia.org/wiki/Arnoldi_iteration) for computing a partial eigen decomposition, which is used fo the inversion i.e.
where \(D\) is a diagonal matrix with the top (in absolute value) rank_estimate
eigenvalues of the Hessian
and \(V\) contains the corresponding eigenvectors.
For more information, see Arnoldi.
PARAMETER | DESCRIPTION |
---|---|
model |
A PyTorch model. The Hessian will be calculated with respect to this model's parameters.
TYPE:
|
loss |
A callable that takes the model's output and target as input and returns the scalar loss. |
hessian_regularization |
Optional regularization parameter added to the Hessian-vector product for numerical stability.
TYPE:
|
rank_estimate |
The number of eigenvalues and corresponding eigenvectors to compute. Represents the desired rank of the Hessian approximation.
TYPE:
|
krylov_dimension |
The number of Krylov vectors to use for the Lanczos method. Defaults to min(model's number of parameters, max(2 times rank_estimate + 1, 20)). |
tol |
The stopping criteria for the Lanczos algorithm.
Ignored if
TYPE:
|
max_iter |
The maximum number of iterations for the Lanczos method.
Ignored if |
eigen_computation_on_gpu |
If True, tries to execute the eigen pair approximation on the model's device via a cupy implementation. Ensure the model size or rank_estimate is appropriate for device memory. If False, the eigen pair approximation is executed on the CPU by the scipy wrapper to ARPACK.
TYPE:
|
precompute_grad |
If True, the full data gradient is precomputed and kept in memory, which can speed up the hessian vector product computation. Set this to False, if you can't afford to keep the full computation graph in memory.
TYPE:
|
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors
¶
Compute approximation of
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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise inverse Hessian matrix vector products |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences
¶
influences(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Compute the approximation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. For all input tensors it is assumed, that the first dimension is the batch dimension (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors
¶
influences_from_factors(
z_test_factors: Tensor,
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. 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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
fit
¶
fit(data: DataLoader) -> ArnoldiInfluence
Fitting corresponds to the computation of the low rank decomposition
of the Hessian defined by the provided data loader.
PARAMETER | DESCRIPTION |
---|---|
data |
The data to compute the Hessian with.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ArnoldiInfluence
|
The fitted instance. |
Source code in src/pydvl/influence/torch/influence_function_model.py
EkfacInfluence
¶
EkfacInfluence(
model: Module,
update_diagonal: bool = False,
hessian_regularization: float = 0.0,
progress: bool = False,
)
Bases: TorchInfluenceFunctionModel
Approximately solves the linear system Hx = b, where H is the Hessian of a model with the empirical categorical cross entropy as loss function and b is the given right-hand side vector. It employs the EK-FAC method, which is based on the kronecker factorization of the Hessian.
Contrary to the other influence function methods, this implementation can only be used for classification tasks with a cross entropy loss function. However, it is much faster than the other methods and can be used efficiently for very large datasets and models. For more information, see Eigenvalue Corrected K-FAC.
PARAMETER | DESCRIPTION |
---|---|
model |
A PyTorch model. The Hessian will be calculated with respect to this model's parameters.
TYPE:
|
update_diagonal |
If True, the diagonal values in the ekfac representation are refitted from the training data after calculating the KFAC blocks. This provides a more accurate approximation of the Hessian, but it is computationally more expensive.
TYPE:
|
hessian_regularization |
Regularization of the hessian.
TYPE:
|
progress |
If True, display progress bars.
TYPE:
|
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors
¶
Compute approximation of
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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise inverse Hessian matrix vector products |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences
¶
influences(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Compute the approximation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. For all input tensors it is assumed, that the first dimension is the batch dimension (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors
¶
influences_from_factors(
z_test_factors: Tensor,
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. 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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
fit
¶
fit(data: DataLoader) -> EkfacInfluence
Compute the KFAC blocks for each layer of the model, using the provided data. It then creates an EkfacRepresentation object that stores the KFAC blocks for each layer, their eigenvalue decomposition and diagonal values.
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_by_layer
¶
influences_by_layer(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Dict[str, Tensor]
Compute the influence of the data on the test data for each layer of the model.
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Tensor]
|
A dictionary containing the influence of the data on the test data for each |
Dict[str, Tensor]
|
layer of the model, with the layer name as key. |
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors_by_layer
¶
Computes the approximation of
for each layer of the model separately.
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Tensor]
|
A dictionary containing the influence factors for each layer of the model, |
Dict[str, Tensor]
|
with the layer name as key. |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors_by_layer
¶
influences_from_factors_by_layer(
z_test_factors: Dict[str, Tensor],
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Dict[str, Tensor]
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case for each layer of the model separately. The gradients are meant to be per sample of the batch \((x, y)\).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, 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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Tensor]
|
A dictionary containing the influence of the data on the test data |
Dict[str, Tensor]
|
for each layer of the model, with the layer name as key. |
Source code in src/pydvl/influence/torch/influence_function_model.py
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 |
|
explore_hessian_regularization
¶
explore_hessian_regularization(
x: Tensor, y: Tensor, regularization_values: List[float]
) -> Dict[float, Dict[str, Tensor]]
Efficiently computes the influence for input x and label y for each layer of the model, for different values of the hessian regularization parameter. This is done by computing the gradient of the loss function for the input x and label y only once and then solving the Hessian Vector Product for each regularization value. This is useful for finding the optimal regularization value and for exploring how robust the influence values are to changes in the regularization value.
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
regularization_values |
list of regularization values to use |
RETURNS | DESCRIPTION |
---|---|
Dict[float, Dict[str, Tensor]]
|
A dictionary containing with keys being the regularization values and values |
Dict[float, Dict[str, Tensor]]
|
being dictionaries containing the influences for each layer of the model, |
Dict[float, Dict[str, Tensor]]
|
with the layer name as key. |
Source code in src/pydvl/influence/torch/influence_function_model.py
NystroemSketchInfluence
¶
NystroemSketchInfluence(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
hessian_regularization: float,
rank: int,
)
Bases: TorchInfluenceFunctionModel
Given a model and training data, it uses a low-rank approximation of the Hessian (derived via random projection Nyström approximation) in combination with the Sherman–Morrison–Woodbury formula to calculate the inverse of the Hessian Vector Product. More concrete, it computes a low-rank approximation
in factorized form and approximates the action of the inverse Hessian via
PARAMETER | DESCRIPTION |
---|---|
model |
A PyTorch model. The Hessian will be calculated with respect to this model's parameters.
TYPE:
|
loss |
A callable that takes the model's output and target as input and returns the scalar loss. |
hessian_regularization |
Optional regularization parameter added to the Hessian-vector product for numerical stability.
TYPE:
|
rank |
rank of the low-rank approximation
TYPE:
|
Source code in src/pydvl/influence/torch/influence_function_model.py
influence_factors
¶
Compute approximation of
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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x |
model input to use in the gradient computations
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise inverse Hessian matrix vector products |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences
¶
influences(
x_test: Tensor,
y_test: Tensor,
x: Optional[Tensor] = None,
y: Optional[Tensor] = None,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Compute the approximation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. For all input tensors it is assumed, that the first dimension is the batch dimension (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
x_test |
model input to use in the gradient computations of \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
y_test |
label tensor to compute gradients
TYPE:
|
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_{\text{test}}\) |
y |
optional label tensor to compute gradients |
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |
Source code in src/pydvl/influence/torch/influence_function_model.py
influences_from_factors
¶
influences_from_factors(
z_test_factors: Tensor,
x: Tensor,
y: Tensor,
mode: InfluenceMode = InfluenceMode.Up,
) -> Tensor
Computation of
for the case of up-weighting influence, resp.
for the perturbation type influence case. 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 (in case, you want to provide a single sample z, call z.unsqueeze(0) if no batch dimension is present).
PARAMETER | DESCRIPTION |
---|---|
z_test_factors |
pre-computed tensor, approximating \(H^{-1}\nabla_{\theta} \ell(y_{\text{test}}, f_{\theta}(x_{\text{test}}))\)
TYPE:
|
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))\)
TYPE:
|
y |
label tensor to compute gradients
TYPE:
|
mode |
enum value of InfluenceMode
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tensor representing the element-wise scalar products for the provided batch |