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.

Frequently Asked Questions

How to generate random numbers following normal distribution in PyTorch using torch.normal() method?

You can generate random numbers following normal distribution in PyTorch using the torch.normal() method by providing tensors for mean and standard deviation.

What are the steps to create normal distribution in PyTorch by specifying values directly for mean and standard deviation?

To create normal distribution in PyTorch by specifying values directly for mean and standard deviation, you first need to import the PyTorch library, define the mean and standard deviation tensors, and then use torch.normal() method to create the distribution.

How can I define a tensor for mean in PyTorch when creating a normal distribution?

You can define a tensor for mean in PyTorch when creating a normal distribution by using the torch.tensor() function with the desired mean values.

What is the purpose of defining a tensor for standard deviation when creating a normal distribution in PyTorch?

Defining a tensor for standard deviation in PyTorch when creating a normal distribution helps specify the spread of data around the mean, influencing the shape of the generated distribution.

How do I create a tensor of random numbers following normal distribution in PyTorch after defining mean and standard deviation tensors?

After defining the mean and standard deviation tensors in PyTorch, you can create a tensor of random numbers following normal distribution by using the torch.normal() method with the specified mean and standard deviation tensors.

What is the significance of utilizing torch.normal() method in PyTorch for generating normal distribution?

The torch.normal() method in PyTorch is significant for generating normal distribution as it efficiently samples random numbers from the specified normal distribution defined by mean and standard deviation tensors.

Can I create multiple tensors of random numbers following normal distribution in PyTorch using a single torch.normal() call?

Yes, you can create multiple tensors of random numbers following normal distribution in PyTorch using a single torch.normal() call by providing corresponding mean and standard deviation tensors for each.

Is it possible to visualize the normal distribution generated in PyTorch after using the torch.normal() method?

Yes, you can visualize the normal distribution generated in PyTorch after using the torch.normal() method by plotting the generated tensor of random numbers to observe the distribution shape.