uniform_random

paddle.fluid.layers.uniform_random(shape, dtype='float32', min=-1.0, max=1.0, seed=0)[source]

This OP initializes a variable with random values sampled from a uniform distribution in the range [min, max).

Examples:

Input:
  shape = [1, 2]

Output:
  result=[[0.8505902, 0.8397286]]
Parameters
  • shape (list|tuple|Variable) – The shape of the output Tensor, if the shape is a list or tuple, its elements can be an integer or a Tensor with the shape [1], and the type of the Tensor is int64. If the shape is a Variable, it is a 1-D Tensor, and the type of the Tensor is int64.

  • dtype (np.dtype|core.VarDesc.VarType|str, optional) – The type of the output Tensor. Supported data types: float32, float64. Default: float32.

  • min (float, optional) – The lower bound on the range of random values to generate, the min is included in the range. Default -1.0.

  • max (float, optional) – The upper bound on the range of random values to generate, the max is excluded in the range. Default 1.0.

  • seed (int, optional) – Random seed used for generating samples. 0 means use a seed generated by the system. Note that if seed is not 0, this operator will always generate the same random numbers every time. Default 0.

Returns

A Tensor of the specified shape filled with uniform_random values.

Return type

Variable

Raises

TypeError – The shape type should be list or tupple or variable.

Examples

import paddle.fluid as fluid

# example 1:
# attr shape is a list which doesn't contain tensor Variable.
result_1 = fluid.layers.uniform_random(shape=[3, 4])

# example 2:
# attr shape is a list which contains tensor Variable.
dim_1 = fluid.layers.fill_constant([1],"int64",3)
result_2 = fluid.layers.uniform_random(shape=[dim_1, 5])

# example 3:
# attr shape is a Variable, the data type must be int64
var_shape = fluid.data(name='var_shape', shape=[2], dtype="int64")
result_3 = fluid.layers.uniform_random(var_shape)