快捷方式

torch.func.jacfwd

torch.func.jacfwd(func, argnums=0, has_aux=False, *, randomness='error')[source]

使用前向模式自動微分計算 func 關於索引為 argnum 的引數的雅可比矩陣

引數
  • func (function) – 一個 Python 函式,接受一個或多個引數(其中一個必須是 Tensor),並返回一個或多個 Tensor

  • argnums (intTuple[int]) – 可選,整數或整數元組,指定對哪些引數計算雅可比矩陣。預設值:0。

  • has_aux (bool) – 標誌,指示 func 返回一個 (output, aux) 元組,其中第一個元素是要進行微分的函式的輸出,第二個元素是不會進行微分的輔助物件。預設值:False。

  • randomness (str) – 標誌,指示使用何種型別的隨機性。更多細節請參閱 vmap()。允許值:“different”、“same”、“error”。預設值:“error”

返回

返回一個函式,該函式接受與 func 相同的輸入,並返回 func 關於索引為 argnums 的引數的雅可比矩陣。如果 has_aux is True,則返回的函式將返回一個 (jacobian, aux) 元組,其中 jacobian 是雅可比矩陣,aux 是由 func 返回的輔助物件。

注意

您可能會遇到此 API 報錯,提示“運算元 X 未實現前向模式 AD”。如果遇到這種情況,請提交錯誤報告,我們會優先處理。另一種選擇是使用 jacrev(),它具有更好的運算元覆蓋範圍。

對於逐點的一元運算,其基本用法將得到一個對角陣列作為雅可比矩陣

>>> from torch.func import jacfwd
>>> x = torch.randn(5)
>>> jacobian = jacfwd(torch.sin)(x)
>>> expected = torch.diag(torch.cos(x))
>>> assert torch.allclose(jacobian, expected)

jacfwd() 可以與 vmap 組合以生成批處理雅可比矩陣

>>> from torch.func import jacfwd, vmap
>>> x = torch.randn(64, 5)
>>> jacobian = vmap(jacfwd(torch.sin))(x)
>>> assert jacobian.shape == (64, 5, 5)

如果您想計算函式的輸出以及函式的雅可比矩陣,請使用 has_aux 標誌將輸出作為輔助物件返回

>>> from torch.func import jacfwd
>>> x = torch.randn(5)
>>>
>>> def f(x):
>>>   return x.sin()
>>>
>>> def g(x):
>>>   result = f(x)
>>>   return result, result
>>>
>>> jacobian_f, f_x = jacfwd(g, has_aux=True)(x)
>>> assert torch.allclose(f_x, f(x))

此外,jacrev() 可以與自身或 jacrev() 組合以生成 Hessian 矩陣

>>> from torch.func import jacfwd, jacrev
>>> def f(x):
>>>   return x.sin().sum()
>>>
>>> x = torch.randn(5)
>>> hessian = jacfwd(jacrev(f))(x)
>>> assert torch.allclose(hessian, torch.diag(-x.sin()))

預設情況下,jacfwd() 計算關於第一個輸入的雅可比矩陣。然而,透過使用 argnums,它可以計算關於不同引數的雅可比矩陣

>>> from torch.func import jacfwd
>>> def f(x, y):
>>>   return x + y ** 2
>>>
>>> x, y = torch.randn(5), torch.randn(5)
>>> jacobian = jacfwd(f, argnums=1)(x, y)
>>> expected = torch.diag(2 * y)
>>> assert torch.allclose(jacobian, expected)

此外,向 argnums 傳入一個元組將計算關於多個引數的雅可比矩陣

>>> from torch.func import jacfwd
>>> def f(x, y):
>>>   return x + y ** 2
>>>
>>> x, y = torch.randn(5), torch.randn(5)
>>> jacobian = jacfwd(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 全面的開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深入教程

檢視教程

資源

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

檢視資源