Skip to content
fn

gradcheck

→bool True if all gradients agree within tolerance.
gradcheck(func: Callable[..., Tensor | tuple[Tensor, ...]], inputs: Sequence[Tensor], eps: float = 1e-06, atol: float = 1e-05, rtol: float = 0.001, raise_exception: bool = True)

Compare analytical gradients from backward() against finite-difference Jacobians.

Implemented entirely with the Lucid C++ engine — no numpy. Only the inputs that require grad are differentiated, as in the reference framework; the others are held constant, so an index or a mask can be passed among them.

Parameters

funccallable
Scalar-valued function mapping Tensor inputs to a single-element Tensor.
inputssequence of Tensor
Inputs to func. Those with requires_grad=True (float tensors) are checked; the rest pass through unchanged.
epsfloat= 1e-06
Finite-difference step size (default 1e-6).
atolfloat= 1e-05
Tolerances for the gradient comparison.
rtolfloat= 1e-05
Tolerances for the gradient comparison.
raise_exceptionbool= True
Raise AssertionError on mismatch (default True), else return False.

Raises

ValueError
If no input requires grad, or func is not scalar-valued.

Examples

>>> import lucid
>>> from lucid.autograd import gradcheck
>>> x = lucid.randn(3, dtype=lucid.float64, requires_grad=True)
>>> index = lucid.tensor([0, 2])
>>> gradcheck(lambda x, i: (x.gather(0, i) ** 2).sum(), [x, index])
True

Used by 1