How to create a normal distribution in pytorch

后端 未结 6 1740
别跟我提以往
别跟我提以往 2021-01-03 22:48

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.

6条回答
  •  难免孤独
    2021-01-03 23:18

    For a standard normal distribution (i.e. mean=0 and variance=1), you can use torch.randn()

    For your case of custom mean and std, you can use torch.distributions.Normal()


    Init signature:
    tdist.Normal(loc, scale, validate_args=None)

    Docstring:
    Creates a normal (also called Gaussian) distribution parameterized by loc and scale.

    Args:
    loc (float or Tensor): mean of the distribution (often referred to as mu)
    scale (float or Tensor): standard deviation of the distribution (often referred to as sigma)


    Here's an example:

    In [32]: import torch.distributions as tdist
    
    In [33]: n = tdist.Normal(torch.tensor([4.0]), torch.tensor([0.5]))
    
    In [34]: n.sample((2,))
    Out[34]: 
    tensor([[ 3.6577],
            [ 4.7001]])
    

提交回复
热议问题