From 4054b96ea30721148c1feb43bacd46d2beebc682 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sun, 24 Aug 2025 14:08:18 +0530 Subject: [PATCH 1/5] [Term Entry] PyTorch Tensor Operations: .erf() --- .../tensor-operations/terms/erf/erf.md | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 docs/content/pytorch/concepts/tensor-operations/terms/erf/erf.md diff --git a/docs/content/pytorch/concepts/tensor-operations/terms/erf/erf.md b/docs/content/pytorch/concepts/tensor-operations/terms/erf/erf.md new file mode 100644 index 00000000000..4ac8c158187 --- /dev/null +++ b/docs/content/pytorch/concepts/tensor-operations/terms/erf/erf.md @@ -0,0 +1,151 @@ +--- +Title: 'erf()' +Description: 'Computes the error function element-wise for each element in the input tensor' +Subjects: + - 'Data Science' + - 'Machine Learning' +Tags: + - 'Functions' + - 'Math' + - 'PyTorch' + - 'Tensors' +CatalogContent: + - 'learn-pytorch' + - 'paths/machine-learning' +--- + +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 + +```pseudo +torch.erf(input, *, out=None) → Tensor +``` + +**Parameters:** + +- `input` (Tensor): The input tensor containing the values for which to compute the error function +- `out` (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: + +```py +import torch + +# Create a 1D tensor with sample values +input_tensor = torch.tensor([0.0, 1.0, -1.0, 2.0, -2.0]) +print("Input tensor:", input_tensor) + +# Compute the error function +result = torch.erf(input_tensor) +print("Error function result:", result) +``` + +The output of this code is: + +```shell +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 approaching ±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: + +```py +import torch + +# Create a batch of 2D tensors for processing multiple samples +batch_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 batch +erf_result = torch.erf(batch_tensor) +print("Error function applied to batch:\n", erf_result) +``` + +The output of this code is: + +```shell +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: + +```py +import torch +import 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 activations +activations = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]) +print("Input activations:", activations) + +# Apply GELU using erf +gelu_output = gelu_erf(activations) +print("GELU output using erf:", gelu_output) + +# Compare with PyTorch's built-in GELU +pytorch_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: + +```shell +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. From 48e4ee3da9c7ba93d1494a205f01f8b059e345c2 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sun, 24 Aug 2025 14:09:56 +0530 Subject: [PATCH 2/5] Rename docs/content/pytorch/concepts/tensor-operations/terms/erf/erf.md to content/pytorch/concepts/tensor-operations/terms/erf/erf.md --- .../pytorch/concepts/tensor-operations/terms/erf/erf.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {docs/content => content}/pytorch/concepts/tensor-operations/terms/erf/erf.md (100%) diff --git a/docs/content/pytorch/concepts/tensor-operations/terms/erf/erf.md b/content/pytorch/concepts/tensor-operations/terms/erf/erf.md similarity index 100% rename from docs/content/pytorch/concepts/tensor-operations/terms/erf/erf.md rename to content/pytorch/concepts/tensor-operations/terms/erf/erf.md From e03f72e1849d40176416b945d084ce7d9320d9aa Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:47:14 +0530 Subject: [PATCH 3/5] Update content/pytorch/concepts/tensor-operations/terms/erf/erf.md --- content/pytorch/concepts/tensor-operations/terms/erf/erf.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/erf/erf.md b/content/pytorch/concepts/tensor-operations/terms/erf/erf.md index 4ac8c158187..a0176ea32b2 100644 --- a/content/pytorch/concepts/tensor-operations/terms/erf/erf.md +++ b/content/pytorch/concepts/tensor-operations/terms/erf/erf.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/machine-learning' --- -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. +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 From 5f7e0d54bf0ab3ac2b890540830f273c509aa39b Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:51:22 +0530 Subject: [PATCH 4/5] Update content/pytorch/concepts/tensor-operations/terms/erf/erf.md --- content/pytorch/concepts/tensor-operations/terms/erf/erf.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/erf/erf.md b/content/pytorch/concepts/tensor-operations/terms/erf/erf.md index a0176ea32b2..d6f80487666 100644 --- a/content/pytorch/concepts/tensor-operations/terms/erf/erf.md +++ b/content/pytorch/concepts/tensor-operations/terms/erf/erf.md @@ -33,7 +33,7 @@ A new tensor with the same shape as `input`, containing the computed error funct ## Example 1: Basic `.erf()` Usage -This example demonstrates the basic implementation of the `erf()` function with a simple 1D tensor: +This example demonstrates the basic implementation of the `.erf()` function with a simple 1D tensor: ```py import torch From 260093f082bedd9b93a69b91a245aab7fe8c08b1 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Tue, 28 Oct 2025 14:51:53 +0530 Subject: [PATCH 5/5] Update content/pytorch/concepts/tensor-operations/terms/erf/erf.md --- content/pytorch/concepts/tensor-operations/terms/erf/erf.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/erf/erf.md b/content/pytorch/concepts/tensor-operations/terms/erf/erf.md index d6f80487666..49b82a5967b 100644 --- a/content/pytorch/concepts/tensor-operations/terms/erf/erf.md +++ b/content/pytorch/concepts/tensor-operations/terms/erf/erf.md @@ -54,7 +54,7 @@ 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 approaching ±1 for large positive or negative values. +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()`