Functional
hvp(func, params, vec, reverse_only=True)
¶
Computes the Hessian-vector product (HVP) for a given function at given parameters, i.e.
This function can operate in two modes, either reverse-mode autodiff only or both forward- and reverse-mode autodiff.
PARAMETER | DESCRIPTION |
---|---|
func |
The scalar-valued function for which the HVP is computed.
TYPE:
|
params |
The parameters at which the HVP is computed.
TYPE:
|
vec |
The vector with which the Hessian is multiplied.
TYPE:
|
reverse_only |
Whether to use only reverse-mode autodiff (True, default) or both forward- and reverse-mode autodiff (False).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TorchTensorContainerType
|
The HVP of the function at the given parameters with the given vector. |
Example:
>>> def f(z): return torch.sum(z**2)
>>> u = torch.ones(10, requires_grad=True)
>>> v = torch.ones(10)
>>> hvp_vec = hvp(f, u, v)
>>> assert torch.allclose(hvp_vec, torch.full((10, ), 2.0))
Source code in src/pydvl/influence/torch/functional.py
batch_hvp_gen(model, loss, data_loader, reverse_only=True)
¶
Generates a sequence of batch Hessian-vector product (HVP) computations for the provided model, loss function, and data loader. If \(f_i\) is the model's loss on the \(i\)-th batch and \(\theta\) the model parameters, this is the sequence of the callable matrix vector products for the matrices
i.e. iterating over the data_loader, yielding partial function calls for calculating HVPs.
PARAMETER | DESCRIPTION |
---|---|
model |
The PyTorch model for which the HVP is calculated.
TYPE:
|
loss |
The loss function used to calculate the gradient and HVP. |
data_loader |
PyTorch DataLoader object containing the dataset for which the HVP is calculated.
TYPE:
|
reverse_only |
Whether to use only reverse-mode autodiff (True, default) or both forward- and reverse-mode autodiff (False).
TYPE:
|
YIELDS | DESCRIPTION |
---|---|
Callable[[Tensor], Tensor]
|
Partial functions |
Source code in src/pydvl/influence/torch/functional.py
empirical_loss_function(model, loss, data_loader)
¶
Creates a function to compute the empirical loss of a given model on a given dataset. If we denote the model parameters with \( \theta \), the resulting function approximates:
Args: - model: The model for which the loss should be computed. - loss: The loss function to be used. - data_loader: The data loader for iterating over the dataset.
RETURNS | DESCRIPTION |
---|---|
Callable[[Dict[str, Tensor]], Tensor]
|
A function that computes the empirical loss of the model on the dataset for given model parameters. |
Source code in src/pydvl/influence/torch/functional.py
batch_loss_function(model, loss, x, y)
¶
Creates a function to compute the loss of a given model on a given batch of data, i.e. for the \(i\)-th batch \(B_i\)
PARAMETER | DESCRIPTION |
---|---|
model |
The model for which the loss should be computed.
TYPE:
|
loss |
The loss function to be used. |
x |
The input data for the batch.
TYPE:
|
y |
The true labels for the batch.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Callable[[Dict[str, Tensor]], Tensor]
|
A function that computes the loss of the model on the batch for given model parameters. |
Source code in src/pydvl/influence/torch/functional.py
get_hvp_function(model, loss, data_loader, use_hessian_avg=True, reverse_only=True, track_gradients=False)
¶
Returns a function that calculates the approximate Hessian-vector product for a given vector. If you want to
compute the exact hessian, i.e., pulling all data into memory and compute a full gradient computation, use
the function hvp
.
PARAMETER | DESCRIPTION |
---|---|
model |
A PyTorch module representing the model whose loss function's Hessian is to be computed.
TYPE:
|
loss |
A callable that takes the model's output and target as input and returns the scalar loss. |
data_loader |
A DataLoader instance that provides batches of data for calculating the Hessian-vector product. Each batch from the DataLoader is assumed to return a tuple where the first element is the model's input and the second element is the target output.
TYPE:
|
use_hessian_avg |
If True, the returned function uses batch-wise Hessian computation via batch_loss_function and averages the results. If False, the function uses backpropagation on the full empirical_loss_function, which is more accurate than averaging the batch hessians, but probably has a way higher memory usage.
TYPE:
|
reverse_only |
Whether to use only reverse-mode autodiff (True, default) or both forward- and reverse-mode autodiff (False).
TYPE:
|
track_gradients |
Whether to track gradients for the resulting tensor of the hessian vector products are (False, default).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Callable[[Tensor], Tensor]
|
A function that takes a single argument, a vector, and returns the product of the Hessian of the |
Source code in src/pydvl/influence/torch/functional.py
Created: 2023-09-02