oneflow.normal

oneflow.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 mean is a tensor with the mean of each output element’s normal distribution

The std is a tensor with the standard deviation of each output element’s normal distribution

The shapes of mean and std don’t need to match, but the total number of elements in each tensor need to be the same.

Note

Infers the output shape from input arrays mean and std. The output shape will have a dimensionality equal to the max of mean and std. Dimensions with size 1 in either mean or std are expanded to match the other.

Parameters
  • mean (Tensor) – the tensor of per-element means

  • std (Tensor) – the tensor of per-element standard deviations

Keyword Arguments
  • generator (Generator, optional) – Random number generator. Defaults to oneflow::DefaultGenerator if not provided.

  • out (Tensor, optional) – Output tensor, will be resized and filled with the result. If not provided, a new tensor is created.

Example:

>>> import oneflow as flow
>>> generator = flow.Generator()
>>> generator.manual_seed(0) 
<oneflow._oneflow_internal.Generator object at ...>
>>> z = flow.normal(mean=flow.arange(1., 11.), std=flow.arange(1, 0, -0.1), generator=generator)
>>> z[:5]
tensor([3.2122, 3.0468, 3.6192, 4.3387, 5.6261], dtype=oneflow.float32)

normal(mean=0.0, std, *, generator=None, out=None) -> Tensor.

Similar to the function above, but the means are shared among all drawn elements.

Parameters
  • mean (float, optional) – the mean for all distributions

  • std (Tensor) – the tensor of per-element standard deviations

Keyword Arguments
  • generator (Generator, optional) – Random number generator. Defaults to oneflow::DefaultGenerator if not provided.

  • out (Tensor, optional) – Output tensor, will be resized and filled with the result. If not provided, a new tensor is created.

Example:

>>> import oneflow as flow
>>> flow.normal(mean=0.5, std=flow.arange(1., 6.)).shape
oneflow.Size([5])

normal(mean, std=1.0, *, generator=None, out=None) -> Tensor Similar to the function above, but the standard deviations are shared among all drawn elements.

Parameters
  • mean (Tensor) – the tensor of per-element means

  • std (float, optional) – the standard deviation

Keyword Arguments
  • generator (Generator, optional) – Random number generator. Defaults to oneflow::DefaultGenerator if not provided.

  • out (Tensor) – The output tensor

Returns

The output tensor, with random normal values.

Return type

Tensor

Example:

>>> import oneflow as flow
>>> flow.normal(mean=flow.arange(1., 6.)).shape
oneflow.Size([5])

normal(mean, std, size, *, out=None, placement=None, sbp=None, generator=None, dtype=None, device=None, requires_grad=False) -> Tensor Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.

Parameters
  • mean (float) – the mean for all distributions

  • std (float) – the standard deviation for all distributions

  • size (int...) – a sequence of integers defining the shape of the output tensor.

Keyword Arguments
  • out (Tensor, optional) – the output tensor.

  • placement (flow.placement, optional) – The desired device of returned global tensor. If None, will construct local tensor.

  • sbp (flow.sbp, optional) – The desired sbp of returned global tensor. It must be equal with the numbers of placement.

  • generator (oneflow.Generator, optional) – a pseudorandom number generator for sampling

  • dtype (oneflow.dtype, optional) – the desired data type of returned tensor. Default: oneflow.float32.

  • device – the desired device of returned tensor. Default: cpu.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

>>> import oneflow as flow
>>> generator = flow.Generator()
>>> generator.manual_seed(0) 
<oneflow._oneflow_internal.Generator object at ...>
>>> y = flow.normal(0, 1, 5, generator=generator)
>>> y
tensor([2.2122, 1.1631, 0.7740, 0.4838, 1.0434], dtype=oneflow.float32)