loss
#
Utilities for defining loss functions.
Functions:
-
to_closure
–Convert a loss function to a closure function used by second-order optimizers.
-
approximate_hessian
–Compute the outer product approximation of the hessian of a least squares
to_closure
#
to_closure(
loss_fn: Callable[Concatenate[Tensor, P], Tensor],
*args: args,
**kwargs: kwargs
) -> ClosureFn
Convert a loss function to a closure function used by second-order optimizers.
Parameters:
-
loss_fn
(Callable[Concatenate[Tensor, P], Tensor]
) –The loss function to convert. This should take in a tensor of parameters with
shape=(n,)
, and optionally a set ofargs
andkwargs
. -
*args
(args
, default:()
) –Positional arguments passed to
loss_fn
. -
**kwargs
(kwargs
, default:{}
) –Keyword arguments passed to
loss_fn
.
Returns:
-
ClosureFn
–A closure function that takes in a tensor of parameters with
shape=(n,)
, a boolean flag indicating whether to compute the gradient, and a boolean flag indicating whether to compute the Hessian. It returns a tuple of the loss value, the gradient, and the Hessian.
Source code in descent/utils/loss.py
approximate_hessian
#
Compute the outer product approximation of the hessian of a least squares
loss function of the sum sum((y_pred - y_ref)**2)
.
Parameters:
-
x
(Tensor
) –The parameter tensor with
shape=(n_parameters,)
. -
y_pred
(Tensor
) –The values predicted using
x
withshape=(n_predications,)
.
Returns:
-
–
The outer product approximation of the hessian with ``shape=n_parameters