torch.gradient#
- torch.gradient(input, *, spacing=1, dim=None, edge_order=1) List of Tensors #
使用 二阶精度中心差分法 和一阶或二阶的边界估计,来估算函数 。
梯度的计算是基于采样点进行的。默认情况下,当未指定
spacing
时,采样点完全由input
决定,并且输入坐标到输出的映射与张量索引到值的映射相同。例如,对于一个三维input
,所描述的函数为 ,并且 。当指定
spacing
时,它会修改input
和输入坐标之间的关系。这将在下面的“关键字参数”部分详细说明。梯度的计算方法是独立估算 的每个偏导数。如果 属于 (即具有至少 3 个连续导数),则此估算准确。通过提供更接近的采样点可以改进估算。数学上,每个内部点的偏导数使用 带有余项的泰勒定理 进行估算。令 为一个内部点,其左侧和右侧的相邻点分别为 和 ,那么 和 可以使用以下公式估算:
利用 这一事实,并求解线性方程组,我们得到:
注意
我们以同样的方式估算复数域函数 的梯度。
边界点处每个偏导数的值计算方式不同。请参见下面的
edge_order
。- 参数
input (
Tensor
) – 代表函数值的张量- 关键字参数
spacing (
scalar
,list of scalar
,list of Tensor
, optional) –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
,list of int
, optional) – 近似梯度的维度或维度。默认情况下,计算每个维度的偏导数。请注意,当指定dim
时,spacing
参数的元素必须与指定的dim
相对应。edge_order (
int
, optional) – 1 或 2,分别用于边界(“edge”)值的 一阶 或 二阶 估算。请注意,当指定edge_order
时,input
的每个维度大小应至少为 edge_order+1。
示例
>>> # 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]]))