Open in App
Not now

# How to Create a Normal Distribution in Python PyTorch

• Last Updated : 06 Jun, 2022

In this article, we will discuss how to create Normal Distribution in Pytorch in Python.

## torch.normal()

torch.normal() method is used to create a tensor of random numbers. It will take two input parameters. the first parameter is the mean value and the second parameter is the standard deviation (std).

We can specify the values for the mean and standard deviation directly or we can provide a tensor of elements. This method will return a tensor with random numbers which are returned based on the mean and standard deviation.

Syntax: torch.normal(mean, std)

Parameters:

• mean is the first parameter which takes tensor as an input.
• std refers to the standard deviation which is the second parameter that takes tensor as an input

Return: This method returns a tensor of random numbers resulted from separate normal distribution whose mean and standard deviation are equal to provided mean and std.

Example 1: In this example, we are creating two tensors with 5 elements each. one for mean and second for standard deviation. Then we are going to create a normal distribution from the mean and standard deviation(std) values.

## Python3

 `# import the torch module` `import` `torch`   `# create the mean with 5 values` `mean ``=` `torch.tensor([``1.0``, ``2.0``, ``3.0``, ``4.0``, ``5.0``])`   `# create the standard deviation with 5 values` `std ``=` `torch.tensor([``1.22``, ``0.78``, ``0.56``, ``1.23``, ``0.23``])`   `# create normal distribution` `print``(torch.normal(mean, std))`

Output:

tensor([-0.0367,  1.7494,  2.3784,  4.2227,  5.0095])

Example 2: In this example, we are creating two tensors with only a single element each. one for mean and second for standard deviation. Then we are going to create a normal distribution from the mean and standard deviation(std) value.

## Python3

 `# import the torch module` `import` `torch`   `# create the mean with single value` `mean ``=` `torch.tensor(``3.4``)`   `# create the standard deviation with` `# single value` `std ``=` `torch.tensor(``4.2``)`   `# create normal distribution` `print``(torch.normal(mean, std))`

Output:

`tensor(1.3712)`

My Personal Notes arrow_drop_up
Related Articles