pydvl.influence.torch.operator
¶
GaussNewtonOperator
¶
GaussNewtonOperator(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
dataloader: DataLoader,
restrict_to: Optional[Dict[str, Parameter]] = None,
)
Bases: _AveragingBatchOperator[GaussNewtonBatchOperation, PointAveraging]
Given a model and loss function computes the Gauss-Newton vector or matrix product with respect to the model parameters on a batch, i.e.
where model is a torch.nn.Module and \(v\) is a vector or matrix, and average the results over the batches provided by the data loader.
PARAMETER | DESCRIPTION |
---|---|
model |
The model.
TYPE:
|
loss |
The loss function. |
dataloader |
The data loader providing batches of data.
TYPE:
|
restrict_to |
The parameters to restrict the differentiation to,
i.e. the corresponding sub-matrix of the Jacobian. If None, the full
Jacobian is used. Make sure the input matches the corrct dimension, i.e. the
last dimension must be equal to the property |
Source code in src/pydvl/influence/torch/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
apply_to_dict
¶
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
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Tensor]
|
A dictionary of tensors after applying the operator |
Source code in src/pydvl/influence/torch/base.py
HessianOperator
¶
HessianOperator(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
dataloader: DataLoader,
restrict_to: Optional[Dict[str, Parameter]] = None,
)
Bases: _AveragingBatchOperator[HessianBatchOperation, ChunkAveraging]
Given a model and loss function computes the Hessian vector or matrix product with respect to the model parameters for a given batch, i.e.
where model is a torch.nn.Module and \(v\) is a vector or matrix, and average the results over the batches provided by the data loader.
PARAMETER | DESCRIPTION |
---|---|
model |
The model.
TYPE:
|
loss |
The loss function. |
dataloader |
The data loader providing batches of data.
TYPE:
|
restrict_to |
The parameters to restrict the second order differentiation to,
i.e. the corresponding sub-matrix of the Hessian. If None, the full Hessian
is used. Make sure the input matches the corrct dimension, i.e. the
last dimension must be equal to the property |
Source code in src/pydvl/influence/torch/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
apply_to_dict
¶
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
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Tensor]
|
A dictionary of tensors after applying the operator |
Source code in src/pydvl/influence/torch/base.py
InverseHarmonicMeanOperator
¶
InverseHarmonicMeanOperator(
model: Module,
loss: Callable[[Tensor, Tensor], Tensor],
dataloader: DataLoader,
regularization: float,
restrict_to: Optional[Dict[str, Parameter]] = None,
)
Bases: _AveragingBatchOperator[InverseHarmonicMeanBatchOperation, PointAveraging]
Given a model and loss function computes an approximation of the inverse Gauss-Newton vector or matrix product per batch and averages the results.
Viewing the damped Gauss-newton matrix
as an arithmetic mean of the rank-\(1\) updates, this operator replaces it with the harmonic mean of the rank-\(1\) updates, i.e.
and computes
for any given batch \(b\), where model is a torch.nn.Module and \(v\) is a vector or matrix.
In other words, it switches the order of summation and inversion, which resolves
to the inverse harmonic mean
of the rank-\(1\) updates. The results are averaged
over the batches provided by the data loader.
The inverses of the rank-\(1\) updates are not calculated explicitly, but instead a vectorized version of the Sherman–Morrison formula is applied.
For more information, see Inverse Harmonic Mean.
PARAMETER | DESCRIPTION |
---|---|
model |
The model.
TYPE:
|
loss |
The loss function. |
dataloader |
The data loader providing batches of data.
TYPE:
|
restrict_to |
The parameters to restrict the differentiation to,
i.e. the corresponding sub-matrix of the Jacobian. If None, the full
Jacobian is used. Make sure the input matches the corrct dimension, i.e. the
last dimension must be equal to the property |
Source code in src/pydvl/influence/torch/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
apply_to_dict
¶
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
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Tensor]
|
A dictionary of tensors after applying the operator |
Source code in src/pydvl/influence/torch/base.py
DirectSolveOperator
¶
DirectSolveOperator(
matrix: Tensor,
regularization: Optional[float] = None,
in_place_regularization: bool = False,
)
Bases: TensorOperator
Given a matrix \(A\) and an optional regularization parameter \(\lambda\), computes the solution of the system \((A+\lambda I)x = b\), where \(b\) is a vector or a matrix. Internally, it uses the routine torch.linalg.solve.
PARAMETER | DESCRIPTION |
---|---|
matrix |
the system matrix
TYPE:
|
regularization |
the regularization parameter |
in_place_regularization |
If True, the input matrix is modified in-place, by adding the regularization value to the diagonal.
TYPE:
|
Source code in src/pydvl/influence/torch/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
LissaOperator
¶
LissaOperator(
batch_operation: BatchOperationType,
data: DataLoader,
regularization: Optional[float] = None,
maxiter: int = 1000,
dampen: float = 0.0,
scale: float = 10.0,
rtol: float = 0.0001,
progress: bool = False,
warn_on_max_iteration: bool = True,
)
Bases: TensorOperator
, Generic[BatchOperationType]
Uses LISSA, Linear time Stochastic Second-Order Algorithm, to iteratively approximate the solution of the system \((A + \lambda I)x = b\). 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 |
---|---|
batch_operation |
The
TYPE:
|
data |
a pytorch dataloader
TYPE:
|
regularization |
Optional regularization parameter added to the Hessian-vector product for numerical stability. |
maxiter |
Maximum number of iterations.
TYPE:
|
dampen |
Dampening factor, defaults to 0 for no dampening.
TYPE:
|
scale |
Scaling factor, defaults to 10.
TYPE:
|
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/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
LowRankOperator
¶
LowRankOperator(
low_rank_representation: LowRankProductRepresentation,
regularization: Optional[float] = None,
exact: bool = True,
)
Bases: TensorOperator
Given a low rank representation of a matrix
with a diagonal matrix \(D\) and an optional regularization parameter \(\lambda\), computes
$$ (V D V^T+\lambda I)^{-1}b$$.
Depending on the value of the exact
flag, the inverse action is computed exactly
using the [Sherman–Morrison–Woodbury formula]
(https://en.wikipedia.org/wiki/Woodbury_matrix_identity). If exact
is set to
False
, the inverse action is approximated by
Args:
Source code in src/pydvl/influence/torch/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
MatrixOperator
¶
MatrixOperator(matrix: Tensor)
Bases: TensorOperator
A simple wrapper for a torch.Tensor acting as a matrix mapping.
Source code in src/pydvl/influence/torch/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |
Source code in src/pydvl/influence/types.py
CgOperator
¶
CgOperator(
operator: TensorOperator,
regularization: Optional[float] = None,
rtol: float = 1e-07,
atol: float = 1e-07,
maxiter: Optional[int] = None,
progress: bool = False,
preconditioner: Optional[Preconditioner] = None,
use_block_cg: bool = False,
warn_on_max_iteration: bool = True,
)
Bases: TensorOperator
Given an operator , it uses conjugate gradient to calculate the action of its inverse. More precisely, it finds x such that \(Ax = A\), with \(A\) being the matrix represented by the operator. For more info, see Conjugate Gradient.
PARAMETER | DESCRIPTION |
---|---|
operator |
TYPE:
|
regularization |
Optional regularization parameter added to the matrix vector product for numerical stability. |
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:
|
preconditioner |
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/operator.py
apply
¶
Applies the operator to a tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor |
A tensor, whose tailing dimension must conform to the operator's input size
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the result of the operator application. |