uniform¶
- paddle. uniform ( shape, dtype=None, min=- 1.0, max=1.0, seed=0, name=None ) [source]
-
Returns a Tensor filled with random values sampled from a uniform distribution in the range [
min
,max
), withshape
anddtype
.Examples:
Input: shape = [1, 2] Output: result=[[0.8505902, 0.8397286]]
- Parameters
-
shape (tuple|list|Tensor) – Shape of the Tensor to be created. The data type is
int32
orint64
. Ifshape
is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. Ifshape
is an Tensor, it should be an 1-D Tensor which represents a list.dtype (str|np.dtype, optional) – The data type of the output Tensor. Supported data types: float32, float64. Default is None, use global default dtype (see
get_default_dtype
for details).min (float|int, optional) – The lower bound on the range of random values to generate,
min
is included in the range. Default is -1.0.max (float|int, optional) – The upper bound on the range of random values to generate,
max
is excluded in the range. Default is 1.0.seed (int, optional) – Random seed used for generating samples. If seed is 0, it will use the seed of the global default generator (which can be set by paddle.seed). Note that if seed is not 0, this operator will always generate the same random numbers every time. Default is 0.
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
- Returns
-
A Tensor filled with random values sampled from a uniform distribution in the range [
min
,max
), withshape
anddtype
. - Return type
-
Tensor
Examples
>>> import paddle >>> # example 1: >>> # attr shape is a list which doesn't contain Tensor. >>> out1 = paddle.uniform(shape=[3, 4]) >>> print(out1) >>> Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0.38170254, -0.47945309, 0.39794648, -0.94233936], [-0.85296679, -0.76094693, 0.10565400, 0.59155810], [ 0.11681318, -0.42144555, -0.81596589, 0.62113667]]) >>> >>> # example 2: >>> # attr shape is a list which contains Tensor. >>> dim1 = paddle.to_tensor(2, 'int64') >>> dim2 = paddle.to_tensor(3, 'int32') >>> out2 = paddle.uniform(shape=[dim1, dim2]) >>> print(out2) >>> Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0.00294012, -0.07210171, -0.44236207], [ 0.70089281, 0.21500075, -0.22084606]]) >>> >>> # example 3: >>> # attr shape is a Tensor, the data type must be int64 or int32. >>> shape_tensor = paddle.to_tensor([2, 3]) >>> out3 = paddle.uniform(shape_tensor) >>> print(out3) >>> Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0.60801756, 0.32448411, 0.90269291], [-0.66421294, -0.95218551, -0.51022208]]) >>>