快捷方式

torch.diff

torch.diff(input, n=1, dim=-1, prepend=None, append=None) Tensor

計算給定維度上的 n 階前向差分。

一階差分由 out[i] = input[i + 1] - input[i] 給出。高階差分透過遞迴使用 torch.diff() 計算。

引數
  • input (Tensor) – 用於計算差分的張量

  • n (int, 可選) – 遞迴計算差分的次數

  • dim (int, 可選) – 沿其計算差分的維度。預設為最後一個維度。

  • prepend (Tensor, 可選) – 在計算差分之前,沿 dim 維度新增到 input 前面的值。它們的維度必須與 input 等效,且形狀必須與 input 在 dim 維度外的形狀匹配。

  • append (Tensor, 可選) – 在計算差分之前,沿 dim 維度新增到 input 後面的值。它們的維度必須與 input 等效,且形狀必須與 input 在 dim 維度外的形狀匹配。

關鍵字引數

out (Tensor, 可選) – 輸出張量。

示例

>>> a = torch.tensor([1, 3, 2])
>>> torch.diff(a)
tensor([ 2, -1])
>>> b = torch.tensor([4, 5])
>>> torch.diff(a, append=b)
tensor([ 2, -1,  2,  1])
>>> c = torch.tensor([[1, 2, 3], [3, 4, 5]])
>>> torch.diff(c, dim=0)
tensor([[2, 2, 2]])
>>> torch.diff(c, dim=1)
tensor([[1, 1],
        [1, 1]])

文件

訪問 PyTorch 的全面開發者文件

檢視文件

教程

獲取適合初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並解答您的疑問

檢視資源