torch.normal#
- torch.normal(mean, std, *, generator=None, out=None) Tensor#
Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.
The
meanis a tensor with the mean of each output element’s normal distributionThe
stdis a tensor with the standard deviation of each output element’s normal distributionThe shapes of
meanandstddon’t need to match, but the total number of elements in each tensor need to be the same.Note
When the shapes do not match, the shape of
meanis used as the shape for the returned output tensorNote
When
stdis a CUDA tensor, this function synchronizes its device with the CPU.- Parameters:
- Keyword Arguments:
generator (
torch.Generator, optional) – a pseudorandom number generator for samplingout (Tensor, optional) – the output tensor.
Example:
>>> torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1)) tensor([ 1.0425, 3.5672, 2.7969, 4.2925, 4.7229, 6.2134, 8.0505, 8.1408, 9.0563, 10.0566])
- torch.normal(mean=0.0, std, *, out=None) Tensor
Similar to the function above, but the means are shared among all drawn elements.
- Parameters:
- Keyword Arguments:
out (Tensor, optional) – the output tensor.
Example:
>>> torch.normal(mean=0.5, std=torch.arange(1., 6.)) tensor([-1.2793, -1.0732, -2.0687, 5.1177, -1.2303])
- torch.normal(mean, std=1.0, *, out=None) Tensor
Similar to the function above, but the standard deviations are shared among all drawn elements.
- Parameters:
- Keyword Arguments:
out (Tensor, optional) – the output tensor
Example:
>>> torch.normal(mean=torch.arange(1., 6.)) tensor([ 1.1552, 2.6148, 2.6535, 5.8318, 4.2361])
- torch.normal(mean, std, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) Tensor
Similar to the function above, but the means and standard deviations are shared among all drawn elements. The resulting tensor has size given by
size.- Parameters:
- Keyword Arguments:
generator (
torch.Generator, optional) – a pseudorandom number generator for samplingout (Tensor, optional) – the output tensor.
dtype (
torch.dtype, optional) – the desired data type of returned tensor. Default: ifNone, uses a global default (seetorch.set_default_dtype()).layout (
torch.layout, optional) – the desired layout of returned Tensor. Default:torch.strided.device (
torch.device, optional) – the desired device of returned tensor. Default: ifNone, uses the current device for the default tensor type (seetorch.set_default_device()).devicewill be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default:
False.pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default:
False.
Example:
>>> torch.normal(2, 3, size=(1, 4)) tensor([[-1.3987, -1.9544, 3.6048, 0.7909]])