dual_level¶
- class torch.autograd.forward_ad.dual_level[來源][來源]¶
forward AD 的上下文管理器,所有 forward AD 計算都必須在
dual_level
上下文中發生。注意
dual_level
上下文適當地進入和退出 dual level 以控制當前的 forward AD level,此 level 預設由本 API 中的其他函數使用。我們目前沒有計畫支援巢狀的
dual_level
context,因此僅支援單一 forward AD 層級。 若要計算更高階的 forward grads,可以使用torch.func.jvp()
。範例
>>> x = torch.tensor([1]) >>> x_t = torch.tensor([1]) >>> with dual_level(): ... inp = make_dual(x, x_t) ... # Do computations with inp ... out = your_fn(inp) ... _, grad = unpack_dual(out) >>> grad is None False >>> # After exiting the level, the grad is deleted >>> _, grad_after = unpack_dual(out) >>> grad is None True
請參閱 forward-mode AD 教學 以取得關於如何使用此 API 的詳細步驟。