pytorch

How to Calculate the Error Function of a Tensor in PyTorch?

An error function is a mathematical function that represents the probability of a random variable falling within a particular range. It is frequently used in statistics, machine learning, etc. PyTorch provides a “torch.special.erf()” method to calculate the error function of the specific tensor. Users need to provide a tensor as input to this method and it returns a tensor of the same type and shape/size as output.

This blog will illustrate the method to calculate/find the error function of tensors in PyTorch.

How to Calculate/Find the Error Function of a Tensor in PyTorch?

To calculate the error function of a specific tensor in PyTorch, the “torch.special.erf()” method is used. Users need to pass the desired tensor to this method to find its error function. It applies the element-wise error function to the individual element of the input tensor.

Go through the next provided examples for a better understanding.

Example 1: Calculate/Find Error Function of 1D Tensor

In the first section, we will create a 1D tensor and compute its error function. Let’s follow the provided steps:

Step 1: Import PyTorch Library
First, import the “torch” library to compute the error function:

import torch

Step 2: Create 1D Tensor
Then, create a 1D tensor using the “torch.tensor()” function and print its elements. Here, we are creating the following “Tens1” 1D tensor from a list:

Tens1 = torch.tensor([-1, 0.8 ,-0.22 , -0.9, -3])

print(Tens1)

The tensor has been created successfully:

Step 3: Calculate Error Function
Now, utilize the “torch.special.erf()” method to calculate the error function of the “Tens1” tensor:

tens_err = torch.special.erf(Tens1)

Step 4: Display Computed Error Function
Finally, display the calculated error function for verification:

print(tens_err)

The below output shows the calculated error function of the “Tens1” tensor:

Example 2: Calculate/Find Error Function of 2D Tensor

In the second example, we will create a 2D tensor and find its error function. Let’s follow the below step-by-step procedure:

Step 1: Import PyTorch Library
First, import the “torch” library to compute the error function:

import torch

Step 2: Create 2D Tensor
Then, create a 2D tensor and print its elements. Here, we are creating the following “Tens2” 2D tensor from the random numbers:

Tens2 = torch.randn(2,2,3)

print(Tens2)

This has created the 2D random tensor:

Step 3: Calculate Error Function
Now, compute the error function of the “Tens2” tensor using the “torch.special.erf()” method:

tens2_err = torch.special.erf(Tens2)

Step 4: Display Computed Error Function
Finally, print the calculated error function:

print(tens2_err)

In the below output, the calculated error function of the “Tens2” tensor can be seen:

We have efficiently explained the method of calculating the error function in PyTorch.

Note: You can access our Google Colab Notebook at this link.

Conclusion

To calculate/find the error function of a tensor in PyTorch, first, import the “torch” library. Then, create the desired 1D or 2D tensor and view its elements. Next, use the “torch.special.erf()” method to find/compute the error function of the input tensor. Lastly, print the calculated error function. This blog has illustrated the method to calculate/find the error function of tensors in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.