functorch.jacrev¶
-
functorch.
jacrev
(func, argnums=0, *, has_aux=False, chunk_size=None, _preallocate_and_copy=False)[原始碼]¶ 使用反向模式自動微分計算
func
關於索引argnum
處的參數的雅可比矩陣。注意事項
使用
chunk_size=1
等同於使用 for 迴圈逐行計算雅可比矩陣,也就是說vmap()
的限制條件不適用。- 參數
func (函式) – 一個 Python 函式,它接受一個或多個參數(其中一個必須是張量),並返回一個或多個張量。
argnums (int 或 Tuple[int]) – 可選,整數或整數元組,指定要計算雅可比矩陣的參數。預設值:0。
has_aux (bool) – 標誌,指示
func
返回一個(輸出, 輔助)
元組,其中第一個元素是要微分的函式的輸出,第二個元素是不會被微分的輔助物件。預設值:False。chunk_size (None 或 int) – 如果為 None(預設值),則使用最大區塊大小(相當於在 vjp 上執行單個 vmap 來計算雅可比矩陣)。如果為 1,則使用 for 迴圈逐行計算雅可比矩陣。如果不是 None,則一次計算
chunk_size
行雅可比矩陣(相當於在 vjp 上執行多個 vmap)。如果您在計算雅可比矩陣時遇到記憶體問題,請嘗試指定一個非 None 的 chunk_size。
- 返回
回傳一個函式,該函式接受與
func
相同的輸入,並回傳func
關於argnums
指定參數的雅可比矩陣。如果has_aux 為 True
,則回傳的函式會回傳一個(jacobian, aux)
元組,其中jacobian
是雅可比矩陣,而aux
是func
回傳的輔助物件。
對於逐點、單元運算的基本用法,將會得到一個對角矩陣作為雅可比矩陣。
>>> from torch.func import jacrev >>> x = torch.randn(5) >>> jacobian = jacrev(torch.sin)(x) >>> expected = torch.diag(torch.cos(x)) >>> assert torch.allclose(jacobian, expected)
如果您想同時計算函式的輸出和函式的雅可比矩陣,請使用
has_aux
標記將輸出作為輔助物件回傳。>>> from torch.func import jacrev >>> x = torch.randn(5) >>> >>> def f(x): >>> return x.sin() >>> >>> def g(x): >>> result = f(x) >>> return result, result >>> >>> jacobian_f, f_x = jacrev(g, has_aux=True)(x) >>> assert torch.allclose(f_x, f(x))
jacrev()
可以與 vmap 組合使用以產生批次的雅可比矩陣。>>> from torch.func import jacrev, vmap >>> x = torch.randn(64, 5) >>> jacobian = vmap(jacrev(torch.sin))(x) >>> assert jacobian.shape == (64, 5, 5)
此外,
jacrev()
可以與自身組合使用以產生黑塞矩陣。>>> from torch.func import jacrev >>> def f(x): >>> return x.sin().sum() >>> >>> x = torch.randn(5) >>> hessian = jacrev(jacrev(f))(x) >>> assert torch.allclose(hessian, torch.diag(-x.sin()))
預設情況下,
jacrev()
計算關於第一個輸入的雅可比矩陣。然而,它可以透過使用argnums
計算關於不同參數的雅可比矩陣。>>> from torch.func import jacrev >>> def f(x, y): >>> return x + y ** 2 >>> >>> x, y = torch.randn(5), torch.randn(5) >>> jacobian = jacrev(f, argnums=1)(x, y) >>> expected = torch.diag(2 * y) >>> assert torch.allclose(jacobian, expected)
此外,將一個元組傳遞給
argnums
將會計算關於多個參數的雅可比矩陣。>>> from torch.func import jacrev >>> def f(x, y): >>> return x + y ** 2 >>> >>> x, y = torch.randn(5), torch.randn(5) >>> jacobian = jacrev(f, argnums=(0, 1))(x, y) >>> expectedX = torch.diag(torch.ones_like(x)) >>> expectedY = torch.diag(2 * y) >>> assert torch.allclose(jacobian[0], expectedX) >>> assert torch.allclose(jacobian[1], expectedY)
注意事項
將 PyTorch 的
torch.no_grad
與jacrev
一起使用。情況 1:在函式內使用torch.no_grad
。>>> def f(x): >>> with torch.no_grad(): >>> c = x ** 2 >>> return x - c
在這種情況下,
jacrev(f)(x)
將會遵循內部的torch.no_grad
。情況 2:在
torch.no_grad
上下文管理器內使用jacrev
。>>> with torch.no_grad(): >>> jacrev(f)(x)
在這種情況下,
jacrev
將會遵循內部的torch.no_grad
,但不遵循外部的。這是因為jacrev
是一個「函式轉換」:它的結果不應該取決於f
之外的上下文管理器的結果。警告
我們已將 functorch 整合到 PyTorch 中。作為整合的最後一步,functorch.jacrev 從 PyTorch 2.0 開始已被棄用,並將在 PyTorch >= 2.3 的未來版本中刪除。請改用 torch.func.jacrev;有關更多詳細資訊,請參閱 PyTorch 2.0 版本說明和/或 torch.func 遷移指南 https://pytorch.dev.org.tw/docs/master/func.migrating.html