• 文档 >
  • torch.func >
  • torch.func API 参考 >
  • torch.func.jacrev
快捷键

torch.func.jacrev

torch.func.jacrev(func, argnums=0, *, has_aux=False, chunk_size=None, _preallocate_and_copy=False)[source]

使用反向模式自动微分计算 func 关于索引 argnum 的 arg(s) 的雅可比矩阵。

注意

使用 chunk_size=1 等价于逐行计算雅可比矩阵,即不适用 vmap() 的约束。

参数:
  • func (函数) – 一个 Python 函数,接受一个或多个参数,其中必须有一个是 Tensor,并返回一个或多个 Tensor

  • argnums (int 或 Tuple[int]) – 可选,整数或整数元组,表示要获取雅可比相对于的参数。默认:0。

  • has_aux (bool) – 标志表示 func 返回一个 (output, aux) 元组,其中第一个元素是要微分函数的输出,第二个元素是辅助对象,这些对象将不会被微分。默认:False。

  • chunk_size (None 或 int) – 如果为 None(默认),则使用最大块大小(相当于对 vjp 执行单个 vmap 来计算雅可比)。如果为 1,则使用 for 循环逐行计算雅可比。如果非 None,则每次计算雅可比 chunk_size 行(相当于对 vjp 执行多次 vmap)。如果在计算雅可比时遇到内存问题,请尝试指定非 None 的 chunk_size。

返回值:

返回一个函数,该函数接受与 func 相同的输入,并返回 func 相对于 argnums 的雅可比矩阵。如果 has_aux is True ,则返回的函数将返回一个 (jacobian, aux) 元组,其中 jacobian 是雅可比矩阵, auxfunc 返回的辅助对象。

使用点对点、一元运算的基本用法将给出对角数组作为雅可比矩阵。

>>> 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() 可以与自己组合以产生 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() 计算相对于第一个输入的雅可比矩阵。但是,可以通过使用 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_gradjacrev 结合。情况 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 外部的上下文管理器的结果。


© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源