fn
grad_and_value
→Callablegrad_and_value(func: Callable[..., Tensor], argnums: int | tuple[int, ...] = 0, has_aux: bool = False)Return a function computing both the gradient and the primal value.
Equivalent to combining grad with an evaluation of func,
but evaluates the forward pass exactly once. This is the canonical
pattern for training loops where the loss value is also needed for
logging or learning-rate scheduling.
Parameters
funcCallableFunction returning a scalar Tensor (or
(scalar, aux) when
has_aux=True).argnumsint or tuple of int= 0Positional argument index/indices to differentiate. Default
0.has_auxbool= FalseIf
True, func must return (loss, aux) and the wrapped
callable returns (grads, (loss, aux)). Default False.Returns
CallableFunction returning (grads, value) — or (grads, (value, aux))
when has_aux=True.
Notes
Mathematically computes both and in
a single forward + backward sweep, saving redundant work compared to
calling func and grad(func) separately.
Examples
>>> import lucid
>>> from lucid.func import grad_and_value
>>> f = lambda x: (x ** 2).sum()
>>> gv = grad_and_value(f)
>>> grads, value = gv(lucid.tensor([1.0, 2.0, 3.0]))
>>> value # 14.0
Tensor(14.)