快捷方式

dual_level

class torch.autograd.forward_ad.dual_level[source][source]

用於前向 AD 的上下文管理器,所有前向 AD 計算必須在 dual_level 上下文內進行。

注意

dual_level 上下文負責適當進入和退出雙層級,從而控制當前前向 AD 層級,API 中的其他函式預設使用此層級。

但是,我們目前不打算支援巢狀的 dual_level 上下文,因此僅支援單個前向 AD 層級。要計算高階前向梯度,可以使用 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

請參閱前向模式 AD 教程,瞭解有關如何使用此 API 的詳細步驟。

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源