randint_like

paddle. randint_like ( x, low=0, high=None, dtype=None, name=None ) [源代码]

返回服从均匀分布的、范围在[low, high)的随机 Tensor,输出的形状与 x 的形状一致,当数据类型 dtype 为 None 时(默认),输出的数据类型与 x 的数据类型一致,当数据类型 dtype 不为 None 时,将输出用户指定的数据类型。当 high 为 None 时(默认),均匀采样的区间为[0, low)。

参数

  • x (Tensor) – 输入的多维 Tensor,数据类型可以是 bool,int32,int64,float16,float32,float64。输出 Tensor 的形状和 x 相同。如果 dtype 为 None,则输出 Tensor 的数据类型与 x 相同。

  • low (int,可选) - 要生成的随机值范围的下限,low 包含在范围中。当 high 为 None 时,均匀采样的区间为[0, low)。默认值为 0。

  • high (int,可选) - 要生成的随机值范围的上限,high 不包含在范围中。默认值为 None,此时范围是[0, low)。

  • dtype (str|np.dtype,可选) - 输出 Tensor 的数据类型,支持 bool,int32,int64,float16,float32,float64。当该参数值为 None 时,输出 Tensor 的数据类型与输入 Tensor 的数据类型一致。默认值为 None。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

Tensor:从区间[lowhigh)内均匀分布采样的随机 Tensor,形状为 x.shape,数据类型为 dtype

代码示例

>>> import paddle

>>> # example 1:
>>> # dtype is None and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out2 = paddle.randint_like(x, low=-5, high=5)
>>> print(out2)
Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 0.]])
>>> print(out2.dtype)
paddle.float32

>>> # example 2:
>>> # dtype is None and the dtype of x is float64
>>> x = paddle.zeros((1,2)).astype("float64")
>>> out2 = paddle.randint_like(x, low=-5, high=5)
>>> print(out2)
Tensor(shape=[1, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
[[ 4., -5.]])
>>> print(out2.dtype)
paddle.float64

>>> # example 3:
>>> # dtype is None and the dtype of x is int32
>>> x = paddle.zeros((1,2)).astype("int32")
>>> out3 = paddle.randint_like(x, low=-5, high=5)
>>> print(out3)
Tensor(shape=[1, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[ 0, -4]])
>>> print(out3.dtype)
paddle.int32

>>> # example 4:
>>> # dtype is None and the dtype of x is int64
>>> x = paddle.zeros((1,2)).astype("int64")
>>> out4 = paddle.randint_like(x, low=-5, high=5)
>>> print(out4)
Tensor(shape=[1, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[ 4, -3]])
>>> print(out4.dtype)
paddle.int64

>>> # example 5:
>>> # dtype is float64 and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out5 = paddle.randint_like(x, low=-5, high=5, dtype="float64")
>>> print(out5)
Tensor(shape=[1, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
[[3., 1.]])
>>> print(out5.dtype)
paddle.float64

>>> # example 6:
>>> # dtype is bool and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out6 = paddle.randint_like(x, low=-5, high=5, dtype="bool")
>>> print(out6)
Tensor(shape=[1, 2], dtype=bool, place=Place(cpu), stop_gradient=True,
[[False, True ]])
>>> print(out6.dtype)
paddle.bool

>>> # example 7:
>>> # dtype is int32 and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out7 = paddle.randint_like(x, low=-5, high=5, dtype="int32")
>>> print(out7)
Tensor(shape=[1, 2], dtype=int32, place=Place(cpu), stop_gradient=True,
[[-2, -2]])
>>> print(out7.dtype)
paddle.int32

>>> # example 8:
>>> # dtype is int64 and the dtype of x is float32
>>> x = paddle.zeros((1,2)).astype("float32")
>>> out8 = paddle.randint_like(x, low=-5, high=5, dtype="int64")
>>> print(out8)
Tensor(shape=[1, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[-5,  4]])
>>> print(out8.dtype)
paddle.int64

>>> # example 9:
>>> # dtype is int64 and the dtype of x is bool
>>> x = paddle.zeros((1,2)).astype("bool")
>>> out9 = paddle.randint_like(x, low=-5, high=5, dtype="int64")
>>> print(out9)
Tensor(shape=[1, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[ 1, -2]])
>>> print(out9.dtype)
paddle.int64

使用本API的教程文档