torch文档里的diff方法原文
时间: 2023-06-15 16:05:30 浏览: 86
PyTorch文档中diff方法的原文如下:
```
torch.diff(input, n=1, dim=-1, prepend=None, append=None) -> Tensor
```
Computes the n-th discrete difference along given axis.
The first order difference is given by out[i] = input[i + 1] - input[i] along the given axis, higher order differences are calculated by using diff recursively.
Parameters:
- input (Tensor) – the input tensor.
- n (int, optional) – the order of the difference. Default: 1
- dim (int, optional) – the dimension to take the difference along. Default: -1
- prepend (Tensor, optional) – tensor to prepend to input. Its leading dimension must match input‘s dim-th dimension. Default: None
- append (Tensor, optional) – tensor to append to input. Its leading dimension must match input‘s dim-th dimension. Default: None
Returns: Tensor – the n-th discrete difference along the given axis.
Example:
```
>>> x = torch.tensor([1, 2, 4, 7, 0])
>>> torch.diff(x)
tensor([ 1, 2, 3, -7])
>>> torch.diff(x, n=2)
tensor([ 1, 1, -10])
>>> x = torch.randn(2, 3)
>>> torch.diff(x, dim=0)
tensor([[-0.5765, 1.3316, -0.5607],
[ 0.0553, -1.2247, -0.0795]])
>>> prepend = torch.randn(1, 3)
>>> append = torch.randn(1, 3)
>>> torch.diff(x, prepend=prepend, append=append)
tensor([[[-1.3040, 0.4054, 0.9327],
[-1.0124, -0.6604, 0.8175],
[ 1.4238, -1.1742, -1.5091]],
[[-1.6820, 0.9269, 1.1364],
[ 1.0689, -0.5643, -1.3558],
[-1.8033, 0.9473, 0.8262]]])
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)