PyTorch torch.gradient Function
Pytorch torch Reference Manual
torch.gradientis a function in PyTorch used to compute the gradient of a tensor.
Function Definition
torch.gradient(input, *, dim=None, spacing=1, edge_order=1)
Usage Example
Example
import torch
# Create a 1D tensor
x = torch.tensor([1.0, 2.0, 4.0, 7.0, 11.0])
# Compute the gradient
grad = torch.gradient(x)
print("Gradient:", grad[0])
# Create a 1D tensor
x = torch.tensor([1.0, 2.0, 4.0, 7.0, 11.0])
# Compute the gradient
grad = torch.gradient(x)
print("Gradient:", grad[0])
The output result is:
梯度: tensor([1., 2., 3., 4.])
Other Extensions