PyTorch erf()
The .erf() computes the Gauss error function element-wise for each element in the input tensor. The error function is a mathematical function that appears frequently in probability, statistics, and partial differential equations, particularly in the context of the normal distribution.
Syntax
torch.erf(input, *, out=None) → Tensor
Parameters:
input(Tensor): The input tensor containing the values for which to compute the error functionout(Tensor, optional): The output tensor to store the result
Return value:
A new tensor with the same shape as input, containing the computed error function values for each element.
Example 1: Basic .erf() Usage
This example demonstrates the basic implementation of the .erf() function with a simple 1D tensor:
import torch# Create a 1D tensor with sample valuesinput_tensor = torch.tensor([0.0, 1.0, -1.0, 2.0, -2.0])print("Input tensor:", input_tensor)# Compute the error functionresult = torch.erf(input_tensor)print("Error function result:", result)
The output of this code is:
Input tensor: tensor([ 0., 1., -1., 2., -2.])Error function result: tensor([ 0.0000, 0.8427, -0.8427, 0.9953, -0.9953])
The error function produces values between -1 and 1, with erf(0) = 0, and the function approaches ±1 for large positive or negative values.
Example 2: Batch Processing with .erf()
This example shows how to apply the error function to multi-dimensional tensors for batch processing scenarios commonly used in machine learning:
import torch# Create a batch of 2D tensors for processing multiple samplesbatch_tensor = torch.tensor([[[-0.5, 0.8, 1.2], [0.3, -0.9, 1.5]],[[0.7, -0.3, 2.1], [-1.1, 0.6, -0.4]]])print("Batch tensor shape:", batch_tensor.shape)print("Input batch:\n", batch_tensor)# Apply error function to the entire batcherf_result = torch.erf(batch_tensor)print("Error function applied to batch:\n", erf_result)
The output of this code is:
Batch tensor shape: torch.Size([2, 2, 3])Input batch:tensor([[[-0.5000, 0.8000, 1.2000],[ 0.3000, -0.9000, 1.5000]],[[ 0.7000, -0.3000, 2.1000],[-1.1000, 0.6000, -0.4000]]])Error function applied to batch:tensor([[[-0.5205, 0.7421, 0.9103],[ 0.3286, -0.7969, 0.9661]],[[ 0.6778, -0.3286, 0.9970],[-0.8802, 0.6039, -0.4284]]])
This example demonstrates how erf() processes each element independently while maintaining the tensor’s original shape, making it ideal for neural network operations where batch processing is essential.
Example 3: .erf() in Activation Functions
This example illustrates using the error function as part of the GELU (Gaussian Error Linear Unit) activation function, which is commonly used in transformer models and modern deep learning architectures:
import torchimport torch.nn as nn# Create a custom GELU activation using erf()def gelu_erf(x):# GELU implementation using error function# GELU(x) = 0.5 * x * (1 + erf(x / sqrt(2)))return 0.5 * x * (1.0 + torch.erf(x / torch.sqrt(torch.tensor(2.0))))# Sample input representing neural network activationsactivations = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])print("Input activations:", activations)# Apply GELU using erfgelu_output = gelu_erf(activations)print("GELU output using erf:", gelu_output)# Compare with PyTorch's built-in GELUpytorch_gelu = nn.GELU()builtin_output = pytorch_gelu(activations)print("PyTorch GELU output:", builtin_output)print("Difference:", torch.abs(gelu_output - builtin_output))
The output of this code is:
Input activations: tensor([-2., -1., 0., 1., 2.])GELU output using erf: tensor([-0.0455, -0.1587, 0.0000, 0.8413, 1.9545])PyTorch GELU output: tensor([-0.0455, -0.1587, 0.0000, 0.8413, 1.9545])Difference: tensor([0.0000, 0.0000, 0.0000, 0.0000, 0.0000])
This example shows how the error function integrates seamlessly with activation function implementations, providing mathematical precision for advanced neural network architectures.
Frequently Asked Questions
1. What is torch erf?
torch.erf() is PyTorch’s implementation of the mathematical error function (also known as the Gauss error function). It computes the error function element-wise for input tensors.
2. How do you get the value out of a tensor in PyTorch?
To extract values from a PyTorch tensor, you can use .item() for single-element tensors, .tolist() to convert to Python lists, .numpy() to convert to NumPy arrays, or standard indexing like tensor[0] for specific elements. For example: value = torch.erf(torch.tensor(1.0)).item() returns the scalar value.
3. What does torch tensor() do?
torch.tensor() creates a new tensor from data such as lists, NumPy arrays, or scalar values. It copies the data and allows you to specify data type and device.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Learn to build machine learning models with Python.
- Includes 10 Courses
- With Certificate
- Beginner Friendly.23 hours