Twice differentiable
TensorType = TypeVar('TensorType', bound=Sequence)
module-attribute
¶
Type variable for tensors, i.e. sequences of numbers
ModelType = TypeVar('ModelType', bound='TwiceDifferentiable')
module-attribute
¶
Type variable for twice differentiable models
DataLoaderType = TypeVar('DataLoaderType', bound=Iterable)
module-attribute
¶
Type variable for data loaders
InverseHvpResult
dataclass
¶
Bases: Generic[TensorType]
Container class for results of solving a problem \(Ax=b\)
PARAMETER | DESCRIPTION |
---|---|
x |
solution of a problem \(Ax=b\)
TYPE:
|
info |
additional information, to couple with the solution itself |
TwiceDifferentiable
¶
Bases: ABC
, Generic[TensorType]
Abstract base class for wrappers of differentiable models and losses. Meant to be subclassed for each supported framework. Provides methods to compute gradients and second derivative of the loss wrt. the model parameters
num_params: int
abstractmethod
property
¶
Returns the number of parameters of the model
parameters: List[TensorType]
abstractmethod
property
¶
Returns all the model parameters that require differentiation
grad(x, y, create_graph=False)
¶
Calculates gradient of model parameters with respect to the model parameters.
PARAMETER | DESCRIPTION |
---|---|
x |
A matrix representing the features \(x_i\).
TYPE:
|
y |
A matrix representing the target values \(y_i\).
TYPE:
|
create_graph |
Used for further differentiation on input parameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
An array with the gradients of the model. |
Source code in src/pydvl/influence/twice_differentiable.py
hessian(x, y)
¶
Calculates the full Hessian of \(L(f(x),y)\) with respect to the model parameters given data \(x\) and \(y\).
PARAMETER | DESCRIPTION |
---|---|
x |
An array representing the features \(x_i\).
TYPE:
|
y |
An array representing the target values \(y_i\).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A tensor representing the Hessian of the model, i.e. the second derivative with respect to the model parameters. |
Source code in src/pydvl/influence/twice_differentiable.py
mvp(grad_xy, v, backprop_on, *, progress=False)
abstractmethod
staticmethod
¶
Calculates the second order derivative of the model along directions \(v\).
The second order derivative can be selected through the backprop_on
argument.
PARAMETER | DESCRIPTION |
---|---|
grad_xy |
An array [P] holding the gradients of the model parameters with respect to input \(x\) and
labels \(y\). \(P\) is the number of parameters of the model. Typically obtained through
TYPE:
|
v |
An array ([DxP] or even one-dimensional [D]) which multiplies the matrix. \(D\) is the number of directions.
TYPE:
|
progress |
If
TYPE:
|
backprop_on |
Tensor used in the second backpropagation. The first one is along \(x\) and \(y\)
as defined via
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorType
|
A matrix representing the implicit matrix-vector product of the model along the given directions.
Output shape is [DxM], where \(M\) is the number of elements of |
Source code in src/pydvl/influence/twice_differentiable.py
TensorUtilities
¶
Bases: Generic[TensorType, ModelType]
, ABC
__init_subclass__(**kwargs)
¶
Automatically registers non-abstract subclasses in the registry.
This method checks if twice_differentiable_type
is defined in the subclass and if it is of the correct type.
If either attribute is missing or incorrect, a TypeError
is raised.
PARAMETER | DESCRIPTION |
---|---|
kwargs |
Additional keyword arguments.
DEFAULT:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the subclass does not define |
Source code in src/pydvl/influence/twice_differentiable.py
einsum(equation, *operands)
abstractmethod
staticmethod
¶
Sums the product of the elements of the input operands
along dimensions specified using a notation
based on the Einstein summation convention.
Source code in src/pydvl/influence/twice_differentiable.py
cat(a, **kwargs)
abstractmethod
staticmethod
¶
stack(a, **kwargs)
abstractmethod
staticmethod
¶
unsqueeze(x, dim)
abstractmethod
staticmethod
¶
get_element(x, idx)
abstractmethod
staticmethod
¶
slice(x, start, stop, axis=0)
abstractmethod
staticmethod
¶
shape(x)
abstractmethod
staticmethod
¶
reshape(x, shape)
abstractmethod
staticmethod
¶
cat_gen(a, resulting_shape, model)
abstractmethod
staticmethod
¶
Concatenate tensors from a generator. Resulting tensor is of shape resulting_shape and compatible to model
Source code in src/pydvl/influence/twice_differentiable.py
from_twice_differentiable(twice_diff)
classmethod
¶
Factory method to create an instance of a subclass TensorUtilities from an instance of a subclass of TwiceDifferentiable.
PARAMETER | DESCRIPTION |
---|---|
twice_diff |
An instance of a subclass of TwiceDifferentiable for which a corresponding TensorUtilities object is required.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Type[TensorUtilities]
|
An subclass of TensorUtilities registered to the provided subclass instance of TwiceDifferentiable object. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If there's no registered TensorUtilities for the provided TwiceDifferentiable type. |
Source code in src/pydvl/influence/twice_differentiable.py
Created: 2023-09-02