torch.autograd.forward_ad.unpack_dual — PyTorch 2.7 documentation (original) (raw)

Shortcuts

torch.autograd.forward_ad.unpack_dual(tensor, *, level=None)[source][source]

Unpack a “dual tensor” to get both its Tensor value and its forward AD gradient.

The result is a namedtuple (primal, tangent) where primal is a view oftensor’s primal and tangent is tensor’s tangent as-is. Neither of these tensors can be dual tensor of level level.

This function is backward differentiable.

Example:

with dual_level(): ... inp = make_dual(x, x_t) ... out = f(inp) ... y, jvp = unpack_dual(out) ... jvp = unpack_dual(out).tangent

Please see the forward-mode AD tutorialfor detailed steps on how to use this API.

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.