Skip to content
fn

detach

→Tensor
detach(input: Tensor)

Return a tensor that shares data but is detached from the autograd graph.

The returned tensor has requires_grad=False and is no longer a leaf of any computation; gradients flowing into it during backward will be silently dropped. Storage is shared, so mutating the detached view will affect the original (and may invalidate saved tensors).

Parameters

inputTensor
Source tensor.

Returns

Tensor

Detached view sharing storage with input.

Notes

Common idiom for capturing intermediate values for logging without holding graph references that would inhibit memory reclamation. Pair with clone (i.e. x.detach().clone()) when you also need an independent buffer.

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 2.0], requires_grad=True)
>>> y = lucid.detach(x)
>>> y.requires_grad
False