enable_grad
enable_grad()Context manager / decorator that (re-)enables gradient tracking.
Counterpart of no_grad: turns gradient recording back
on inside the wrapped scope, even when an outer scope has
disabled it. Useful when a small differentiable subroutine
runs inside a larger no_grad block — for example, an
inner training step embedded in an outer evaluation loop, or
explicit gradient computation for diagnostic plots inside a
decorated inference function.
The previous gradient mode is restored on exit (RAII), so the
surrounding no_grad continues to apply once the inner
scope finishes.
Parameters
NoneAttributes
_prevbool__enter__
time and restored at __exit__.Notes
Setting the flag back to True re-arms autograd to record
the chain rule
for every op executed inside the scope.
Examples
>>> import lucid
>>> from lucid.autograd import no_grad, enable_grad
>>> x = lucid.tensor([1.0, 2.0], requires_grad=True)
>>> with no_grad():
... with enable_grad():
... y = (x * x).sum()
>>> y.requires_grad
TrueMethods (3)
__call__
→_F__call__(fn: _F)Forward to the underlying callable (see class docstring).
__enter__
→enable_grad__enter__()Enter the context. Returns self so the value can be bound via with ... as.
__exit__
→None__exit__(args: object = ())Exit the context, restoring any state that was modified on entry.