mindspore.ops.uniform

mindspore.ops.uniform(shape, minval, maxval, seed=None, dtype=mstype.float32)[source]

Generates random numbers according to the Uniform random number distribution.

Note

The number in tensor minval should be strictly less than maxval at any position after broadcasting.

Parameters
  • shape (tuple) – The shape of random tensor to be generated.

  • minval (Tensor) – The distribution parameter a. It defines the minimum possible generated value, with int32 or float32 data type. If dtype is int32, only one number is allowed.

  • maxval (Tensor) – The distribution parameter b. It defines the maximum possible generated value, with int32 or float32 data type. If dtype is int32, only one number is allowed.

  • seed (int) – Seed is used as entropy source for the random number engines to generate pseudo-random numbers, must be non-negative. Default: None, which will be treated as 0.

  • dtype (mindspore.dtype) – type of the Uniform distribution. If it is int32, it generates numbers from discrete uniform distribution; if it is float32, it generates numbers from continuous uniform distribution. It only supports these two data types. Default: mstype.float32.

Returns

Tensor. The shape should be equal to the broadcasted shape between the input shape and shapes of minval and maxval. The dtype is designated as the input dtype.

Raises
  • TypeError – If shape is not tuple.

  • TypeError – If ‘minval’ or ‘maxval’ is neither int32 nor float32 and dtype of ‘minval’ is not the same as ‘maxval’

  • TypeError – If seed is not an int.

  • TypeError – If ‘dtype’ is neither int32 nor float32.

Supported Platforms:

Ascend GPU

Examples

>>> import mindspore.ops.composite as C
>>> from mindspore.common import dtype as mstype
>>> from mindspore import Tensor
>>> # For discrete uniform distribution, only one number is allowed for both minval and maxval:
>>> shape = (4, 2)
>>> minval = Tensor(1, mstype.int32)
>>> maxval = Tensor(2, mstype.int32)
>>> output = C.uniform(shape, minval, maxval, seed=5, dtype=mstype.int32)
>>>
>>> # For continuous uniform distribution, minval and maxval can be multi-dimentional:
>>> shape = (3, 1, 2)
>>> minval = Tensor(np.array([[3, 4], [5, 6]]), mstype.float32)
>>> maxval = Tensor([8.0, 10.0], mstype.float32)
>>> output = C.uniform(shape, minval, maxval, seed=5)
>>> result = output.shape
>>> print(result)
(3, 2, 2)