torch.autograd.functional.hessian#
- torch.autograd.functional.hessian(func, inputs, create_graph=False, strict=False, vectorize=False, outer_jacobian_strategy='reverse-mode')[原始碼]#
計算給定標量函式的 Hessian。
- 引數
func (function) – 一個接受 Tensor 輸入並返回具有單個元素的 Tensor 的 Python 函式。
create_graph (bool, optional) – 如果為
True,則 Hessian 將以可微分的方式計算。請注意,當strict為False時,結果不能要求梯度或與輸入斷開連線。預設為False。strict (bool, optional) – 如果為
True,當檢測到存在某個輸入,所有輸出都與該輸入無關時,將引發錯誤。如果為False,我們將為該輸入返回一個零張量作為 Hessian,這與數學預期值一致。預設為False。vectorize (bool, optional) – 此功能仍處於實驗階段。如果您正在尋找更少實驗性且效能更高的替代方案,請考慮使用
torch.func.hessian()。在計算 Hessian 時,通常我們會為 Hessian 的每一行呼叫一次autograd.grad。如果此標誌為True,我們將使用 vmap 原型功能作為後端來向量化對autograd.grad的呼叫,因此我們只調用一次,而不是每行呼叫一次。這在許多用例中應能提高效能,但由於此功能尚不完整,可能會出現效能下降。請使用 torch._C._debug_only_display_vmap_fallback_warnings(True) 顯示任何效能警告,如果您的用例存在警告,請向我們提交 issue。預設為False。outer_jacobian_strategy (str, optional) – Hessian 透過計算 Jacobian 的 Jacobian 來計算。內部 Jacobian 始終以反向模式 AD 計算。將策略設定為
"forward-mode"或"reverse-mode"決定了外部 Jacobian 是使用前向模式還是反向模式 AD 計算。目前,使用"forward-mode"計算外部 Jacobian 需要vectorized=True。預設為"reverse-mode"。
- 返回
如果有一個輸入,這將是一個包含輸入 Hessian 的單個張量。如果它是一個元組,那麼 Hessian 將是一個元組的元組,其中
Hessian[i][j]將包含第i個輸入和第j個輸入的 Hessian,大小為第i個輸入的大小加上第j個輸入的大小之和。Hessian[i][j]將具有與相應的第i個輸入相同的 dtype 和 device。- 返回型別
Hessian (Tensor 或張量元組)
示例
>>> def pow_reducer(x): ... return x.pow(3).sum() >>> inputs = torch.rand(2, 2) >>> hessian(pow_reducer, inputs) tensor([[[[5.2265, 0.0000], [0.0000, 0.0000]], [[0.0000, 4.8221], [0.0000, 0.0000]]], [[[0.0000, 0.0000], [1.9456, 0.0000]], [[0.0000, 0.0000], [0.0000, 3.2550]]]])
>>> hessian(pow_reducer, inputs, create_graph=True) tensor([[[[5.2265, 0.0000], [0.0000, 0.0000]], [[0.0000, 4.8221], [0.0000, 0.0000]]], [[[0.0000, 0.0000], [1.9456, 0.0000]], [[0.0000, 0.0000], [0.0000, 3.2550]]]], grad_fn=<ViewBackward>)
>>> def pow_adder_reducer(x, y): ... return (2 * x.pow(2) + 3 * y.pow(2)).sum() >>> inputs = (torch.rand(2), torch.rand(2)) >>> hessian(pow_adder_reducer, inputs) ((tensor([[4., 0.], [0., 4.]]), tensor([[0., 0.], [0., 0.]])), (tensor([[0., 0.], [0., 0.]]), tensor([[6., 0.], [0., 6.]])))