random_normal#

ivy.random_normal(*, mean=0.0, std=1.0, shape=None, dtype=None, seed=None, device=None, out=None)[source]#

Draws samples from a normal distribution.

Parameters:
  • mean (Union[float, NativeArray, Array], default: 0.0) – The mean of the normal distribution to sample from. Default is 0.0.

  • std (Union[float, NativeArray, Array], default: 1.0) – The standard deviation of the normal distribution to sample from. Must be non-negative. Default is 1.0.

  • shape (Optional[Union[Shape, NativeShape]], default: None) – If the given shape is, e.g (m, n, k), then m * n * k samples are drawn. Can only be specified when mean and std are numeric values, else exception will be raised. Default is None, where a single value is returned.

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – output array data type. If dtype is None, the output array data type will be the default floating-point data type. Default None

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None).

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret

Drawn samples from the parameterized normal distribution.

Examples

>>> ivy.random_normal()
ivy.array(-0.22346112)
>>> ivy.random_normal(shape=3)
ivy.array([-0.73  ,  0.0922, -0.515 ])
>>> ivy.random_normal(shape=(2, 3), seed=42)
ivy.array([[ 0.49671414, -0.1382643 ,  0.64768857],
       [ 1.5230298 , -0.23415337, -0.23413695]])
>>> ivy.random_normal(mean=3.0, std=6.0)
ivy.array(4.9213753)
>>> ivy.random_normal(mean=1.0, std=2.0, shape=(2,1))
ivy.array([[2.19],
           [2.78]])
>>> z = ivy.zeros(())
>>> ivy.random_normal(mean=1.0, std=2.0, out=z)
ivy.array(0.12818667)
>>> ivy.random_normal(mean=1.0, std=2.0, shape=(2,2), device='cpu')
ivy.array([[ 2.91 ,  1.3  ],
           [ 3.37 , -0.799]])
>>> ivy.random_normal(mean=1.0, std=2.0, shape=(2,2), device='cpu',
...                   dtype='int32')
ivy.array([[ 0, -1],
           [ 0,  3]])
>>> z = ivy.zeros((1,2))
>>> ivy.random_normal(mean=1.0, std=2.0, shape=(1,2), device='cpu',
...                   dtype='float64', out=z)
ivy.array([[-2.01, -1.95]])
>>> x = ivy.array([4.8, 5.6])
>>> y = ivy.array([9.8, 7.4])
>>> ivy.random_normal(mean=x, std=y)
ivy.array([ 4.43 , -0.469])
>>> z = ivy.zeros((2,))
>>> ivy.random_normal(mean=x, std=y, out=z)
ivy.array([0.287, 8.55 ])
>>> ivy.random_normal(mean=x, std=y, device='cpu')
ivy.array([18.9, 15.2])
>>> ivy.random_normal(mean=x, std=y, device='cpu', dtype='float64')
ivy.array([-4.1   , -0.0366])
>>> z = ivy.zeros((2,))
>>> ivy.random_normal(mean=x, std=y, device='cpu', dtype='float64', out=z)
ivy.array([12.4, 11. ])
Array.random_normal(self, /, *, std=1.0, shape=None, device=None, dtype=None, seed=None, out=None)[source]#

ivy.Array instance method variant of ivy.random_normal. This method simply wraps the function, and so the docstring for ivy.random_normal also applies to this method with minimal changes.

Parameters:
  • self (Array) – The mean of the normal distribution to sample from. Default is 0.0.

  • std (Union[float, Array, NativeArray], default: 1.0) – The standard deviation of the normal distribution to sample from. Must be non-negative. Default is 1.0.

  • shape (Optional[Union[Shape, NativeShape]], default: None) – If the given shape is, e.g (m, n, k), then m * n * k samples are drawn. Can only be specified when mean and std are numeric values, else exception will be raised. Default is None, where a single value is returned.

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None).

  • dtype (Optional[Union[Dtype, NativeDtype]], default: None) – output array data type. If dtype is None, the output array data type will be the default floating-point data type. Default None

  • seed (Optional[int], default: None) – A python integer. Used to create a random seed distribution

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – Drawn samples from the parameterized normal distribution.

Examples

>>> x = ivy.array([[9.8, 3.4], [5.8, 7.2]])
>>> x.random_normal(std=10.2)
ivy.array([[19.   , -6.44 ],
           [ 5.72 ,  0.235]])
>>> x.random_normal(std=10.2, device='cpu')
ivy.array([[18.7 , 25.2 ],
           [27.5 , -3.22]])
>>> x.random_normal(std=14.2, dtype='float16')
ivy.array([[26.6 , 12.1 ],
           [ 4.56,  5.49]])
>>> x.random_normal(std=10.8, device='cpu', dtype='float64')
ivy.array([[ 1.02, -1.39],
           [14.2 , -1.  ]])
>>> z = ivy.ones((2,2))
>>> x.random_normal(std=11.2, device='cpu', dtype='float64', out=z)
ivy.array([[ 7.72, -8.32],
           [ 4.95, 15.8 ]])
>>> x = ivy.array([8.7, 9.3])
>>> y = ivy.array([12.8, 14.5])
>>> x.random_normal(std=y)
ivy.array([-10.8,  12.1])
>>> x.random_normal(std=y, device='cpu')
ivy.array([ 13. , -26.9])
>>> x.random_normal(std=y, dtype='float16')
ivy.array([14.3  , -0.807])
>>> x.random_normal(std=y, device='cpu', dtype='float64')
ivy.array([21.3 ,  3.85])
>>> z = ivy.ones((2,))
>>> x.random_normal(std=y, device='cpu', dtype='float64', out=z)
ivy.array([ 4.32, 42.2 ])
Container.random_normal(self, /, *, std=1.0, shape=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, device=None, dtype=None, seed=None, out=None)[source]#

ivy.Container instance method variant of ivy.random_normal. This method simply wraps the function, and so the docstring for ivy.random_normal also applies to this method with minimal changes.

Parameters:
  • self (Container) – The mean of the normal distribution to sample from. Default is 0.0.

  • std (Union[float, Container, Array, NativeArray], default: 1.0) – The standard deviation of the normal distribution to sample from. Must be non-negative. Default is 1.0.

  • shape (Optional[Union[Shape, NativeShape, Container]], default: None) – If the given shape is, e.g (m, n, k), then m * n * k samples are drawn. Can only be specified when mean and std are numeric values, else exception will be raised. Default is None, where a single value is returned.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • device (Optional[Union[Device, NativeDevice, Container]], default: None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (Default value = None).

  • dtype (Optional[Union[Dtype, NativeDtype, Container]], default: None) – output array data type. If dtype is None, the output array data type will be the default floating-point data type. Default None

  • seed (Optional[Union[int, Container]], default: None) – A python integer. Used to create a random seed distribution

  • out (Optional[Container], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – Drawn samples from the parameterized normal distribution.

Examples

>>> x = ivy.Container(a=ivy.array([7.5,6.7,0.9]),
...                   b=ivy.array([8.7,9.8,4.5]))
>>> x.random_normal(std=17.4)
{
    a: ivy.array([11.9, -22.9, -24.8]),
    b: ivy.array([44.3, -21.6, 2.03])
}
>>> x.random_normal(std=10.2, device='cpu')
{
    a: ivy.array([7.82, 6.21, -0.431]),
    b: ivy.array([13.8, 9.9, 7.64])
}
>>> x.random_normal(std=14.2, dtype='float16')
{
    a: ivy.array([-18.3, -3.42, 9.55]),
    b: ivy.array([-1.31, 7.68, -6.93])
}
>>> x.random_normal(std=10.8, device='cpu', dtype='float64')
{
    a: ivy.array([13.4, -3.14, 10.7]),
    b: ivy.array([11.7, 4.85, 5.83])
}
>>> z = ivy.Container(a=ivy.zeros((3,)), b=ivy.ones((3,)))
>>> x.random_normal(std=11.2, device='cpu', dtype='float64', out=z)
{
    a: ivy.array([-6.84, 0.274, 14.2]),
    b: ivy.array([29.1, 7.19, 3.])
}
>>> y = ivy.Container(a=10.4, b=17.4)
>>> x.random_normal(std=y)
{
    a: ivy.array([-9.5, 8.54, -9.13]),
    b: ivy.array([-24.5, 18.9, 11.])
}
>>> x.random_normal(std=y, device='cpu')
{
    a: ivy.array([8.47, 8.23, 8.69]),
    b: ivy.array([10.7, 16.2, 16.1])
}
>>> x.random_normal(std=y, dtype='float16')
{
    a: ivy.array([8.22, -15.9, 10.4]),
    b: ivy.array([19.9, 11.5, -2.15])
}
>>> x.random_normal(std=y, device='cpu', dtype='float64')
{
    a: ivy.array([19.6, -4.08, 6.09]),
    b: ivy.array([-23.9, 6.86, 17.6])
}
>>> z = ivy.Container(a=ivy.zeros((3,)), b=ivy.ones((3,)))
>>> x.random_normal(std=y, device='cpu', dtype='float64', out=z)
{
    a: ivy.array([14.7, 8.99, 8.46]),
    b: ivy.array([22.9, -5.97, -1.28])
}
>>> x = ivy.Container(a=ivy.array([[9.8,7.6],[6.5,2.3]]),
...                   b=ivy.array([[0.9,2.4],[7.6,5.4]]))
>>> y = ivy.Container(a=ivy.array([[10.9,32.4],[18.7,19.6]]),
...                   b=ivy.array([[4.3,5.6],[23.4,54.3]]))
>>> x.random_normal(std=y)
{
    a: ivy.array([[10.6, 7.89],
                  [9.39, 19.4]]),
    b: ivy.array([[3.76, 4.68],
                  [17.7, 24.]])
}
>>> x.random_normal(std=y, device='cpu')
{
    a: ivy.array([[30.9, 24.6],
                  [29.9, -25.3]]),
    b: ivy.array([[8.02, 1.92],
                  [-5.34, -54.1]])
}
>>> x.random_normal(std=y, dtype='float16')
{
    a: ivy.array([[7.82, -35.],
                  [11.7, 0.696]]),
    b: ivy.array([[-4.07, -2.91],
                  [19.2, 46.8]])
}
>>> x.random_normal(std=y, device='cpu', dtype='float64')
{
    a: ivy.array([[25.4, 28.3],
                  [19.6, -9.83]]),
    b: ivy.array([[2.95, 2.48],
                  [-30.8, -40.1]])
}
>>> z = ivy.Container(a=ivy.zeros((2,2)), b=ivy.ones((2,2)))
>>> x.random_normal(std=y, device='cpu', dtype='float64', out=z)
{
    a: ivy.array([[2.8, -45.6],
                  [-10.4, 0.65]]),
    b: ivy.array([[3.8, 1.43],
                  [23., 29.4]])
}