How to create a normal distribution in pytorch

dq.shen picture dq.shen · Jul 2, 2018 · Viewed 32.1k times · Source

I want to create a random normal distribution in pytorch and mean and std are 4, 0.5 respectively. I didn't find a API for it. Anyone knows? Thanks very much.

Answer

Furkan picture Furkan · Oct 15, 2019

You can easily use torch.Tensor.normal_() method.

Let's create a matrix Z (a 1d tensor) of dimension 1 × 5, filled with random elements samples from the normal distribution parameterized by mean = 4 and std = 0.5.

torch.empty(5).normal_(mean=4,std=0.5)

Result:

tensor([4.1450, 4.0104, 4.0228, 4.4689, 3.7810])