loss
#
Utilities for defining loss functions.
Functions:
-
to_closure
–Convert a loss function to a closure function used by second-order optimizers.
-
combine_closures
–Combine multiple closures into a single closure.
-
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
combine_closures
#
combine_closures(
closures: dict[str, ClosureFn],
weights: dict[str, float] | None = None,
verbose: bool = False,
) -> ClosureFn
Combine multiple closures into a single closure.
Parameters:
-
closures
(dict[str, ClosureFn]
) –A dictionary of closure functions.
-
weights
(dict[str, float] | None
, default:None
) –Optional dictionary of weights for each closure function.
-
verbose
(bool
, default:False
) –Whether to log the loss of each closure function.
Returns:
-
ClosureFn
–A combined closure function.
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