捷徑

torch.gradient

torch.gradient(input, *, spacing=1, dim=None, edge_order=1) Tensor 列表

使用二階精確的中心差分法,以及邊界上的一階或二階估計,來估計函數 g:RnRg : \mathbb{R}^n \rightarrow \mathbb{R} 在一個或多個維度上的梯度。參考:二階精確的中心差分法

函數 gg 的梯度是使用樣本估算的。 預設情況下,如果未指定 spacing,則樣本完全由 input 描述,並且輸入座標到輸出的映射與張量從索引到值的映射相同。 例如,對於一個三維的 input,所描述的函數是 g:R3Rg : \mathbb{R}^3 \rightarrow \mathbb{R},並且 g(1,2,3) ==input[1,2,3]g(1, 2, 3)\ == input[1, 2, 3]

當指定 spacing 時,它會修改 input 和輸入座標之間的關係。 這在下面的「關鍵字引數」部分中詳細說明。

梯度是透過獨立估算 gg 的每個偏導數來估算的。 如果 ggC3C^3 中(它至少有 3 個連續導數),則此估算很準確,並且可以通過提供更接近的樣本來改進估算。 從數學上講,偏導數的每個內部點的值都是使用帶餘項的泰勒定理估算的。 令 xx 為一個內部點,其左側和右側的相鄰點分別為 xhlx-h_lx+hrx+h_rf(x+hr)f(x+h_r)f(xhl)f(x-h_l) 可以使用以下方法估算:

f(x+hr)=f(x)+hrf(x)+hr2f(x)2+hr3f(ξ1)6,ξ1(x,x+hr)f(xhl)=f(x)hlf(x)+hl2f(x)2hl3f(ξ2)6,ξ2(x,xhl)\begin{aligned} f(x+h_r) = f(x) + h_r f'(x) + {h_r}^2 \frac{f''(x)}{2} + {h_r}^3 \frac{f'''(\xi_1)}{6}, \xi_1 \in (x, x+h_r) \\ f(x-h_l) = f(x) - h_l f'(x) + {h_l}^2 \frac{f''(x)}{2} - {h_l}^3 \frac{f'''(\xi_2)}{6}, \xi_2 \in (x, x-h_l) \\ \end{aligned}

利用 fC3f \in C^3 的事實並求解線性系統,我們推導出:

f(x)hl2f(x+hr)hr2f(xhl)+(hr2hl2)f(x)hrhl2+hr2hlf'(x) \approx \frac{ {h_l}^2 f(x+h_r) - {h_r}^2 f(x-h_l) + ({h_r}^2-{h_l}^2 ) f(x) }{ {h_r} {h_l}^2 + {h_r}^2 {h_l} }

注意

我們以相同的方式估算複數域中函數 g:CnCg : \mathbb{C}^n \rightarrow \mathbb{C} 的梯度。

每個偏導數在邊界點的值的計算方式不同。 請參閱下面的 edge_order。

參數

input (Tensor) – 表示函數值的張量

關鍵字參數
  • spacing (scalar, scalar 的列表, Tensor 的列表, 選用) – spacing 可用於修改 input 張量的索引與樣本座標之間的關係。如果 spacing 是一個純量,則索引會乘以該純量以產生座標。例如,如果 spacing=2,則索引 (1, 2, 3) 會變成座標 (2, 4, 6)。如果 spacing 是一個純量的列表,則對應的索引會相乘。例如,如果 spacing=(2, -1, 3),則索引 (1, 2, 3) 會變成座標 (2, -2, 9)。最後,如果 spacing 是一個一維張量的列表,則每個張量指定對應維度的座標。例如,如果索引是 (1, 2, 3) 且張量是 (t0, t1, t2),則座標是 (t0[1], t1[2], t2[3])

  • dim (int, int 的列表, 選用) – 用於近似梯度的維度或多個維度。預設情況下,計算每個維度中的偏梯度。請注意,當指定 dim 時,spacing 參數的元素必須與指定的維度相對應。”

  • edge_order (int, 選用) – 1 或 2,分別用於邊界(“邊緣”)值的一階二階估計。

範例

>>> # Estimates the gradient of f(x)=x^2 at points [-2, -1, 2, 4]
>>> coordinates = (torch.tensor([-2., -1., 1., 4.]),)
>>> values = torch.tensor([4., 1., 1., 16.], )
>>> torch.gradient(values, spacing = coordinates)
(tensor([-3., -2., 2., 5.]),)

>>> # Estimates the gradient of the R^2 -> R function whose samples are
>>> # described by the tensor t. Implicit coordinates are [0, 1] for the outermost
>>> # dimension and [0, 1, 2, 3] for the innermost dimension, and function estimates
>>> # partial derivative for both dimensions.
>>> t = torch.tensor([[1, 2, 4, 8], [10, 20, 40, 80]])
>>> torch.gradient(t)
(tensor([[ 9., 18., 36., 72.],
         [ 9., 18., 36., 72.]]),
 tensor([[ 1.0000, 1.5000, 3.0000, 4.0000],
         [10.0000, 15.0000, 30.0000, 40.0000]]))

>>> # A scalar value for spacing modifies the relationship between tensor indices
>>> # and input coordinates by multiplying the indices to find the
>>> # coordinates. For example, below the indices of the innermost
>>> # 0, 1, 2, 3 translate to coordinates of [0, 2, 4, 6], and the indices of
>>> # the outermost dimension 0, 1 translate to coordinates of [0, 2].
>>> torch.gradient(t, spacing = 2.0) # dim = None (implicitly [0, 1])
(tensor([[ 4.5000, 9.0000, 18.0000, 36.0000],
          [ 4.5000, 9.0000, 18.0000, 36.0000]]),
 tensor([[ 0.5000, 0.7500, 1.5000, 2.0000],
          [ 5.0000, 7.5000, 15.0000, 20.0000]]))
>>> # doubling the spacing between samples halves the estimated partial gradients.

>>>
>>> # Estimates only the partial derivative for dimension 1
>>> torch.gradient(t, dim = 1) # spacing = None (implicitly 1.)
(tensor([[ 1.0000, 1.5000, 3.0000, 4.0000],
         [10.0000, 15.0000, 30.0000, 40.0000]]),)

>>> # When spacing is a list of scalars, the relationship between the tensor
>>> # indices and input coordinates changes based on dimension.
>>> # For example, below, the indices of the innermost dimension 0, 1, 2, 3 translate
>>> # to coordinates of [0, 3, 6, 9], and the indices of the outermost dimension
>>> # 0, 1 translate to coordinates of [0, 2].
>>> torch.gradient(t, spacing = [3., 2.])
(tensor([[ 4.5000, 9.0000, 18.0000, 36.0000],
         [ 4.5000, 9.0000, 18.0000, 36.0000]]),
 tensor([[ 0.3333, 0.5000, 1.0000, 1.3333],
         [ 3.3333, 5.0000, 10.0000, 13.3333]]))

>>> # The following example is a replication of the previous one with explicit
>>> # coordinates.
>>> coords = (torch.tensor([0, 2]), torch.tensor([0, 3, 6, 9]))
>>> torch.gradient(t, spacing = coords)
(tensor([[ 4.5000, 9.0000, 18.0000, 36.0000],
         [ 4.5000, 9.0000, 18.0000, 36.0000]]),
 tensor([[ 0.3333, 0.5000, 1.0000, 1.3333],
         [ 3.3333, 5.0000, 10.0000, 13.3333]]))

文件

取得 PyTorch 的全面開發者文件

檢視文件

教學

取得適用於初學者和進階開發者的深入教學

檢視教學

資源

尋找開發資源並獲得您的問題解答

檢視資源