快捷鍵

torch.cumulative_trapezoid

torch.cumulative_trapezoid(y, x=None, *, dx=None, dim=-1) Tensor

沿著 dim 累積計算梯形法則。預設情況下,假定元素之間的間距為 1,但可以使用 dx 來指定不同的常數間距,並且可以使用 x 來指定沿著 dim 的任意間距。

如需更多詳細資訊,請閱讀 torch.trapezoid()torch.trapezoid() 與此函數之間的差異在於,torch.trapezoid() 會針對每次積分傳回一個值,而此函數會針對積分中的每個間距傳回一個累積值。這類似於 .sum 傳回一個值,而 .cumsum 傳回一個累積總和。

參數
  • y (Tensor) – 計算梯形法則時使用的值。

  • x (Tensor) – 如果指定,則如上所述定義值之間的間距。

關鍵字參數
  • dx (float) – 值之間的常數間距。如果未指定 xdx,則預設為 1。有效地將結果乘以其值。

  • dim (int) – 沿其計算梯形法則的維度。預設為最後一個(最內層)維度。

範例

>>> # Cumulatively computes the trapezoidal rule in 1D, spacing is implicitly 1.
>>> y = torch.tensor([1, 5, 10])
>>> torch.cumulative_trapezoid(y)
tensor([3., 10.5])

>>> # Computes the same trapezoidal rule directly up to each element to verify
>>> (1 + 5) / 2
3.0
>>> (1 + 10 + 10) / 2
10.5

>>> # Cumulatively computes the trapezoidal rule in 1D with constant spacing of 2
>>> # NOTE: the result is the same as before, but multiplied by 2
>>> torch.cumulative_trapezoid(y, dx=2)
tensor([6., 21.])

>>> # Cumulatively computes the trapezoidal rule in 1D with arbitrary spacing
>>> x = torch.tensor([1, 3, 6])
>>> torch.cumulative_trapezoid(y, x)
tensor([6., 28.5])

>>> # Computes the same trapezoidal rule directly up to each element to verify
>>> ((3 - 1) * (1 + 5)) / 2
6.0
>>> ((3 - 1) * (1 + 5) + (6 - 3) * (5 + 10)) / 2
28.5

>>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 matrix
>>> y = torch.arange(9).reshape(3, 3)
tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
>>> torch.cumulative_trapezoid(y)
tensor([[ 0.5,  2.],
        [ 3.5,  8.],
        [ 6.5, 14.]])

>>> # Cumulatively computes the trapezoidal rule for each column of the matrix
>>> torch.cumulative_trapezoid(y, dim=0)
tensor([[ 1.5,  2.5,  3.5],
        [ 6.0,  8.0, 10.0]])

>>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 ones matrix
>>> #   with the same arbitrary spacing
>>> y = torch.ones(3, 3)
>>> x = torch.tensor([1, 3, 6])
>>> torch.cumulative_trapezoid(y, x)
tensor([[2., 5.],
        [2., 5.],
        [2., 5.]])

>>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 ones matrix
>>> #   with different arbitrary spacing per row
>>> y = torch.ones(3, 3)
>>> x = torch.tensor([[1, 2, 3], [1, 3, 5], [1, 4, 7]])
>>> torch.cumulative_trapezoid(y, x)
tensor([[1., 2.],
        [2., 4.],
        [3., 6.]])

文件

取得 PyTorch 的完整開發人員文件

檢視文件

教學課程

取得適合初學者和進階開發人員的深入教學課程

檢視教學課程

資源

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

檢視資源