Shortcuts

torch.diff

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

计算沿给定维度的第n个前向差分。

一阶差分由 out[i] = input[i + 1] - input[i] 给出。高阶差分通过递归使用 torch.diff() 计算。

Parameters
  • 输入 (Tensor) – 要计算差异的张量

  • n (int, 可选) – 递归计算差异的次数

  • dim (int, 可选) – 计算差异的维度。 默认是最后一个维度。

  • prepend (Tensor, 可选) – 在计算差值之前,沿 dim 方向插入或附加到 input 的值。它们的维度必须与输入的维度相等,并且它们的形状必须与输入的形状匹配,除了在 dim 方向上。

  • append (Tensor, 可选) – 在计算差值之前,沿 dim 追加或前置到 input 的值。它们的维度必须与输入的维度相等,并且它们的形状必须与输入的形状匹配,除了在 dim 上。

Keyword Arguments

输出 (张量, 可选) – 输出张量。

示例:

>>> 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]])