Shortcuts

torch.gradient

torch.gradient(input, *, spacing=None, dim=None, edge_order=1) → List of Tensors

This function is analogous to NumPy’s gradient function.

Parameters

{input}

Keyword Arguments
  • spacing (scalar, list of scalar, list of Tensor, optional) – implicitly or explicitly represents

  • coordinates the function is evaluated at (the) –

  • dim (int, list of python:int, optional) – the dimension or dimensions to approximate the gradient over.

  • edge_order (int, optional) – unsupported (must be equal to its default value which is 1.)

Example

>>> t = torch.tensor([1, 2, 4, 7, 11, 16], dtype=torch.float)
>>> torch.gradient(t)
tensor([1. , 1.5, 2.5, 3.5, 4.5, 5. ])
>>> coords = torch.tensor([0., 1., 1.5, 3.5, 4., 6.], dtype=torch.float)
>>> torch.gradient(t, spacing=(coords,))
tensor([1. ,  3. ,  3.5,  6.7,  6.9,  2.5])

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources