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 的詳細步驟。