torch.func.jacrev#
- torch.func.jacrev(func, argnums=0, *, has_aux=False, chunk_size=None, _preallocate_and_copy=False)[原始碼]#
使用反向模式自動微分計算
func相對於argnum索引處的引數(們)的雅可比矩陣。注意
使用
chunk_size=1等同於透過 for 迴圈逐行計算 Jacobian,即vmap()的約束不適用。- 引數
func (function) – A Python function that takes one or more arguments, one of which must be a Tensor, and returns one or more Tensors
argnums (int 或 Tuple[int]) – 可選,整數或整數元組,指定要計算其 Jacobian 的引數。預設為:0。
has_aux (bool) – 指示
func返回一個(output, aux)元組的標誌,其中第一個元素是要微分的函式的輸出,第二個元素是不會被微分的輔助物件。預設為:False。chunk_size (None 或 int) – 如果為 None(預設),則使用最大塊大小(等同於對 vjp 進行單個 vmap 來計算 Jacobian)。如果為 1,則逐行透過 for 迴圈計算 Jacobian。如果不是 None,則一次計算
chunk_size行的 Jacobian(等同於進行多次 vmap over vjp)。如果您在計算 Jacobian 時遇到記憶體問題,請嘗試指定一個非 None 的 chunk_size。
- 返回
返回一個函式,該函式接受與
func相同的輸入,並返回func相對於argnums中指定引數的 Jacobian。如果has_aux 為 True,則返回的函式將返回一個(jacobian, aux)元組,其中jacobian是 Jacobian,aux是func返回的輔助物件。
A basic usage with a pointwise, unary operation will give a diagonal array as the Jacobian
>>> 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)
If you would like to compute the output of the function as well as the jacobian of the function, use the
has_auxflag to return the output as an auxiliary object>>> 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 組合以生成批次 Jacobian。>>> from torch.func import jacrev, vmap >>> x = torch.randn(64, 5) >>> jacobian = vmap(jacrev(torch.sin))(x) >>> assert jacobian.shape == (64, 5, 5)
此外,
jacrev()可以與自身組合以生成 Hessian。>>> 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()計算相對於第一個輸入的 Jacobian。但是,您可以使用argnums來計算相對於其他引數的 Jacobian。>>> 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)
Additionally, passing a tuple to
argnumswill compute the Jacobian with respect to multiple arguments>>> 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外部的上下文管理器的結果。