The normal distribution is often utilized in statistics and probability where data is symmetrically distributed around the mean. It shows that data near the mean more frequently occurred than data far from the mean. Sometimes, users may be required to generate random numbers/values that follow the normal distribution. PyTorch offers the “torch.normal()” method to perform this operation. This method/function takes two arguments i.e. mean and standard deviation. It returns a tensor where each element/number is sampled from normal distribution.

This article will explain various methods to create normal distribution in PyTorch.

How to Create Normal Distribution in PyTorch?

To create normal distribution in PyTorch, two methods are used:

  • Method 1: By Providing Tensors for Mean and Standard Deviation
  • Method 2: By Specifying Values for Mean and Standard Deviation Directly

Method 1: Create Normal Distribution by Providing Tensors for Mean and Standard Deviation

To create normal distribution in PyTorch, follow the below-provided steps:

Step 1: Import PyTorch Library

First, use the provided line to import the “torch” library for creating normal distribution:

import torch

Step 2: Define Tensor for Mean

Then, define a tensor for the mean and display its elements. For example, we are defining the following “mean” 1D tensor using the “torch.tensor()” function:

mean = torch.tensor([2.0, 9.0, 1.0, 7.0, 4.0])

print(mean)

This has created a tensor for mean:

Step 3: Define Tensor for Standard Deviation

Next, define a tensor for the standard deviation. Here, we are defining a “std” 1D tensor:

std = torch.tensor([1.52, 0.98, 0.26, 1.82, 0.39])

print(std)

This has created a tensor for standard deviation:

Step 4: Create a Normal Distribution

Now, utilize the “torch.normal()” method and pass the above-created “mean” and “std” tensors as an argument to create the tensor of random numbers (normal distribution):

Norm_tens = torch.normal(mean, std)

Step 5: Print Computed Tensor

Finally, display the computed tensor of random numbers:

print(Norm_tens)

In the below output, the tensor of random numbers can be seen: 

Method 2: Create Normal Distribution by Specifying Values for Mean and Standard Deviation Directly

Users can also create a normal distribution by specifying values for mean and standard deviation directly in the “torch.normal()” method. 

Step 1: Import PyTorch Library

Install the “torch” library using the provided line:

import torch

Step 2: Create a Normal Distribution 

Now, use the “torch.normal()” method and specify the value for mean and standard deviation directly in it to create the normal distribution:

norm_tens = torch.normal(mean=0.2, std=torch.arange(1., 6.))

Step 3: Display Computed Tensor

Lastly, print the computed tensor of random numbers:

print(norm_tens)

In the below output, it can be observed that the mean is distributed along all drawn tensor elements:

We have efficiently explained the methods of creating normal distribution in PyTorch.

Note: The link to our Google Colab Notebook is accessible right here

Conclusion

To create normal distribution in PyTorch, first, install the “torch” library. Then, define the desired tensors for mean and standard deviation and view their elements. Next, use the “torch.normal()” method and pass the tensors as an argument to create the tensor of random numbers. Lastly, print the computed tensor of random numbers. Alternatively, users can specify the value for mean and standard deviation directly in the “torch.normal()” method to create the normal distribution. This article has explained various methods to create normal distribution in PyTorch.