How to Compute the Heaviside Step Function for Each Element in Input in PyTorch? Last Updated : 03 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 each element. This method accepts input and values as parameters. The parameters type should be tensor only. If the input < 0 then it return 0. whereas, if input > 0 then this method 1 respectively. If the input=0 then this method returns a value the same as the values (one of the parameters). Below is the syntax of the given method: Syntax: torch.heaviside(input, value) Parameters: input (Tensor): This is our input tensor.value (Tensor): This value is a tensor and it's where input is 0. Return: This method returns the computed heaviside step function. Example 1 In this example, we compute the Heaviside step function for each element in the given 1D tensor. Python3 # Import the required libraries import torch # define two tensors input_tens = torch.tensor([0.3, -1.2, 0, 2.0, 0.9]) values_tens = torch.tensor([0.2]) # display above defined tensors print(" The Input Tensor: ", input_tens) print(" The Values Tensor: ", values_tens) # compute heaviside step function for each # element hea = torch.heaviside(input_tens, values_tens) # Display Output print(" computed Heaviside step function for each element: \n", hea) Output: Example 2 In the following example, we compute the Heaviside step function for each element in the given 2D tensor. Python3 # Import the required libraries import torch # define a 2D tensor for input input_tens = input = torch.tensor([[-2.9, 0.0, -1.6, 2.5], [0.0, -1.2, 0.0, 0.0], [-2.3, 0.0, 1.8, -1.3], [0.0, 2.2, -1.3, 0.0]]) # define a tensor for values values_tens = torch.tensor([0.2, 0.3, 0.4, 0.5]) # display above defined tensors print("\n\n The Input Tensor: \n", input_tens) print("\n The Values Tensor: \n", values_tens) # compute heaviside step function for each # element hea = torch.heaviside(input_tens, values_tens) # Display Output print("\n computed Heaviside step function for each element: \n", hea) Output: Comment More infoAdvertise with us Next Article How to Apply Rectified Linear Unit Function Element-Wise in PyTorch? M mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads How to Compute the Logistic Sigmoid Function of Tensor Elements in PyTorch In this article, we will see how to compute the logistic sigmoid function of Tensor Elements in PyTorch. The torch.special.expit() & torch.sigmoid() methods are logistic functions in a tensor. torch.sigmoid() is an alias of torch.special.expit() method.  So, these methods will take the torch ten 2 min read How to compute the element-wise angle of given input tensor in PyTorch? In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch. torch.angle() method Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angl 3 min read How to compute element-wise entropy of an input tensor in PyTorch In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method. torch.special.entr() method torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input a 2 min read How to compute the inverse hyperbolic sine in PyTorch? In this article, we are going to discuss how to compute the inverse hyperbolic sine in PyTorch. torch.asinh() method: The torch.asinh() method is used to compute the inverse hyperbolic sine of each element present in a given input tensor. This method accepts both real and complex-valued as input. I 3 min read How to Apply Rectified Linear Unit Function Element-Wise in PyTorch? In this article, we are going to see How to Apply Rectified Linear Unit Function Element-Wise in PyTorch in Python. We can Rectify Linear Unit Function Element-Wise by using torch.nn.ReLU() method. torch.nn.ReLU() method In PyTorch, torch.nn.ReLu() method replaces all the negative values with 0 and 2 min read Apply torch.inverse() Function of PyTorch to Every Sample in the Batch PyTorch is a deep learning framework that provides a variety of functions to perform different operations on tensors. One such function is torch.inverse(), which can be used to compute the inverse of a square matrix. Sometimes we may have a batch of matrices, where each matrix represents some data t 3 min read Like