How to Estimate the Gradient of a Function in One or More Dimensions in PyTorch? Last Updated : 05 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are going to see how to estimate the gradient of a function in one or more dimensions in PyTorch. torch.gradient() function torch.gradient() method estimates the gradient of a function in one or more dimensions using the second-order accurate central differences method, and the function can be defined on a real or complex domain. For controllers and optimizers, gradient estimations are quite valuable. Gradient descent is a prominent optimization method that requires an estimate of the output derivatives with respect to each input at a given location. Let's have a look at the syntax of the given method first: Syntax: torch.gradient(values) Parameters: values(Tensor): this parameter is represents the values of the function.Example 1 In this example, we estimate the gradient of a function for a 1D tensor. Python3 # Import required library import torch # define the tensor tens = torch.tensor([-2., 1., -3., 4., 5.]) print(" Input tensor: ", tens) # define a function def fun(tens): return tens**2+5 # values of function values = fun(tens) # display values print(" Function Values: ", values) # estimate the gradients of fun grad = torch.gradient(values) # Display result print(" Estimated Gradients of fun() - ", grad) Output: Example 2 In this example, we estimate the gradient of a function for a 2D tensor. Python3 # Import required library import torch # define the tensor tens = torch.tensor([[-1., 3., -5.], [-4., 5., 2.], [-2., 3., 4.], ]) print("\n Input tensor: \n", tens) # define a function def fun(tens): return tens**3 # values of function values = fun(tens) # display values print("\n Function Values: \n", values) # estimate the gradients of fun in dim=0 grad_dim_0 = torch.gradient(values, dim=0) print("\n Estimated Gradients of fun() in dim=0 - \n", grad_dim_0) # estimate the gradients of fun in dim=1 grad_dim_1 = torch.gradient(values, dim=1) print("\n Estimated Gradients of fun() in dim=1 - \n", grad_dim_1) Output: Comment More infoAdvertise with us Next Article How to Find Mean Across the Image Channels in PyTorch? M mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads How to Differentiate a Gradient in PyTorch? PyTorch is an open-source machine-learning framework based on the Torch library. It is built by the Facebook AI team. It is used for Computer vision and Natural Language Processing applications. PyTorch uses tensors to use the power of GPU. Differentiation is part of Calculus. So, In this article, 8 min read How to implement a gradient descent in Python to find a local minimum ? Gradient Descent is an iterative algorithm that is used to minimize a function by finding the optimal parameters. Gradient Descent can be applied to any dimension function i.e. 1-D, 2-D, 3-D. In this article, we will be working on finding global minima for parabolic function (2-D) and will be implem 8 min read How Does torch.argmax Work for 4-Dimensions in Pytorch In this article, we are going to discuss how does the torch.argmax work for 4-Dimensions with detailed examples. Torch.argmax() Method Torch.argmax() method accepts a tensor and returns the indices of the maximum values of the input tensor across a specified dimension/axis. If the input tensor exist 5 min read How to Find Mean Across the Image Channels in PyTorch? In this article, we are going to see how to find mean across the image channels in PyTorch. We have to compute the mean of an image across the channels Red, Green, and, Blue. we can find the mean across the image channel by using torch.mean() method. torch.mean() method torch.mean() method is used t 2 min read How to Compute the Heaviside Step Function for Each Element in Input in PyTorch? In this article, we are going to cover how to compute the Heaviside step function for each element in input in PyTorch using Python. We can compute this with the help of torch.heaviside() method. torch.heaviside() method The torch.heaviside() method is used to compute the Heaviside step function for 2 min read How to perform element-wise addition on tensors in PyTorch? In this article, we are going to see how to perform element-wise addition on tensors in PyTorch in Python. We can perform element-wise addition using torch.add() function. This function also allows us to perform addition on the same or different dimensions of tensors. If tensors are different in di 3 min read Like